SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
DObject.h
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 17, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#ifndef DOBJECT_H
22#define DOBJECT_H
23
24#include <variant>
25#include <memory>
26#include <vector>
27#include <string>
28#include <mutex>
29#include <map>
30#include <functional>
31
40
89class DObject {
90public:
97 using DataType = std::variant<
98 bool, short int, int, long int, float, double,
99 long double, std::string, std::monostate, std::vector<int>,
100 std::vector<float>, std::vector<double>, std::vector<std::string>,
101 std::vector<std::vector<int>>, std::vector<std::vector<float>>,
102 std::vector<std::vector<double>>,
103 std::vector<std::vector<std::vector<int>>>,
104 std::vector<std::vector<std::vector<float>>>,
105 std::vector<std::vector<std::vector<double>>>
106 >;
107
108 // Mapping of data types to their string representations
109 std::map<int, std::string> dataTypeMap = {
110 {0, "bool"}, {1, "short int"}, {2, "int"}, {3, "long int"}, {4, "float"},
111 {5, "double"}, {6, "long double"}, {7, "string"}, {8, "std::monostate"},
112 {9, "vector<int>"}, {10, "vector<float>"}, {11, "vector<double>"},
113 {12, "vector<string>"}, {13, "vector<vector<int>"},
114 {14, "vector<vector<float>"}, {15, "vector<vector<double>"},
115 {16, "vector<vector<vector<int>>"}, {17, "vector<vector<vector<float>>"},
116 {18, "vector<vector<vector<double>>"}
117 };
118
119
125 using Plugin = std::function<void(DObject&)>;
126
132 DObject();
133
139 DObject(const DataType& data);
140
148 const DataType& getData() const noexcept;
149
155 template <typename T>
156 T getDataAs() const {
157 if (std::holds_alternative<T>(data_)) {
158 return std::get<T>(data_);
159 } else {
160 throw std::bad_variant_access();
161 }
162 }
163
171 void setData(const DataType& data);
172
180 void setDebugging(bool enableDebug);
181
187 bool isDebuggingEnabled() const noexcept;
188
194 int setErrorCode(int code) noexcept;
195
201 int getErrorCode() const noexcept;
202
203
212 void registerPlugin(const std::string& id, Plugin plugin);
213
221 void unregisterPlugin(const std::string& id);
222
230 void runPlugin(const std::string& id);
231
237 void runAllPlugins();
238
248 friend std::ostream& operator<<(std::ostream& os, const DObject& obj);
249
250private:
252 bool debugEnabled_ = false;
253 int errorCode_ = 0;
254 std::map<std::string, Plugin> plugins_;
255};
256
257#endif // DOBJECT_H
A universal data container class.
Definition DObject.h:89
DObject()
Default constructor.
Definition DObject.cpp:33
void runAllPlugins()
Executes all registered plugins in the registry.
Definition DObject.cpp:117
void registerPlugin(const std::string &id, Plugin plugin)
Registers a plugin with the DObject.
Definition DObject.cpp:87
std::map< int, std::string > dataTypeMap
Definition DObject.h:109
int setErrorCode(int code) noexcept
Definition DObject.cpp:74
const DataType & getData() const noexcept
Retrieves the data stored in the DObject.
Definition DObject.cpp:46
std::variant< bool, short int, int, long int, float, double, long double, std::string, std::monostate, std::vector< int >, std::vector< float >, std::vector< double >, std::vector< std::string >, std::vector< std::vector< int > >, std::vector< std::vector< float > >, std::vector< std::vector< double > >, std::vector< std::vector< std::vector< int > > >, std::vector< std::vector< std::vector< float > > >, std::vector< std::vector< std::vector< double > > > > DataType
Supported data types for the DObject.
Definition DObject.h:97
void setDebugging(bool enableDebug)
Enables or disables debugging and tracing for the DObject.
Definition DObject.cpp:60
void runPlugin(const std::string &id)
Executes a plugin by its identifier.
Definition DObject.cpp:106
void unregisterPlugin(const std::string &id)
Unregisters a plugin by its identifier.
Definition DObject.cpp:97
void setData(const DataType &data)
Sets the data for the DObject.
Definition DObject.cpp:53
T getDataAs() const
Retrieves the data stored in the DObject as a typed object so that std::get<T>() is not needed.
Definition DObject.h:156
bool debugEnabled_
Indicates whether debugging is enabled.
Definition DObject.h:252
DataType data_
The main data stored in the DObject.
Definition DObject.h:251
int errorCode_
Error code tracked by the DObject.
Definition DObject.h:253
bool isDebuggingEnabled() const noexcept
Checks if debugging is enabled for the DObject.
Definition DObject.cpp:67
std::map< std::string, Plugin > plugins_
Registry for dynamically registered plugins.
Definition DObject.h:254
std::function< void(DObject &)> Plugin
Placeholder type for plugins.
Definition DObject.h:125
int getErrorCode() const noexcept
Get the error code tracked by the DObject.
Definition DObject.cpp:80