root/trunk/whisperlib/common/base/types.h

Revision 7, 5.8 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache & Catalin Popescu
31
32 #ifndef __COMMON_BASE_TYPES_H__
33 #define __COMMON_BASE_TYPES_H__
34
35 #include <sys/types.h>
36 #include <stddef.h>
37 #include <string>
38
39 //////////////////////////////////////////////////////////////////////
40
41 typedef signed char int8;
42 typedef unsigned char uint8;
43
44 typedef signed short int16;
45 typedef unsigned short uint16;
46
47 typedef int32_t int32;
48 typedef u_int32_t uint32;
49
50 typedef int64_t int64;
51 typedef u_int64_t uint64;
52
53 //////////////////////////////////////////////////////////////////////
54
55 static const int8  kMaxInt8  = (static_cast<int8>(0x7f));
56 static const int8  kMinInt8  = (static_cast<int8>(0x80));
57 static const int16 kMaxInt16 = (static_cast<int16>(0x7fff));
58 static const int16 kMinInt16 = (static_cast<int16>(0x8000));
59 static const int32 kMaxInt32 = (static_cast<int32>(0x7fffffff));
60 static const int32 kMinInt32 = (static_cast<int32>(0x80000000));
61 static const int64 kMaxInt64 = (static_cast<int64>(0x7fffffffffffffffLL));
62 static const int64 kMinInt64 = (static_cast<int64>(0x8000000000000000LL));
63
64 static const uint8  kMaxUInt8  = (static_cast<uint8>(0xff));
65 static const uint16 kMaxUInt16 = (static_cast<uint16>(0xffff));
66 static const uint32 kMaxUInt32 = (static_cast<uint32>(0xffffffff));
67 static const uint64 kMaxUInt64 = (static_cast<uint64>(0xffffffffffffffffLL));
68
69 //////////////////////////////////////////////////////////////////////
70
71
72 // None of this trouble for now ..
73 // The current character type.
74 // #ifdef _UNICODE
75 // typedef wchar_t char_t;
76 // typedef wstring tstring;
77 // #else
78 // typedef char char_t;
79 // typedef string tstring;
80 // #endif // _UNICODE
81
82 //////////////////////////////////////////////////////////////////////
83
84 #define CONSIDER(cond) case cond: return #cond;
85 #define NUMBEROF(things)                                            \
86     ((sizeof(things) / sizeof(*(things))) /                         \
87     static_cast<size_t>(!(sizeof(things) % sizeof(*(things)))))
88
89 #define INVALID_FD_VALUE (-1)
90
91 // A macro to disallow the evil copy constructor and operator= functions
92 // This should be used in the private: declarations for a class
93 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName)    \
94   TypeName(const TypeName&);                    \
95   void operator=(const TypeName&)
96
97 //////////////////////////////////////////////////////////////////////
98
99 // Hash helper functions for where is undefined
100
101 #if defined(__GLIBCXX__) || (defined(__GLIBCPP__) && __GLIBCPP__ >= 20020514)
102
103 // we accept 'std' and '__gnu_cxx' namespaces without full specification
104 // to reduce the clutter
105 namespace std { }
106 namespace  __gnu_cxx { }
107
108 using namespace std;
109 using namespace __gnu_cxx;
110
111 # if !defined(__GNUC__) || __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
112 #   define WHISPER_HAS_HASH_FUN    1
113 #   define WHISPER_HASH_FUN_HEADER <ext/hash_fun.h>
114
115 #   define WHISPER_HASH_SET_HEADER <ext/hash_set>
116 #   define WHISPER_HASH_MAP_HEADER <ext/hash_map>
117
118 # else
119 #   define WHISPER_HAS_FUN         0
120 #   define WHISPER_HASH_FUN_HEADER <backward/hash_fun>
121
122 #   define WHISPER_HASH_SET_HEADER <tr1/unordered_set>
123 #   define WHISPER_HASH_MAP_HEADER <tr1/unordered_map>
124
125 #   define hash_map tr1::unordered_map
126 #   define hash_set tr1::unordered_set
127 # endif
128
129 #else
130 # error "Please use at leaset  GCC >= 3.1.0"
131 #endif
132
133 #if WHISPER_HAS_HASH_FUN
134
135 #include WHISPER_HASH_FUN_HEADER
136
137 namespace __gnu_cxx {
138 #if __WORDSIZE != 64
139   template<> struct hash<int64> {
140     size_t operator()(const int64 in) const {
141       const int64 ret = (in >> 32L) ^ (in & 0xFFFFFFFF);
142       return static_cast<size_t>(ret);
143     }
144   };
145   template<> struct hash<uint64> {
146     size_t operator()(const uint64 in) const {
147       const int64 ret = (in >> 32L) ^ (in & 0xFFFFFFFF);
148       return static_cast<size_t>(ret);
149     }
150   };
151 #endif
152   template<typename T> struct hash<T*> {
153     size_t operator()(const T* p) const {
154       return (size_t) p;
155     }
156   };
157   template<> struct hash<string> {
158     size_t operator()(const string& s) const {
159       uint32 a = 63689;
160       uint32 b = 378551;
161       const char* p = s.data();
162       int len =  s.size();
163       uint32 h = 0;
164       while ( len-- ) {
165         h = h * a + *p++;
166         a *= b;
167       }
168       return h;
169     }
170   };
171 }
172
173 #endif  // WHISPER_HAS_FUN
174
175 //////////////////////////////////////////////////////////////////////
176
177
178 #endif   // __COMMON_BASE_TYPES_H__
Note: See TracBrowser for help on using the browser.