cfgfile 0.2.11
Loading...
Searching...
No Matches
tag_scalar_vector.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__TAG_SCALAR_VECTOR_HPP__INCLUDED
32#define CFGFILE__TAG_SCALAR_VECTOR_HPP__INCLUDED
33
34//cfgfile include.
35#include "tag.hpp"
36#include "constraint.hpp"
37#include "format.hpp"
38#include "string_format.hpp"
39#include "types.hpp"
40#include "exceptions.hpp"
41#include "parser_info.hpp"
42
43#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
44// Qt include.
45#include <QDomDocument>
46#include <QDomElement>
47#include <QDomText>
48#endif
49
50// C++ include.
51#include <vector>
52
53
54namespace cfgfile {
55
56//
57// tag_scalar_vector_t
58//
59
61template< class T, class Trait = string_trait_t >
63 : public tag_t< Trait >
64{
65public:
67 typedef std::vector< T > values_vector_t;
68
70 explicit tag_scalar_vector_t( const typename Trait::string_t & name,
71 bool is_mandatory = false )
73 , m_constraint( nullptr )
74 {
75 }
76
79 const typename Trait::string_t & name,
80 bool is_mandatory = false )
82 , m_constraint( nullptr )
83 {
84 }
85
89
91 typename values_vector_t::size_type
92 size() const
93 {
94 return m_values.size();
95 }
96
98 const T &
99 at( typename values_vector_t::size_type index ) const
100 {
101 return m_values.at( index );
102 }
103
105 const values_vector_t &
106 values() const
107 {
108 return m_values;
109 }
110
116 void
117 set_value( const T & v )
118 {
119 if( m_constraint )
120 {
121 if( !m_constraint->check( v ) )
123 Trait::from_ascii( "Invalid value: \"" ) +
124 typename Trait::string_t(
126 Trait::from_ascii( "\". Value must match to the "
127 "constraint in tag \"" ) +
128 this->name() + Trait::from_ascii( "\"." ) );
129 }
130
131 m_values.push_back( v );
132
133 this->set_defined();
134 }
135
137 void
138 set_values( const values_vector_t & v )
139 {
140 m_values = v;
141
142 this->set_defined();
143 }
144
152 void
153 query_opt_values( values_vector_t & receiver )
154 {
155 if( this->is_defined() )
156 receiver = m_values;
157 }
158
160 void
162 {
163 m_constraint = c;
164 }
165
167 typename Trait::string_t print( int indent = 0 ) const override
168 {
169 typename Trait::string_t result;
170
171 if( this->is_defined() )
172 {
173 result.append( typename Trait::string_t( indent,
176 result.append( this->name() );
177
178 for( const T & v : m_values )
179 {
181
182 typename Trait::string_t value =
184
185 value = to_cfgfile_format< Trait >( value );
186
187 result.append( value );
188 }
189
190 if( !this->children().empty() )
191 {
193
194 for( const tag_t< Trait > * tag : this->children() )
195 result.append( tag->print( indent + 1 ) );
196
197 result.append( typename Trait::string_t( indent,
199 }
200
203 }
204
205 return result;
206 }
207
208#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
210 void print( QDomDocument & doc, QDomElement * parent = 0 ) const override
211 {
212 if( this->is_defined() )
213 {
214 QDomElement this_element = doc.createElement( this->name() );
215
216 if( !parent )
217 doc.appendChild( this_element );
218 else
219 parent->appendChild( this_element );
220
221 typename values_vector_t::size_type i = 1;
222
223 for( const T & v : m_values )
224 {
225 typename Trait::string_t value =
227
228 value = to_cfgfile_format< Trait >( value );
229
230 QString tmp = value;
231
232 if( tmp.startsWith( const_t< Trait >::c_quotes ) &&
233 tmp.endsWith( const_t< Trait >::c_quotes ) )
234 tmp = tmp.mid( 1, tmp.length() - 2 );
235
236 this_element.setAttribute( QString( "a" ) + QString::number( i ),
237 tmp );
238
239 ++i;
240 }
241
242 if( !this->children().empty() )
243 {
244 for( const tag_t< Trait > * tag : this->children() )
245 tag->print( doc, &this_element );
246 }
247 }
248 }
249#endif
250
252 void on_finish( const parser_info_t< Trait > & info ) override
253 {
254 for( const tag_t< Trait > * tag : this->children() )
255 {
256 if( tag->is_mandatory() && !tag->is_defined() )
258 Trait::from_ascii( "Undefined child mandatory tag: \"" ) +
259 tag->name() +
260 Trait::from_ascii( "\". Where parent is: \"" ) +
261 this->name() + Trait::from_ascii( "\". In file \"" ) +
262 info.file_name() + Trait::from_ascii( "\" on line " ) +
263 Trait::to_string( info.line_number() ) +
264 Trait::from_ascii( "." ) );
265 }
266 }
267
270 const typename Trait::string_t & str ) override
271 {
272 if( this->is_any_child_defined() )
274 Trait::from_ascii( "Value \"" ) + str +
275 Trait::from_ascii( "\" for tag \"" ) + this->name() +
276 Trait::from_ascii( "\" must be defined before any child "
277 "tag. In file \"" ) +
278 info.file_name() + Trait::from_ascii( "\" on line " ) +
279 Trait::to_string( info.line_number() ) +
280 Trait::from_ascii( "." ) );
281
283
284 if( m_constraint )
285 {
286 if( !m_constraint->check( value ) )
288 Trait::from_ascii( "Invalid value: \"" ) +
289 str + Trait::from_ascii( "\". Value must match to the "
290 "constraint in tag \"" ) +
291 this->name() + Trait::from_ascii( "\". In file \"" ) +
292 info.file_name() + Trait::from_ascii( "\" on line " ) +
293 Trait::to_string( info.line_number() ) +
294 Trait::from_ascii( "." ) );
295 }
296
297 m_values.push_back( value );
298
299 this->set_defined();
300 }
301
302private:
304 values_vector_t m_values;
306 constraint_t< T > * m_constraint;
307}; // class tag_scalar_vector_t
308
309} /* namespace cfgfile */
310
311#endif // CFGFILE__TAG_SCALAR_VECTOR_HPP__INCLUDED
Exception in the library.
Definition exceptions.hpp:51
Format template value to the string and back.
Definition format.hpp:57
static T from_string(const parser_info_t< Trait > &, const typename Trait::string_t &)
Format value from string.
static Trait::string_t to_string(const T &)
Format value to string.
Tag with multiple scalar values.
Definition tag_scalar_vector.hpp:64
void on_string(const parser_info_t< Trait > &info, const typename Trait::string_t &str) override
Called when string found.
Definition tag_scalar_vector.hpp:269
void set_constraint(constraint_t< T > *c)
Set constraint for the tag's value.
Definition tag_scalar_vector.hpp:161
void on_finish(const parser_info_t< Trait > &info) override
Called when tag parsing finished.
Definition tag_scalar_vector.hpp:252
tag_scalar_vector_t(const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag_scalar_vector.hpp:70
void set_value(const T &v)
Set value.
Definition tag_scalar_vector.hpp:117
void print(QDomDocument &doc, QDomElement *parent=0) const override
Print tag to the output.
Definition tag_scalar_vector.hpp:210
void set_values(const values_vector_t &v)
Set values.
Definition tag_scalar_vector.hpp:138
std::vector< T > values_vector_t
Vector of values.
Definition tag_scalar_vector.hpp:67
tag_scalar_vector_t(tag_t< Trait > &owner, const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag_scalar_vector.hpp:78
void query_opt_values(values_vector_t &receiver)
Query optional values.
Definition tag_scalar_vector.hpp:153
const values_vector_t & values() const
Definition tag_scalar_vector.hpp:106
const T & at(typename values_vector_t::size_type index) const
Definition tag_scalar_vector.hpp:99
~tag_scalar_vector_t()
Definition tag_scalar_vector.hpp:86
values_vector_t::size_type size() const
Definition tag_scalar_vector.hpp:92
Trait::string_t print(int indent=0) const override
Print tag to the output.
Definition tag_scalar_vector.hpp:167
Base class for the tags in the configuration file.
Definition tag.hpp:58
void set_defined(bool on=true)
Set "defined" property.
Definition tag.hpp:164
bool is_mandatory() const
Definition tag.hpp:146
bool is_any_child_defined() const
Definition tag.hpp:220
bool is_defined() const
Definition tag.hpp:152
const tag_t< Trait > * parent() const
Definition tag.hpp:134
virtual const child_tags_list_t & children() const
Definition tag.hpp:182
const Trait::string_t & name() const
Definition tag.hpp:140
Definition const.hpp:38
Definition const.hpp:45