Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions include/rfl/avro/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,29 @@ struct Reader {
avro_value_get_boolean(_var.val_, &result);
return (result != 0);

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type == AVRO_DOUBLE) {
double result = 0.0;
const auto err = avro_value_get_double(_var.val_, &result);
if (err) {
return error("Could not cast to double.");
}
return static_cast<T>(result);
} else if (type == AVRO_INT32) {
} else if (type == AVRO_FLOAT) {
float result = 0.0;
const auto err = avro_value_get_float(_var.val_, &result);
if (err) {
return error("Could not cast to float.");
}
return static_cast<T>(result);
} else {
return rfl::error(
"Could not cast to numeric value. The type must be float "
"or double.");
}

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (type == AVRO_INT32) {
int32_t result = 0;
const auto err = avro_value_get_int(_var.val_, &result);
if (err) {
Expand All @@ -108,17 +121,10 @@ struct Reader {
return error("Could not cast to int64.");
}
return static_cast<T>(result);
} else if (type == AVRO_FLOAT) {
float result = 0.0;
const auto err = avro_value_get_float(_var.val_, &result);
if (err) {
return error("Could not cast to float.");
}
return static_cast<T>(result);
} else {
return rfl::error(
"Could not cast to numeric value. The type must be integral.");
}
return rfl::error(
"Could not cast to numeric value. The type must be integral, float "
"or double.");

} else if constexpr (internal::is_literal_v<T>) {
int value = 0;
Expand Down
20 changes: 14 additions & 6 deletions include/rfl/bson/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct Reader {
return error(
"Could not cast to string. The type must be UTF8 or symbol.");
}

} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (btype != BSON_TYPE_BINARY) {
Expand All @@ -123,17 +124,23 @@ struct Reader {
const auto data =
internal::ptr_cast<const std::byte*>(value.v_binary.data);
return rfl::Bytestring(data, data + value.v_binary.data_len);

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != BSON_TYPE_BOOL) {
return error("Could not cast to boolean.");
}
return value.v_bool;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
case BSON_TYPE_DOUBLE:
return static_cast<T>(value.v_double);

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (btype != BSON_TYPE_DOUBLE) {
return error(
"Could not cast to numeric value. The type must be double, "
"int32, int64 or date_time.");
}
return static_cast<T>(value.v_double);

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
case BSON_TYPE_INT32:
return static_cast<T>(value.v_int32);

Expand All @@ -145,9 +152,10 @@ struct Reader {

default:
return error(
"Could not cast to numeric value. The type must be double, "
"Could not cast to numeric value. The type must be "
"int32, int64 or date_time.");
}

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
if (btype != BSON_TYPE_OID) {
return error("Could not cast to OID.");
Expand Down
16 changes: 8 additions & 8 deletions include/rfl/capnproto/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ class Reader {
}
return _var.val_.as<bool>();

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type != capnp::DynamicValue::FLOAT) {
return error("Could not cast to float.");
}
return static_cast<T>(_var.val_.as<double>());

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
switch (type) {
case capnp::DynamicValue::INT:
return static_cast<T>(_var.val_.as<int64_t>());

case capnp::DynamicValue::UINT:
return static_cast<T>(_var.val_.as<uint64_t>());

case capnp::DynamicValue::FLOAT:
return static_cast<T>(_var.val_.as<double>());

default:
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to an integer.");
}

} else if constexpr (internal::is_literal_v<T>) {
Expand Down
14 changes: 9 additions & 5 deletions include/rfl/cbor/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Reader {
return error("Could not cast to string.");
}
return _var.val_->as<std::string>();

} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.val_->is_byte_string()) {
Expand All @@ -79,25 +80,28 @@ class Reader {
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
return error("Could not cast to boolean.");
}
return _var.val_->as<bool>();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (_var.val_->is_double()) {
return static_cast<T>(_var.val_->as<double>());
}
return error("Could not cast to double.");

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (_var.val_->is_int64()) {
return static_cast<T>(_var.val_->as<int64_t>());
}
if (_var.val_->is_uint64()) {
return static_cast<T>(_var.val_->as<uint64_t>());
}
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to integer.");

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
9 changes: 7 additions & 2 deletions include/rfl/flexbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct Reader {
return error("Could not cast to a string.");
}
return std::string(_var.AsString().c_str());

} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.IsBlob()) {
Expand All @@ -82,21 +83,25 @@ struct Reader {
const auto blob = _var.AsBlob();
const auto data = internal::ptr_cast<const std::byte*>(blob.data());
return rfl::Bytestring(data, data + blob.size());

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.IsBool()) {
return error("Could not cast to boolean.");
}
return _var.AsBool();

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!_var.IsNumeric()) {
if (!_var.IsFloat()) {
return error("Could not cast to double.");
}
return static_cast<T>(_var.AsDouble());

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!_var.IsNumeric()) {
if (!_var.IsIntOrUint()) {
return error("Could not cast to int.");
}
return static_cast<T>(_var.AsInt64());

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
5 changes: 5 additions & 0 deletions include/rfl/json/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,31 @@ struct Reader {
return error("Could not cast to string.");
}
return std::string(r);

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!yyjson_is_bool(_var.val_)) {
return error("Could not cast to boolean.");
}
return yyjson_get_bool(_var.val_);

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!yyjson_is_num(_var.val_)) {
return error("Could not cast to double.");
}
return static_cast<T>(yyjson_get_num(_var.val_));

} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
if (!yyjson_is_int(_var.val_)) {
return error("Could not cast to int.");
}
return static_cast<T>(yyjson_get_uint(_var.val_));

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!yyjson_is_int(_var.val_)) {
return error("Could not cast to int.");
}
return static_cast<T>(yyjson_get_sint(_var.val_));

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
4 changes: 4 additions & 0 deletions include/rfl/msgpack/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct Reader {
}
const auto str = _var.via.str;
return std::string(str.ptr, str.size);

} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (type != MSGPACK_OBJECT_BIN) {
Expand All @@ -72,11 +73,13 @@ struct Reader {
const auto bin = _var.via.bin;
const auto data = internal::ptr_cast<const std::byte*>(bin.ptr);
return rfl::Bytestring(data, data + bin.size);

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != MSGPACK_OBJECT_BOOLEAN) {
return error("Could not cast to boolean.");
}
return _var.via.boolean;

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 ||
type == MSGPACK_OBJECT_FLOAT) {
Expand All @@ -85,6 +88,7 @@ struct Reader {
return error(
"Could not cast to numeric value. The type must be float "
"or double.");

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
return static_cast<T>(_var.via.u64);
Expand Down
4 changes: 4 additions & 0 deletions include/rfl/toml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,25 @@ struct Reader {
return error("Could not cast to std::string!");
}
return _var->as_string();

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var->is_boolean()) {
return error("Could not cast to bool!");
}
return _var->as_boolean();

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!_var->is_floating()) {
return error("Could not cast to double!");
}
return static_cast<std::remove_cvref_t<T>>(_var->as_floating());

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!_var->is_integer()) {
return error("Could not cast to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(_var->as_integer());

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
14 changes: 9 additions & 5 deletions include/rfl/ubjson/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Reader {
return error("Could not cast to string.");
}
return _var.val_->as<std::string>();

} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.val_->is<std::vector<uint8_t>>()) {
Expand All @@ -79,25 +80,28 @@ class Reader {
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
return error("Could not cast to boolean.");
}
return _var.val_->as<bool>();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (_var.val_->is_double()) {
return static_cast<T>(_var.val_->as<double>());
}
return error("Could not cast to floating point value.");

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (_var.val_->is_int64()) {
return static_cast<T>(_var.val_->as<int64_t>());
}
if (_var.val_->is_uint64()) {
return static_cast<T>(_var.val_->as<uint64_t>());
}
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to integer.");

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
4 changes: 4 additions & 0 deletions include/rfl/xml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ struct Reader {

if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return std::visit(get_value, _var.node_or_attribute_);

} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
return std::visit(get_value, _var.node_or_attribute_) == "true";

} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto str = std::visit(get_value, _var.node_or_attribute_);
try {
Expand All @@ -93,13 +95,15 @@ struct Reader {
return error("Could not cast '" + std::string(str) +
"' to floating point value.");
}

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto str = std::visit(get_value, _var.node_or_attribute_);
try {
return static_cast<T>(std::stoi(str));
} catch (std::exception& e) {
return error("Could not cast '" + std::string(str) + "' to integer.");
}

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
Expand Down
3 changes: 3 additions & 0 deletions include/rfl/yaml/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ struct Reader {
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_floating_point<std::remove_cvref_t<T>>()) {
return _var.node_.as<std::remove_cvref_t<T>>();

} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
return static_cast<T>(_var.node_.as<std::remove_cvref_t<int64_t>>());

} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}

} catch (std::exception& e) {
return error(e.what());
}
Expand Down
Loading