GNode library (C++)
Loading...
Searching...
No Matches
data.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 * Public License. The full license is in the file LICENSE, distributed with
3 * this software. */
4
17#pragma once
18#include <memory>
19#include <string>
20#include <typeinfo>
21
22namespace gnode
23{
24
33{
34public:
39 explicit BaseData(std::string type) : type(type) {}
40
44 virtual ~BaseData() = default;
45
52 bool is_same_type(const BaseData &data) const
53 {
54 return this->get_type() == data.get_type();
55 }
56
61 std::string get_type() const { return this->type; }
62
67 virtual void *get_value_ptr() const = 0;
68
69private:
70 std::string type;
71};
72
82template <typename T> class Data : public BaseData
83{
84public:
97 template <typename... Args>
98 explicit Data(Args &&...args) : BaseData(typeid(T).name())
99 {
100 this->value = T(std::forward<Args>(args)...);
101 }
102
107 T *get_value_ref() { return &value; }
108
113 void *get_value_ptr() const override { return const_cast<T *>(&value); }
114
115private:
116 T value{};
117};
118
119} // namespace gnode
Abstract base class representing generic data with type information.
Definition data.hpp:33
virtual ~BaseData()=default
Virtual destructor for BaseData.
BaseData(std::string type)
Constructs a BaseData object with the specified type.
Definition data.hpp:39
std::string type
A string representing the type of the data.
Definition data.hpp:70
bool is_same_type(const BaseData &data) const
Checks if the type of this data matches the type of another BaseData object.
Definition data.hpp:52
virtual void * get_value_ptr() const =0
Pure virtual method to retrieve a pointer to the stored value.
std::string get_type() const
Retrieves the type of the data as a string.
Definition data.hpp:61
Template class for holding data of a specific type.
Definition data.hpp:83
void * get_value_ptr() const override
Retrieves a pointer to the stored value.
Definition data.hpp:113
T value
The value of type T stored in this object.
Definition data.hpp:116
Data(Args &&...args)
Constructs a Data object with the type name of T and initializes its value.
Definition data.hpp:98
T * get_value_ref()
Retrieves a reference to the stored value.
Definition data.hpp:107
Definition data.hpp:23