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 <vector>
24
#include <string_view>
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

53
#if defined(__llvm__)
54
#define CHAISCRIPT_CLANG
55
#endif
56

57

58
#ifdef  CHAISCRIPT_HAS_DECLSPEC
59
#define CHAISCRIPT_MODULE_EXPORT extern "C" __declspec(dllexport)
60
#else
61
#define CHAISCRIPT_MODULE_EXPORT extern "C" 
62
#endif
63

64
#if defined(CHAISCRIPT_MSVC) || (defined(__GNUC__) && __GNUC__ >= 5) || defined(CHAISCRIPT_CLANG)
65
#define CHAISCRIPT_UTF16_UTF32
66
#endif
67

68
#ifdef _DEBUG
69
#define CHAISCRIPT_DEBUG true
70
#else
71
#define CHAISCRIPT_DEBUG false
72
#endif
73

74
#include <memory>
75
#include <string>
76
#include <cmath>
77

78
namespace chaiscript {
79
  constexpr static const int version_major = 7;
80
  constexpr static const int version_minor = 0;
81
  constexpr static const int version_patch = 0;
82

83
  constexpr static const char *compiler_version = CHAISCRIPT_COMPILER_VERSION;
84
  constexpr static const char *compiler_name = CHAISCRIPT_COMPILER_NAME;
85
  constexpr static const bool debug_build = CHAISCRIPT_DEBUG;
86

87
  template<typename B, typename D, typename ...Arg>
88 1
  inline std::shared_ptr<B> make_shared(Arg && ... arg)
89
  {
90
#ifdef CHAISCRIPT_USE_STD_MAKE_SHARED
91
    return std::make_shared<D>(std::forward<Arg>(arg)...);
92
#else
93 1
    return std::shared_ptr<B>(static_cast<B*>(new D(std::forward<Arg>(arg)...)));
94
#endif
95
  }
96

97
  template<typename B, typename D, typename ...Arg>
98 1
  inline std::unique_ptr<B> make_unique(Arg && ... arg)
99
  {
100
#ifdef CHAISCRIPT_USE_STD_MAKE_SHARED
101
    return std::make_unique<D>(std::forward<Arg>(arg)...);
102
#else
103 1
    return std::unique_ptr<B>(static_cast<B*>(new D(std::forward<Arg>(arg)...)));
104
#endif
105
  }
106

107
  struct Build_Info {
108 1
    [[nodiscard]] constexpr static int version_major() noexcept
109
    {
110 1
      return chaiscript::version_major;
111
    }
112

113 1
    [[nodiscard]] constexpr static int version_minor() noexcept
114
    {
115 1
      return chaiscript::version_minor;
116
    }
117

118 1
    [[nodiscard]] constexpr static int version_patch() noexcept
119
    {
120 1
      return chaiscript::version_patch;
121
    }
122

123 1
    [[nodiscard]] static std::string version()
124
    {
125 1
      return std::to_string(version_major()) + '.' + std::to_string(version_minor()) + '.' + std::to_string(version_patch());
126
    }
127

128 0
    [[nodiscard]] static std::string compiler_id()
129
    {
130 0
      return compiler_name() + '-' + compiler_version();
131
    }
132

133
    [[nodiscard]] static std::string build_id()
134
    {
135
      return compiler_id() + (debug_build()?"-Debug":"-Release");
136
    }
137

138 0
    [[nodiscard]] static std::string compiler_version()
139
    {
140 0
      return chaiscript::compiler_version;
141
    }
142

143 0
    [[nodiscard]] static std::string compiler_name()
144
    {
145 0
      return chaiscript::compiler_name;
146
    }
147

148 0
    [[nodiscard]] constexpr static bool debug_build() noexcept
149
    {
150 0
      return chaiscript::debug_build;
151
    }
152
  };
153

154

155
  template<typename T>
156 1
    [[nodiscard]] constexpr auto parse_num(const std::string_view t_str) noexcept -> typename std::enable_if<std::is_integral<T>::value, T>::type
157
    {
158 1
      T t = 0;
159 1
      for (const auto c : t_str) {
160 1
        if (c < '0' || c > '9') {
161 0
          return t;
162
        }
163 1
        t *= 10;
164 1
        t += c - '0';
165
      }
166 1
      return t;
167
    }
168

169

170
    template<typename T>
171 1
    [[nodiscard]] auto parse_num(const std::string_view t_str) -> typename std::enable_if<!std::is_integral<T>::value, T>::type
172
    {
173 1
       T t = 0;
174 1
       T base{};
175 1
       T decimal_place = 0;
176 1
       int exponent = 0;
177

178 1
       for (const auto c : t_str) {
179 1
          switch (c)
180
          {
181 1
          case '.':
182 1
             decimal_place = 10;
183 1
             break;
184 1
          case 'e':
185
          case 'E':
186 1
             exponent = 1;
187 1
             decimal_place = 0;
188 1
             base = t;
189 1
             t = 0;
190 1
             break;
191 1
          case '-':
192 1
             exponent = -1;
193 1
             break;
194 1
          case '+':
195 1
             break;
196 1
          case '0':
197
          case '1':
198
          case '2':
199
          case '3':
200
          case '4':
201
          case '5':
202
          case '6':
203
          case '7':
204
          case '8':
205
          case '9':
206 1
             if (decimal_place < 10) {
207 1
                t *= 10;
208 1
                t += static_cast<T>(c - '0');
209
             }
210
             else {
211 1
                t += static_cast<T>(c - '0') / decimal_place;
212 1
                decimal_place *= 10;
213
             }
214 1
             break;
215 0
          default:
216 0
             break;
217
          }
218
       }
219 1
       return exponent ? base * std::pow(T(10), t * static_cast<T>(exponent)) : t;
220
    }
221

222
  struct str_equal {
223 1
    [[nodiscard]] bool operator()(const std::string &t_lhs, const std::string &t_rhs) const noexcept {
224 1
      return t_lhs == t_rhs;
225
    }
226
    template<typename LHS, typename RHS>
227 1
      [[nodiscard]] constexpr bool operator()(const LHS &t_lhs, const RHS &t_rhs) const noexcept {
228 1
        return std::equal(t_lhs.begin(), t_lhs.end(), t_rhs.begin(), t_rhs.end());
229
      }
230
    struct is_transparent{};
231
  };
232

233

234
  struct str_less {
235 1
    [[nodiscard]] bool operator()(const std::string &t_lhs, const std::string &t_rhs) const noexcept {
236 1
      return t_lhs < t_rhs;
237
    }
238
    template<typename LHS, typename RHS>
239 1
      [[nodiscard]] constexpr bool operator()(const LHS &t_lhs, const RHS &t_rhs) const noexcept {
240 1
        return std::lexicographical_compare(t_lhs.begin(), t_lhs.end(), t_rhs.begin(), t_rhs.end());
241
      }
242
    struct is_transparent{};
243
  };
244

245
  enum class Options
246
  {
247
    No_Load_Modules,
248
    Load_Modules,
249
    No_External_Scripts,
250
    External_Scripts
251
  };
252

253
  template<typename From, typename To>
254
  struct is_nothrow_forward_constructible
255
    : std::bool_constant<noexcept(To{std::declval<From>()})>
256
  {
257
  };
258

259
  template< class From, class To >
260
  inline constexpr bool is_nothrow_forward_constructible_v 
261
    = is_nothrow_forward_constructible<From, To>::value;
262

263
  template<typename Container, typename ... T>
264 1
  [[nodiscard]] constexpr auto make_container(T && ... t)
265
  {
266 1
    Container c;
267 1
    c.reserve(sizeof...(t));
268 1
    (c.push_back(std::forward<T>(t)), ...);
269 1
    return c;
270
  }
271

272

273
  template<typename ... T>
274 1
  [[nodiscard]] auto make_vector(T &&... t) -> std::vector<std::common_type_t<std::decay_t<T>...>>
275
  {
276
    using container_type = 
277
      std::vector<std::common_type_t<std::decay_t<T>...>>;
278

279 1
    return make_container<container_type>(std::forward<T>(t)...);
280
  }
281

282

283

284 1
  static inline std::vector<Options> default_options()
285
  {
286
#ifdef CHAISCRIPT_NO_DYNLOAD
287
    return {Options::No_Load_Modules, Options::External_Scripts};
288
#else
289 1
    return {Options::Load_Modules, Options::External_Scripts};
290
#endif
291
  }
292
}
293
#endif
294

Read our documentation on viewing source code .

Loading