2 Copyright (C) 2014 Waves Audio Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __WCFourCC_h__
21 #define __WCFourCC_h__
24 #include "BasicTypes/WCFourCC.h"
27 //#include "BasicTypes/WTByteOrder.h"
28 #include "WCFixedString.h"
31 // These are preprocessor macros rather than inline functions because most compilers can't
32 // resolve functions at compile-time.
33 #if _BYTEORDER_BIG_ENDIAN==1
34 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
35 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
36 #define FOURCC_COMPILER(a, b, c, d) FOURCC_BIG(a,b,c,d)
37 #elif _BYTEORDER_BIG_ENDIAN==0
38 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
39 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
40 #define FOURCC_COMPILER(a, b, c, d) FOURCC_LITTLE(a,b,c,d)
42 #error _BYTEORDER_BIG_ENDIAN not defined proparly
43 #endif // _BYTEORDER_HPP_BIG_ENDIAN
45 typedef uint32_t WTFourCharCode;
47 #ifndef kEnableWCFourCCDebug
48 #define kEnableWCFourCCDebug 0 // set to 1 to enable debug members
56 static WTFourCharCode stored_from_iter(_iter& i)
58 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]);
63 // static const WCFourCC kDefaultFourCC_prv;
65 static WCFourCC kDefaultFourCC_prv() { return WCFourCC(); }
67 // change this line will change the byte order in which WCFourCC keeps the four char code
68 static const wvNS::wvBO::byte_order_type s_stored_byte_order = wvNS::wvBO::compiler_byte_order;
70 WCFourCC(const char a, const char b, const char c, const char d) :
71 m_stored_value(s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? FOURCC_BIG(a,b,c,d) : FOURCC_LITTLE(a,b,c,d))
73 #if kEnableWCFourCCDebug == 1
74 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
79 m_stored_value(FOURCC_BIG('?','?','?','?')) // since the four chars are the same, there is no need to choose between big & little
81 #if kEnableWCFourCCDebug == 1
82 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
86 WCFourCC(const WTFourCharCode in_fourCharCode, const wvNS::wvBO::byte_order_type in_byteOrder = wvNS::wvBO::compiler_byte_order) :
87 m_stored_value(in_byteOrder==s_stored_byte_order ? in_fourCharCode : wvNS::wvBO::swap32(in_fourCharCode))
89 #if kEnableWCFourCCDebug == 1
90 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
94 explicit WCFourCC(const char* in_source_string) :
95 m_stored_value(stored_from_iter(in_source_string))
97 #if kEnableWCFourCCDebug == 1
98 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
102 explicit WCFourCC(const WCFixedStringBase& in_source_string) :
103 m_stored_value(stored_from_iter(in_source_string))
105 #if kEnableWCFourCCDebug == 1
106 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
110 WTFourCharCode GetAsSomeEndian(const wvNS::wvBO::byte_order_type in_byteOrder) const
112 return s_stored_byte_order==in_byteOrder ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
115 WTFourCharCode GetAsBigEndian() const
117 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
120 WTFourCharCode GetAsLittleEndian() const
122 return s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
125 WTFourCharCode GetAsCompilerEndian() const
127 return s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
130 WTFourCharCode GetAsStored() const
132 return m_stored_value;
135 char operator[](const unsigned int in_character_index) const
137 return char(m_stored_value >> (8 * (s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? 3-in_character_index : in_character_index)));
140 char& operator[](const unsigned int in_character_index)
142 return reinterpret_cast<char*>(&m_stored_value)[s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? 3-in_character_index : in_character_index];
147 return sizeof(WTFourCharCode);
150 static size_t max_size()
155 static size_t capacity()
160 WCFixedString4 GetString() const
162 WCFixedString4 retVal;
163 retVal << operator[](0) << operator[](1) << operator[](2) << operator[](3);
168 #if kEnableWCFourCCDebug == 1
169 const char* c_str() const
171 return m_c_str_stored_value;
178 #if kEnableWCFourCCDebug == 1
182 WTFourCharCode m_stored_value;
183 #if kEnableWCFourCCDebug == 1
184 char m_c_str_stored_value[sizeof(WTFourCharCode)+1];
188 WCFourCC& operator=(const WTFourCharCode); // we want initialization from literal to be dome through the constructor
191 inline bool operator<(const WCFourCC in_left, const WCFourCC in_right)
193 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) < in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
195 inline bool operator==(const WCFourCC in_left, const WCFourCC in_right)
197 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) == in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
200 inline bool operator!=(const WCFourCC in_left, const WCFourCC in_right)
202 return ! operator==(in_left, in_right);
206 #define kDefaultFourCC WCFourCC::kDefaultFourCC_prv()
208 static const WCFourCC kZeroFourCC(0, wvNS::wvBO::compiler_byte_order);
210 #endif //#if !defined(__WCFourCC_h__)