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  if (isNull())
95  {
96  bool breakpoint = true;
97  }
98  assert(!isNull());
99 
100  setValue(str);
101  return *this;
102 }
103 
104 const IniVariable &IniVariable::operator=(int i)
105 {
106  assert(!isNull());
107 
108  setValue(i);
109  return *this;
110 }
111 
112 const IniVariable &IniVariable::operator=(unsigned int i)
113 {
114  assert(!isNull());
115 
116  setValue(i);
117  return *this;
118 }
119 
120 const IniVariable &IniVariable::operator=(short i)
121 {
122  assert(!isNull());
123 
124  setValue(i);
125  return *this;
126 }
127 
128 const IniVariable &IniVariable::operator=(unsigned short i)
129 {
130  assert(!isNull());
131 
132  setValue(i);
133  return *this;
134 }
135 
136 const IniVariable &IniVariable::operator=(bool b)
137 {
138  return *this = static_cast<int>(b);
139 }
140 
141 const IniVariable &IniVariable::operator=(float f)
142 {
143  assert(!isNull());
144 
145  setValue(f);
146  return *this;
147 }
148 
149 const QString& IniVariable::key()
150 {
151  return d->key;
152 }
153 
155 {
156  return d->section.isNull();
157 }
158 
159 void IniVariable::setValue(const QVariant& value)
160 {
161  d->section.setValue(d->key, value);
162 }
163 
164 QVariant IniVariable::value() const
165 {
166  return d->section.value(d->key);
167 }
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...