Specs
Beautiful C++ Test Framework
Loading...
Searching...
No Matches
SpecVariableCollection.h
Go to the documentation of this file.
1#pragma once
2
3#include <Specs/API.h>
4#include <collections.h>
5
6namespace SpecsCpp {
7
9 ISpecGroup* _parent = nullptr;
10 collections_map<std::string, IVoidPointer*> _variables;
11 collections_map<std::string, bool> _destructable;
12
13 public:
14 SpecVariableCollection(ISpecGroup* group = nullptr) : _parent(group) {}
15
17
18 void set(const char* name, IVoidPointer* variable, bool destructable = true) override {
19 variable->delete_rule()->set_destruct_on_delete(destructable);
20 _variables[name] = variable;
21 }
22
23 bool exists(const char* name) const override {
24 if (_variables.find(name) != _variables.end()) return true;
25 if (_parent != nullptr) return _parent->has_var(name);
26 return false;
27 }
28
29 IVoidPointer* get(const char* name) const override {
30 auto found = _variables.find(name);
31 if (found != _variables.end()) return found->second;
32 if (_parent != nullptr) return _parent->var(name);
33 return nullptr;
34 }
35
36 bool is_destructable(const char* name) const override {
37 if (auto* voidPtr = get(name)) return voidPtr->delete_rule()->destruct_on_delete();
38 return false;
39 }
40
41 void set_destructable(const char* name, bool destructable = true) override {
42 if (auto* voidPtr = get(name))
43 voidPtr->delete_rule()->set_destruct_on_delete(destructable);
44 }
45
46 void foreach_variable(ForEachVariableFn* fn) const override {
47 for (const auto& [name, variable] : _variables) fn->invoke(variable);
48 }
49
50 void clear() override {
51 for (const auto& [name, variable] : _variables) delete variable;
52 _variables.clear();
53 }
54
55 void unset(const char* name) override {
56 auto found = _variables.find(name);
57 if (found != _variables.end()) {
58 delete found->second;
59 _variables.erase(found);
60 }
61 }
62 };
63}
bool exists(const char *name) const override
void foreach_variable(ForEachVariableFn *fn) const override
SpecVariableCollection(ISpecGroup *group=nullptr)
void unset(const char *name) override
bool is_destructable(const char *name) const override
void set_destructable(const char *name, bool destructable=true) override
void set(const char *name, IVoidPointer *variable, bool destructable=true) override
IVoidPointer * get(const char *name) const override
Definition API.h:3
IVoidPointer * var(const char *name) const
Definition API.h:229
bool has_var(const char *name) const
Definition API.h:227
IFunctionPointer< void(IVoidPointer *)> ForEachVariableFn
Definition API.h:136