ChaiScript
chaiscript_defines.hpp
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-2018, Jason Turner (jason@emptycrate.com)
5 // http://www.chaiscript.com
6 
7 #ifndef CHAISCRIPT_DEFINES_HPP_
8 #define CHAISCRIPT_DEFINES_HPP_
9 
10 #ifdef _MSC_VER
11 #define CHAISCRIPT_STRINGIZE(x) "" #x
12 #define CHAISCRIPT_STRINGIZE_EXPANDED(x) CHAISCRIPT_STRINGIZE(x)
13 #define CHAISCRIPT_COMPILER_VERSION CHAISCRIPT_STRINGIZE_EXPANDED(_MSC_FULL_VER)
14 #define CHAISCRIPT_MSVC _MSC_VER
15 #define CHAISCRIPT_HAS_DECLSPEC
16 
17 static_assert(_MSC_FULL_VER >= 190024210, "Visual C++ 2015 Update 3 or later required");
18 
19 #else
20 #define CHAISCRIPT_COMPILER_VERSION __VERSION__
21 #endif
22 
23 #include <string_view>
24 #include <vector>
25 
26 #if defined(_LIBCPP_VERSION)
27 #define CHAISCRIPT_LIBCPP
28 #endif
29 
30 #if defined(_WIN32) || defined(__CYGWIN__)
31 #define CHAISCRIPT_WINDOWS
32 #endif
33 
34 #if defined(_WIN32)
35 #if defined(__llvm__)
36 #define CHAISCRIPT_COMPILER_NAME "clang(windows)"
37 #elif defined(__GNUC__)
38 #define CHAISCRIPT_COMPILER_NAME "gcc(mingw)"
39 #else
40 #define CHAISCRIPT_COMPILER_NAME "msvc"
41 #endif
42 #else
43 #if defined(__llvm__)
44 #define CHAISCRIPT_COMPILER_NAME "clang"
45 #elif defined(__GNUC__)
46 #define CHAISCRIPT_COMPILER_NAME "gcc"
47 #else
48 #define CHAISCRIPT_COMPILER_NAME "unknown"
49 #endif
50 #endif
51 
52 #if defined(__llvm__)
53 #define CHAISCRIPT_CLANG
54 #endif
55 
56 #ifdef CHAISCRIPT_HAS_DECLSPEC
57 #define CHAISCRIPT_MODULE_EXPORT extern "C" __declspec(dllexport)
58 #else
59 #define CHAISCRIPT_MODULE_EXPORT extern "C"
60 #endif
61 
62 #if defined(CHAISCRIPT_MSVC) || (defined(__GNUC__) && __GNUC__ >= 5) || defined(CHAISCRIPT_CLANG)
63 #define CHAISCRIPT_UTF16_UTF32
64 #endif
65 
66 #ifdef _DEBUG
67 #define CHAISCRIPT_DEBUG true
68 #else
69 #define CHAISCRIPT_DEBUG false
70 #endif
71 
72 #include <cmath>
73 #include <memory>
74 #include <string>
75 
76 namespace chaiscript {
77  constexpr static const int version_major = 7;
78  constexpr static const int version_minor = 0;
79  constexpr static const int version_patch = 0;
80 
81  constexpr static const char *compiler_version = CHAISCRIPT_COMPILER_VERSION;
82  constexpr static const char *compiler_name = CHAISCRIPT_COMPILER_NAME;
83  constexpr static const bool debug_build = CHAISCRIPT_DEBUG;
84 
85  template<typename B, typename D, typename... Arg>
86  inline std::shared_ptr<B> make_shared(Arg &&...arg) {
87 #ifdef CHAISCRIPT_USE_STD_MAKE_SHARED
88  return std::make_shared<D>(std::forward<Arg>(arg)...);
89 #else
90  return std::shared_ptr<B>(static_cast<B *>(new D(std::forward<Arg>(arg)...)));
91 #endif
92  }
93 
94  template<typename B, typename D, typename... Arg>
95  inline std::unique_ptr<B> make_unique(Arg &&...arg) {
96 #ifdef CHAISCRIPT_USE_STD_MAKE_SHARED
97  return std::make_unique<D>(std::forward<Arg>(arg)...);
98 #else
99  return std::unique_ptr<B>(static_cast<B *>(new D(std::forward<Arg>(arg)...)));
100 #endif
101  }
102 
103  struct Build_Info {
104  [[nodiscard]] constexpr static int version_major() noexcept { return chaiscript::version_major; }
105 
106  [[nodiscard]] constexpr static int version_minor() noexcept { return chaiscript::version_minor; }
107 
108  [[nodiscard]] constexpr static int version_patch() noexcept { return chaiscript::version_patch; }
109 
110  [[nodiscard]] static std::string version() {
111  return std::to_string(version_major()) + '.' + std::to_string(version_minor()) + '.' + std::to_string(version_patch());
112  }
113 
114  [[nodiscard]] static std::string compiler_id() { return compiler_name() + '-' + compiler_version(); }
115 
116  [[nodiscard]] static std::string build_id() { return compiler_id() + (debug_build() ? "-Debug" : "-Release"); }
117 
118  [[nodiscard]] static std::string compiler_version() { return chaiscript::compiler_version; }
119 
120  [[nodiscard]] static std::string compiler_name() { return chaiscript::compiler_name; }
121 
122  [[nodiscard]] constexpr static bool debug_build() noexcept { return chaiscript::debug_build; }
123  };
124 
125  template<typename T>
126  [[nodiscard]] constexpr auto parse_num(const std::string_view t_str) noexcept -> typename std::enable_if<std::is_integral<T>::value, T>::type {
127  T t = 0;
128  for (const auto c : t_str) {
129  if (c < '0' || c > '9') {
130  return t;
131  }
132  t *= 10;
133  t += c - '0';
134  }
135  return t;
136  }
137 
138  template<typename T>
139  [[nodiscard]] auto parse_num(const std::string_view t_str) -> typename std::enable_if<!std::is_integral<T>::value, T>::type {
140  T t = 0;
141  T base{};
142  T decimal_place = 0;
143  int exponent = 0;
144 
145  for (const auto c : t_str) {
146  switch (c) {
147  case '.':
148  decimal_place = 10;
149  break;
150  case 'e':
151  case 'E':
152  exponent = 1;
153  decimal_place = 0;
154  base = t;
155  t = 0;
156  break;
157  case '-':
158  exponent = -1;
159  break;
160  case '+':
161  break;
162  case '0':
163  case '1':
164  case '2':
165  case '3':
166  case '4':
167  case '5':
168  case '6':
169  case '7':
170  case '8':
171  case '9':
172  if (decimal_place < 10) {
173  t *= 10;
174  t += static_cast<T>(c - '0');
175  } else {
176  t += static_cast<T>(c - '0') / decimal_place;
177  decimal_place *= 10;
178  }
179  break;
180  default:
181  break;
182  }
183  }
184  return exponent ? base * std::pow(T(10), t * static_cast<T>(exponent)) : t;
185  }
186 
187  struct str_equal {
188  [[nodiscard]] bool operator()(const std::string &t_lhs, const std::string &t_rhs) const noexcept { return t_lhs == t_rhs; }
189  template<typename LHS, typename RHS>
190  [[nodiscard]] constexpr bool operator()(const LHS &t_lhs, const RHS &t_rhs) const noexcept {
191  return std::equal(t_lhs.begin(), t_lhs.end(), t_rhs.begin(), t_rhs.end());
192  }
193  struct is_transparent {
194  };
195  };
196 
197  struct str_less {
198  [[nodiscard]] bool operator()(const std::string &t_lhs, const std::string &t_rhs) const noexcept { return t_lhs < t_rhs; }
199  template<typename LHS, typename RHS>
200  [[nodiscard]] constexpr bool operator()(const LHS &t_lhs, const RHS &t_rhs) const noexcept {
201  return std::lexicographical_compare(t_lhs.begin(), t_lhs.end(), t_rhs.begin(), t_rhs.end());
202  }
203  struct is_transparent {
204  };
205  };
206 
207  enum class Options {
208  No_Load_Modules,
209  Load_Modules,
210  No_External_Scripts,
211  External_Scripts
212  };
213 
214  template<typename From, typename To>
215  struct is_nothrow_forward_constructible : std::bool_constant<noexcept(To{std::declval<From>()})> {
216  };
217 
218  template<class From, class To>
219  static inline constexpr bool is_nothrow_forward_constructible_v = is_nothrow_forward_constructible<From, To>::value;
220 
221  template<typename Container, typename... T>
222  [[nodiscard]] constexpr auto make_container(T &&...t) {
223  Container c;
224  c.reserve(sizeof...(t));
225  (c.push_back(std::forward<T>(t)), ...);
226  return c;
227  }
228 
229  template<typename... T>
230  [[nodiscard]] auto make_vector(T &&...t) -> std::vector<std::common_type_t<std::decay_t<T>...>> {
231  using container_type = std::vector<std::common_type_t<std::decay_t<T>...>>;
232 
233  return make_container<container_type>(std::forward<T>(t)...);
234  }
235 
236  [[nodiscard]] inline std::vector<Options> default_options() {
237 #ifdef CHAISCRIPT_NO_DYNLOAD
238  return {Options::No_Load_Modules, Options::External_Scripts};
239 #else
240  return {Options::Load_Modules, Options::External_Scripts};
241 #endif
242  }
243 } // namespace chaiscript
244 #endif
Definition: compiled_tests.cpp:1116
Definition: chaiscript_defines.hpp:197
Definition: chaiscript_defines.hpp:193
Namespace chaiscript contains every API call that the average user will be concerned with...
Definition: chaiscript_defines.hpp:187
Definition: chaiscript_defines.hpp:203
Definition: chaiscript_defines.hpp:103
Definition: chaiscript_defines.hpp:215