~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NTemplate.h

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef NTEMPLATE_H
 
2
#define NTEMPLATE_H
 
3
 
 
4
#include <cmath>
 
5
#include <cwchar>
 
6
#include <cstring>
 
7
#include <algorithm>
 
8
 
 
9
NAMESPACE_BEGIN
 
10
 
 
11
// Number of elements in an array.
 
12
#define INL_ARRAY_COUNT( array ) \
 
13
    ( sizeof(array) / sizeof((array)[0]) )
 
14
 
 
15
 
 
16
// -----------------------------------------------------------------------------
 
17
// Type information.
 
18
// -----------------------------------------------------------------------------
 
19
 
 
20
// Is void
 
21
template <typename U> struct VoidTraits
 
22
{
 
23
    enum { result = 0 };
 
24
};
 
25
// Full specialization of template <typename T> NTraitIsVoid
 
26
template <> struct VoidTraits<void>
 
27
{
 
28
    enum { result = 1 };
 
29
};
 
30
 
 
31
// Is pointer
 
32
template <typename U> struct PointerTraits
 
33
{
 
34
    enum { result = 0 };
 
35
};
 
36
// Partial specialization
 
37
template <typename U> struct PointerTraits<U*> 
 
38
{
 
39
    enum { result = 1 };
 
40
};
 
41
 
 
42
template <typename U> struct UnicodeCharTraits
 
43
{
 
44
    enum { result = 0 };
 
45
};
 
46
 
 
47
template <> struct UnicodeCharTraits<wchar_t>
 
48
{
 
49
    enum { result = 1 };
 
50
};
 
51
 
 
52
template <typename U> struct AnsiCharTraits
 
53
{
 
54
    enum { result = 0 };
 
55
};
 
56
 
 
57
template <> struct AnsiCharTraits<char>
 
58
{
 
59
    enum { result = 1 };
 
60
};
 
61
 
 
62
// Base type information for atomic types which pass by value.
 
63
template<typename T>
 
64
class TypeTraitsNoConstructor
 
65
{
 
66
public:
 
67
    typedef T ConstInitType;
 
68
    enum { HasConstructor = 0   };
 
69
    enum { HasDestructor = 0    };
 
70
};
 
71
 
 
72
// Base type information for constructed types which pass by reference.
 
73
template<typename T>
 
74
class TypeTraitsConstructor
 
75
{
 
76
public:
 
77
    typedef const T& ConstInitType;
 
78
    enum { HasConstructor = 1   };
 
79
    enum { HasDestructor = 1    };
 
80
};
 
81
 
 
82
 
 
83
// The default behaviour is for types to behave as constructed types.
 
84
template<typename T>
 
85
class ConstructorTraits : public TypeTraitsConstructor<T>{};
 
86
 
 
87
// Pointers don't have a constructor.
 
88
template<typename T>
 
89
class ConstructorTraits<T*>: public TypeTraitsNoConstructor<T*> {};
 
90
 
 
91
template <> class ConstructorTraits<bool>               : public TypeTraitsNoConstructor<bool>     {};
 
92
template <> class ConstructorTraits<char>               : public TypeTraitsNoConstructor<char>     {};
 
93
template <> class ConstructorTraits<unsigned char>      : public TypeTraitsNoConstructor<unsigned char>     {};
 
94
template <> class ConstructorTraits<short>              : public TypeTraitsNoConstructor<short>     {};
 
95
template <> class ConstructorTraits<unsigned short>     : public TypeTraitsNoConstructor<unsigned short>    {};
 
96
template <> class ConstructorTraits<int>                : public TypeTraitsNoConstructor<int>      {};
 
97
template <> class ConstructorTraits<unsigned int>       : public TypeTraitsNoConstructor<unsigned int>      {};
 
98
template <> class ConstructorTraits<long>               : public TypeTraitsNoConstructor<long>    {};
 
99
template <> class ConstructorTraits<unsigned long>      : public TypeTraitsNoConstructor<unsigned long>    {};
 
100
template <> class ConstructorTraits<long long>          : public TypeTraitsNoConstructor<long long>    {};
 
101
template <> class ConstructorTraits<unsigned long long> : public TypeTraitsNoConstructor<unsigned long long>    {};
 
102
template <> class ConstructorTraits<float>              : public TypeTraitsNoConstructor<float>    {};
 
103
template <> class ConstructorTraits<double>             : public TypeTraitsNoConstructor<double>   {};
 
104
 
 
105
template<typename T>
 
106
class NTypeTraits
 
107
{
 
108
private:
 
109
 
 
110
public:
 
111
    enum { IsVoid = VoidTraits<T>::result };
 
112
    enum { IsPointer = PointerTraits<T>::result };
 
113
    enum { NeedsConstructor = ConstructorTraits<T>::HasConstructor };
 
114
    enum { NeedsDestructor = ConstructorTraits<T>::HasDestructor };
 
115
    enum { IsAnsiChar = AnsiCharTraits<T>::result };
 
116
    enum { IsUnicodeChar = UnicodeCharTraits<T>::result };
 
117
};
 
118
 
 
119
template <typename T>
 
120
struct RemovePointerFromType
 
121
{ typedef T type; };
 
122
 
 
123
template <typename T>
 
124
struct RemovePointerFromType<T*>
 
125
 
126
    typedef T type; 
 
127
};
 
128
template <typename T>
 
129
struct RemovePointerFromType<const T*>
 
130
 
131
    typedef T type; 
 
132
};
 
133
template <typename T>
 
134
struct RemovePointerFromType<volatile T*>
 
135
 
136
    typedef T type; 
 
137
};
 
138
template <typename T>
 
139
struct RemovePointerFromType<const volatile T*>
 
140
 
141
    typedef T type; 
 
142
};
 
143
 
 
144
// Determining if an integer is a power of 2
 
145
// http://graphics.stanford.edu/~seander/bithacks.html
 
146
 
 
147
 
 
148
// From: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
 
149
//    John Maloney has pointed out various problems with the previous implementation.
 
150
//    One of the major issues was the amount of heap allocation going on.
 
151
//    He suggested that a lot of this be removed to speed up the algorithm.
 
152
//    Below are two versions based on his excellent suggestions. The char* version 
 
153
//    is at least 10 times faster than the code above. The new std::string version
 
154
//    is 3 times faster than before. Although the char* version is faster, you should
 
155
//    check that you have allocated enough space to hold the output.
 
156
 
 
157
// C++ version std::string style "itoa":
 
158
tstring IntegerToChar(int value, int base);
 
159
// C++ version char* style "itoa":
 
160
TCHAR* IntegerToChar( int value, TCHAR* result, int base );
 
161
 
 
162
// convert an hexadecimal TCHAR to t_u32 // i.e. "0x0000000f" to 15
 
163
t_u32 HexCharToInteger(const TCHAR* s);
 
164
// convert a TCHAR to INT
 
165
BOOL CharToInteger(const TCHAR* digit, INT& ret);
 
166
int CharToInteger(const TCHAR* digit);
 
167
 
 
168
 
 
169
// convert a TCHAR to double
 
170
BOOL CharToDouble(const TCHAR* digit, double& ret);
 
171
double CharToDouble(const TCHAR* digit);
 
172
 
 
173
// convert a TCHAR to float
 
174
BOOL CharToFloat(const TCHAR* digit, float& ret);
 
175
float CharToFloat(const TCHAR* digit);
 
176
 
 
177
TCHAR* DoubleToChar(double d);
 
178
 
 
179
NAMESPACE_END
 
180
 
 
181
#endif // NTEMPLATE_H
 
182