summaryrefslogtreecommitdiff
path: root/src/KM_platform.h
blob: defcd8a23b41c13cf9791b9df49dc661ed88853f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright (c) 2004-2015, John Hurst
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
  /*! \file    KM_platform.h
    \version $Id$
    \brief   platform portability
  */

#ifndef _KM_PLATFORM_H_
# define _KM_PLATFORM_H_

#if defined(__APPLE__) && defined(__MACH__)
#  define KM_MACOSX
#  ifdef __BIG_ENDIAN__
#   define KM_BIG_ENDIAN
#  endif
# endif

# ifdef KM_WIN32
#  define WIN32_LEAN_AND_MEAN
#  define VC_EXTRALEAN
#  include <windows.h>
#  include <stdlib.h>
#  include <stdio.h>
#  include <stdarg.h>
#  pragma warning(disable:4786)			// Ignore "identifer > 255 characters" warning

typedef unsigned __int64   ui64_t;
typedef __int64            i64_t;
#  define i64_C(c)  (i64_t)(c)
#  define ui64_C(c) (ui64_t)(c)
#  define snprintf _snprintf
#  define vsnprintf _vsnprintf

# else // KM_WIN32
typedef unsigned long long ui64_t;
typedef long long          i64_t;
#  define i64_C(c)  c##LL
#  define ui64_C(c) c##ULL

# endif // KM_WIN32

# include <stdio.h>
# include <assert.h>
# include <stdlib.h>
# include <limits.h>

typedef unsigned char  byte_t;
typedef char           i8_t;
typedef unsigned char  ui8_t;
typedef short          i16_t;
typedef unsigned short ui16_t;
typedef int            i32_t;
typedef unsigned int   ui32_t;


namespace Kumu
{
  inline ui16_t Swap2(ui16_t i)
    {
      return ( (i << 8) | (( i & 0xff00) >> 8) );
    }

  inline ui32_t Swap4(ui32_t i)
    {
      return
	( (i & 0x000000ffUL) << 24 ) |
	( (i & 0xff000000UL) >> 24 ) |
	( (i & 0x0000ff00UL) << 8  ) |
	( (i & 0x00ff0000UL) >> 8  );
    }

  inline ui64_t Swap8(ui64_t i)
    {
      return
	( (i & ui64_C(0x00000000000000FF)) << 56 ) |
	( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
	( (i & ui64_C(0x000000000000FF00)) << 40 ) |
	( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
	( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
	( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
	( (i & ui64_C(0x00000000FF000000)) << 8  ) |
	( (i & ui64_C(0x000000FF00000000)) >> 8  );
    }

  //
  template<class T>
    inline T xmin(T lhs, T rhs) {
    return (lhs < rhs) ? lhs : rhs;
  }

  //
  template<class T>
    inline T xmax(T lhs, T rhs) {
    return (lhs > rhs) ? lhs : rhs;
  }

  //
  template<class T>
    inline T xclamp(T v, T l, T h) {
    if ( v < l ) { return l; }
    if ( v > h ) { return h; }
    return v;
  }

  //
  template<class T>
    inline T xabs(T n) {
    if ( n < 0 ) { return -n; }
    return n;
  }

  // read an integer from byte-structured storage
  template<class T>
  inline T    cp2i(const byte_t* p) { return *(T*)p; }

  // write an integer to byte-structured storage
  template<class T>
  inline void i2p(T i, byte_t* p) { *(T*)p = i; }


# ifdef KM_BIG_ENDIAN
#  define KM_i16_LE(i)        Kumu::Swap2(i)
#  define KM_i32_LE(i)        Kumu::Swap4(i)
#  define KM_i64_LE(i)        Kumu::Swap8(i)
#  define KM_i16_BE(i)        (i)
#  define KM_i32_BE(i)        (i)
#  define KM_i64_BE(i)        (i)
# else
#  define KM_i16_LE(i)        (i)
#  define KM_i32_LE(i)        (i)
#  define KM_i64_LE(i)        (i)
#  define KM_i16_BE(i)        Kumu::Swap2(i)
#  define KM_i32_BE(i)        Kumu::Swap4(i)
#  define KM_i64_BE(i)        Kumu::Swap8(i)
# endif // KM_BIG_ENDIAN

  // A non-reference counting, auto-delete container for internal
  // member object pointers.
  template <class T>
    class mem_ptr
    {
      mem_ptr(T&);

    protected:
      T* m_p; // the thing we point to

    public:
      mem_ptr() : m_p(0) {}
      mem_ptr(T* p) : m_p(p) {}
      ~mem_ptr() { delete m_p; }

      inline T&   operator*()  const { return *m_p; }
      inline T*   operator->() const { assert(m_p!=0); return m_p; }
      inline      operator T*()const { return m_p; }
      inline const mem_ptr<T>& operator=(T* p) { this->set(p); return *this; }
      inline T*   set(T* p)          { delete m_p; m_p = p; return m_p; }
      inline T*   get()        const { return m_p; }
      inline void release()          { m_p = 0; }
      inline bool empty()      const { return m_p == 0; }
    };

} // namespace Kumu

// Produces copy constructor boilerplate. Allows convenient private
// declatarion of copy constructors to prevent the compiler from
// silently manufacturing default methods.
# define KM_NO_COPY_CONSTRUCT(T)   \
          T(const T&); \
          T& operator=(const T&)

/*
// Example
  class foo
    {
      KM_NO_COPY_CONSTRUCT(foo); // accessing private mthods will cause compile time error
    public:
      // ...
    };
*/

// Produces copy constructor boilerplate. Implements
// copy and assignment, see example below
# define KM_EXPLICIT_COPY_CONSTRUCT(T)	\
  T(const T&);				\
  const T& operator=(const T&)

# define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(N, T)	\
  void T##_copy_impl(N::T& lhs, const N::T& rhs)	\
  {

#define KM_COPY_ITEM(I) lhs.I = rhs.I;

# define KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(N, T)	\
  }							\
  N::T::T(const N::T& rhs) { T##_copy_impl(*this, rhs); }		\
  const N::T& N::T::operator=(const N::T& rhs) { T##_copy_impl(*this, rhs); return *this; }

/*
// Example
namespace bar {
  class foo
    {
    public:
      std::string param_a;
      int param_b;

      KM_EXPLICIT_COPY_CONSTRUCT(foo);
      // ...
    };
}

//
KM_EXPLICIT_COPY_CONSTRUCT_IMPL_START(bar, foo)
KM_COPY_ITEM(param_a)
KM_COPY_ITEM(param_b)
KM_EXPLICIT_COPY_CONSTRUCT_IMPL_END(bar, foo)
*/

#endif // _KM_PLATFORM_H_

//
// KM_platform.h
//