5 #include "BasicTypes/WCFourCC.h"
8 //#include "BasicTypes/WTByteOrder.h"
9 #include "WCFixedString.h"
12 // These are preprocessor macros rather than inline functions because most compilers can't
13 // resolve functions at compile-time.
14 #if _BYTEORDER_BIG_ENDIAN==1
15 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
16 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
17 #define FOURCC_COMPILER(a, b, c, d) FOURCC_BIG(a,b,c,d)
18 #elif _BYTEORDER_BIG_ENDIAN==0
19 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
20 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
21 #define FOURCC_COMPILER(a, b, c, d) FOURCC_LITTLE(a,b,c,d)
23 #error _BYTEORDER_BIG_ENDIAN not defined proparly
24 #endif // _BYTEORDER_HPP_BIG_ENDIAN
26 typedef uint32_t WTFourCharCode;
28 #ifndef kEnableWCFourCCDebug
29 #define kEnableWCFourCCDebug 0 // set to 1 to enable debug members
37 static WTFourCharCode stored_from_iter(_iter& i)
39 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? FOURCC_BIG(i[0], i[1], i[2], i[3]) : FOURCC_LITTLE(i[0], i[1], i[2], i[3]);
44 // static const WCFourCC kDefaultFourCC_prv;
46 static WCFourCC kDefaultFourCC_prv() { return WCFourCC(); }
48 // change this line will change the byte order in which WCFourCC keeps the four char code
49 static const wvNS::wvBO::byte_order_type s_stored_byte_order = wvNS::wvBO::compiler_byte_order;
51 WCFourCC(const char a, const char b, const char c, const char d) :
52 m_stored_value(s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? FOURCC_BIG(a,b,c,d) : FOURCC_LITTLE(a,b,c,d))
54 #if kEnableWCFourCCDebug == 1
55 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
60 m_stored_value(FOURCC_BIG('?','?','?','?')) // since the four chars are the same, there is no need to choose between big & little
62 #if kEnableWCFourCCDebug == 1
63 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
67 WCFourCC(const WTFourCharCode in_fourCharCode, const wvNS::wvBO::byte_order_type in_byteOrder = wvNS::wvBO::compiler_byte_order) :
68 m_stored_value(in_byteOrder==s_stored_byte_order ? in_fourCharCode : wvNS::wvBO::swap32(in_fourCharCode))
70 #if kEnableWCFourCCDebug == 1
71 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
75 explicit WCFourCC(const char* in_source_string) :
76 m_stored_value(stored_from_iter(in_source_string))
78 #if kEnableWCFourCCDebug == 1
79 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
83 explicit WCFourCC(const WCFixedStringBase& in_source_string) :
84 m_stored_value(stored_from_iter(in_source_string))
86 #if kEnableWCFourCCDebug == 1
87 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
91 WTFourCharCode GetAsSomeEndian(const wvNS::wvBO::byte_order_type in_byteOrder) const
93 return s_stored_byte_order==in_byteOrder ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
96 WTFourCharCode GetAsBigEndian() const
98 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
101 WTFourCharCode GetAsLittleEndian() const
103 return s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
106 WTFourCharCode GetAsCompilerEndian() const
108 return s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
111 WTFourCharCode GetAsStored() const
113 return m_stored_value;
116 char operator[](const unsigned int in_character_index) const
118 return char(m_stored_value >> (8 * (s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? 3-in_character_index : in_character_index)));
121 char& operator[](const unsigned int in_character_index)
123 return reinterpret_cast<char*>(&m_stored_value)[s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? 3-in_character_index : in_character_index];
128 return sizeof(WTFourCharCode);
131 static size_t max_size()
136 static size_t capacity()
141 WCFixedString4 GetString() const
143 WCFixedString4 retVal;
144 retVal << operator[](0) << operator[](1) << operator[](2) << operator[](3);
149 #if kEnableWCFourCCDebug == 1
150 const char* c_str() const
152 return m_c_str_stored_value;
159 #if kEnableWCFourCCDebug == 1
163 WTFourCharCode m_stored_value;
164 #if kEnableWCFourCCDebug == 1
165 char m_c_str_stored_value[sizeof(WTFourCharCode)+1];
169 WCFourCC& operator=(const WTFourCharCode); // we want initialization from literal to be dome through the constructor
172 inline bool operator<(const WCFourCC in_left, const WCFourCC in_right)
174 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) < in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
176 inline bool operator==(const WCFourCC in_left, const WCFourCC in_right)
178 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) == in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
181 inline bool operator!=(const WCFourCC in_left, const WCFourCC in_right)
183 return ! operator==(in_left, in_right);
187 #define kDefaultFourCC WCFourCC::kDefaultFourCC_prv()
189 static const WCFourCC kZeroFourCC(0, wvNS::wvBO::compiler_byte_order);
191 #endif //#if !defined(__WCFourCC_h__)