Medial Code Documentation
Loading...
Searching...
No Matches
type_traits.h
Go to the documentation of this file.
1
6#ifndef DMLC_TYPE_TRAITS_H_
7#define DMLC_TYPE_TRAITS_H_
8
9#include "./base.h"
10#if DMLC_USE_CXX11
11#include <type_traits>
12#endif
13#include <string>
14
15namespace dmlc {
20template<typename T>
21struct is_pod {
22#if DMLC_USE_CXX11
24 static const bool value = std::is_pod<T>::value;
25#else
27 static const bool value = false;
28#endif
29};
30
31
36template<typename T>
38#if DMLC_USE_CXX11
40 static const bool value = std::is_integral<T>::value;
41#else
43 static const bool value = false;
44#endif
45};
46
51template<typename T>
53#if DMLC_USE_CXX11
55 static const bool value = std::is_floating_point<T>::value;
56#else
58 static const bool value = false;
59#endif
60};
61
66template<typename T>
68#if DMLC_USE_CXX11
70 static const bool value = std::is_arithmetic<T>::value;
71#else
73 static const bool value = (dmlc::is_integral<T>::value ||
75#endif
76};
77
85template<typename T>
90 static inline std::string value() {
91 return "";
92 }
93};
94
100template<typename T>
101inline std::string type_name() {
103}
104
109template<typename T>
112 static const bool value = false;
113};
114
122template<bool cond, typename Then, typename Else>
124
126#define DMLC_DECLARE_TRAITS(Trait, Type, Value) \
127 template<> \
128 struct Trait<Type> { \
129 static const bool value = Value; \
130 }
131
133#define DMLC_DECLARE_TYPE_NAME(Type, Name) \
134 template<> \
135 struct type_name_helper<Type> { \
136 static inline std::string value() { \
137 return Name; \
138 } \
139 }
140
142// declare special traits when C++11 is not available
143#if DMLC_USE_CXX11 == 0
144DMLC_DECLARE_TRAITS(is_pod, char, true);
145DMLC_DECLARE_TRAITS(is_pod, int8_t, true);
146DMLC_DECLARE_TRAITS(is_pod, int16_t, true);
147DMLC_DECLARE_TRAITS(is_pod, int32_t, true);
148DMLC_DECLARE_TRAITS(is_pod, int64_t, true);
149DMLC_DECLARE_TRAITS(is_pod, uint8_t, true);
150DMLC_DECLARE_TRAITS(is_pod, uint16_t, true);
151DMLC_DECLARE_TRAITS(is_pod, uint32_t, true);
152DMLC_DECLARE_TRAITS(is_pod, uint64_t, true);
153DMLC_DECLARE_TRAITS(is_pod, float, true);
154DMLC_DECLARE_TRAITS(is_pod, double, true);
155
156DMLC_DECLARE_TRAITS(is_integral, char, true);
157DMLC_DECLARE_TRAITS(is_integral, int8_t, true);
158DMLC_DECLARE_TRAITS(is_integral, int16_t, true);
159DMLC_DECLARE_TRAITS(is_integral, int32_t, true);
160DMLC_DECLARE_TRAITS(is_integral, int64_t, true);
161DMLC_DECLARE_TRAITS(is_integral, uint8_t, true);
162DMLC_DECLARE_TRAITS(is_integral, uint16_t, true);
163DMLC_DECLARE_TRAITS(is_integral, uint32_t, true);
164DMLC_DECLARE_TRAITS(is_integral, uint64_t, true);
165
166DMLC_DECLARE_TRAITS(is_floating_point, float, true);
167DMLC_DECLARE_TRAITS(is_floating_point, double, true);
168
169#endif
170
171DMLC_DECLARE_TYPE_NAME(float, "float");
172DMLC_DECLARE_TYPE_NAME(double, "double");
173DMLC_DECLARE_TYPE_NAME(int, "int");
174DMLC_DECLARE_TYPE_NAME(int64_t, "long");
175DMLC_DECLARE_TYPE_NAME(uint32_t, "int (non-negative)");
176DMLC_DECLARE_TYPE_NAME(uint64_t, "long (non-negative)");
177DMLC_DECLARE_TYPE_NAME(std::string, "string");
178DMLC_DECLARE_TYPE_NAME(bool, "boolean");
179DMLC_DECLARE_TYPE_NAME(void*, "ptr");
180
181template<typename Then, typename Else>
182struct IfThenElseType<true, Then, Else> {
183 typedef Then Type;
184};
185
186template<typename Then, typename Else>
187struct IfThenElseType<false, Then, Else> {
188 typedef Else Type;
189};
191} // namespace dmlc
192#endif // DMLC_TYPE_TRAITS_H_
namespace for dmlc
Definition array_view.h:12
std::string type_name()
the string representation of type name
Definition type_traits.h:101
Macros common to all headers.
template to select type based on condition For example, IfThenElseType<true, int, float>::Type will g...
Definition type_traits.h:123
whether a type have save/load function
Definition type_traits.h:110
static const bool value
the value of the traits
Definition type_traits.h:112
whether a type is arithemetic type
Definition type_traits.h:67
static const bool value
the value of the traits
Definition type_traits.h:73
whether a type is floating point type
Definition type_traits.h:52
static const bool value
the value of the traits
Definition type_traits.h:58
whether a type is integer type
Definition type_traits.h:37
static const bool value
the value of the traits
Definition type_traits.h:43
whether a type is pod type
Definition type_traits.h:21
static const bool value
the value of the traits
Definition type_traits.h:27
helper class to construct a string that represents type name
Definition type_traits.h:86
static std::string value()
Definition type_traits.h:90
#define DMLC_DECLARE_TRAITS(Trait, Type, Value)
macro to quickly declare traits information
Definition type_traits.h:126
#define DMLC_DECLARE_TYPE_NAME(Type, Name)
macro to quickly declare traits information
Definition type_traits.h:133