Merge branch 'feature_enum_utility_helper' into Fix_Crash_From_CppCon

This commit is contained in:
Jason Turner
2016-01-03 18:13:48 -07:00
6 changed files with 131 additions and 31 deletions

View File

@@ -961,6 +961,40 @@ namespace chaiscript
}
void parse(const char_type t_char, const int line, const int col, const std::string &filename) {
const bool is_octal_char = t_char >= '0' && t_char <= '7';
if (is_octal) {
if (is_octal_char) {
octal_matches.push_back(t_char);
if (octal_matches.size() == 3) {
process_octal();
}
return;
} else {
process_octal();
}
} else if (is_hex) {
const bool is_hex_char = (t_char >= '0' && t_char <= '9')
|| (t_char >= 'a' && t_char <= 'f')
|| (t_char >= 'A' && t_char <= 'F');
if (is_hex_char) {
hex_matches.push_back(t_char);
if (hex_matches.size() == 2*sizeof(char_type)) {
// This rule differs from the C/C++ standard, but ChaiScript
// does not offer the same workaround options, and having
// hexadecimal sequences longer than can fit into the char
// type is undefined behavior anyway.
process_hex();
}
return;
} else {
process_hex();
}
}
if (t_char == '\\') {
if (is_escaped) {
match.push_back('\\');
@@ -970,31 +1004,7 @@ namespace chaiscript
}
} else {
if (is_escaped) {
const bool is_octal_char = t_char >= '0' && t_char <= '7';
if (is_octal) {
if (is_octal_char) {
octal_matches.push_back(t_char);
if (octal_matches.size() == 3) {
process_octal();
}
} else {
process_octal();
match.push_back(t_char);
}
} else if (is_hex) {
const bool is_hex_char = (t_char >= '0' && t_char <= '9')
|| (t_char >= 'a' && t_char <= 'f')
|| (t_char >= 'A' && t_char <= 'F');
if (is_hex_char) {
hex_matches.push_back(t_char);
} else {
process_hex();
match.push_back(t_char);
}
} else if (is_octal_char) {
if (is_octal_char) {
is_octal = true;
octal_matches.push_back(t_char);
} else if (t_char == 'x') {

View File

@@ -421,22 +421,22 @@ class JSON
Class Type;
};
JSON Array() {
inline JSON Array() {
return JSON::Make( JSON::Class::Array );
}
template <typename... T>
JSON Array( T... args ) {
inline JSON Array( T... args ) {
JSON arr = JSON::Make( JSON::Class::Array );
arr.append( args... );
return arr;
}
JSON Object() {
inline JSON Object() {
return JSON::Make( JSON::Class::Object );
}
std::ostream& operator<<( std::ostream &os, const JSON &json ) {
inline std::ostream& operator<<( std::ostream &os, const JSON &json ) {
os << json.dump();
return os;
}
@@ -636,7 +636,7 @@ namespace {
}
}
JSON JSON::Load( const string &str ) {
inline JSON JSON::Load( const string &str ) {
size_t offset = 0;
return parse_next( str, offset );
}

View File

@@ -8,12 +8,14 @@
#define CHAISCRIPT_UTILITY_UTILITY_HPP_
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "../chaiscript.hpp"
#include "../dispatchkit/proxy_functions.hpp"
#include "../dispatchkit/type_info.hpp"
#include "../dispatchkit/operators.hpp"
namespace chaiscript
@@ -62,6 +64,32 @@ namespace chaiscript
t_module.add(fun.first, fun.second);
}
}
template<typename Enum, typename ModuleType>
typename std::enable_if<std::is_enum<Enum>::value, void>::type
add_class(ModuleType &t_module,
const std::string &t_class_name,
const std::vector<std::pair<typename std::underlying_type<Enum>::type, std::string>> &t_constants)
{
t_module.add(chaiscript::user_type<Enum>(), t_class_name);
t_module.add(chaiscript::constructor<Enum ()>(), t_class_name);
t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
t_module.add([](){
// add some comparison and assignment operators
using namespace chaiscript::bootstrap::operators;
return assign<Enum>(not_equal<Enum>(equal<Enum>()));
}());
t_module.add(chaiscript::fun([](const Enum &e, const typename std::underlying_type<Enum>::type &i) { return e == i; }), "==");
t_module.add(chaiscript::fun([](const typename std::underlying_type<Enum>::type &i, const Enum &e) { return i == e; }), "==");
for (const auto &constant : t_constants)
{
t_module.add_global_const(chaiscript::const_var(Enum(constant.first)), constant.second);
}
}
}
}