Specs
Beautiful C++ Test Framework
Loading...
Searching...
No Matches
SpecDataValue.h
Go to the documentation of this file.
1#pragma once
2
3#include <Specs/API.h>
4
5#include <string>
6
7namespace SpecsCpp {
8
10 std::string _asString = "";
11 std::string _key;
13 IVoidPointer* _value = nullptr;
14 void* _unmanagedValue = nullptr;
15
16 public:
17 SpecDataValue(const char* key, SpecDataValueType type, IVoidPointer* value)
18 : _key(key), _type(type), _value(value) {}
19
20 SpecDataValue(const char* key, SpecDataValueType type, void* value)
21 : _key(key), _type(type), _unmanagedValue(value) {}
22
23 SpecDataValue(const char* key, SpecDataValueType type, bool value)
24 : _key(key), _type(type), _value(new VoidPointer<bool>(new bool(value))) {}
25
26 SpecDataValue(const char* key, SpecDataValueType type, int value)
27 : _key(key), _type(type), _value(new VoidPointer<int>(new int(value))) {}
28
29 SpecDataValue(const char* key, SpecDataValueType type, unsigned int value)
30 : _key(key),
31 _type(type),
32 _value(new VoidPointer<unsigned int>(new unsigned int(value))) {}
33
34 SpecDataValue(const char* key, SpecDataValueType type, double value)
35 : _key(key), _type(type), _value(new VoidPointer<double>(new double(value))) {}
36
37 SpecDataValue(const char* key, SpecDataValueType type, const char* value)
38 : _key(key),
39 _type(type),
40 _value(new VoidPointer<std::string>(new std::string(value))) {}
41
42 ~SpecDataValue() override {
43 if (_value) delete _value;
44 }
45
46 SpecDataValueType type() const override { return _type; }
47
48 const char* key() const override { return _key.c_str(); }
49 void key(const char* key) override { _key = key; }
50
51 bool bool_value() const override {
52 if (_type != SpecDataValueType::Boolean) return false;
53 return _value->as<bool>();
54 }
55
56 void bool_value(bool value) override {
58 _value = new VoidPointer<bool>(new bool(value));
59 }
60
61 int int_value() const override {
62 if (_type != SpecDataValueType::Integer) return 0;
63 return _value->as<int>();
64 }
65
66 void int_value(int value) override {
68 _value = new VoidPointer<int>(new int(value));
69 }
70
71 unsigned int unsigned_int_value() const override {
72 if (_type != SpecDataValueType::UnsignedInteger) return 0;
73 return _value->as<unsigned int>();
74 }
75
76 void unsigned_int_value(unsigned int value) override {
78 _value = new VoidPointer<unsigned int>(new unsigned int(value));
79 }
80
81 double float_value() const override {
82 if (_type != SpecDataValueType::Float) return 0;
83 return _value->as<double>();
84 }
85
86 void float_value(double value) override {
88 _value = new VoidPointer<double>(new double(value));
89 }
90
91 const char* string_value() const override {
92 if (_type != SpecDataValueType::String) return nullptr;
93 // TODO whaaaaa this isn't safe!
94 return _value->as<std::string*>()->c_str();
95 }
96
97 void string_value(const char* value) override {
99 _value = new VoidPointer<std::string>(new std::string(value));
100 }
101
102 void* pointer_value() const override {
103 if (_type != SpecDataValueType::Pointer) return nullptr;
104 return _unmanagedValue ? _unmanagedValue : _value->void_ptr();
105 }
106
107 void pointer_value(void* value) override {
109 _unmanagedValue = value;
110 }
111
112 void pointer_value(IVoidPointer* value) override {
114 _value = value;
115 }
116
117 bool is_bool() const override { return _type == SpecDataValueType::Boolean; }
118 bool is_int() const override { return _type == SpecDataValueType::Integer; }
119 bool is_unsigned_int() const override {
121 }
122 bool is_float() const override { return _type == SpecDataValueType::Float; }
123 bool is_string() const override { return _type == SpecDataValueType::String; }
124 bool is_pointer() const override { return _type == SpecDataValueType::Pointer; }
125
126 const char* to_string() override {
127 switch (_type) {
129 _asString = bool_value() ? "true" : "false";
130 break;
132 _asString = std::to_string(int_value());
133 break;
135 _asString = std::to_string(unsigned_int_value());
136 break;
138 _asString = std::to_string(float_value());
139 break;
141 _asString = string_value();
142 break;
144 _asString = "(pointer)";
145 break;
146 }
147 return _asString.c_str();
148 }
149
150 static ISpecDataValue* create_bool(const char* key, bool value) {
151 return new SpecDataValue(key, SpecDataValueType::Boolean, value);
152 }
153 static ISpecDataValue* create_int(const char* key, int value) {
154 return new SpecDataValue(key, SpecDataValueType::Integer, value);
155 }
156 static ISpecDataValue* create_unsigned_int(const char* key, unsigned int value) {
158 }
159 static ISpecDataValue* create_float(const char* key, double value) {
160 return new SpecDataValue(key, SpecDataValueType::Float, value);
161 }
162 static ISpecDataValue* create_string(const char* key, const char* value) {
163 return new SpecDataValue(key, SpecDataValueType::String, value);
164 }
165 static ISpecDataValue* create_pointer(const char* key, void* value) {
166 return new SpecDataValue(key, SpecDataValueType::Pointer, value);
167 }
168 static ISpecDataValue* create_pointer(const char* key, IVoidPointer* value) {
169 return new SpecDataValue(key, SpecDataValueType::Pointer, value);
170 }
171
172 static ISpecDataValue* create(const char* key, bool value) {
173 return create_bool(key, value);
174 }
175 static ISpecDataValue* create(const char* key, int value) { return create_int(key, value); }
176 static ISpecDataValue* create(const char* key, unsigned int value) {
177 return create_unsigned_int(key, value);
178 }
179 static ISpecDataValue* create(const char* key, double value) {
180 return create_float(key, value);
181 }
182 static ISpecDataValue* create(const char* key, const char* value) {
183 return create_string(key, value);
184 }
185 static ISpecDataValue* create(const char* key, void* value) {
186 return create_pointer(key, value);
187 }
188 static ISpecDataValue* create(const char* key, IVoidPointer* value) {
189 return create_pointer(key, value);
190 }
191 };
192}
SpecDataValue(const char *key, SpecDataValueType type, int value)
void * pointer_value() const override
static ISpecDataValue * create(const char *key, void *value)
SpecDataValueType type() const override
static ISpecDataValue * create_bool(const char *key, bool value)
const char * key() const override
unsigned int unsigned_int_value() const override
static ISpecDataValue * create(const char *key, double value)
bool bool_value() const override
static ISpecDataValue * create_unsigned_int(const char *key, unsigned int value)
SpecDataValue(const char *key, SpecDataValueType type, const char *value)
static ISpecDataValue * create(const char *key, const char *value)
void string_value(const char *value) override
SpecDataValue(const char *key, SpecDataValueType type, void *value)
bool is_pointer() const override
static ISpecDataValue * create(const char *key, bool value)
SpecDataValue(const char *key, SpecDataValueType type, double value)
bool is_bool() const override
void unsigned_int_value(unsigned int value) override
void pointer_value(IVoidPointer *value) override
void key(const char *key) override
static ISpecDataValue * create_string(const char *key, const char *value)
void pointer_value(void *value) override
static ISpecDataValue * create(const char *key, unsigned int value)
bool is_string() const override
SpecDataValue(const char *key, SpecDataValueType type, bool value)
SpecDataValue(const char *key, SpecDataValueType type, IVoidPointer *value)
bool is_unsigned_int() const override
static ISpecDataValue * create_float(const char *key, double value)
bool is_float() const override
void float_value(double value) override
const char * string_value() const override
static ISpecDataValue * create(const char *key, int value)
void bool_value(bool value) override
const char * to_string() override
static ISpecDataValue * create_int(const char *key, int value)
static ISpecDataValue * create(const char *key, IVoidPointer *value)
SpecDataValue(const char *key, SpecDataValueType type, unsigned int value)
bool is_int() const override
static ISpecDataValue * create_pointer(const char *key, IVoidPointer *value)
void int_value(int value) override
double float_value() const override
int int_value() const override
static ISpecDataValue * create_pointer(const char *key, void *value)
Definition API.h:3
SpecDataValueType
Definition API.h:26