SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Properties.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
8 #include <fstream>
9 
10 
11 namespace SWC { namespace Config {
12 
13 void Properties::reset() noexcept {
14  for (const auto& kv : m_map)
15  delete kv.second;
16  m_map.clear();
17 }
18 
19 SWC_SHOULD_NOT_INLINE
21  bool only_guarded) {
22  for(const auto& kv : opts.map) {
23  if(has(kv.first.c_str()) &&
24  (kv.second->is_default() ||
25  (only_guarded && !kv.second->is_guarded())))
26  continue;
27  set(kv.first.c_str(), kv.second);
28  }
29 }
30 
31 SWC_SHOULD_NOT_INLINE
32 void Properties::load(const std::string& fname,
33  const Config::ParserConfig& filedesc,
34  const Config::ParserConfig& cmddesc,
35  bool allow_unregistered,
36  bool only_guarded) {
37  Config::Parser prs(false);
38  {
39  prs.config.add(filedesc);
40  prs.config.add(cmddesc);
41 
42  std::ifstream in(fname.c_str());
43  prs.parse_filedata(in);
44  }
45  load_from(prs.get_options(), only_guarded);
46  (void)allow_unregistered;
47 }
48 
49 SWC_SHOULD_NOT_INLINE
50 void Properties::reload(const std::string& fname,
51  const Config::ParserConfig& filedesc,
52  const Config::ParserConfig& cmddesc) {
53  try {
54  load(fname, filedesc, cmddesc, true);
55 
56  } catch(...) {
59  << "CONFIG_BAD_CFG_FILE " << fname << ": " << e;
60  );
61  }
62 }
63 
64 void Properties::alias(const char* primary, const char* secondary) {
65  m_alias_map[primary] = secondary;
66  m_alias_map[secondary] = primary;
67 }
68 
69 SWC_SHOULD_NOT_INLINE
70 void Properties::set(const char* name, Property::Value::Ptr p) {
71  auto ptr = get_ptr(name, true);
72  if(!ptr) {
73  m_map.emplace(name, p->make_new());
74 
75  } else if(!p->is_default()) {
76  ptr->set_from(p);
77  ptr->default_value(false);
78  }
79 }
80 
81 SWC_SHOULD_NOT_INLINE
82 bool Properties::has(const char* name) const noexcept {
83  for(auto it=m_map.cbegin(); it!=m_map.cend(); ++it) {
84  if(Condition::str_eq(it->first.c_str(), name))
85  return true;
86  }
87  for(auto it=m_alias_map.cbegin(); it!=m_alias_map.cend(); ++it) {
88  if(Condition::str_eq(it->first.c_str(), name))
89  return m_map.find(it->second) != m_map.cend();
90  }
91  return false;
92 }
93 
94 bool Properties::defaulted(const char* name) const {
95  return get_ptr(name)->is_default();
96 }
97 
98 std::string Properties::to_string(const char* name) const {
99  return get_ptr(name)->to_string();
100 }
101 
102 void Properties::get_names(Strings& names) const {
103  names.reserve(m_map.size());
104  for(auto it = m_map.cbegin(); it != m_map.cend(); ++it)
105  names.push_back(it->first);
106 }
107 
108 void Properties::remove(const char* name) {
109  for(auto it=m_map.cbegin(); it!=m_map.cend(); ++it) {
110  if(Condition::str_eq(it->first.c_str(), name)) {
111  delete it->second;
112  m_map.erase(it);
113  return;
114  }
115  }
116 }
117 
118 SWC_SHOULD_NOT_INLINE
120  bool null_ok) const {
121  for(auto it=m_map.cbegin(); it!=m_map.cend(); ++it) {
122  if(Condition::str_eq(it->first.c_str(), name))
123  return it->second;
124  }
125  for(auto it=m_alias_map.cbegin(); it!=m_alias_map.cend(); ++it) {
126  if(Condition::str_eq(it->first.c_str(), name)) {
127  auto prop = m_map.find(it->second);
128  if(prop != m_map.cend())
129  return prop->second;
130  break;
131  }
132  }
133  if(null_ok)
134  return nullptr;
135 
137  "getting value of '%s' - missing", name);
138 }
139 
140 void Properties::print(std::ostream& out, bool include_default) const {
141  bool isdefault;
142  for(const auto& kv : m_map) {
143  isdefault = kv.second->is_default();
144  if(include_default || !isdefault) {
145  out << kv.first << '=' << kv.second->to_string();
146  if(isdefault)
147  out << " (default)";
148  out << '\n';
149  }
150  }
151 }
152 
153 
154 
155 }}
156 
SWC::Config::Property::Value::to_string
static const char *SWC_CONST_FUNC to_string(Type type) noexcept
Definition: Property.cc:13
SWC::Config::Properties::reload
void reload(const std::string &fname, const Config::ParserConfig &filedesc, const Config::ParserConfig &cmddesc)
Definition: Properties.cc:50
SWC::Config::Parser
Definition: PropertiesParser.h:168
SWC::Config::ParserConfig::add
ParserConfig & add(const ParserConfig &other_cfg)
Definition: PropertiesParser.cc:175
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::Config::Properties::remove
void remove(const char *name)
Definition: Properties.cc:108
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
SWC::Config::Properties::print
void print(std::ostream &out, bool include_default=false) const
Definition: Properties.cc:140
Properties.h
SWC::Config::Parser::get_options
const Options &SWC_CONST_FUNC get_options() const noexcept
Definition: PropertiesParser.cc:581
SWC::Config::Property::Value::is_default
bool is_default() const noexcept
Definition: Property.cc:90
SWC::Config::Properties::defaulted
bool defaulted(const char *name) const
Definition: Properties.cc:94
SWC::Config::Properties::get_names
void get_names(Strings &names) const
Definition: Properties.cc:102
SWC::Config::Parser::config
ParserConfig config
Definition: PropertiesParser.h:171
SWC::Condition::str_eq
bool str_eq(const char *s1, const char *s2) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:237
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Config::Properties::set
void set(const char *name, Property::Value::Ptr p)
Definition: Properties.cc:70
SWC::Config::Property::Value::make_new
virtual Ptr make_new(const Strings &values=Strings())=0
SWC::Config::Parser::Options::map
std::map< std::string, Property::Value::Ptr > map
Definition: PropertiesParser.h:177
SWC::Config::Properties::has
bool SWC_PURE_FUNC has(const char *name) const noexcept
Definition: Properties.cc:82
SWC_THROWF
#define SWC_THROWF(_code_, _fmt_,...)
Definition: Exception.h:136
SWC::Config::Properties::load
void load(const std::string &fname, const Config::ParserConfig &filedesc, const Config::ParserConfig &cmddesc, bool allow_unregistered=false, bool only_guarded=false)
Definition: Properties.cc:32
SWC::Config::ParserConfig
Definition: PropertiesParser.h:64
SWC::Config::Parser::parse_filedata
void parse_filedata(std::ifstream &in)
Definition: PropertiesParser.cc:385
SWC::Config::Properties::m_alias_map
AliasMap m_alias_map
Definition: Properties.h:139
SWC::Config::Properties::load_from
void load_from(const Config::Parser::Options &opts, bool only_guarded=false)
Definition: Properties.cc:20
SWC::Config::Property::Value
Definition: Property.h:35
SWC::Config::Parser::Options
Definition: PropertiesParser.h:175
SWC::Core::Vector< std::string >
SWC::Config::Properties::to_string
std::string to_string(const char *name) const
Definition: Properties.cc:98
SWC::Config::Properties::get_ptr
Property::Value::Ptr get_ptr(const char *name, bool null_ok=false) const
Definition: Properties.cc:119
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Config::Properties::m_map
Map m_map
Definition: Properties.h:138
SWC::Error::CONFIG_GET_ERROR
@ CONFIG_GET_ERROR
Definition: Error.h:80
SWC::Core::Vector::push_back
SWC_CAN_INLINE void push_back(ArgsT &&... args)
Definition: Vector.h:331
SWC::Error::Exception
Definition: Exception.h:21
SWC::Config::Properties::alias
void alias(const char *primary, const char *secondary)
Definition: Properties.cc:64
SWC::Config::Properties::reset
void reset() noexcept
Definition: Properties.cc:13
SWC::Core::Vector::reserve
SWC_CAN_INLINE void reserve(size_type cap)
Definition: Vector.h:288