Loading...
Searching...
No Matches
DObject.h
Go to the documentation of this file.
/* ***********************************************************************
//
// Copyright (C) 2025 -- The 4D-STAR Collaboration
// File Author: Emily Boudreaux
// Last Modified: March 17, 2025
//
// 4DSSE is free software; you can use it and/or modify
// it under the terms and restrictions the GNU General Library Public
// License version 3 (GPLv3) as published by the Free Software Foundation.
//
// 4DSSE is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this software; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// *********************************************************************** */
#ifndef DOBJECT_H
#define DOBJECT_H
#include <variant>
#include <memory>
#include <vector>
#include <string>
#include <mutex>
#include <map>
#include <functional>
class DObject {
public:
using DataType = 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>>>
>;
// Mapping of data types to their string representations
std::map<int, std::string> dataTypeMap = {
{0, "bool"}, {1, "short int"}, {2, "int"}, {3, "long int"}, {4, "float"},
{5, "double"}, {6, "long double"}, {7, "string"}, {8, "std::monostate"},
{9, "vector<int>"}, {10, "vector<float>"}, {11, "vector<double>"},
{12, "vector<string>"}, {13, "vector<vector<int>"},
{14, "vector<vector<float>"}, {15, "vector<vector<double>"},
{16, "vector<vector<vector<int>>"}, {17, "vector<vector<vector<float>>"},
{18, "vector<vector<vector<double>>"}
};
using Plugin = std::function<void(DObject&)>;
DObject();
DObject(const DataType& data);
const DataType& getData() const noexcept;
template <typename T>
T getDataAs() const {
if (std::holds_alternative<T>(data_)) {
return std::get<T>(data_);
} else {
throw std::bad_variant_access();
}
}
void setData(const DataType& data);
void setDebugging(bool enableDebug);
bool isDebuggingEnabled() const noexcept;
int setErrorCode(int code) noexcept;
int getErrorCode() const noexcept;
void registerPlugin(const std::string& id, Plugin plugin);
void unregisterPlugin(const std::string& id);
void runPlugin(const std::string& id);
void runAllPlugins();
friend std::ostream& operator<<(std::ostream& os, const DObject& obj);
private:
DataType data_;
bool debugEnabled_ = false;
int errorCode_ = 0;
std::map<std::string, Plugin> plugins_;
};
#endif // DOBJECT_H
DObject
A universal data container class.
Definition DObject.h:89
DObject::DObject
DObject()
Default constructor.
Definition DObject.cpp:33
DObject::runAllPlugins
void runAllPlugins()
Executes all registered plugins in the registry.
Definition DObject.cpp:117
DObject::registerPlugin
void registerPlugin(const std::string &id, Plugin plugin)
Registers a plugin with the DObject.
Definition DObject.cpp:87
DObject::dataTypeMap
std::map< int, std::string > dataTypeMap
Definition DObject.h:109
DObject::setErrorCode
int setErrorCode(int code) noexcept
Definition DObject.cpp:74
DObject::getData
const DataType & getData() const noexcept
Retrieves the data stored in the DObject.
Definition DObject.cpp:46
DObject::DataType
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
DObject::setDebugging
void setDebugging(bool enableDebug)
Enables or disables debugging and tracing for the DObject.
Definition DObject.cpp:60
DObject::runPlugin
void runPlugin(const std::string &id)
Executes a plugin by its identifier.
Definition DObject.cpp:106
DObject::unregisterPlugin
void unregisterPlugin(const std::string &id)
Unregisters a plugin by its identifier.
Definition DObject.cpp:97
DObject::setData
void setData(const DataType &data)
Sets the data for the DObject.
Definition DObject.cpp:53
DObject::getDataAs
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
DObject::debugEnabled_
bool debugEnabled_
Indicates whether debugging is enabled.
Definition DObject.h:252
DObject::data_
DataType data_
The main data stored in the DObject.
Definition DObject.h:251
DObject::errorCode_
int errorCode_
Error code tracked by the DObject.
Definition DObject.h:253
DObject::isDebuggingEnabled
bool isDebuggingEnabled() const noexcept
Checks if debugging is enabled for the DObject.
Definition DObject.cpp:67
DObject::plugins_
std::map< std::string, Plugin > plugins_
Registry for dynamically registered plugins.
Definition DObject.h:254
DObject::Plugin
std::function< void(DObject &)> Plugin
Placeholder type for plugins.
Definition DObject.h:125
DObject::getErrorCode
int getErrorCode() const noexcept
Get the error code tracked by the DObject.
Definition DObject.cpp:80