cfgfile 0.2.11
Loading...
Searching...
No Matches
tag.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__TAG_HPP__INCLUDED
32#define CFGFILE__TAG_HPP__INCLUDED
33
34#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
35// Qt include.
36#include <QDomDocument>
37#include <QDomElement>
38#endif
39
40// cfgfile include.
41#include "parser_info.hpp"
42#include "types.hpp"
43#include "exceptions.hpp"
44
45// C++ include.
46#include <vector>
47#include <algorithm>
48
49
50namespace cfgfile {
51
52//
53// tag_t
54//
55
57template< typename Trait = string_trait_t >
58class tag_t {
59public:
60 template< typename T1 > friend class parser_t;
61
63 typedef std::vector< tag_t< Trait >* > child_tags_list_t;
64
66 explicit tag_t( const typename Trait::string_t & name,
67 bool is_mandatory = false )
68 : m_name( name )
69 , m_is_mandatory( is_mandatory )
70 , m_is_defined( false )
71 , m_parent( nullptr )
72 , m_line_number( -1 )
73 , m_column_number( -1 )
74 {
75 }
76
78 tag_t( tag_t< Trait > & owner, const typename Trait::string_t & name,
79 bool is_mandatory = false )
80 : m_name( name )
81 , m_is_mandatory( is_mandatory )
82 , m_is_defined( false )
83 , m_parent( nullptr )
84 , m_line_number( -1 )
85 , m_column_number( -1 )
86 {
87 owner.add_child( *this );
88 }
89
90 virtual ~tag_t()
91 {
92 }
93
96 {
97 if( std::find_if( m_child_tags.cbegin(), m_child_tags.cend(),
98 [ &tag ] ( const tag_t< Trait > * t )
99 {
100 return ( tag.name() == t->name() );
101 } ) != m_child_tags.cend() )
102 {
104 Trait::from_ascii( "Tag with name \"" ) +
105 tag.name() +
106 Trait::from_ascii( "\" already exists in parent tag \"" ) +
107 this->name() +
108 Trait::from_ascii( "\"." ) );
109 }
110
111 if( std::find( m_child_tags.cbegin(), m_child_tags.cend(), &tag ) ==
112 m_child_tags.cend() )
113 {
114 m_child_tags.push_back( &tag );
115
116 tag.set_parent( this );
117 }
118 }
119
122 {
123 auto it = std::find( m_child_tags.cbegin(), m_child_tags.cend(), &tag );
124
125 if( it != m_child_tags.cend() )
126 {
127 m_child_tags.erase( it );
128
129 tag.set_parent( nullptr );
130 }
131 }
132
134 const tag_t< Trait > * parent() const
135 {
136 return m_parent;
137 }
138
140 const typename Trait::string_t & name() const
141 {
142 return m_name;
143 }
144
146 bool is_mandatory() const
147 {
148 return m_is_mandatory;
149 }
150
152 bool is_defined() const
153 {
154 for( const tag_t< Trait > * tag : children() )
155 {
156 if( tag->is_mandatory() && !tag->is_defined() )
157 return false;
158 }
159
160 return m_is_defined;
161 }
162
164 void set_defined( bool on = true )
165 {
166 m_is_defined = on;
167 }
168
170 typename Trait::pos_t line_number() const
171 {
172 return m_line_number;
173 }
174
176 typename Trait::pos_t column_number() const
177 {
178 return m_column_number;
179 }
180
182 virtual const child_tags_list_t & children() const
183 {
184 return m_child_tags;
185 }
186
188 virtual typename Trait::string_t print( int indent = 0 ) const = 0;
189
190#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
192 virtual void print( QDomDocument & doc,
193 QDomElement * parent = 0 ) const = 0;
194#endif
195
197 virtual void on_start( const parser_info_t< Trait > & info )
198 {
199 m_line_number = info.line_number();
200 m_column_number = info.column_number();
201 }
202
204 virtual void on_finish( const parser_info_t< Trait > & info ) = 0;
205
207 virtual void on_string( const parser_info_t< Trait > & info,
208 const typename Trait::string_t & str ) = 0;
209
210protected:
211 template< class T1, class T2 > friend class tag_vector_of_tags_t;
212
215 {
216 m_parent = p;
217 }
218
221 {
222 return std::any_of( m_child_tags.cbegin(), m_child_tags.cend(),
223 [] ( tag_t< Trait > * tag ) { return ( tag->is_defined() ); } );
224 }
225
228 {
229 return m_is_defined;
230 }
231
232private:
234
235
236 typename Trait::string_t m_name;
238 bool m_is_mandatory;
240 bool m_is_defined;
242 child_tags_list_t m_child_tags;
244 const tag_t< Trait > * m_parent;
246 typename Trait::pos_t m_line_number;
248 typename Trait::pos_t m_column_number;
249}; // class tag_t
250
251} /* namespace cfgfile */
252
253#endif // CFGFILE__TAG_HPP__INCLUDED
Exception in the library.
Definition exceptions.hpp:51
Parser of the configuration file.
Definition parser.hpp:520
Base class for the tags in the configuration file.
Definition tag.hpp:58
bool is_defined_member_value() const
Definition tag.hpp:227
tag_t(tag_t< Trait > &owner, const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag.hpp:78
void add_child(tag_t< Trait > &tag)
Add child tag.
Definition tag.hpp:95
void set_defined(bool on=true)
Set "defined" property.
Definition tag.hpp:164
virtual void on_string(const parser_info_t< Trait > &info, const typename Trait::string_t &str)=0
Called when string found.
Trait::pos_t column_number() const
Definition tag.hpp:176
bool is_mandatory() const
Definition tag.hpp:146
virtual ~tag_t()
Definition tag.hpp:90
void remove_child(tag_t< Trait > &tag)
Remove child tag.
Definition tag.hpp:121
virtual void on_start(const parser_info_t< Trait > &info)
Called when tag parsing started.
Definition tag.hpp:197
tag_t(const typename Trait::string_t &name, bool is_mandatory=false)
Construct tag.
Definition tag.hpp:66
bool is_any_child_defined() const
Definition tag.hpp:220
Trait::pos_t line_number() const
Definition tag.hpp:170
bool is_defined() const
Definition tag.hpp:152
const tag_t< Trait > * parent() const
Definition tag.hpp:134
std::vector< tag_t< Trait > * > child_tags_list_t
List with children.
Definition tag.hpp:63
virtual const child_tags_list_t & children() const
Definition tag.hpp:182
const Trait::string_t & name() const
Definition tag.hpp:140
virtual void on_finish(const parser_info_t< Trait > &info)=0
Called when tag parsing finished.
virtual Trait::string_t print(int indent=0) const =0
Print tag to the output.
virtual void print(QDomDocument &doc, QDomElement *parent=0) const =0
Print tag to the output.
void set_parent(const tag_t< Trait > *p)
Set parent tag.
Definition tag.hpp:214
Tag with multiple instancies of the given tag.
Definition tag_vector_of_tags.hpp:62
Definition const.hpp:38
#define DISABLE_COPY(Class)
Macro for disabling copy.
Definition types.hpp:580