2 Copyright (C) 2013 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.
19 #ifndef __WCFourCC_h__
20 #define __WCFourCC_h__
23 #include "BasicTypes/WCFourCC.h"
26 //#include "BasicTypes/WTByteOrder.h"
27 #include "WCFixedString.h"
30 // These are preprocessor macros rather than inline functions because most compilers can't
31 // resolve functions at compile-time.
32 #if _BYTEORDER_BIG_ENDIAN==1
33 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
34 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
35 #define FOURCC_COMPILER(a, b, c, d) FOURCC_BIG(a,b,c,d)
36 #elif _BYTEORDER_BIG_ENDIAN==0
37 #define FOURCC_BIG(a, b, c, d) ((uint32_t(a)<< 0)|(uint32_t(b)<< 8)|(uint32_t(c)<<16)|(uint32_t(d)<<24))
38 #define FOURCC_LITTLE(a, b, c, d) ((uint32_t(a)<<24)|(uint32_t(b)<<16)|(uint32_t(c)<< 8)|(uint32_t(d)<< 0))
39 #define FOURCC_COMPILER(a, b, c, d) FOURCC_LITTLE(a,b,c,d)
41 #error _BYTEORDER_BIG_ENDIAN not defined proparly
42 #endif // _BYTEORDER_HPP_BIG_ENDIAN
44 typedef uint32_t WTFourCharCode;
46 #ifndef kEnableWCFourCCDebug
47 #define kEnableWCFourCCDebug 0 // set to 1 to enable debug members
55 static WTFourCharCode stored_from_iter(_iter& i)
57 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]);
62 // static const WCFourCC kDefaultFourCC_prv;
64 static WCFourCC kDefaultFourCC_prv() { return WCFourCC(); }
66 // change this line will change the byte order in which WCFourCC keeps the four char code
67 static const wvNS::wvBO::byte_order_type s_stored_byte_order = wvNS::wvBO::compiler_byte_order;
69 WCFourCC(const char a, const char b, const char c, const char d) :
70 m_stored_value(s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? FOURCC_BIG(a,b,c,d) : FOURCC_LITTLE(a,b,c,d))
72 #if kEnableWCFourCCDebug == 1
73 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
78 m_stored_value(FOURCC_BIG('?','?','?','?')) // since the four chars are the same, there is no need to choose between big & little
80 #if kEnableWCFourCCDebug == 1
81 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
85 WCFourCC(const WTFourCharCode in_fourCharCode, const wvNS::wvBO::byte_order_type in_byteOrder = wvNS::wvBO::compiler_byte_order) :
86 m_stored_value(in_byteOrder==s_stored_byte_order ? in_fourCharCode : wvNS::wvBO::swap32(in_fourCharCode))
88 #if kEnableWCFourCCDebug == 1
89 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
93 explicit WCFourCC(const char* in_source_string) :
94 m_stored_value(stored_from_iter(in_source_string))
96 #if kEnableWCFourCCDebug == 1
97 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
101 explicit WCFourCC(const WCFixedStringBase& in_source_string) :
102 m_stored_value(stored_from_iter(in_source_string))
104 #if kEnableWCFourCCDebug == 1
105 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
109 WTFourCharCode GetAsSomeEndian(const wvNS::wvBO::byte_order_type in_byteOrder) const
111 return s_stored_byte_order==in_byteOrder ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
114 WTFourCharCode GetAsBigEndian() const
116 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
119 WTFourCharCode GetAsLittleEndian() const
121 return s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
124 WTFourCharCode GetAsCompilerEndian() const
126 return s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
129 WTFourCharCode GetAsStored() const
131 return m_stored_value;
134 char operator[](const unsigned int in_character_index) const
136 return char(m_stored_value >> (8 * (s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? 3-in_character_index : in_character_index)));
139 char& operator[](const unsigned int in_character_index)
141 return reinterpret_cast<char*>(&m_stored_value)[s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? 3-in_character_index : in_character_index];
146 return sizeof(WTFourCharCode);
149 static size_t max_size()
154 static size_t capacity()
159 WCFixedString4 GetString() const
161 WCFixedString4 retVal;
162 retVal << operator[](0) << operator[](1) << operator[](2) << operator[](3);
167 #if kEnableWCFourCCDebug == 1
168 const char* c_str() const
170 return m_c_str_stored_value;
177 #if kEnableWCFourCCDebug == 1
181 WTFourCharCode m_stored_value;
182 #if kEnableWCFourCCDebug == 1
183 char m_c_str_stored_value[sizeof(WTFourCharCode)+1];
187 WCFourCC& operator=(const WTFourCharCode); // we want initialization from literal to be dome through the constructor
190 inline bool operator<(const WCFourCC in_left, const WCFourCC in_right)
192 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) < in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
194 inline bool operator==(const WCFourCC in_left, const WCFourCC in_right)
196 return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) == in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
199 inline bool operator!=(const WCFourCC in_left, const WCFourCC in_right)
201 return ! operator==(in_left, in_right);
205 #define kDefaultFourCC WCFourCC::kDefaultFourCC_prv()
207 static const WCFourCC kZeroFourCC(0, wvNS::wvBO::compiler_byte_order);
209 #endif //#if !defined(__WCFourCC_h__)