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 const std::string &get_type() const { return this->type; }
62
67 virtual const void *get_value_ptr() const = 0;
68 virtual void *get_value_ptr() = 0;
69
70private:
71 std::string type;
72};
73
83template <typename T> class Data : public BaseData
84{
85public:
98 template <typename... Args>
99 explicit Data(Args &&...args)
100 : BaseData(typeid(T).name()), value(std::forward<Args>(args)...)
101 {
102 }
103
108 T *get_value_ref() { return &this->value; }
109
114 const void *get_value_ptr() const override { return &this->value; }
115 void *get_value_ptr() { return &this->value; }
116
117private:
118 T value{};
119};
120
121} // 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:71
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()=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
const std::string & get_type() const
Retrieves the type of the data as a string.
Definition data.hpp:61
virtual const void * get_value_ptr() const =0
Pure virtual method to retrieve a pointer to the stored value.
Template class for holding data of a specific type.
Definition data.hpp:84
const void * get_value_ptr() const override
Retrieves a pointer to the stored value.
Definition data.hpp:114
T value
The value of type T stored in this object.
Definition data.hpp:118
Data(Args &&...args)
Constructs a Data object with the type name of T and initializes its value.
Definition data.hpp:99
void * get_value_ptr()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition data.hpp:115
T * get_value_ref()
Retrieves a reference to the stored value.
Definition data.hpp:108
Definition data.hpp:23