14#pragma warning(disable:2586)
30 template <>
string ts(
const PropArray &p,
int width,
char notation,
int precision,
char fill);
32 using PropBase = std::variant<int64, double, bool, string, DVect2,
37 namespace PropertyConcepts {
42 template <
class T,
class V>
44 std::holds_alternative<T>(std::declval<V>());
47 concept IntNoBool = std::integral<T> and (not std::same_as<T, bool>);
52 class Property :
public PropBase {
54 enum class Type { Int, Double, Bool,
String, DVect2,
55 DVect3, I64Vect2, I64Vect3,
Matrix, DAVect2,
59 Property(): PropBase(
nullptr) {}
62 BASE_EXPORT std::partial_ordering operator<=>(
const Property &p)
const;
66 inline Type type()
const;
69 inline bool isNull()
const {
return type()==Type::Null; }
70 inline bool isValid()
const {
return not isNull(); }
75 bool canConvert()
const {
static_assert(
sizeof(T)==0);
return false; }
79 bool canConvertVec()
const;
83 bool canConvertType()
const {
return canConvert<decltype(std::get<static_cast<int>(t)>(*this))>(); }
87 T to()
const {
static_assert(
sizeof(T)==0);
return false; }
89 std::vector<T> toVec()
const;
93 auto toType()
const {
return to<decltype(std::get<static_cast<int>(t)>(*this))>(); }
98 typename std::tuple<T,bool> toTest()
const;
102 typename std::variant_alternative_t<static_cast<int>(t),Property> toTestType()
const;
104 template <
typename T>
105 void setValue(
const T &t) { *
this = t; }
106 template <
typename T>
107 void setVec(
const std::vector<T> &v);
109 template <
typename T>
110 T value()
const {
return to<T>(); }
113 inline double toDouble()
const;
114 inline string toString()
const;
115 inline int64 toInt()
const;
116 inline uint32 toUInt()
const;
117 inline double toDouble(
bool *ok)
const;
118 inline int64 toInt(
bool *ok)
const;
120 static constexpr Type DVectType(uint32 dim) {
return dim==2 ? Type::DVect2 : Type::DVect3; }
121 static constexpr Type DAVectType(uint32 dim) {
return dim==2 ? Type::DAVect2 : Type::DAVect3; }
122 static constexpr Type IVectType(uint32 dim) {
return dim==2 ? Type::I64Vect2 : Type::I64Vect3; }
125 Property::Type Property::type()
const {
126 return static_cast<Type
>(index());
130 auto &vl =
static_cast<const std::vector<base::Property> &
>(lhs);
131 auto &vr =
static_cast<const std::vector<base::Property> &
>(rhs);
135 template <>
BASE_EXPORT bool Property::canConvert<int64>()
const;
136 template <>
BASE_EXPORT bool Property::canConvert<double>()
const;
137 template <>
BASE_EXPORT bool Property::canConvert<bool>()
const;
138 template <>
BASE_EXPORT bool Property::canConvert<string>()
const;
139 template <>
BASE_EXPORT bool Property::canConvert<DVect2>()
const;
140 template <>
BASE_EXPORT bool Property::canConvert<DVect3>()
const;
141 template <>
BASE_EXPORT bool Property::canConvert<I64Vect2>()
const;
142 template <>
BASE_EXPORT bool Property::canConvert<I64Vect3>()
const;
143 template <>
BASE_EXPORT bool Property::canConvert<itasca::Mat>()
const;
144 template <>
BASE_EXPORT bool Property::canConvert<DAVect2>()
const;
145 template <>
BASE_EXPORT bool Property::canConvert<DAVect3>()
const;
146 template <>
BASE_EXPORT bool Property::canConvert<Quat2>()
const;
147 template <>
BASE_EXPORT bool Property::canConvert<Quat3>()
const;
148 template <>
BASE_EXPORT bool Property::canConvert<SymTensor>()
const;
149 template <>
BASE_EXPORT bool Property::canConvert<PropArray>()
const;
151 template <>
BASE_EXPORT int64 Property::to<int64>()
const;
152 template <>
BASE_EXPORT double Property::to<double>()
const;
153 template <>
BASE_EXPORT bool Property::to<bool>()
const;
154 template <>
BASE_EXPORT string Property::to<string>()
const;
155 template <>
BASE_EXPORT DVect2 Property::to<DVect2>()
const;
156 template <>
BASE_EXPORT DVect3 Property::to<DVect3>()
const;
157 template <>
BASE_EXPORT I64Vect2 Property::to<I64Vect2>()
const;
158 template <>
BASE_EXPORT I64Vect3 Property::to<I64Vect3>()
const;
159 template <>
BASE_EXPORT itasca::Mat Property::to<itasca::Mat>()
const;
160 template <>
BASE_EXPORT DAVect2 Property::to<DAVect2>()
const;
161 template <>
BASE_EXPORT DAVect3 Property::to<DAVect3>()
const;
162 template <>
BASE_EXPORT Quat2 Property::to<Quat2>()
const;
163 template <>
BASE_EXPORT Quat3 Property::to<Quat3>()
const;
164 template <>
BASE_EXPORT SymTensor Property::to<SymTensor>()
const;
167 class PropertyConvertException :
public Exception {
169 PropertyConvertException(Property::Type from,Property::Type
to) :
170 Exception(
"Error converting Property from {} to {}.",
171 Property::nameFromType(from),Property::nameFromType(
to)) {
175 template <
typename T>
176 void Property::setVec(
const std::vector<T> &v) {
179 for (
size_t i=0;i<v.size();++i) {
181 pa.back().setValue(v[i]);
182 if (not i) t = pa.back().type();
183 else if (t!=pa.back().type())
throw Exception(
"All elements of a property array must be of the same type.");
188 double Property::toDouble()
const {
return to<double>(); }
189 string Property::toString()
const {
return to<string>(); }
190 int64 Property::toInt()
const {
return to<int64>();}
191 uint32 Property::toUInt()
const {
return static_cast<uint32
>(to<int64>()); }
193 double Property::toDouble(
bool *ok)
const {
194 *ok = canConvert<double>();
195 if (*ok)
return to<double>();
199 int64 Property::toInt(
bool *ok)
const {
200 *ok = canConvert<int64>();
201 if (*ok)
return to<int64>();
205 template <
typename T>
206 typename std::tuple<T,bool> Property::toTest()
const {
207 bool b = canConvert<T>();
213 template <Property::Type t>
214 typename std::variant_alternative_t<static_cast<int>(t),
Property> Property::toTestType()
const {
215 using target_type =
decltype(std::get<static_cast<int>(t)>(*this));
216 using return_type = std::tuple<target_type,bool>;
217 bool b = canConvert<target_type>();
218 if (b)
return return_type(to<target_type>(),
true);
219 return return_type(target_type{},
false);
222 template <
typename T>
223 bool Property::canConvertVec()
const {
224 if (type()!=Type::Array)
return false;
225 auto &a = std::get<PropArray>(*
this);
226 if (not a.size())
return true;
227 return a[0].canConvert<T>();
230 template <
typename T>
231 std::vector<T> Property::toVec()
const {
232 if (type()!=Type::Array)
throw PropertyConvertException(type(),Type::Array);
233 auto &a = std::get<PropArray>(*
this);
234 if (not a.size())
return {};
237 ret.push_back(v.to<T>());
243 template <Property::Type t>
244 const auto &get(
const Property &v) {
return std::get<static_cast<int>(t)>(v); }
248 BASE_EXPORT string ts<base::Property>(
const base::Property &p,
int width,
char notation,
int precision,
char fill);
253 PropDesc(
const string &name,Property::Type type,UVect2 size) : name_(name), type_(type), size_(size) { }
256 Property::Type type_ = Property::Type::Int;
257 UVect2 size_ = UVect2(0);
258 auto operator<=>(
const PropDesc &p)
const =
default;
268struct std::formatter<base::Property> :
public std::formatter<string> {
269 template <
typename ParseContext>
270 constexpr auto parse(ParseContext &ctx) {
return std::formatter<string>::parse(ctx); }
272 template <
typename FormatContext>
273 constexpr auto format(
base::Property const &val, FormatContext &ctx)
const {
274 return std::formatter<string>::format(val.to<
string>(), ctx);
includes std::string and additional functions not included in the standard.
A template-based matrix class, size fixed at compile time. Defaults to symmetric sized matrix.
Definition matrix.h:22
2D quaternion-like utility class. In this case only the angle (in radians) is stored as opposed to th...
Definition quat.h:20
3D quaternion utility class.
Definition quat.h:112
A symmetric 2nd order tensor.
Definition symtensor.h:22
std::basic_string< char8 > String
std::string of type Char
Definition basebool.h:9
#define BASE_EXPORT
Definition basedef.h:25
constexpr D to(const T &t)
This template function serves as an alternative to static_cast<T>().
Definition to.h:28
2D and 3D quaternion utility classes.
2D and 3D vector utility classes.