HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 <map>
75 #include <string>
76 #include <vector>
77 
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <assert.h>
82 
83 #include <Alembic/Util/Export.h>
84 
85 #ifdef _MSC_VER
86 
87 #ifndef WIN32_LEAN_AND_MEAN
88 #define WIN32_LEAN_AND_MEAN
89 #endif
90 
91 // avoid windows min/max predefined macro conflicts
92 #ifndef NOMINMAX
93 #define NOMINMAX
94 #endif
95 
96 // needed for mutex stuff
97 #include <Windows.h>
98 #endif
99 
100 // needed for std min/max
101 #include <algorithm>
102 
103 #ifndef ALEMBIC_VERSION_NS
104 #define ALEMBIC_VERSION_NS v8
105 #endif
106 
107 namespace Alembic {
108 namespace Util {
109 namespace ALEMBIC_VERSION_NS {
110 
111 // similiar to boost::noncopyable
112 // explicitly hides copy construction and copy assignment
114 {
115 protected:
118 
119 private:
120  noncopyable( const noncopyable& );
121  const noncopyable& operator=( const noncopyable& );
122 };
123 
124 #ifdef ALEMBIC_LIB_USES_BOOST
125 using boost::dynamic_pointer_cast;
126 using boost::enable_shared_from_this;
127 using boost::shared_ptr;
128 using boost::static_pointer_cast;
129 using boost::weak_ptr;
130 using boost::unordered_map;
131 
132 #elif defined(ALEMBIC_LIB_USES_TR1)
133 using std::tr1::dynamic_pointer_cast;
134 using std::tr1::enable_shared_from_this;
135 using std::tr1::shared_ptr;
136 using std::tr1::static_pointer_cast;
137 using std::tr1::weak_ptr;
138 using std::tr1::unordered_map;
139 
140 #else
141 using std::dynamic_pointer_cast;
142 using std::enable_shared_from_this;
143 using std::shared_ptr;
144 using std::static_pointer_cast;
145 using std::weak_ptr;
146 using std::unordered_map;
147 using std::unique_ptr;
148 #endif
149 
150 #if defined(ALEMBIC_LIB_USES_BOOST) || defined(ALEMBIC_LIB_USES_TR1)
151 
152 // define a very simple scoped ptr since unique_ptr isn't consistently
153 // available on boost versions. Otherwise we could use boost::scoped_ptr
154 // or the deprecated std::auto_ptr for tr1.
155 template<typename T>
156 class unique_ptr : noncopyable
157 {
158 public:
159  unique_ptr()
160  {
161  p = NULL;
162  }
163 
164  unique_ptr( T* val ) : p(val)
165  {
166  }
167 
168  ~unique_ptr()
169  {
170  if ( p )
171  {
172  delete p;
173  }
174  }
175 
176  void reset( T* val )
177  {
178  if ( p )
179  {
180  delete p;
181  }
182  p = val;
183  }
184 
185  T* operator->() const
186  {
187  return p;
188  }
189 private:
190  T* p;
191 };
192 
193 #endif
194 
195 // similiar to boost::totally_ordered
196 // only need < and == operators and this fills in the rest
197 template < class T >
199 {
200  friend bool operator > ( const T& x, const T& y )
201  {
202  return y < x;
203  }
204 
205  friend bool operator <= ( const T& x, const T& y )
206  {
207  return !( y < x );
208  }
209 
210  friend bool operator >= ( const T& x, const T& y )
211  {
212  return !( x < y );
213  }
214 
215  friend bool operator != ( const T& x, const T& y )
216  {
217  return !( x == y );
218  }
219 };
220 
221 // inspired by boost::mutex
222 #ifdef _MSC_VER
223 
224 class mutex : noncopyable
225 {
226 public:
227  mutex()
228  {
229  m = CreateMutex( NULL, FALSE, NULL );
230  }
231 
232  ~mutex()
233  {
234  CloseHandle( m );
235  }
236 
237  void lock()
238  {
239  WaitForSingleObject( m, INFINITE );
240  }
241 
242  void unlock()
243  {
244  ReleaseMutex( m );
245  }
246 
247 private:
248  HANDLE m;
249 };
250 
251 #else
252 
253 
255 {
256 public:
258  {
259  pthread_mutex_init( &m, NULL );
260  }
261 
263  {
264  pthread_mutex_destroy( &m );
265  }
266 
267  void lock()
268  {
269  pthread_mutex_lock( &m );
270  }
271 
272  void unlock()
273  {
274  pthread_mutex_unlock( &m );
275  }
276 
277 private:
278  pthread_mutex_t m;
279 };
280 
281 #endif
282 
284 {
285 public:
286  scoped_lock( mutex & l ) : m( l )
287  {
288  m.lock();
289  }
290 
292  {
293  m.unlock();
294  }
295 
296 private:
297  mutex & m;
298 };
299 
300 } // End namespace ALEMBIC_VERSION_NS
301 
302 using namespace ALEMBIC_VERSION_NS;
303 
304 } // End namespace Util
305 } // End namespace Alembic
306 
307 #endif
GLint y
Definition: glcorearb.h:102
friend bool operator>=(const T &x, const T &y)
Definition: Foundation.h:210
friend bool operator>(const T &x, const T &y)
Definition: Foundation.h:200
friend bool operator<=(const T &x, const T &y)
Definition: Foundation.h:205
GLint GLenum GLint x
Definition: glcorearb.h:408
GLuint GLfloat * val
Definition: glcorearb.h:1607
friend bool operator!=(const T &x, const T &y)
Definition: Foundation.h:215
#define ALEMBIC_EXPORT
Definition: Export.h:51
#define ALEMBIC_VERSION_NS
Definition: Foundation.h:104