-
Notifications
You must be signed in to change notification settings - Fork 4
/
config_value.cpp
205 lines (188 loc) · 3.94 KB
/
config_value.cpp
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
/*----------------------------------------------------------------------
* $Id: config_value.cpp,v 1.2 2005-09-24 19:36:58 ganzhorn Exp $
*
*
* Copyright (C) 2005 Salem Ganzhorn <eyekode@yahoo.com>
*
* This file is part of kccmp - Kernel Config CoMPare
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public License
* as published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*----------------------------------------------------------------------*/
#include "config_value.hpp"
#include <assert.h>
config_value::config_value()
: m_type(Integer)
{
m_data.integer = 0;
}
config_value::config_value(
const config_value &cv
)
: m_type(Integer)
{
m_data.str = NULL;
*this = cv;
}
config_value::config_value(
value_constant vc
) : m_type(ValueConstant)
{
m_data.vc = vc;
}
config_value::config_value(
int i
) : m_type(Integer)
{
m_data.integer = i;
}
config_value::config_value(
double d
) : m_type(Real)
{
m_data.real = d;
}
config_value::config_value(
const std::string &str
) : m_type(String)
{
m_data.str = new std::string(str);
}
config_value::~config_value()
{
free();
}
void
config_value::free()
{
if( String == m_type ) {
delete m_data.str;
}
}
const config_value&
config_value::operator= ( const config_value &cv )
{
free();
m_type = cv.m_type;
switch( cv.m_type ) {
case Real:
m_data.real = cv.m_data.real;
break;
case Integer:
m_data.integer = cv.m_data.integer;
break;
case ValueConstant:
m_data.vc = cv.m_data.vc;
break;
case String:
m_data.str = new std::string(*cv.m_data.str);
break;
default:
assert(0);
}
return *this;
}
const config_value&
config_value::operator= ( int i )
{
free();
m_type = Integer;
m_data.integer = i;
return *this;
}
const config_value&
config_value::operator= ( double d )
{
free();
m_type = Real;
m_data.real = d;
return *this;
}
const config_value&
config_value::operator= ( const std::string &str )
{
if( String == m_type ) {
*m_data.str = str;
} else {
m_type = String;
m_data.str = new std::string(str);
}
return *this;
}
const config_value&
config_value::operator= ( value_constant vc ) {
free();
m_type = ValueConstant;
m_data.vc = vc;
return *this;
}
bool
config_value::operator== (
const config_value &cv
) const
{
if( m_type == cv.m_type ) {
switch( m_type ) {
case Real:
return( m_data.real == cv.m_data.real );
break;
case Integer:
return( m_data.integer == cv.m_data.integer );
break;
case ValueConstant:
return( m_data.vc == cv.m_data.vc );
break;
case String:
return( *m_data.str == *cv.m_data.str );
break;
default:
assert(0);
return false;
}
} else {
return false;
}
}
std::ostream&
operator<<(
std::ostream& out,
const config_value &cv
)
{
switch( cv.m_type ) {
case config_value::Real:
out << cv.m_data.real;
break;
case config_value::Integer:
out << cv.m_data.integer;
break;
case config_value::ValueConstant:
switch( cv.m_data.vc ) {
case config_value::Yes:
out << "Yes";
break;
case config_value::No:
out << "No";
break;
case config_value::Module:
out << "Module";
break;
}
break;
case config_value::String:
out << *cv.m_data.str;
break;
}
return out;
}