added dist hook for new napali source.
[asdcplib.git] / src / KM_platform.h
1 /*
2 Copyright (c) 2004-2009, 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$
29     \brief   platform portability
30   */
31
32 #ifndef _KM_PLATFORM_H_
33 # define _KM_PLATFORM_H_
34
35 # ifdef __APPLE__
36 #  ifdef __BIG_ENDIAN__
37 #   define KM_BIG_ENDIAN
38 #  endif
39 # endif
40
41 # ifdef KM_WIN32
42 #  define WIN32_LEAN_AND_MEAN
43 #  define VC_EXTRALEAN
44 #  include <windows.h>
45 /* we like the "SendMessage" name, so get rid of the preprocessor define
46  * and replace with an inline function */
47 #  undef SendMessage
48 #ifdef UNICODE\r
49 inline\r
50 WINUSERAPI\r
51 LRESULT\r
52 WINAPI\r
53 SendMessage(\r
54     __in HWND hWnd,\r
55     __in UINT Msg,\r
56     __in WPARAM wParam,\r
57     __in LPARAM lParam)\r
58 {\r
59         return SendMessageW(hWnd, Msg, wParam, lParam);\r
60 }\r
61 #else\r
62 inline\r
63 WINUSERAPI\r
64 LRESULT\r
65 WINAPI\r
66 SendMessage(\r
67     __in HWND hWnd,\r
68     __in UINT Msg,\r
69     __in WPARAM wParam,\r
70     __in LPARAM lParam)\r
71 {\r
72         return SendMessageA(hWnd, Msg, wParam, lParam);\r
73 }\r
74 #endif // !UNICODE\r
75
76 #  include <stdlib.h>
77 #  include <stdio.h>
78 #  include <stdarg.h>
79 #  pragma warning(disable:4786)                 // Ignore "identifer > 255 characters" warning
80
81 typedef unsigned __int64   ui64_t;
82 typedef __int64            i64_t;
83 #  define i64_C(c)  (i64_t)(c)
84 #  define ui64_C(c) (ui64_t)(c)
85 #  define snprintf _snprintf
86 #  define vsnprintf _vsnprintf
87
88 # else // KM_WIN32
89 typedef unsigned long long ui64_t;
90 typedef long long          i64_t;
91 #  define i64_C(c)  c##LL
92 #  define ui64_C(c) c##ULL
93
94 # endif // KM_WIN32
95
96 # include <stdio.h>
97 # include <assert.h>
98 # include <stdlib.h>
99 # include <limits.h>
100
101 typedef unsigned char  byte_t;
102 typedef char           i8_t;
103 typedef unsigned char  ui8_t;
104 typedef short          i16_t;
105 typedef unsigned short ui16_t;
106 typedef int            i32_t;
107 typedef unsigned int   ui32_t;
108
109
110 namespace Kumu
111 {
112   inline ui16_t Swap2(ui16_t i)
113     {
114       return ( (i << 8) | (( i & 0xff00) >> 8) );
115     }
116
117   inline ui32_t Swap4(ui32_t i)
118     {
119       return
120         ( (i & 0x000000ffUL) << 24 ) |
121         ( (i & 0xff000000UL) >> 24 ) |
122         ( (i & 0x0000ff00UL) << 8  ) |
123         ( (i & 0x00ff0000UL) >> 8  );
124     }
125
126   inline ui64_t Swap8(ui64_t i)
127     {
128       return
129         ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
130         ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
131         ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
132         ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
133         ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
134         ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
135         ( (i & ui64_C(0x00000000FF000000)) << 8  ) |
136         ( (i & ui64_C(0x000000FF00000000)) >> 8  );
137     }
138
139   //
140   template<class T>
141     inline T xmin(T lhs, T rhs) {
142     return (lhs < rhs) ? lhs : rhs;
143   }
144
145   //
146   template<class T>
147     inline T xmax(T lhs, T rhs) {
148     return (lhs > rhs) ? lhs : rhs;
149   }
150
151   //
152   template<class T>
153     inline T xclamp(T v, T l, T h) {
154     if ( v < l ) return l;
155     if ( v > h ) return h;
156     return v;
157   }
158
159
160   // read an integer from byte-structured storage
161   template<class T>
162   inline T    cp2i(const byte_t* p) { return *(T*)p; }
163
164   // write an integer to byte-structured storage
165   template<class T>
166   inline void i2p(T i, byte_t* p) { *(T*)p = i; }
167
168 # ifdef KM_BIG_ENDIAN
169 #  define KM_i16_LE(i)        Kumu::Swap2(i)
170 #  define KM_i32_LE(i)        Kumu::Swap4(i)
171 #  define KM_i64_LE(i)        Kumu::Swap8(i)
172 #  define KM_i16_BE(i)        (i)
173 #  define KM_i32_BE(i)        (i)
174 #  define KM_i64_BE(i)        (i)
175 # else
176 #  define KM_i16_LE(i)        (i)
177 #  define KM_i32_LE(i)        (i)
178 #  define KM_i64_LE(i)        (i)
179 #  define KM_i16_BE(i)        Kumu::Swap2(i)
180 #  define KM_i32_BE(i)        Kumu::Swap4(i)
181 #  define KM_i64_BE(i)        Kumu::Swap8(i)
182 # endif // KM_BIG_ENDIAN
183
184   // A non-reference counting, auto-delete container for internal
185   // member object pointers.
186   template <class T>
187     class mem_ptr
188     {
189       mem_ptr(T&);
190
191     protected:
192       T* m_p; // the thing we point to
193
194     public:
195       mem_ptr() : m_p(0) {}
196       mem_ptr(T* p) : m_p(p) {}
197       ~mem_ptr() { delete m_p; }
198
199       inline T&   operator*()  const { return *m_p; }
200       inline T*   operator->() const { return m_p; }
201       inline      operator T*()const { return m_p; }
202       inline const mem_ptr<T>& operator=(T* p) { this->set(p); return *this; }
203       inline T*   set(T* p)          { delete m_p; m_p = p; return m_p; }
204       inline T*   get()        const { return m_p; }
205       inline void release()          { m_p = 0; }
206       inline bool empty()      const { return m_p == 0; }
207     };
208
209 } // namespace Kumu
210
211 // Produces copy constructor boilerplate. Allows convenient private
212 // declatarion of copy constructors to prevent the compiler from
213 // silently manufacturing default methods.
214 # define KM_NO_COPY_CONSTRUCT(T)   \
215           T(const T&); \
216           T& operator=(const T&)
217
218 /*
219 // Example
220   class foo
221     {
222       KM_NO_COPY_CONSTRUCT(foo); // accessing private mthods will cause compile time error
223     public:
224       // ...
225     };
226 */
227
228 // Produces copy constructor boilerplate. Implements
229 // copy and assignment, see example below
230 # define KM_EXPLICIT_COPY_CONSTRUCT(T)  \
231   T(const T&);                          \
232   const T& operator=(const T&)
233
234 # define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(N, T)    \
235   void T##_copy_impl(N::T& lhs, const N::T& rhs)        \
236   {
237
238 #define KM_COPY_ITEM(I) lhs.I = rhs.I;
239
240 # define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(N, T)      \
241   }                                                     \
242   N::T::T(const N::T& rhs) { T##_copy_impl(*this, rhs); }               \
243   const N::T& N::T::operator=(const N::T& rhs) { T##_copy_impl(*this, rhs); return *this; }
244
245 /*
246 // Example
247 namespace bar {
248   class foo
249     {
250     public:
251       std::string param_a;
252       int param_b;
253
254       KM_EXPLICIT_COPY_CONSTRUCT(foo);
255       // ...
256     };
257 }
258
259 //
260 KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(bar, foo)
261 KM_COPY_ITEM(param_a)
262 KM_COPY_ITEM(param_b)
263 KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(bar, foo)
264 */
265
266 #endif // _KM_PLATFORM_H_
267
268 //
269 // KM_platform.h
270 //