27 const T& convert(
const boost::any& any) noexcept {
28 return *boost::any_cast<T>(&any);
32 template <
class T,
class StringType>
33 T fromString(StringType&& valueStr) {
43 T makePrimitiveOf(
const boost::any& any,
TypeID type);
46 bool makePrimitiveOf<bool>(
const boost::any& any,
TypeID type) {
49 return convert<bool>(any);
51 return (
bool)convert<int>(any);
53 return (
bool)convert<std::int64_t>(any);
55 return (
bool)convert<float>(any);
57 return (
bool)convert<double>(any);
59 return internal::fromString<bool>(convert<std::string>(any));
67 int makePrimitiveOf<int>(
const boost::any& any,
TypeID type) {
70 return (
int)convert<bool>(any);
72 return convert<int>(any);
74 return (
int)convert<std::int64_t>(any);
75 case TypeID::Float32: {
76 if((
float)
static_cast<int>(convert<float>(any)) != convert<float>(any))
77 throw Exception(
"conversion of [type = %s] to [T = %s] results in truncation of the value",
79 return (
int)convert<float>(any);
81 case TypeID::Float64: {
82 if((
double)
static_cast<int>(convert<double>(any)) != convert<double>(any))
83 throw Exception(
"conversion of [type = %s] to [T = %s] results in truncation of the value",
85 return (
int)convert<double>(any);
88 return internal::fromString<int>(convert<std::string>(any));
96 std::int64_t makePrimitiveOf<std::int64_t>(
const boost::any& any,
TypeID type) {
99 return (std::int64_t)convert<bool>(any);
101 return (std::int64_t)convert<int>(any);
103 return convert<std::int64_t>(any);
104 case TypeID::Float32: {
105 if((
float)
static_cast<std::int64_t
>(convert<float>(any)) != convert<float>(any))
106 throw Exception(
"conversion of [type = %s] to [T = %s] results in truncation of the value",
108 return (std::int64_t)convert<float>(any);
110 case TypeID::Float64: {
111 if((
double)
static_cast<std::int64_t
>(convert<double>(any)) != convert<double>(any))
112 throw Exception(
"conversion of [type = %s] to [T = %s] results in truncation of the value",
114 return (std::int64_t)convert<double>(any);
117 return internal::fromString<std::int64_t>(convert<std::string>(any));
119 throw Exception(
"cannot convert [type = %s] to [T = std::int64_t]",
TypeUtil::toString(type));
125 float makePrimitiveOf<float>(
const boost::any& any,
TypeID type) {
127 case TypeID::Boolean:
128 return (
float)convert<bool>(any);
130 return (
float)convert<int>(any);
132 return (
float)convert<std::int64_t>(any);
133 case TypeID::Float32:
134 return convert<float>(any);
135 case TypeID::Float64:
136 return (
float)convert<double>(any);
138 return internal::fromString<float>(convert<std::string>(any));
140 throw Exception(
"cannot convert [type = %s] to [T = float]",
TypeUtil::toString(type));
146 double makePrimitiveOf<double>(
const boost::any& any,
TypeID type) {
148 case TypeID::Boolean:
149 return (
double)convert<bool>(any);
151 return (
double)convert<int>(any);
153 return (
double)convert<std::int64_t>(any);
154 case TypeID::Float32:
155 return (
double)convert<float>(any);
156 case TypeID::Float64:
157 return convert<double>(any);
159 return internal::fromString<double>(convert<std::string>(any));
161 throw Exception(
"cannot convert [type = %s] to [T = double]",
TypeUtil::toString(type));
167 std::string makePrimitiveOf<std::string>(
const boost::any& any,
TypeID type) {
169 case TypeID::Boolean:
170 return (convert<bool>(any) ?
"true" :
"false");
172 return std::to_string(convert<int>(any));
174 return std::to_string(convert<std::int64_t>(any));
175 case TypeID::Float32:
176 return std::to_string(convert<float>(any));
177 case TypeID::Float64:
178 return std::to_string(convert<double>(any));
180 return convert<std::string>(any);
182 throw Exception(
"cannot convert [type = %s] to [T = std::string]",
TypeUtil::toString(type));
188 template <
class T,
class ArrayType = Array<T>>
189 ArrayType makeArrayOf(
const boost::any& any,
TypeID type) {
191 throw Exception(
"cannot convert non-array [type = %s] to array [T = %s]",
197 case TypeID::Boolean: {
198 const auto& array = convert<Array<bool>>(any);
199 for(
const auto& a : array)
200 arrayT.push_back(makePrimitiveOf<T>(boost::any(
bool(a)), TypeID::Boolean));
203 case TypeID::Int32: {
204 const auto& array = convert<Array<int>>(any);
205 for(
const auto& a : array)
206 arrayT.push_back(makePrimitiveOf<T>(boost::any(
int(a)), TypeID::Int32));
209 case TypeID::Int64: {
210 const auto& array = convert<Array<std::int64_t>>(any);
211 for(
const auto& a : array)
212 arrayT.push_back(makePrimitiveOf<T>(boost::any(std::int64_t(a)), TypeID::Int64));
215 case TypeID::Float32: {
216 const auto& array = convert<Array<float>>(any);
217 for(
const auto& a : array)
218 arrayT.push_back(makePrimitiveOf<T>(boost::any(
float(a)), TypeID::Float32));
221 case TypeID::Float64: {
222 const auto& array = convert<Array<double>>(any);
223 for(
const auto& a : array)
224 arrayT.push_back(makePrimitiveOf<T>(boost::any(
double(a)), TypeID::Float64));
227 case TypeID::String: {
228 const auto& array = convert<Array<std::string>>(any);
229 for(
const auto& a : array)
230 arrayT.push_back(makePrimitiveOf<T>(boost::any(std::string(a)), TypeID::String));
243 if(type_ != right.type_)
249 case TypeID::Boolean:
250 return (convert<bool>() == right.convert<
bool>());
252 return (convert<int>() == right.convert<
int>());
254 return (convert<std::int64_t>() == right.convert<std::int64_t>());
255 case TypeID::Float32:
256 return (convert<float>() == right.convert<
float>());
257 case TypeID::Float64:
258 return (convert<double>() == right.convert<
double>());
260 return (convert<std::string>() == right.convert<std::string>());
263 case TypeID::ArrayOfBoolean:
265 case TypeID::ArrayOfInt32:
267 case TypeID::ArrayOfInt64:
269 case TypeID::ArrayOfFloat32:
271 case TypeID::ArrayOfFloat64:
273 case TypeID::ArrayOfString:
285 return internal::makePrimitiveOf<bool>(any_, type_);
290 return internal::makePrimitiveOf<int>(any_, type_);
295 return internal::makePrimitiveOf<std::int64_t>(any_, type_);
300 return internal::makePrimitiveOf<float>(any_, type_);
305 return internal::makePrimitiveOf<double>(any_, type_);
310 return internal::makePrimitiveOf<std::string>(any_, type_);
315 return internal::makeArrayOf<bool>(any_, type_);
320 return internal::makeArrayOf<int>(any_, type_);
325 return internal::makeArrayOf<std::int64_t>(any_, type_);
330 return internal::makeArrayOf<float>(any_, type_);
335 return internal::makeArrayOf<double>(any_, type_);
340 return internal::makeArrayOf<std::string>(any_, type_);
static bool isArray(TypeID id) noexcept
Check if type is an array of types.
std::vector< T > Array
Array class used by serialbox to store meta-information.
Namespace of the serialbox library.
TypeID
Type-id of types recognized by serialbox.
static std::string toString(TypeID id)
Convert to string.
static TypeID getPrimitive(TypeID id) noexcept
Return the underlying primitve type.
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.