2.5.11.
[asdcplib-cth.git] / src / KM_platform.h
1 /*
2 Copyright (c) 2004-2015, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27   /*! \file    KM_platform.h
28     \version $Id: KM_platform.h,v 1.9 2015/10/07 16:41:23 jhurst Exp $
29     \brief   platform portability
30   */
31
32 #ifndef _KM_PLATFORM_H_
33 # define _KM_PLATFORM_H_
34
35 #if defined(__APPLE__) && defined(__MACH__)
36 #  define KM_MACOSX
37 #  ifdef __BIG_ENDIAN__
38 #   define KM_BIG_ENDIAN
39 #  endif
40 # endif
41
42 # ifdef KM_WIN32
43 #  define WIN32_LEAN_AND_MEAN
44 #  define VC_EXTRALEAN
45 #  include <windows.h>
46 #  include <stdlib.h>
47 #  include <stdio.h>
48 #  include <stdarg.h>
49 #  pragma warning(disable:4786)                 // Ignore "identifer > 255 characters" warning
50
51 typedef unsigned __int64   ui64_t;
52 typedef __int64            i64_t;
53 #  define i64_C(c)  (i64_t)(c)
54 #  define ui64_C(c) (ui64_t)(c)
55 #  define snprintf _snprintf
56 #  define vsnprintf _vsnprintf
57
58 # else // KM_WIN32
59 typedef unsigned long long ui64_t;
60 typedef long long          i64_t;
61 #  define i64_C(c)  c##LL
62 #  define ui64_C(c) c##ULL
63
64 # endif // KM_WIN32
65
66 # include <stdio.h>
67 # include <assert.h>
68 # include <stdlib.h>
69 # include <limits.h>
70
71 typedef unsigned char  byte_t;
72 typedef char           i8_t;
73 typedef unsigned char  ui8_t;
74 typedef short          i16_t;
75 typedef unsigned short ui16_t;
76 typedef int            i32_t;
77 typedef unsigned int   ui32_t;
78
79
80 namespace Kumu
81 {
82   inline ui16_t Swap2(ui16_t i)
83     {
84       return ( (i << 8) | (( i & 0xff00) >> 8) );
85     }
86
87   inline ui32_t Swap4(ui32_t i)
88     {
89       return
90         ( (i & 0x000000ffUL) << 24 ) |
91         ( (i & 0xff000000UL) >> 24 ) |
92         ( (i & 0x0000ff00UL) << 8  ) |
93         ( (i & 0x00ff0000UL) >> 8  );
94     }
95
96   inline ui64_t Swap8(ui64_t i)
97     {
98       return
99         ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
100         ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
101         ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
102         ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
103         ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
104         ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
105         ( (i & ui64_C(0x00000000FF000000)) << 8  ) |
106         ( (i & ui64_C(0x000000FF00000000)) >> 8  );
107     }
108
109   //
110   template<class T>
111     inline T xmin(T lhs, T rhs) {
112     return (lhs < rhs) ? lhs : rhs;
113   }
114
115   //
116   template<class T>
117     inline T xmax(T lhs, T rhs) {
118     return (lhs > rhs) ? lhs : rhs;
119   }
120
121   //
122   template<class T>
123     inline T xclamp(T v, T l, T h) {
124     if ( v < l ) { return l; }
125     if ( v > h ) { return h; }
126     return v;
127   }
128
129   //
130   template<class T>
131     inline T xabs(T n) {
132     if ( n < 0 ) { return -n; }
133     return n;
134   }
135
136   // read an integer from byte-structured storage
137   template<class T>
138   inline T    cp2i(const byte_t* p) { return *(T*)p; }
139
140   // write an integer to byte-structured storage
141   template<class T>
142   inline void i2p(T i, byte_t* p) { *(T*)p = i; }
143
144
145 # ifdef KM_BIG_ENDIAN
146 #  define KM_i16_LE(i)        Kumu::Swap2(i)
147 #  define KM_i32_LE(i)        Kumu::Swap4(i)
148 #  define KM_i64_LE(i)        Kumu::Swap8(i)
149 #  define KM_i16_BE(i)        (i)
150 #  define KM_i32_BE(i)        (i)
151 #  define KM_i64_BE(i)        (i)
152 # else
153 #  define KM_i16_LE(i)        (i)
154 #  define KM_i32_LE(i)        (i)
155 #  define KM_i64_LE(i)        (i)
156 #  define KM_i16_BE(i)        Kumu::Swap2(i)
157 #  define KM_i32_BE(i)        Kumu::Swap4(i)
158 #  define KM_i64_BE(i)        Kumu::Swap8(i)
159 # endif // KM_BIG_ENDIAN
160
161   // A non-reference counting, auto-delete container for internal
162   // member object pointers.
163   template <class T>
164     class mem_ptr
165     {
166       mem_ptr(T&);
167
168     protected:
169       T* m_p; // the thing we point to
170
171     public:
172       mem_ptr() : m_p(0) {}
173       mem_ptr(T* p) : m_p(p) {}
174       ~mem_ptr() { delete m_p; }
175
176       inline T&   operator*()  const { return *m_p; }
177       inline T*   operator->() const { assert(m_p!=0); return m_p; }
178       inline      operator T*()const { return m_p; }
179       inline const mem_ptr<T>& operator=(T* p) { this->set(p); return *this; }
180       inline T*   set(T* p)          { delete m_p; m_p = p; return m_p; }
181       inline T*   get()        const { return m_p; }
182       inline void release()          { m_p = 0; }
183       inline bool empty()      const { return m_p == 0; }
184     };
185
186 } // namespace Kumu
187
188 // Produces copy constructor boilerplate. Allows convenient private
189 // declatarion of copy constructors to prevent the compiler from
190 // silently manufacturing default methods.
191 # define KM_NO_COPY_CONSTRUCT(T)   \
192           T(const T&); \
193           T& operator=(const T&)
194
195 /*
196 // Example
197   class foo
198     {
199       KM_NO_COPY_CONSTRUCT(foo); // accessing private mthods will cause compile time error
200     public:
201       // ...
202     };
203 */
204
205 // Produces copy constructor boilerplate. Implements
206 // copy and assignment, see example below
207 # define KM_EXPLICIT_COPY_CONSTRUCT(T)  \
208   T(const T&);                          \
209   const T& operator=(const T&)
210
211 # define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(N, T)    \
212   void T##_copy_impl(N::T& lhs, const N::T& rhs)        \
213   {
214
215 #define KM_COPY_ITEM(I) lhs.I = rhs.I;
216
217 # define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(N, T)      \
218   }                                                     \
219   N::T::T(const N::T& rhs) { T##_copy_impl(*this, rhs); }               \
220   const N::T& N::T::operator=(const N::T& rhs) { T##_copy_impl(*this, rhs); return *this; }
221
222 /*
223 // Example
224 namespace bar {
225   class foo
226     {
227     public:
228       std::string param_a;
229       int param_b;
230
231       KM_EXPLICIT_COPY_CONSTRUCT(foo);
232       // ...
233     };
234 }
235
236 //
237 KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(bar, foo)
238 KM_COPY_ITEM(param_a)
239 KM_COPY_ITEM(param_b)
240 KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(bar, foo)
241 */
242
243 #endif // _KM_PLATFORM_H_
244
245 //
246 // KM_platform.h
247 //