]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_types.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_types.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_types.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 namespace vogl
33 {
34     typedef uint8_t uint8;
35     typedef int8_t int8;
36     typedef uint16_t uint16;
37     typedef int16_t int16;
38     typedef uint32_t uint32;
39     typedef uint32 uint;
40     typedef int32_t int32;
41
42     const uint8 cUINT8_MIN = 0;
43     const uint8 cUINT8_MAX = 0xFFU;
44     const uint16 cUINT16_MIN = 0;
45     const uint16 cUINT16_MAX = 0xFFFFU;
46     const uint32 cUINT32_MIN = 0;
47     const uint32 cUINT32_MAX = 0xFFFFFFFFU;
48     const uint64_t cUINT64_MIN = 0;
49     const uint64_t cUINT64_MAX = 0xFFFFFFFFFFFFFFFFULL; //0xFFFFFFFFFFFFFFFFui64;
50
51     const int8 cINT8_MIN = -128;
52     const int8 cINT8_MAX = 127;
53     const int16 cINT16_MIN = -32768;
54     const int16 cINT16_MAX = 32767;
55     const int32 cINT32_MIN = (-2147483647 - 1);
56     const int32 cINT32_MAX = 2147483647;
57     const int64_t cINT64_MIN = (int64_t)0x8000000000000000ULL; //(-9223372036854775807i64 - 1);
58     const int64_t cINT64_MAX = (int64_t)0x7FFFFFFFFFFFFFFFULL; // 9223372036854775807i64;
59
60     typedef uintptr_t uint_ptr;
61     typedef uintptr_t uint32_ptr;
62     typedef std::ptrdiff_t signed_size_t;
63     typedef uintptr_t ptr_bits_t;
64
65     enum eVarArg
66     {
67         cVarArg
68     };
69     enum eClear
70     {
71         cClear
72     };
73     enum eZero
74     {
75         cZero
76     };
77     enum eNoClamp
78     {
79         cNoClamp
80     };
81     enum
82     {
83         cInvalidIndex = -1
84     };
85     enum eBitwise
86     {
87         cBitwise
88     };
89
90     const uint cIntBits = 32;
91
92     template <bool condition, class Then, class Else>
93     struct template_if
94     {
95         typedef Then result;
96     };
97     template <class Then, class Else>
98     struct template_if<false, Then, Else>
99     {
100         typedef Else result;
101     };
102
103     struct empty_type
104     {
105         inline bool operator==(const empty_type &rhs) const
106         {
107             VOGL_NOTE_UNUSED(rhs);
108             return true;
109         }
110         inline bool operator<(const empty_type &rhs) const
111         {
112             VOGL_NOTE_UNUSED(rhs);
113             return false;
114         }
115     };
116
117     class json_value;
118
119     inline bool json_serialize(const empty_type &, json_value &)
120     {
121         return false;
122     }
123
124     inline bool json_deserialize(empty_type &, const json_value &)
125     {
126         return false;
127     }
128
129     template <typename T>
130     struct is_empty_type
131     {
132         enum
133         {
134             cValue = false
135         };
136     };
137
138     template <>
139     struct is_empty_type<empty_type>
140     {
141         enum
142         {
143             cValue = true
144         };
145     };
146
147     uint32 fast_hash(const void *p, int len);
148
149     // Bitwise hasher: Directly hashes key's bits
150     template <typename T>
151     struct bit_hasher
152     {
153         inline size_t operator()(const T &key) const
154         {
155             return static_cast<size_t>(fast_hash(&key, sizeof(key)));
156         }
157     };
158
159     // Intrusive hasher: Requires object implement a get_hash() method
160     template <typename T>
161     struct intrusive_hasher
162     {
163         inline size_t operator()(const T &key) const
164         {
165             return static_cast<size_t>(key.get_hash());
166         }
167     };
168
169     // Default hasher: Casts object to size_t
170     template <typename T>
171     struct hasher
172     {
173         inline size_t operator()(const T &key) const
174         {
175             return static_cast<size_t>(key);
176         }
177     };
178
179     // Now define some common specializations of hasher<> because it's the default used by hash_map and some types can't be static_cast to size_t.
180     template <typename T>
181     struct hasher<T *> : bit_hasher<T *>
182     {
183     };
184
185 #define VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(T) \
186     template <> struct hasher<T> : bit_hasher<T>       \
187     {                                                  \
188     }
189     VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(float);
190     VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(double);
191     VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(long double);
192     VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(int64_t);
193     VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION(uint64_t);
194 #undef VOGL_DEFINE_BITWISE_HASHER_SPECIALIZATION
195
196     template <typename T>
197     struct equal_to
198     {
199         inline bool operator()(const T &a, const T &b) const
200         {
201             return a == b;
202         }
203     };
204
205     template <typename T>
206     struct equal_to_using_less_than
207     {
208         inline bool operator()(const T &a, const T &b) const
209         {
210             return !(a < b) && !(b < a);
211         }
212     };
213
214     template <typename T>
215     struct less_than
216     {
217         inline bool operator()(const T &a, const T &b) const
218         {
219             return a < b;
220         }
221     };
222
223 } // namespace vogl