template<typename T, typename U>
void add_property_immutable(T property_name, U property_value) {
auto get_v8_data_type = []<typename X>(X&& property_name_or_value) -> int {
using removed_reference = std::remove_reference<X>::type;
using removed_pointer = std::remove_pointer<removed_reference>::type;
using removed_const = std::remove_const<removed_pointer>::type;
if(std::is_same_v<bool, removed_const>) { return BOOL; }
else if(std::is_same_v<int, removed_const>) { return INTEGER;}
else if(std::is_same_v<char, removed_const>) { return CHAR; }
else if(std::is_same_v<double, removed_const> || std::is_same_v<float, removed_const>) { return NUMBER; }
else if(std::is_same_v<std::string, removed_const>) { return STRING; }
return -1;
};
int property_name_data_type = get_v8_data_type(property_name);
if (property_name_data_type == CHAR) {
property_name_string = std::string(property_name);
}
else if(property_name_data_type == STRING) {
property_name_string = property_name;
}
else {
throw std::invalid_argument("slim::plugin::plugin::add_property_immutable requires a string or char type for the property name");
}
int property_value_data_type = get_v8_data_type(property_value, true);
if (property_value_data_type == BOOL) {
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), v8::Boolean::New(isolate, property_value), v8::ReadOnly);
}
else if(property_value_data_type == CHAR || property_value_data_type == STRING) {
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), slim::utilities::StringToV8String(isolate, property_value), v8::ReadOnly);
}
else if(property_value_data_type == INTEGER) {
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), v8::Integer::New(isolate, property_value), v8::ReadOnly);
}
else if(property_value_data_type == NUMBER) {
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), v8::Number::New(isolate, property_value), v8::ReadOnly);
}
else {
throw std::invalid_argument("slim::plugin::plugin::add_property_immutable requires a primitive type for the property value");
}
}
These calls from an external source:
os_plugin.add_property_immutable("test_var1", "test string");
os_plugin.add_property_immutable("number", 5);
Generate compiler errors like these:
os_plugin.add_property_immutable("test_var1", "test string")
error: invalid conversion from 'const char*' to 'int32_t' {aka 'int'} [-fpermissive]
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), v8::Integer::New(isolate, property_value), v8::ReadOnly);
error: cannot convert 'const char*' to 'double'
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), v8::Number::New(isolate, property_value), v8::ReadOnly)
os_plugin.add_property_immutable("number", 5);
error: could not convert 'property_value' from 'int' to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), slim::utilities::StringToV8String(isolate, property_value), v8::ReadOnly);
This error occurs when compiling using:
if constexpr (property_value_data_type == BOOL)
error: the value of ‘property_value_data_type’ is not usable in a constant expression
if constexpr (property_value_data_type == BOOL)
note: ‘int property_value_data_type’ is not const
int property_value_data_type = get_v8_data_type(property_value, true);
I have tried using a switch statement, which also gives the same errors. I am wondering what I have done wrong with this? It seems like the compiler is hitting every if/else in the chain instead of stopping on the correct if/else block.
I have tried making the if block a constexpr, but another set of errors crops up.
The uppercased variables are macros assigned int values. I have made them const int expressions for them, with no change in results.