HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Foundation.h
Go to the documentation of this file.
1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2015,
4 // Sony Pictures Imageworks Inc. and
5 // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following disclaimer
16 // in the documentation and/or other materials provided with the
17 // distribution.
18 // * Neither the name of Industrial Light & Magic nor the names of
19 // its contributors may be used to endorse or promote products derived
20 // from this software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 //-*****************************************************************************
35 
36 #ifndef Alembic_Util_Foundation_h
37 #define Alembic_Util_Foundation_h
38 
39 #include <Alembic/Util/Config.h>
40 
41 #ifdef ALEMBIC_LIB_USES_BOOST
42 #include <boost/type_traits.hpp>
43 #include <boost/ref.hpp>
44 #include <boost/format.hpp>
45 #include <boost/smart_ptr.hpp>
46 #include <boost/static_assert.hpp>
47 #include <boost/utility.hpp>
48 #include <boost/cstdint.hpp>
49 #include <boost/array.hpp>
50 #include <boost/operators.hpp>
51 #include <boost/foreach.hpp>
52 #include <boost/unordered_map.hpp>
53 
54 // tr1 is not available in older versions of Visual Studio i.e. _MSC_VER <= 1600
55 #elif defined(ALEMBIC_LIB_USES_TR1)
56 #include <tr1/memory>
57 #include <tr1/unordered_map>
58 
59 // default to C++11
60 #else
61 #include <unordered_map>
62 #endif
63 
64 #include <memory>
65 
66 #include <half.h>
67 
68 #include <iomanip>
69 #include <iostream>
70 #include <sstream>
71 #include <exception>
72 #include <limits>
73 
74 #include <list>
75 #include <map>
76 #include <string>
77 #include <vector>
78 
79 #include <cstdio>
80 #include <cstdlib>
81 #include <cstring>
82 #include <cassert>
83 
84 #include <Alembic/Util/Export.h>
85 
86 #ifdef _MSC_VER
87 
88 #ifndef WIN32_LEAN_AND_MEAN
89 #define WIN32_LEAN_AND_MEAN
90 #endif
91 
92 // avoid windows min/max predefined macro conflicts
93 #ifndef NOMINMAX
94 #define NOMINMAX
95 #endif
96 
97 // needed for mutex stuff
98 #include <Windows.h>
99 #endif
100 
101 // needed for std min/max
102 #include <algorithm>
103 
104 // When we change this version (because of an ABI change) Make sure to at
105 // LEAST bump the minor version in CMakeLists.txt i.e. PROJECT_VERSION_MINOR
106 // This way we will hopefully not break any distros that kindly include us.
107 // See Issue243
108 #ifndef ALEMBIC_VERSION_NS
109 #define ALEMBIC_VERSION_NS v15_sidefx
110 #endif
111 
112 namespace Alembic {
113 namespace Util {
114 namespace ALEMBIC_VERSION_NS {
115 
116 // similiar to boost::noncopyable
117 // explicitly hides copy construction and copy assignment
119 {
120 protected:
123 
124 private:
125  noncopyable( const noncopyable& );
126  const noncopyable& operator=( const noncopyable& );
127 };
128 
129 #ifdef ALEMBIC_LIB_USES_BOOST
130 using boost::dynamic_pointer_cast;
131 using boost::enable_shared_from_this;
132 using boost::shared_ptr;
133 using boost::static_pointer_cast;
134 using boost::weak_ptr;
135 using boost::unordered_map;
136 
137 #elif defined(ALEMBIC_LIB_USES_TR1)
138 using std::tr1::dynamic_pointer_cast;
139 using std::tr1::enable_shared_from_this;
140 using std::tr1::shared_ptr;
141 using std::tr1::static_pointer_cast;
142 using std::tr1::weak_ptr;
143 using std::tr1::unordered_map;
144 
145 #else
146 using std::dynamic_pointer_cast;
147 using std::enable_shared_from_this;
148 using std::shared_ptr;
149 using std::static_pointer_cast;
150 using std::weak_ptr;
151 using std::unordered_map;
152 using std::unique_ptr;
153 #endif
154 
155 #if defined(ALEMBIC_LIB_USES_BOOST) || defined(ALEMBIC_LIB_USES_TR1)
156 
157 // define a very simple scoped ptr since unique_ptr isn't consistently
158 // available on boost versions. Otherwise we could use boost::scoped_ptr
159 // or the deprecated std::auto_ptr for tr1.
160 template<typename T>
161 class unique_ptr : noncopyable
162 {
163 public:
164  unique_ptr()
165  {
166  p = NULL;
167  }
168 
169  unique_ptr( T* val ) : p(val)
170  {
171  }
172 
173  ~unique_ptr()
174  {
175  delete p;
176  }
177 
178  void reset( T* val )
179  {
180  delete p;
181  p = val;
182  }
183 
184  T* operator->() const
185  {
186  return p;
187  }
188 private:
189  T* p;
190 };
191 
192 #endif
193 
194 // similiar to boost::totally_ordered
195 // only need < and == operators and this fills in the rest
196 template < class T >
198 {
199  friend bool operator > ( const T& x, const T& y )
200  {
201  return y < x;
202  }
203 
204  friend bool operator <= ( const T& x, const T& y )
205  {
206  return !( y < x );
207  }
208 
209  friend bool operator >= ( const T& x, const T& y )
210  {
211  return !( x < y );
212  }
213 
214  friend bool operator != ( const T& x, const T& y )
215  {
216  return !( x == y );
217  }
218 };
219 
220 // inspired by boost::mutex
221 #ifdef _MSC_VER
222 
223 class mutex : noncopyable
224 {
225 public:
226  mutex()
227  {
228  InitializeCriticalSection(&cs);
229  }
230 
231  ~mutex()
232  {
233  DeleteCriticalSection(&cs);
234  }
235 
236  void lock()
237  {
238  EnterCriticalSection(&cs);
239  }
240 
241  void unlock()
242  {
243  LeaveCriticalSection(&cs);
244  }
245 
246 private:
247  CRITICAL_SECTION cs;
248 };
249 
250 #else
251 
252 
254 {
255 public:
257  {
258  pthread_mutex_init( &m, NULL );
259  }
260 
262  {
263  pthread_mutex_destroy( &m );
264  }
265 
266  void lock()
267  {
268  pthread_mutex_lock( &m );
269  }
270 
271  void unlock()
272  {
273  pthread_mutex_unlock( &m );
274  }
275 
276 private:
277  pthread_mutex_t m;
278 };
279 
280 #endif
281 
283 {
284 public:
285  scoped_lock( mutex & l ) : m( l )
286  {
287  m.lock();
288  }
289 
291  {
292  m.unlock();
293  }
294 
295 private:
296  mutex & m;
297 };
298 
299 } // End namespace ALEMBIC_VERSION_NS
300 
301 using namespace ALEMBIC_VERSION_NS;
302 
303 } // End namespace Util
304 } // End namespace Alembic
305 
306 #endif
#define ALEMBIC_EXPORT
Definition: Export.h:51
friend bool operator>=(const T &x, const T &y)
Definition: Foundation.h:209
GLdouble l
Definition: glew.h:9164
GLint GLenum GLint x
Definition: glcorearb.h:408
GLfloat GLfloat p
Definition: glew.h:16656
GLboolean reset
Definition: glew.h:4989
friend bool operator>(const T &x, const T &y)
Definition: Foundation.h:199
GLuint GLfloat * val
Definition: glcorearb.h:1607
friend bool operator<=(const T &x, const T &y)
Definition: Foundation.h:204
const GLdouble * m
Definition: glew.h:9166
friend bool operator!=(const T &x, const T &y)
Definition: Foundation.h:214
GLint y
Definition: glcorearb.h:102
#define ALEMBIC_VERSION_NS
Definition: Foundation.h:109