Код: Выделить всё
template
void add_property_immutable(T property_name, U property_value) {
auto get_v8_data_type = [](X&& property_name_or_value) -> int {
using removed_reference = std::remove_reference::type;
using removed_pointer = std::remove_pointer::type;
using removed_const = std::remove_const::type;
if(std::is_same_v) { return BOOL; }
else if(std::is_same_v) { return INTEGER;}
else if(std::is_same_v) { return CHAR; }
else if(std::is_same_v || std::is_same_v) { return NUMBER; }
else if(std::is_same_v) { 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");
}
}
Код: Выделить всё
os_plugin.add_property_immutable("test_var1", "test string");
os_plugin.add_property_immutable("number", 5);
Код: Выделить всё
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'}
plugin_template->Set(slim::utilities::StringToV8String(isolate, property_name_string), slim::utilities::StringToV8String(isolate, property_value), v8::ReadOnly);
Код: Выделить всё
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);
Код: Выделить всё
elseКод: Выделить всё
elseЯ пытался сделать блок if constexpr, но возник другой набор ошибок.
Переменные в верхнем регистре — это макросы, которым присвоены значения int. Я сделал для них выражения const int без каких-либо изменений в результатах.
Подробнее здесь: https://stackoverflow.com/questions/795 ... e-function
Мобильная версия