cfgfile 0.2.11
Loading...
Searching...
No Matches
tag_no_value.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__TAG_NO_VALUE_HPP__INCLUDED
32#define CFGFILE__TAG_NO_VALUE_HPP__INCLUDED
33
34// cfgfile include.
35#include "tag.hpp"
36#include "parser_info.hpp"
37#include "const.hpp"
38#include "exceptions.hpp"
39
40#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
41// Qt include.
42#include <QDomDocument>
43#include <QDomElement>
44#endif
45
46
47namespace cfgfile {
48
49//
50// tag_no_value_t
51//
52
54template< typename Trait = string_trait_t >
56 : public tag_t< Trait >
57{
58public:
60 explicit tag_no_value_t( const typename Trait::string_t & name,
61 bool is_mandatory = false )
63 {
64 }
65
68 const typename Trait::string_t & name, bool is_mandatory = false )
70 {
71 }
72
74 {
75 }
76
78 typename Trait::string_t print( int indent = 0 ) const override
79 {
80 typename Trait::string_t result;
81
82 if( this->is_defined() )
83 {
84 result.append( typename Trait::string_t( indent,
86
88 result.append( this->name() );
89
90 if( !this->children().empty() )
91 {
93
94 for( const tag_t< Trait > * tag : this->children() )
95 result.append( tag->print( indent + 1 ) );
96
97 result.append( typename Trait::string_t( indent,
99 }
100
103 }
104
105 return result;
106 }
107
108#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
110 void print( QDomDocument & doc, QDomElement * parent = 0 ) const override
111 {
112 if( this->is_defined() )
113 {
114 QDomElement this_element = doc.createElement( this->name() );
115
116 if( !parent )
117 doc.appendChild( this_element );
118 else
119 parent->appendChild( this_element );
120
121 if( !this->children().empty() )
122 {
123 for( const tag_t< Trait > * tag : this->children() )
124 tag->print( doc, &this_element );
125 }
126 }
127 }
128#endif
129
131 void on_finish( const parser_info_t< Trait > & info ) override
132 {
133 for( const tag_t< Trait > * tag : this->children() )
134 {
135 if( tag->is_mandatory() && !tag->is_defined() )
137 Trait::from_ascii( "Undefined child mandatory tag: \"" ) +
138 tag->name() +
139 Trait::from_ascii( "\". Where parent is: \"" ) +
140 this->name() +
141 Trait::from_ascii( "\". In file \"" ) + info.file_name() +
142 Trait::from_ascii( "\" on line " ) +
143 Trait::to_string( info.line_number() ) +
144 Trait::from_ascii( "." ) );
145 }
146
147 this->set_defined();
148 }
149
152 const typename Trait::string_t & str ) override
153 {
154 throw exception_t< Trait >( Trait::from_ascii( "Tag \"" ) +
155 this->name() + Trait::from_ascii( "\" doesn't allow any values. "
156 "But we've got this: \"" ) +
157 str + Trait::from_ascii( "\". In file \"" ) + info.file_name() +
158 Trait::from_ascii( "\" on line " ) +
159 Trait::to_string( info.line_number() ) +
160 Trait::from_ascii( "." ) );
161 }
162}; // class tag_no_value_t
163
164} /* namespace cfgfile */
165
166#endif // CFGFILE__TAG_NO_VALUE_HPP__INCLUDED
Exception in the library.
Definition exceptions.hpp:51
Tag without a value.
Definition tag_no_value.hpp:57
void on_string(const parser_info_t< Trait > &info, const typename Trait::string_t &str) override
Called when string found.
Definition tag_no_value.hpp:151
Trait::string_t print(int indent=0) const override
Print tag to the output.
Definition tag_no_value.hpp:78
void on_finish(const parser_info_t< Trait > &info) override
Called when tag parsing finished.
Definition tag_no_value.hpp:131
void print(QDomDocument &doc, QDomElement *parent=0) const override
Print tag to the output.
Definition tag_no_value.hpp:110
tag_no_value_t(const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag_no_value.hpp:60
tag_no_value_t(tag_t< Trait > &owner, const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag_no_value.hpp:67
~tag_no_value_t()
Definition tag_no_value.hpp:73
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_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