PeriDyno 1.0.0
Loading...
Searching...
No Matches
DeclareEnum.h
Go to the documentation of this file.
1
16#pragma once
17#include <map>
18#include <string>
19
20#include "Field.h"
21
22namespace dyno
23{
24
25 typedef std::map<int, std::string> EnumMap;
26 //typedef std::map<std::string, enum_map> enum_map_list;
27
28 #define DECLARE_ENUM(enum_type,...) \
29 enum enum_type{ \
30 __VA_ARGS__ \
31 }; \
32 const std::string full_name_##enum_type = #__VA_ARGS__;
33
34 bool parse_enum_string(const std::string& enum_str, EnumMap& enumKeyValueList);
35
36
37 class PEnum
38 {
39 public:
41 {
42 m_enum_name = "";
43 m_enum_value = -1;
44 }
45
46 PEnum(std::string enum_name, int val, const std::string enum_str)
47 {
49 m_enum_value = val;
50 m_enum_name = enum_name;
51 }
52
53 inline bool operator== (const int val) const
54 {
55 return m_enum_value == val;
56 }
57
58 inline bool operator!= (const int val) const
59 {
60 return m_enum_value != val;
61 }
62
63 int currentKey() { return m_enum_value; }
64
65 std::string currentString() {
67 }
68
69 void setCurrentKey(int index);
70
71 EnumMap& enumMap() { return m_enum_map; }
72
73 private:
74 std::string m_enum_name;
76
78 };
79
84 template<>
85 class FVar<PEnum> : public FBase
86 {
87 public:
88 typedef PEnum VarType;
89 typedef PEnum DataType;
90 typedef FVar<PEnum> FieldType;
91
93
94 FVar(VarType value, std::string name, std::string description, FieldTypeEnum fieldType, OBase* parent)
95 : FBase(name, description, fieldType, parent)
96 {
97 this->setValue(value);
98 }
99
100 ~FVar() override {};
101
102 uint size() override { return 1; }
103
104 void setValue(VarType val) {
105 std::shared_ptr<VarType>& data = this->getDataPtr();
106 if (data == nullptr)
107 {
108 data = std::make_shared<VarType>(val);
109 }
110 else
111 {
112 *data = val;
113 }
114
115 this->update();
116
117 this->tick();
118 }
119
120 VarType getValue() {
121 std::shared_ptr<VarType>& data = this->constDataPtr();
122 return *data;
123 }
124
125 int currentKey() {
126 std::shared_ptr<VarType>& data = this->constDataPtr();
127 return data->currentKey();
128 }
129
130 void setCurrentKey(int index)
131 {
132 std::shared_ptr<VarType>& data = this->getDataPtr();
133
134 data->setCurrentKey(index);
135
137 this->update();
138
139 //Notify this field is updated
140 this->tick();
141 }
142
143 std::string serialize()
144 {
145 if (isEmpty())
146 return "";
147
148 int key = this->constDataPtr()->currentKey();
149
150 std::stringstream ss;
151 ss << key;
152
153 return ss.str();
154 }
155
156 bool deserialize(const std::string& str)
157 {
158 if (str.empty())
159 return false;
160
161 int key = std::stoi(str);
162
163 this->getDataPtr()->setCurrentKey(key);
164
165 return true;
166 }
167
168 bool isEmpty() override {
169 return this->constDataPtr() == nullptr;
170 }
171 };
172
173
174#define DEF_ENUM(enum_type, enum_name, enum_value, desc) \
175private: \
176 FVar<PEnum> var_##enum_name = FVar<PEnum>(PEnum(#enum_type, enum_value, full_name_##enum_type), std::string(#enum_name), desc, FieldTypeEnum::Param, this); \
177public: \
178 inline FVar<PEnum>* var##enum_name() {return &var_##enum_name;}
179
180}
#define DEFINE_FIELD_FUNC(DerivedField, Data, FieldName)
Definition FBase.h:170
OBase * parent()
Definition FBase.cpp:41
void tick()
Definition FBase.cpp:250
virtual void update()
Definition FBase.cpp:201
std::shared_ptr< DataType > & getDataPtr()
Definition Field.h:88
bool isEmpty() override
Definition Field.h:58
std::string serialize() override
Definition Field.h:55
FVar< T > FieldType
Definition Field.h:38
~FVar() override
Definition Field.h:106
uint size() override
Definition Field.h:50
T VarType
Definition Field.h:36
bool deserialize(const std::string &str) override
Definition Field.h:56
T getValue()
Definition Field.h:130
void setValue(T val)
Definition Field.h:111
std::shared_ptr< DataType > & constDataPtr()
Definition Field.h:80
bool operator==(const int val) const
Definition DeclareEnum.h:53
EnumMap m_enum_map
Definition DeclareEnum.h:77
std::string currentString()
Definition DeclareEnum.h:65
PEnum(std::string enum_name, int val, const std::string enum_str)
Definition DeclareEnum.h:46
bool operator!=(const int val) const
Definition DeclareEnum.h:58
std::string m_enum_name
Definition DeclareEnum.h:74
EnumMap & enumMap()
Definition DeclareEnum.h:71
void setCurrentKey(int index)
int currentKey()
Definition DeclareEnum.h:63
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
bool parse_enum_string(const std::string &enum_str, EnumMap &enumKeyValueList)
FieldTypeEnum
Definition FBase.h:30
unsigned int uint
Definition VkProgram.h:14
std::map< int, std::string > EnumMap
Definition DeclareEnum.h:25