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 <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <assert.h>
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 #ifndef ALEMBIC_VERSION_NS
105 #define ALEMBIC_VERSION_NS v11_sidefx
106 #endif
107 
108 namespace Alembic {
109 namespace Util {
110 namespace ALEMBIC_VERSION_NS {
111 
112 // similiar to boost::noncopyable
113 // explicitly hides copy construction and copy assignment
115 {
116 protected:
119 
120 private:
121  noncopyable( const noncopyable& );
122  const noncopyable& operator=( const noncopyable& );
123 };
124 
125 #ifdef ALEMBIC_LIB_USES_BOOST
126 using boost::dynamic_pointer_cast;
127 using boost::enable_shared_from_this;
128 using boost::shared_ptr;
129 using boost::static_pointer_cast;
130 using boost::weak_ptr;
131 using boost::unordered_map;
132 
133 #elif defined(ALEMBIC_LIB_USES_TR1)
134 using std::tr1::dynamic_pointer_cast;
135 using std::tr1::enable_shared_from_this;
136 using std::tr1::shared_ptr;
137 using std::tr1::static_pointer_cast;
138 using std::tr1::weak_ptr;
139 using std::tr1::unordered_map;
140 
141 #else
142 using std::dynamic_pointer_cast;
143 using std::enable_shared_from_this;
144 using std::shared_ptr;
145 using std::static_pointer_cast;
146 using std::weak_ptr;
147 using std::unordered_map;
148 using std::unique_ptr;
149 #endif
150 
151 #if defined(ALEMBIC_LIB_USES_BOOST) || defined(ALEMBIC_LIB_USES_TR1)
152 
153 // define a very simple scoped ptr since unique_ptr isn't consistently
154 // available on boost versions. Otherwise we could use boost::scoped_ptr
155 // or the deprecated std::auto_ptr for tr1.
156 template<typename T>
157 class unique_ptr : noncopyable
158 {
159 public:
160  unique_ptr()
161  {
162  p = NULL;
163  }
164 
165  unique_ptr( T* val ) : p(val)
166  {
167  }
168 
169  ~unique_ptr()
170  {
171  delete p;
172  }
173 
174  void reset( T* val )
175  {
176  delete p;
177  p = val;
178  }
179 
180  T* operator->() const
181  {
182  return p;
183  }
184 private:
185  T* p;
186 };
187 
188 #endif
189 
190 // similiar to boost::totally_ordered
191 // only need < and == operators and this fills in the rest
192 template < class T >
194 {
195  friend bool operator > ( const T& x, const T& y )
196  {
197  return y < x;
198  }
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 !( x < y );
208  }
209 
210  friend bool operator != ( const T& x, const T& y )
211  {
212  return !( x == y );
213  }
214 };
215 
216 // inspired by boost::mutex
217 #ifdef _MSC_VER
218 
219 class mutex : noncopyable
220 {
221 public:
222  mutex()
223  {
224  m = CreateMutex( NULL, FALSE, NULL );
225  }
226 
227  ~mutex()
228  {
229  CloseHandle( m );
230  }
231 
232  void lock()
233  {
234  WaitForSingleObject( m, INFINITE );
235  }
236 
237  void unlock()
238  {
239  ReleaseMutex( m );
240  }
241 
242 private:
243  HANDLE m;
244 };
245 
246 #else
247 
248 
250 {
251 public:
253  {
254  pthread_mutex_init( &m, NULL );
255  }
256 
258  {
259  pthread_mutex_destroy( &m );
260  }
261 
262  void lock()
263  {
264  pthread_mutex_lock( &m );
265  }
266 
267  void unlock()
268  {
269  pthread_mutex_unlock( &m );
270  }
271 
272 private:
273  pthread_mutex_t m;
274 };
275 
276 #endif
277 
279 {
280 public:
281  scoped_lock( mutex & l ) : m( l )
282  {
283  m.lock();
284  }
285 
287  {
288  m.unlock();
289  }
290 
291 private:
292  mutex & m;
293 };
294 
295 } // End namespace ALEMBIC_VERSION_NS
296 
297 using namespace ALEMBIC_VERSION_NS;
298 
299 } // End namespace Util
300 } // End namespace Alembic
301 
302 #endif
GLint y
Definition: glcorearb.h:102
friend bool operator>=(const T &x, const T &y)
Definition: Foundation.h:205
friend bool operator>(const T &x, const T &y)
Definition: Foundation.h:195
friend bool operator<=(const T &x, const T &y)
Definition: Foundation.h:200
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:210
#define ALEMBIC_EXPORT
Definition: Export.h:51
#define ALEMBIC_VERSION_NS
Definition: Foundation.h:105