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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
Please use git log for copyright holder and year information
This file is part of libbash.
libbash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
libbash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libbash. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \file symbols.hpp
/// \brief template implementation for symbols and symbol table
///
#ifndef LIBBASH_CORE_SYMBOLS_HPP_
#define LIBBASH_CORE_SYMBOLS_HPP_
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include "exceptions.h"
///
/// \class converter
/// \brief template class of converter
///
template<typename T>
class converter: public boost::static_visitor<T>
{};
///
/// \class converter
/// \brief specialized converter for long
///
template<>
class converter<long>: public boost::static_visitor<long>
{
public:
/// \brief converter for long value
/// \param value the value to be converted
/// \return the converted long
long operator() (const long value) const
{
return value;
}
/// \brief converter for string value
/// \param value the value to be converted
/// \return the converted long
long operator() (const std::string& value) const
{
long result = 0;
try
{
result = boost::lexical_cast<long>(value);
}
catch(boost::bad_lexical_cast& e)
{
std::cerr << "can't cast " << value << " to long" << std::endl;
}
return result;
}
};
///
/// \class converter
/// \brief specialized converter for string
///
template<>
class converter<std::string>:
public boost::static_visitor<std::string>
{
public:
/// \brief converter for long value
/// \param value the value to be converted
/// \return the converted string
std::string operator() (const long value) const
{
return boost::lexical_cast<std::string>(value);
}
/// \brief converter for string value
/// \param value the value to be converted
/// \return the converted string
std::string operator() (const std::string& value) const
{
return value;
}
};
///
/// \class variable
/// \brief implementation for all variable types
///
class variable
{
/// \brief variable name
std::string name;
/// \brief actual value of the variable. We put string in front of long
/// because we want "" as default string value; Otherwise we
/// will get "0".
std::map<unsigned, boost::variant<std::string, long>> value;
/// \brief whether the variable is readonly
bool readonly;
public:
/// size_type for array length
typedef std::map<unsigned, boost::variant<std::string, long>>::size_type size_type;
/// \brief retrieve variable name
/// \return const string value of variable name
const std::string& get_name() const
{
return name;
}
/// \brief constructor
/// \param name the name of the variable
/// \param v the value of the variable
/// \param ro whether the variable is readonly
/// \param index the index of the variable, use 0 if it's not an array
template <typename T>
variable(const std::string& name,
const T& v,
bool ro=false,
const unsigned index=0)
: name(name), readonly(ro)
{
value[index] = v;
}
/// \brief retrieve actual value of the variable, if index is out of bound,
/// will return the default value of type T
/// \param index the index of the variable, use 0 if it's not an array
/// \return the value of the variable
template<typename T>
T get_value(const unsigned index=0) const
{
static converter<T> visitor;
auto iter = value.find(index);
if(iter == value.end())
return T{};
return boost::apply_visitor(visitor, iter->second);
}
/// \brief retrieve all values of the array
/// \param[out] all_values vector that stores all array values
template<typename T>
void get_all_values(std::vector<T>& all_values) const
{
static converter<T> visitor;
for(auto iter = value.begin(); iter != value.end(); ++iter)
all_values.push_back(
boost::apply_visitor(visitor, iter->second));
}
/// \brief set the value of the variable, raise exception if it's readonly
/// \param new_value the new value to be set
/// \param index array index, use index=0 if it's not an array
template <typename T>
void set_value(const T& new_value,
const unsigned index=0)
{
if(readonly)
throw libbash::readonly_exception(get_name() + " is readonly variable");
value[index] = new_value;
}
/// \brief unset the variable, only used for array variable
/// \param index the index to be unset
void unset_value(const unsigned index)
{
if(readonly)
throw libbash::readonly_exception(get_name() + " is readonly variable");
value.erase(index);
}
/// \brief get the length of a variable
/// \param index the index of the variable, use 0 if it's not an array
/// \return the length of the variable
std::string::size_type get_length(const unsigned index=0) const
{
return get_value<std::string>(index).size();
}
/// \brief get the length of an array variable
/// \return the length of the array
size_type get_array_length() const
{
return value.size();
}
/// \brief check whether the value of the variable is null
/// \return whether the value of the variable is null
bool is_unset(const unsigned index=0) const
{
return value.find(index) == value.end();
}
/// \brief check whether the value of the variable is unset
/// \return whether the value of the variable is unset
bool is_null(const unsigned index=0) const
{
return get_value<std::string>(index) == "";
}
/// \brief check whether the value of the variable is readonly
/// \return whether the value of the variable is readonly
bool is_readonly() const
{
return readonly;
}
int shift(unsigned shift_number)
{
assert(!readonly&&"readonly variables shouldn't be shifted");
// Remove this cast after making arithmetic expansion follow POSIX
unsigned size = boost::numeric_cast<unsigned>(value.size());
if(shift_number > size)
{
return 1;
}
else if(shift_number == size)
{
value.clear();
}
else
{
// copy elements
for(unsigned i = shift_number + 1; i <= size; ++i)
value[i - shift_number] = value[i];
// remove tail elements
for(unsigned i = size - shift_number + 1; i <= size; ++i)
value.erase(i);
}
return 0;
}
};
/// \brief the specialized constructor for arrays
/// \param name the variable name
/// \param v the variable value
/// \param ro whether the variable readonly
template <>
inline variable::variable<>(const std::string& name,
const std::map<unsigned, std::string>& v,
bool ro,
unsigned)
: name(name), value(v.begin(), v.end()), readonly(ro)
{
}
#endif
|