varconf 1.0.3
Configuration library for the Worldforge system.
dynbase.cpp
1/*
2 * dynbase.cpp - implementation of the dynamically derived value container.
3 * Copyright (C) 2001, Ron Steinke
4 * (C) 2003-2004 Alistair Riddoch
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Contact: Joseph Zupko
21 * jaz147@psu.edu
22 *
23 * 189 Reese St.
24 * Old Forge, PA 18518
25 */
26
27#include "dynbase.h"
28
29#include <string>
30
31namespace varconf {
32namespace dynvar {
33
34Base::~Base() = default;
35
36// operator<<, operator== aren't virtual, so we can use
37// static casts to avoid calling copy constructors
38
39Base& Base::operator= (const Base& b)
40{
41 VarBase::operator=(b);
42 // Don't change m_looping
43 return *this;
44}
45
46std::ostream& operator<<(std::ostream& out, Base& v)
47{
48 v.call_set_val();
49 return out << *static_cast<VarBase*>(&v);
50}
51
52bool operator ==(Base& one, const VarBase& two)
53{
54 one.call_set_val();
55 return *static_cast<VarBase*>(&one) == two;
56}
57
58bool operator ==(const VarBase& one, Base& two)
59{
60 two.call_set_val();
61 return one == *static_cast<VarBase*>(&two);
62}
63
64bool operator ==(Base& one, Base& two)
65{
66 one.call_set_val();
67 two.call_set_val();
68 return *static_cast<VarBase*>(&one) == *static_cast<VarBase*>(&two);
69}
70
71Base::operator bool()
72{
73 call_set_val();
74 return VarBase::operator bool();
75}
76
77Base::operator int()
78{
79 call_set_val();
80 return VarBase::operator int();
81}
82
83Base::operator double()
84{
85 call_set_val();
86 return VarBase::operator double();
87}
88
89Base::operator std::string()
90{
91 call_set_val();
92 return VarBase::operator std::string();
93}
94
95bool Base::is_bool()
96{
97 call_set_val();
98 return VarBase::is_bool();
99}
100
101bool Base::is_int()
102{
103 call_set_val();
104 return VarBase::is_int();
105}
106
107bool Base::is_double()
108{
109 call_set_val();
110 return VarBase::is_double();
111}
112
113bool Base::is_string()
114{
115 call_set_val();
116 return VarBase::is_string();
117}
118
119void Base::call_set_val()
120{
121 if(m_looping) { // Circular dependencies
122 // FIXME add a warning, don't fail silently
123 VarBase::operator=(VarBase()); // Set it invalid
124 return;
125 }
126
127 m_looping = true;
128 set_val();
129 m_looping = false;
130}
131
132}} // namespace varconf::dynvar