TailorMade 2.0
Tailor Made is a high-performance C++20 ECS library with on-the-fly entity creation and JSON-based definitions for entities, components, and their relationships.
 
Loading...
Searching...
No Matches
Component.h
Go to the documentation of this file.
1
7
8
9#ifndef _COMPONENT_H
10#define _COMPONENT_H
11
12#include <TM_Tools.h>
21
22
23class Component {
24public:
25 Component() = default;
26
31 Component(const std::string& filename);
32
39 Component(const std::string& name, dataUnMap dataDump);
40
46 void copy(std::shared_ptr<Component> component);
47
51 const std::string& getName();
52
57 const std::string& getType(const std::string& name);
58
62 std::vector<std::string> getNames();
63
69
75 void toString(std::ostream& stream);
76
83 template<typename Type>
84 Type get(const std::string& name);
85
92 template<typename Type>
93 void set(const std::string& name, Type value);
94
102 void add(const std::string& name, const std::string& type);
103
104protected:
108 std::string componentName;
109
114
118 std::mutex mtx;
119
120private:
124 std::string placeholder;
125};
126
127template<typename Type>
128inline Type Component::get(const std::string& name) {
129 std::scoped_lock lock(mtx);
130 try {
131 if (!dataMap.contains(name)) {
132 // No data with this name
133 throw std::runtime_error("Error : no data with the name \"" + name + "\".");
134 }
135
136 std::variant<ECS_Types> data = dataMap[name].second;
137 return std::get<Type>(data); // Be careful of bad conversion here
138 }
139 catch (std::exception& e) {
140 std::cerr << "Component : " << e.what() << std::endl;
141 }
142}
143
144template<typename Type>
145inline void Component::set(const std::string& name, Type value) {
146 std::scoped_lock lock(mtx);
147 try {
148 if (!dataMap.contains(name)) {
149 //No data with this name
150 throw std::runtime_error("Error : no data with the name \"" + name + "\".");
151 }
152 dataMap[name].second = value;
153 }
154 catch (std::exception& e) {
155 std::cerr << "Component : " << e.what() << std::endl; // Be careful, some values will throw an error if you try to implicit cast towards them.
156 // For example, Vector3 ---> integer won't work.
157 }
158}
159
160#endif //_COMPONENT_H
Project TailorMade.
std::unordered_map< std::string, std::pair< std::string, std::variant< ECS_Types > > > dataUnMap
Map of dataName -> {dataType, value}.
Definition TM_Tools.h:160
void set(const std::string &name, Type value)
Set the value of the desired data with the given value.
Definition Component.h:145
std::vector< std::string > getNames()
Return a vector with the names of every data from this component.
Component(const std::string &name, dataUnMap dataDump)
Constructor of the component from its name and data.
Component()=default
const std::string & getType(const std::string &name)
Return a string with the type of the given data (or "" if the data doesn't exist).
void toString(std::ostream &stream)
Append to the given stream a serialized version of the data.
const dataUnMap & getRawData()
Return a const reference of the component's data structure.
std::mutex mtx
Lock used by the components, let them the possibility to be used in thread.
Definition Component.h:118
dataUnMap dataMap
Link the data's name to the pair <type, value> in an unordered_map.
Definition Component.h:113
void add(const std::string &name, const std::string &type)
Add a new data to the component.
Component(const std::string &filename)
Constructor of the component from a file.
const std::string & getName()
Return the component's name.
void copy(std::shared_ptr< Component > component)
Copy the information of the given component in the current one.
Type get(const std::string &name)
Return the value of the data from its name and type.
Definition Component.h:128
std::string componentName
The name of the component.
Definition Component.h:108