update wavesaudio backend, now supports Windows (ASIO) as well as OS X (CoreAudio)
[ardour.git] / libs / backends / wavesaudio / wavesapi / BasicTypes / WCFourCC.h
1 #ifndef __WCFourCC_h__
2         #define __WCFourCC_h__
3         
4 /* Copy to include
5 #include "BasicTypes/WCFourCC.h"
6 */
7
8 //#include "BasicTypes/WTByteOrder.h"
9 #include "WCFixedString.h"
10
11
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)
22 #else
23         #error _BYTEORDER_BIG_ENDIAN not defined proparly
24 #endif // _BYTEORDER_HPP_BIG_ENDIAN
25
26 typedef uint32_t WTFourCharCode;
27
28 #ifndef kEnableWCFourCCDebug
29         #define kEnableWCFourCCDebug 0 // set to 1 to enable debug members
30 #endif
31
32
33 class WCFourCC
34 {
35 private:
36         template<class _iter> 
37         static WTFourCharCode stored_from_iter(_iter& i)
38         {
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]);
40         }
41
42 public:
43
44         //      static const WCFourCC kDefaultFourCC_prv;
45
46         static WCFourCC kDefaultFourCC_prv() { return WCFourCC(); }
47
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;
50
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))
53         {
54 #if kEnableWCFourCCDebug == 1
55                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
56 #endif
57         }
58
59         WCFourCC() :
60                 m_stored_value(FOURCC_BIG('?','?','?','?'))      // since the four chars are the same, there is no need to choose between big & little
61         {
62 #if kEnableWCFourCCDebug == 1
63                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
64 #endif
65         }
66
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))
69         {
70 #if kEnableWCFourCCDebug == 1
71                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
72 #endif
73         }
74
75         explicit WCFourCC(const char* in_source_string) :
76                 m_stored_value(stored_from_iter(in_source_string))
77         {
78 #if kEnableWCFourCCDebug == 1
79                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
80 #endif
81         }
82
83         explicit WCFourCC(const WCFixedStringBase& in_source_string) :
84                 m_stored_value(stored_from_iter(in_source_string))
85         {
86 #if kEnableWCFourCCDebug == 1
87                 m_c_str_stored_value[sizeof(WTFourCharCode)] = '\0';
88 #endif
89         }
90
91         WTFourCharCode GetAsSomeEndian(const wvNS::wvBO::byte_order_type in_byteOrder) const
92         {
93                 return s_stored_byte_order==in_byteOrder ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
94         }
95
96         WTFourCharCode GetAsBigEndian() const
97         {
98                 return s_stored_byte_order==wvNS::wvBO::byte_order_big_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
99         }
100
101         WTFourCharCode GetAsLittleEndian() const
102         {
103                 return s_stored_byte_order==wvNS::wvBO::byte_order_little_endian ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
104         }
105
106         WTFourCharCode GetAsCompilerEndian() const
107         {
108                 return s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? m_stored_value : wvNS::wvBO::swap32(m_stored_value);
109         }
110
111         WTFourCharCode GetAsStored() const
112         {
113                 return m_stored_value;
114         }
115
116         char operator[](const unsigned int in_character_index) const
117         {
118                 return char(m_stored_value >> (8 * (s_stored_byte_order==wvNS::wvBO::compiler_byte_order ? 3-in_character_index : in_character_index)));
119         }
120
121         char& operator[](const unsigned int in_character_index)
122         {
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];
124         }
125     
126     static size_t size()
127     {
128         return sizeof(WTFourCharCode);
129     }
130
131         static size_t max_size()
132         {
133                 return size();
134         }
135     
136         static size_t capacity()
137         {
138                 return size();
139         }
140     
141         WCFixedString4 GetString() const
142         {
143                 WCFixedString4 retVal;
144                 retVal << operator[](0) << operator[](1) << operator[](2) << operator[](3);
145
146                 return retVal;
147         }
148
149 #if kEnableWCFourCCDebug == 1
150         const char* c_str() const
151         {
152                 return m_c_str_stored_value;
153         }
154 #endif
155
156 protected:
157
158 private:
159 #if kEnableWCFourCCDebug == 1
160         union
161         {
162 #endif
163                 WTFourCharCode m_stored_value;
164 #if kEnableWCFourCCDebug == 1
165                 char m_c_str_stored_value[sizeof(WTFourCharCode)+1];
166         };
167 #endif
168
169         WCFourCC& operator=(const WTFourCharCode); // we want initialization from literal to be dome through the constructor
170 };
171
172 inline bool operator<(const WCFourCC in_left, const WCFourCC in_right)
173 {
174         return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) < in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
175 }
176 inline bool operator==(const WCFourCC in_left, const WCFourCC in_right)
177 {
178         return in_left.GetAsSomeEndian(WCFourCC::s_stored_byte_order) == in_right.GetAsSomeEndian(WCFourCC::s_stored_byte_order);
179 }
180
181 inline bool operator!=(const WCFourCC in_left, const WCFourCC in_right)
182 {
183         return ! operator==(in_left, in_right);
184 }
185
186
187 #define kDefaultFourCC WCFourCC::kDefaultFourCC_prv()
188
189 static const WCFourCC kZeroFourCC(0, wvNS::wvBO::compiler_byte_order);
190         
191 #endif //#if !defined(__WCFourCC_h__)
192
193
194