GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
type_info.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9
10
11#ifndef CHAISCRIPT_TYPE_INFO_HPP_
12#define CHAISCRIPT_TYPE_INFO_HPP_
13
14#include <memory>
15#include <type_traits>
16#include <typeinfo>
17#include <string>
18
19namespace chaiscript
20{
21
22 namespace detail
23 {
24 template<typename T>
25 struct Bare_Type
26 {
27 typedef typename std::remove_cv<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::type type;
28 };
29 }
30
31
34 {
35 public:
36 constexpr Type_Info(const bool t_is_const, const bool t_is_reference, const bool t_is_pointer, const bool t_is_void,
37 const bool t_is_arithmetic, const std::type_info *t_ti, const std::type_info *t_bare_ti)
38 : m_type_info(t_ti), m_bare_type_info(t_bare_ti),
39 m_flags((static_cast<unsigned int>(t_is_const) << is_const_flag)
40 + (static_cast<unsigned int>(t_is_reference) << is_reference_flag)
41 + (static_cast<unsigned int>(t_is_pointer) << is_pointer_flag)
42 + (static_cast<unsigned int>(t_is_void) << is_void_flag)
43 + (static_cast<unsigned int>(t_is_arithmetic) << is_arithmetic_flag))
44 {
45 }
46
47 constexpr Type_Info() = default;
48
49 bool operator<(const Type_Info &ti) const noexcept
50 {
51 return m_type_info->before(*ti.m_type_info);
52 }
53
54 constexpr bool operator!=(const Type_Info &ti) const noexcept
55 {
56 return !(operator==(ti));
57 }
58
59 constexpr bool operator!=(const std::type_info &ti) const noexcept
60 {
61 return !(operator==(ti));
62 }
63
64 constexpr bool operator==(const Type_Info &ti) const noexcept
65 {
66 return ti.m_type_info == m_type_info
67 || *ti.m_type_info == *m_type_info;
68 }
69
70 constexpr bool operator==(const std::type_info &ti) const noexcept
71 {
72 return !is_undef() && (*m_type_info) == ti;
73 }
74
75 constexpr bool bare_equal(const Type_Info &ti) const noexcept
76 {
77 return ti.m_bare_type_info == m_bare_type_info
78 || *ti.m_bare_type_info == *m_bare_type_info;
79 }
80
81 constexpr bool bare_equal_type_info(const std::type_info &ti) const noexcept
82 {
83 return !is_undef() && (*m_bare_type_info) == ti;
84 }
85
86 constexpr bool is_const() const noexcept { return (m_flags & (1 << is_const_flag)) != 0; }
87 constexpr bool is_reference() const noexcept { return (m_flags & (1 << is_reference_flag)) != 0; }
88 constexpr bool is_void() const noexcept { return (m_flags & (1 << is_void_flag)) != 0; }
89 constexpr bool is_arithmetic() const noexcept { return (m_flags & (1 << is_arithmetic_flag)) != 0; }
90 constexpr bool is_undef() const noexcept { return (m_flags & (1 << is_undef_flag)) != 0; }
91 constexpr bool is_pointer() const noexcept { return (m_flags & (1 << is_pointer_flag)) != 0; }
92
93 std::string name() const
94 {
95 if (!is_undef())
96 {
97 return m_type_info->name();
98 } else {
99 return "";
100 }
101 }
102
103 std::string bare_name() const
104 {
105 if (!is_undef())
106 {
107 return m_bare_type_info->name();
108 } else {
109 return "";
110 }
111 }
112
113 constexpr const std::type_info *bare_type_info() const
114 {
115 return m_bare_type_info;
116 }
117
118 private:
119 struct Unknown_Type {};
120
121 const std::type_info *m_type_info = &typeid(Unknown_Type);
122 const std::type_info *m_bare_type_info = &typeid(Unknown_Type);
123 static const int is_const_flag = 0;
124 static const int is_reference_flag = 1;
125 static const int is_pointer_flag = 2;
126 static const int is_void_flag = 3;
127 static const int is_arithmetic_flag = 4;
128 static const int is_undef_flag = 5;
129 unsigned int m_flags = (1 << is_undef_flag);
130 };
131
132 namespace detail
133 {
135 template<typename T>
137 {
138 static constexpr Type_Info get()
139 {
140 return Type_Info(std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value,
141 std::is_reference<T>::value, std::is_pointer<T>::value,
142 std::is_void<T>::value,
143 (std::is_arithmetic<T>::value || std::is_arithmetic<typename std::remove_reference<T>::type>::value)
144 && !std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, bool>::value,
145 &typeid(T),
146 &typeid(typename Bare_Type<T>::type));
147 }
148 };
149
150 template<typename T>
151 struct Get_Type_Info<std::shared_ptr<T> >
152 {
153// typedef T type;
154
155 static constexpr Type_Info get()
156 {
157 return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
158 std::is_void<T>::value,
159 std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, bool>::value,
160 &typeid(std::shared_ptr<T> ),
161 &typeid(typename Bare_Type<T>::type));
162 }
163 };
164
165 template<typename T>
166 struct Get_Type_Info<std::shared_ptr<T> &> : Get_Type_Info<std::shared_ptr<T>>
167 {
168 };
169
170 template<typename T>
171 struct Get_Type_Info<const std::shared_ptr<T> &>
172 {
173 static constexpr Type_Info get()
174 {
175 return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
176 std::is_void<T>::value,
177 std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, bool>::value,
178 &typeid(const std::shared_ptr<T> &),
179 &typeid(typename Bare_Type<T>::type));
180 }
181 };
182
183 template<typename T>
184 struct Get_Type_Info<std::reference_wrapper<T> >
185 {
186 static constexpr Type_Info get()
187 {
188 return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
189 std::is_void<T>::value,
190 std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, bool>::value,
191 &typeid(std::reference_wrapper<T> ),
192 &typeid(typename Bare_Type<T>::type));
193 }
194 };
195
196 template<typename T>
197 struct Get_Type_Info<const std::reference_wrapper<T> &>
198 {
199 static constexpr Type_Info get()
200 {
201 return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
202 std::is_void<T>::value,
203 std::is_arithmetic<T>::value && !std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, bool>::value,
204 &typeid(const std::reference_wrapper<T> &),
205 &typeid(typename Bare_Type<T>::type));
206 }
207 };
208
209 }
210
220 template<typename T>
221 constexpr Type_Info user_type(const T &/*t*/)
222 {
224 }
225
226
235 template<typename T>
237 {
239 }
240
241}
242
243#endif
244
Compile time deduced information about a type.
Definition type_info.hpp:34
constexpr bool is_const() const noexcept
Definition type_info.hpp:86
constexpr bool operator!=(const Type_Info &ti) const noexcept
Definition type_info.hpp:54
bool operator<(const Type_Info &ti) const noexcept
Definition type_info.hpp:49
constexpr bool bare_equal_type_info(const std::type_info &ti) const noexcept
Definition type_info.hpp:81
constexpr const std::type_info * bare_type_info() const
Definition type_info.hpp:113
constexpr bool operator==(const std::type_info &ti) const noexcept
Definition type_info.hpp:70
constexpr Type_Info()=default
std::string name() const
Definition type_info.hpp:93
constexpr bool operator!=(const std::type_info &ti) const noexcept
Definition type_info.hpp:59
constexpr bool is_void() const noexcept
Definition type_info.hpp:88
constexpr Type_Info(const bool t_is_const, const bool t_is_reference, const bool t_is_pointer, const bool t_is_void, const bool t_is_arithmetic, const std::type_info *t_ti, const std::type_info *t_bare_ti)
Definition type_info.hpp:36
constexpr bool is_reference() const noexcept
Definition type_info.hpp:87
constexpr bool is_undef() const noexcept
Definition type_info.hpp:90
std::string bare_name() const
Definition type_info.hpp:103
constexpr bool is_arithmetic() const noexcept
Definition type_info.hpp:89
constexpr bool bare_equal(const Type_Info &ti) const noexcept
Definition type_info.hpp:75
constexpr bool is_pointer() const noexcept
Definition type_info.hpp:91
constexpr bool operator==(const Type_Info &ti) const noexcept
Definition type_info.hpp:64
Classes and functions reserved for internal use. Items in this namespace are not supported.
Namespace chaiscript contains every API call that the average user will be concerned with.
constexpr Type_Info user_type()
Creates a Type_Info object representing the templated type.
Definition type_info.hpp:236
STL namespace.
Definition GameApi.h:42
Definition type_info.hpp:26
std::remove_cv< typenamestd::remove_pointer< typenamestd::remove_reference< T >::type >::type >::type type
Definition type_info.hpp:27
static constexpr Type_Info get()
Definition type_info.hpp:199
static constexpr Type_Info get()
Definition type_info.hpp:173
static constexpr Type_Info get()
Definition type_info.hpp:186
static constexpr Type_Info get()
Definition type_info.hpp:155
Helper used to create a Type_Info object.
Definition type_info.hpp:137
static constexpr Type_Info get()
Definition type_info.hpp:138