Pol  Revision:4b29d2b
cfgsect.cpp
Go to the documentation of this file.
1 
7 #include "cfgsect.h"
8 
9 #include "cfgelem.h"
10 #include "cfgfile.h"
11 
12 namespace Pol
13 {
14 namespace Clib
15 {
16 ConfigSection::ConfigSection( ConfigFile& cf, const std::string& sectname, unsigned flags )
17  : _cf( cf ), _sectname( sectname ), _found( false ), _flags( flags )
18 {
19  _cf.allowed_types_.insert( sectname.c_str() );
20 }
21 
23 {
24  if ( !_found && ( _flags & CST_MANDATORY ) )
25  {
26  _cf.display_error( "Mandatory section type '" + _sectname + "' not found", false );
27  throw std::runtime_error( "Mandatory section type '" + _sectname +
28  "' not found in configuration file \"" + _cf.filename() + "\"" );
29  }
30 }
31 
33 {
34  if ( elem.type_is( _sectname.c_str() ) )
35  {
36  if ( _found && ( _flags & CST_UNIQUE ) )
37  {
38  elem.throw_error( "Section type " + _sectname + " found more than once" );
39  }
40  _found = true;
41  return true;
42  }
43  else
44  {
45  return false;
46  }
47 }
48 }
49 }
const std::string & filename() const
Definition: cfgfile.cpp:587
bool matches(const ConfigElem &elem)
Definition: cfgsect.cpp:32
ConfigFile & _cf
Definition: cfgsect.h:33
~ConfigSection() noexcept(false)
Definition: cfgsect.cpp:22
POL_NORETURN void throw_error(const std::string &errmsg) const
Definition: cfgfile.cpp:285
const unsigned CST_UNIQUE
Definition: cfgsect.h:21
std::string _sectname
Definition: cfgsect.h:34
AllowedTypesCont allowed_types_
Definition: cfgfile.h:89
virtual void display_error(const std::string &msg, bool show_curline=true, const ConfigElemBase *elem=NULL, bool error=true) const POL_OVERRIDE
Definition: cfgfile.cpp:962
bool type_is(const char *name) const
Definition: cfgfile.cpp:95
const unsigned CST_MANDATORY
Definition: cfgsect.h:20
ConfigSection(ConfigFile &cf, const std::string &sectname, unsigned flags=CST_MANDATORY|CST_UNIQUE)
Definition: cfgsect.cpp:16
Definition: berror.cpp:12