Itasca C++ Interface
Loading...
Searching...
No Matches
property.h
1#pragma once
2
3// This is intended to replace Variant as a property container in constitutive models
4// in a way that makes more use of modern C++ (and is also less complex)
5
6#include "basestring.h"
7#include "mat.h"
8#include "quat.h"
9#include "vect.h"
10#include <variant>
11
12#ifdef INTELCOMP
13#pragma warning(disable:2586) // Disable warning about name length
14#endif
15
16namespace std {
17 class any;
18}
19
20namespace base {
21 using PropBase = std::variant<int64, double, bool, string, DVect2,
22 DVect3, I64Vect2, I64Vect3, itasca::Mat, DAVect2,
23 DAVect3, Quat2, Quat3, SymTensor>;
24
25 class Property : public PropBase {
26 public:
27 using PropBase::PropBase;
28
29 enum class Type { Int, Double, Bool, String, DVect2,
30 DVect3, I64Vect2, I64Vect3, Matrix, DAVect2,
31 DAVect3, Quat2 , Quat3, Tensor };
32
33 Type type() const { return static_cast<Type>(index()); }
34 BASE_EXPORT std::tuple<Type,UVect2> desc() const;
35 BASE_EXPORT const Property &reset(); // Keep type but set value to default construction.
36
37 // Returns TRUE if the type can be converted to the provided type. Does minimal computation.
38 // In theory we can add more type specializations (int, float, etc) based on existing.
39 template <typename T>
40 bool canConvert() const { static_assert(sizeof(T)==0); return false; } // Default
41 BASE_EXPORT bool canConvert(Type type) const; // Runtime type conversion query
42
43 // Same as canConvert but takes a Type enum as a template argument.
44 template <Type t>
45 bool canConvertType() const { return canConvert<decltype(std::get<static_cast<int>(t)>(*this))>(); }
46
47 // Converts to the type - throws exeption if not able to convert.
48 template <typename T>
49 T to() const { static_assert(sizeof(T)==0); return false; } // DEFAULT
50
51 // Save as to<>() but uses the Type enum as the template argument.
52 template <Type t>
53 auto toType() const { return to<decltype(std::get<static_cast<int>(t)>(*this))>(); }
54
55 // Single call test, returns both value and success boolean. Value is default init if
56 // success is false.
57 template <typename T>
58 typename std::tuple<T,bool> toTest() const;
59
60 // Same as toTest<>() but using Type enum.
61 template <Type t>
62 typename std::variant_alternative_t<static_cast<int>(t),Property> toTestType() const;
63
64 BASE_EXPORT static string nameFromType(Type t);
65
66 };
67
68 template <> BASE_EXPORT bool Property::canConvert<int64>() const;
69 template <> BASE_EXPORT bool Property::canConvert<double>() const;
70 template <> BASE_EXPORT bool Property::canConvert<bool>() const;
71 template <> BASE_EXPORT bool Property::canConvert<string>() const;
72 template <> BASE_EXPORT bool Property::canConvert<DVect2>() const;
73 template <> BASE_EXPORT bool Property::canConvert<DVect3>() const;
74 template <> BASE_EXPORT bool Property::canConvert<I64Vect2>() const;
75 template <> BASE_EXPORT bool Property::canConvert<I64Vect3>() const;
76 template <> BASE_EXPORT bool Property::canConvert<itasca::Mat>() const;
77 template <> BASE_EXPORT bool Property::canConvert<DAVect2>() const;
78 template <> BASE_EXPORT bool Property::canConvert<DAVect3>() const;
79 template <> BASE_EXPORT bool Property::canConvert<Quat2>() const;
80 template <> BASE_EXPORT bool Property::canConvert<Quat3>() const;
81 template <> BASE_EXPORT bool Property::canConvert<SymTensor>() const;
82
83 template <> BASE_EXPORT int64 Property::to<int64>() const;
84 template <> BASE_EXPORT double Property::to<double>() const;
85 template <> BASE_EXPORT bool Property::to<bool>() const;
86 template <> BASE_EXPORT string Property::to<string>() const;
87 template <> BASE_EXPORT DVect2 Property::to<DVect2>() const;
88 template <> BASE_EXPORT DVect3 Property::to<DVect3>() const;
89 template <> BASE_EXPORT I64Vect2 Property::to<I64Vect2>() const;
90 template <> BASE_EXPORT I64Vect3 Property::to<I64Vect3>() const;
91 template <> BASE_EXPORT itasca::Mat Property::to<itasca::Mat>() const;
92 template <> BASE_EXPORT DAVect2 Property::to<DAVect2>() const;
93 template <> BASE_EXPORT DAVect3 Property::to<DAVect3>() const;
94 template <> BASE_EXPORT Quat2 Property::to<Quat2>() const;
95 template <> BASE_EXPORT Quat3 Property::to<Quat3>() const;
96 template <> BASE_EXPORT SymTensor Property::to<SymTensor>() const;
97
98
99
100 template <typename T>
101 typename std::tuple<T,bool> Property::toTest() const {
102 bool b = canConvert<T>();
103 if (b)
104 return {to<T>(),b};
105 return {T{},false};
106 }
107
108 template <Property::Type t>
109 typename std::variant_alternative_t<static_cast<int>(t),Property> Property::toTestType() const {
110 using target_type = decltype(std::get<static_cast<int>(t)>(*this));
111 using return_type = std::tuple<target_type,bool>;
112 bool b = canConvert<target_type>();
113 if (b) return return_type(to<target_type>(),true);
114 return return_type(target_type{},false);
115 }
116
117 //template <typename T>
118 //T Property::to() const {
119 // auto [val,ok] = is<T>();
120 // if (ok==false)
121 // throw Exception("Unable to convert Property index {} to type {}.",index(),typeid(T).name());
122 // return val;
123 //}
124
125 // This allows using the Type enum as a selector in a get, so
126 // get<Property::Bool>(p);
127 template <Property::Type t>
128 const auto &get(const Property &v) { return std::get<static_cast<int>(t)>(v); }
129
130 // String conversion, using the base::ts standard
131 template <>
132 BASE_EXPORT string ts<base::Property>(const base::Property &p, int width, char notation, int precision, char fill);
133
134 // Description of a property type. The information necessary to parse.
135 struct PropDesc {
136 PropDesc() { }
137 PropDesc(const string &name,Property::Type type,UVect2 size) : name_(name), type_(type), size_(size) { }
138 BASE_EXPORT PropDesc(const string &name,const Property &prop);
139 string name_;
140 Property::Type type_ = Property::Type::Int;
141 UVect2 size_ = UVect2(0);
142 BASE_EXPORT bool operator<(const PropDesc &p) const;
143 };
144
145 BASE_EXPORT Property toProperty(const std::any &a);
146 BASE_EXPORT std::any toAny(const Property &p);
147} // namespace base
148// EoF
149
QString helper functions, plus some additions.
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:108
A symmetric 2nd order tensor.
Definition symtensor.h:22
Definition property.h:25
Definition mat.h:28
std::basic_string< char8 > String
std::string of type Char
Definition basebool.h:9
#define BASE_EXPORT
Definition basedef.h:24
2D and 3D quaternion utility classes.
Definition property.h:135
2D and 3D vector utility classes.