-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariableStore.h
97 lines (80 loc) · 2.42 KB
/
VariableStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*-------------------------------------------------------------------------*
*--- ---*
*--- VariableStore.h ---*
*--- ---*
*--- This file declares a class that stores variables. ---*
*--- ---*
*--- ---- ---- ---- ---- ---- ---- ---- ---- ---*
*--- ---*
*--- Version 1a 2018 May 17 Joseph Phillips ---*
*--- ---*
*-------------------------------------------------------------------------*/
class VariableStore
{
// I. Member vars:
// PURPOSE: To map from variable names to their values.
std::map<std::string,Object*> varNameToValueMap_;
// II. Disallowed member vars:
// No copy constructor:
VariableStore (const VariableStore&
);
// No copy assignment op:
VariableStore&
operator= (const VariableStore&
);
protected :
// III. Protected:
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To initialize '*this' to an empty VariableStore object. No
// parameters. No return value.
VariableStore () :
varNameToValueMap_()
{ }
// PURPOSE: To release the resources of '*this'. No parameters. No return
// value.
~VariableStore ()
{
std::map<std::string,Object*>::iterator
iter = varNameToValueMap_.begin();
std::map<std::string,Object*>::iterator
end = varNameToValueMap_.end();
for ( ; iter != end; iter++)
{
safeDelete(iter->second);
}
}
// V. Accessors:
// VI. Mutators:
// VII. Methods that do main and misc work of class:
// PURPOSE: To return a pointer to the current value of the variable
// named 'varName', or to return 'NULL' if it does not have a value.
const Object* get (const std::string& varName
)
{
std::map<std::string,Object*>::iterator
iter = varNameToValueMap_.
find(varName);
std::map<std::string,Object*>::iterator
end = varNameToValueMap_.end();
return( (iter == end) ? NULL : iter->second );
}
// PURPOSE: To
void store (const std::string& varName,
Object* objPtr
)
{
std::map<std::string,Object*>::iterator
iter = varNameToValueMap_.
find(varName);
std::map<std::string,Object*>::iterator
end = varNameToValueMap_.end();
if (iter == end)
varNameToValueMap_[varName] = objPtr;
else
{
safeDelete(iter->second);
iter->second = objPtr;
}
}
};