diff --git a/src/data.cpp b/src/data.cpp index 9143918..a6723fe 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -36,6 +36,60 @@ Value::Helper::type(const Value& v) } } +Value Value::Helper::build(const void* ptr, StoreType type) +{ + TypeCode base{TypeCode::Null}; + switch (type) { + case StoreType::Bool: base = TypeCode::Bool; break; + case StoreType::Integer: base = TypeCode::Int64; break; + case StoreType::UInteger: base = TypeCode::UInt64; break; + case StoreType::Real: base = TypeCode::Float64; break; + case StoreType::String: base = TypeCode::String; break; + case StoreType::Array: { + auto& arr = *static_cast*>(ptr); + switch(arr.original_type()) { +#define CASE(TYPE) case ArrayType::TYPE: base = TypeCode::TYPE ## A; break + CASE(Bool); + CASE(Int8); + CASE(Int16); + CASE(Int32); + CASE(Int64); + CASE(UInt8); + CASE(UInt16); + CASE(UInt32); + CASE(UInt64); + CASE(Float32); + CASE(Float64); + CASE(String); +#undef CASE + case ArrayType::Value: + base = TypeCode::AnyA; + break; + case ArrayType::Null: + throw std::logic_error("Unable to infer ArrayType::Null"); + } + } + break; + case StoreType::Compound: { + auto src = *reinterpret_cast(ptr); + if(src) { + auto dst = TypeDef(src).create(); + dst.assign(src); + return dst; + } + } + base = TypeCode::Any; + break; + case StoreType::Null: + throw std::logic_error("Unable to infer ArrayType::Null"); + } + + Value ret(TypeDef(base).create()); + ret.copyIn(ptr, type); + + return ret; +} + Value::Value(const std::shared_ptr& desc) :desc(nullptr) { diff --git a/src/dataimpl.h b/src/dataimpl.h index 2a3cb51..2c0e8ba 100644 --- a/src/dataimpl.h +++ b/src/dataimpl.h @@ -27,6 +27,8 @@ struct Value::Helper { static inline Value build(const std::shared_ptr& desc, const std::shared_ptr& pstore, const impl::FieldDesc* pdesc); + static Value build(const void* ptr, StoreType type); + static inline std::shared_ptr& store( Value& v) { return v.store; } static inline std::shared_ptr store(const Value& v) { return v.store; } static inline const FieldDesc* desc(const Value& v) { return v.desc; }