/src/alembic/lib/Alembic/Util/Foundation.h
Line | Count | Source (jump to first uncovered line) |
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 | | #include <unordered_map> |
42 | | |
43 | | #include <memory> |
44 | | |
45 | | #include <half.h> |
46 | | |
47 | | #include <iomanip> |
48 | | #include <iostream> |
49 | | #include <sstream> |
50 | | #include <exception> |
51 | | #include <limits> |
52 | | |
53 | | #include <list> |
54 | | #include <map> |
55 | | #include <string> |
56 | | #include <vector> |
57 | | |
58 | | #include <cstdio> |
59 | | #include <cstdlib> |
60 | | #include <cstring> |
61 | | #include <cassert> |
62 | | |
63 | | #include <Alembic/Util/Export.h> |
64 | | |
65 | | #ifdef _MSC_VER |
66 | | |
67 | | #ifndef WIN32_LEAN_AND_MEAN |
68 | | #define WIN32_LEAN_AND_MEAN |
69 | | #endif |
70 | | |
71 | | // avoid windows min/max predefined macro conflicts |
72 | | #ifndef NOMINMAX |
73 | | #define NOMINMAX |
74 | | #endif |
75 | | |
76 | | // needed for mutex stuff |
77 | | #include <Windows.h> |
78 | | #endif |
79 | | |
80 | | // needed for std min/max |
81 | | #include <algorithm> |
82 | | |
83 | | // When we change this version (because of an ABI change) Make sure to at |
84 | | // LEAST bump the minor version in CMakeLists.txt i.e. PROJECT_VERSION_MINOR |
85 | | // This way we will hopefully not break any distros that kindly include us. |
86 | | // See Issue243 |
87 | | #ifndef ALEMBIC_VERSION_NS |
88 | | #define ALEMBIC_VERSION_NS v12 |
89 | | #endif |
90 | | |
91 | | namespace Alembic { |
92 | | namespace Util { |
93 | | namespace ALEMBIC_VERSION_NS { |
94 | | |
95 | | // similiar to boost::noncopyable |
96 | | // explicitly hides copy construction and copy assignment |
97 | | class ALEMBIC_EXPORT noncopyable |
98 | | { |
99 | | protected: |
100 | 4.87k | noncopyable() {} |
101 | 4.87k | ~noncopyable() {} |
102 | | |
103 | | private: |
104 | | noncopyable( const noncopyable& ); |
105 | | const noncopyable& operator=( const noncopyable& ); |
106 | | }; |
107 | | |
108 | | using std::dynamic_pointer_cast; |
109 | | using std::enable_shared_from_this; |
110 | | using std::shared_ptr; |
111 | | using std::static_pointer_cast; |
112 | | using std::weak_ptr; |
113 | | using std::unordered_map; |
114 | | using std::unique_ptr; |
115 | | |
116 | | // similiar to boost::totally_ordered |
117 | | // only need < and == operators and this fills in the rest |
118 | | template < class T > |
119 | | class totally_ordered |
120 | | { |
121 | | friend bool operator > ( const T& x, const T& y ) |
122 | 0 | { |
123 | 0 | return y < x; |
124 | 0 | } |
125 | | |
126 | | friend bool operator <= ( const T& x, const T& y ) |
127 | | { |
128 | | return !( y < x ); |
129 | | } |
130 | | |
131 | | friend bool operator >= ( const T& x, const T& y ) |
132 | | { |
133 | | return !( x < y ); |
134 | | } |
135 | | |
136 | | friend bool operator != ( const T& x, const T& y ) |
137 | | { |
138 | | return !( x == y ); |
139 | | } |
140 | | }; |
141 | | |
142 | | // inspired by boost::mutex |
143 | | #ifdef _MSC_VER |
144 | | |
145 | | class mutex : noncopyable |
146 | | { |
147 | | public: |
148 | | mutex() |
149 | | { |
150 | | InitializeCriticalSection(&cs); |
151 | | } |
152 | | |
153 | | ~mutex() |
154 | | { |
155 | | DeleteCriticalSection(&cs); |
156 | | } |
157 | | |
158 | | void lock() |
159 | | { |
160 | | EnterCriticalSection(&cs); |
161 | | } |
162 | | |
163 | | void unlock() |
164 | | { |
165 | | LeaveCriticalSection(&cs); |
166 | | } |
167 | | |
168 | | private: |
169 | | CRITICAL_SECTION cs; |
170 | | }; |
171 | | |
172 | | #else |
173 | | |
174 | | |
175 | | class mutex : noncopyable |
176 | | { |
177 | | public: |
178 | | mutex() |
179 | 4.10k | { |
180 | 4.10k | pthread_mutex_init( &m, NULL ); |
181 | 4.10k | } |
182 | | |
183 | | ~mutex() |
184 | 4.10k | { |
185 | 4.10k | pthread_mutex_destroy( &m ); |
186 | 4.10k | } |
187 | | |
188 | | void lock() |
189 | 62 | { |
190 | 62 | pthread_mutex_lock( &m ); |
191 | 62 | } |
192 | | |
193 | | void unlock() |
194 | 62 | { |
195 | 62 | pthread_mutex_unlock( &m ); |
196 | 62 | } |
197 | | |
198 | | private: |
199 | | pthread_mutex_t m; |
200 | | }; |
201 | | |
202 | | #endif |
203 | | |
204 | | class scoped_lock : noncopyable |
205 | | { |
206 | | public: |
207 | 62 | scoped_lock( mutex & l ) : m( l ) |
208 | 62 | { |
209 | 62 | m.lock(); |
210 | 62 | } |
211 | | |
212 | | ~scoped_lock() |
213 | 62 | { |
214 | 62 | m.unlock(); |
215 | 62 | } |
216 | | |
217 | | private: |
218 | | mutex & m; |
219 | | }; |
220 | | |
221 | | } // End namespace ALEMBIC_VERSION_NS |
222 | | |
223 | | using namespace ALEMBIC_VERSION_NS; |
224 | | |
225 | | } // End namespace Util |
226 | | } // End namespace Alembic |
227 | | |
228 | | #endif |