inivariable.cpp
1 //------------------------------------------------------------------------------
2 // inivariable.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "inivariable.h"
24 
25 #include "ini/inisection.h"
26 
27 #include <cassert>
28 
29 DClass<IniVariable>
30 {
31  public:
36  IniSection section;
37 
41  QString key;
42 };
43 
44 DPointered(IniVariable)
45 
47 {
48 }
49 
50 IniVariable::IniVariable(const IniSection &section, const QString& key)
51 {
52  d->section = section;
53  d->key = key;
54 }
55 
56 IniVariable::~IniVariable()
57 {
58 }
59 
60 IniVariable::operator bool() const
61 {
62  return value().toInt() != 0;
63 }
64 
65 IniVariable::operator int() const
66 {
67  return value().toInt();
68 }
69 
70 IniVariable::operator unsigned int() const
71 {
72  return value().toUInt();
73 }
74 
75 IniVariable::operator short() const
76 {
77  // It's a mystery why QVariant can't convert to short directly.
78  return value().toString().toShort();
79 }
80 
81 IniVariable::operator unsigned short() const
82 {
83  // It's a mystery why QVariant can't convert to u-short directly.
84  return value().toString().toUShort();
85 }
86 
87 IniVariable::operator float() const
88 {
89  return value().toFloat();
90 }
91 
92 const IniVariable &IniVariable::operator=(const QString &str)
93 {
94  assert(!isNull());
95 
96  setValue(str);
97  return *this;
98 }
99 
100 const IniVariable &IniVariable::operator=(int i)
101 {
102  assert(!isNull());
103 
104  setValue(i);
105  return *this;
106 }
107 
108 const IniVariable &IniVariable::operator=(unsigned int i)
109 {
110  assert(!isNull());
111 
112  setValue(i);
113  return *this;
114 }
115 
116 const IniVariable &IniVariable::operator=(short i)
117 {
118  assert(!isNull());
119 
120  setValue(i);
121  return *this;
122 }
123 
124 const IniVariable &IniVariable::operator=(unsigned short i)
125 {
126  assert(!isNull());
127 
128  setValue(i);
129  return *this;
130 }
131 
132 const IniVariable &IniVariable::operator=(bool b)
133 {
134  return *this = static_cast<int>(b);
135 }
136 
137 const IniVariable &IniVariable::operator=(float f)
138 {
139  assert(!isNull());
140 
141  setValue(f);
142  return *this;
143 }
144 
145 const QString& IniVariable::key()
146 {
147  return d->key;
148 }
149 
151 {
152  return d->section.isNull();
153 }
154 
155 void IniVariable::setValue(const QVariant& value)
156 {
157  d->section.setValue(d->key, value);
158 }
159 
160 QVariant IniVariable::value() const
161 {
162  return d->section.value(d->key);
163 }
INI variable representation.
Definition: inivariable.h:41
QVariant value() const
Extracts the value as QVariant.
IniVariable()
Creates an invalid IniVariable object. Such object should not be used for read/write operations...
void setValue(const QVariant &value)
Explicitly sets the value from QVariant.
const QString & key()
Name of the variable within the section.
INI section representation.
Definition: inisection.h:40
bool isNull() const
If true, IniSection object is not valid and should not be used to perform any actions on the Ini file...