new audio engine backend for native CoreAudio audio I/O, and PortMIDI for MIDI.
[ardour.git] / libs / backends / wavesaudio / wavesapi / BasicTypes / WCFourCC.h
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
3
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.
8
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.
13
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.
17
18 */
19 #ifndef __WCFourCC_h__
20         #define __WCFourCC_h__
21         
22 /* Copy to include
23 #include "BasicTypes/WCFourCC.h"
24 */
25
26 //#include "BasicTypes/WTByteOrder.h"
27 #include "WCFixedString.h"
28
29
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)
40 #else
41         #error _BYTEORDER_BIG_ENDIAN not defined proparly
42 #endif // _BYTEORDER_HPP_BIG_ENDIAN
43
44 typedef uint32_t WTFourCharCode;
45
46 #ifndef kEnableWCFourCCDebug
47         #define kEnableWCFourCCDebug 0 // set to 1 to enable debug members
48 #endif
49
50
51 class WCFourCC
52 {
53 private:
54         template<class _iter> 
55         static WTFourCharCode stored_from_iter(_iter& i)
56         {
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]);
58         }
59
60 public:
61
62         //      static const WCFourCC kDefaultFourCC_prv;
63
64         static WCFourCC kDefaultFourCC_prv() { return WCFourCC(); }
65
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;
68
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))
71         {
72 #if kEnableWCFourCCDebug == 1
73                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
74 #endif
75         }
76
77         WCFourCC() :
78                 m_stored_value(FOURCC_BIG('?','?','?','?'))      // since the four chars are the same, there is no need to choose between big & little
79         {
80 #if kEnableWCFourCCDebug == 1
81                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
82 #endif
83         }
84
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))
87         {
88 #if kEnableWCFourCCDebug == 1
89                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
90 #endif
91         }
92
93         explicit WCFourCC(const char* in_source_string) :
94                 m_stored_value(stored_from_iter(in_source_string))
95         {
96 #if kEnableWCFourCCDebug == 1
97                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
98 #endif
99         }
100
101         explicit WCFourCC(const WCFixedStringBase& in_source_string) :
102                 m_stored_value(stored_from_iter(in_source_string))
103         {
104 #if kEnableWCFourCCDebug == 1
105                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
106 #endif
107         }
108
109         WTFourCharCode GetAsSomeEndian(const wvNS::wvBO::byte_order_type in_byteOrder) const
110         {
111                 return s_stored_byte_order==in_byteOrder ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
112         }
113
114         WTFourCharCode GetAsBigEndian() const
115         {
116                 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
117         }
118
119         WTFourCharCode GetAsLittleEndian() const
120         {
121                 return s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
122         }
123
124         WTFourCharCode GetAsCompilerEndian() const
125         {
126                 return s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
127         }
128
129         WTFourCharCode GetAsStored() const
130         {
131                 return m_stored_value;
132         }
133
134         char operator[](const unsigned int in_character_index) const
135         {
136                 return char(m_stored_value >> (8 * (s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? 3-in_character_index : in_character_index)));
137         }
138
139         char& operator[](const unsigned int in_character_index)
140         {
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];
142         }
143     
144     static size_t size()
145     {
146         return sizeof(WTFourCharCode);
147     }
148
149         static size_t max_size()
150         {
151                 return size();
152         }
153     
154         static size_t capacity()
155         {
156                 return size();
157         }
158     
159         WCFixedString4 GetString() const
160         {
161                 WCFixedString4 retVal;
162                 retVal << operator[](0) << operator[](1) << operator[](2) << operator[](3);
163
164                 return retVal;
165         }
166
167 #if kEnableWCFourCCDebug == 1
168         const char* c_str() const
169         {
170                 return m_c_str_stored_value;
171         }
172 #endif
173
174 protected:
175
176 private:
177 #if kEnableWCFourCCDebug == 1
178         union
179         {
180 #endif
181                 WTFourCharCode m_stored_value;
182 #if kEnableWCFourCCDebug == 1
183                 char m_c_str_stored_value[sizeof(WTFourCharCode)+1];
184         };
185 #endif
186
187         WCFourCC& operator=(const WTFourCharCode); // we want initialization from literal to be dome through the constructor
188 };
189
190 inline bool operator<(const WCFourCC in_left, const WCFourCC in_right)
191 {
192         return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) < in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
193 }
194 inline bool operator==(const WCFourCC in_left, const WCFourCC in_right)
195 {
196         return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) == in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
197 }
198
199 inline bool operator!=(const WCFourCC in_left, const WCFourCC in_right)
200 {
201         return ! operator==(in_left, in_right);
202 }
203
204
205 #define kDefaultFourCC WCFourCC::kDefaultFourCC_prv()
206
207 static const WCFourCC kZeroFourCC(0, wvNS::wvBO::compiler_byte_order);
208         
209 #endif //#if !defined(__WCFourCC_h__)
210
211
212