Fawkes API  Fawkes Development Version
yaml.h
1 
2 /***************************************************************************
3  * yaml.h - Fawkes configuration stored in one or more YAML files
4  *
5  * Created: Wed Aug 01 15:44:33 2012
6  * Copyright 2006-2012 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CONFIG_YAML_H_
25 #define _CONFIG_YAML_H_
26 
27 #include <config/config.h>
28 #include <utils/system/fam.h>
29 #include <yaml-cpp/yaml.h>
30 
31 #include <memory>
32 #include <queue>
33 #include <string>
34 #include <vector>
35 
36 namespace fawkes {
37 
38 class Mutex;
39 class FamThread;
40 class YamlConfigurationNode;
41 
43 {
44 public:
46  YamlConfiguration(const char *sysconfdir, const char *userconfdir = NULL);
47  virtual ~YamlConfiguration();
48 
49  virtual void copy(Configuration *copyconf);
50 
51  virtual void load(const char *file_path);
52 
53  virtual bool exists(const char *path);
54  virtual bool is_float(const char *path);
55  virtual bool is_uint(const char *path);
56  virtual bool is_int(const char *path);
57  virtual bool is_bool(const char *path);
58  virtual bool is_string(const char *path);
59  virtual bool is_list(const char *path);
60 
61  virtual bool is_default(const char *path);
62 
63  virtual std::string get_type(const char *path);
64  virtual float get_float(const char *path);
65  virtual unsigned int get_uint(const char *path);
66  virtual int get_int(const char *path);
67  virtual bool get_bool(const char *path);
68  virtual std::string get_string(const char *path);
69  virtual std::vector<float> get_floats(const char *path);
70  virtual std::vector<unsigned int> get_uints(const char *path);
71  virtual std::vector<int> get_ints(const char *path);
72  virtual std::vector<bool> get_bools(const char *path);
73  virtual std::vector<std::string> get_strings(const char *path);
74  virtual ValueIterator * get_value(const char *path);
75  virtual std::string get_comment(const char *path);
76  virtual std::string get_default_comment(const char *path);
77 
78  virtual void set_float(const char *path, float f);
79  virtual void set_uint(const char *path, unsigned int uint);
80  virtual void set_int(const char *path, int i);
81  virtual void set_bool(const char *path, bool b);
82  virtual void set_string(const char *path, std::string &s);
83  virtual void set_string(const char *path, const char *s);
84  virtual void set_floats(const char *path, std::vector<float> &f);
85  virtual void set_uints(const char *path, std::vector<unsigned int> &uint);
86  virtual void set_ints(const char *path, std::vector<int> &i);
87  virtual void set_bools(const char *path, std::vector<bool> &b);
88  virtual void set_strings(const char *path, std::vector<std::string> &s);
89  virtual void set_strings(const char *path, std::vector<const char *> &s);
90  virtual void set_comment(const char *path, std::string &comment);
91  virtual void set_comment(const char *path, const char *comment);
92 
93  virtual void erase(const char *path);
94 
95  virtual void set_default_float(const char *path, float f);
96  virtual void set_default_uint(const char *path, unsigned int uint);
97  virtual void set_default_int(const char *path, int i);
98  virtual void set_default_bool(const char *path, bool b);
99  virtual void set_default_string(const char *path, std::string &s);
100  virtual void set_default_string(const char *path, const char *s);
101  virtual void set_default_comment(const char *path, const char *comment);
102  virtual void set_default_comment(const char *path, std::string &comment);
103 
104  virtual void erase_default(const char *path);
105 
107  ValueIterator *search(const char *path);
108 
109  void lock();
110  bool try_lock();
111  void unlock();
112 
113  virtual void try_dump();
114 
115  virtual void fam_event(const char *filename, unsigned int mask);
116 
117 public:
119  {
120  public:
122  YamlValueIterator(std::map<std::string, std::shared_ptr<YamlConfigurationNode>> &nodes);
123 
124  virtual ~YamlValueIterator()
125  {
126  }
127  virtual bool next();
128  virtual bool valid() const;
129 
130  virtual const char *path() const;
131  virtual const char *type() const;
132 
133  virtual bool is_float() const;
134  virtual bool is_uint() const;
135  virtual bool is_int() const;
136  virtual bool is_bool() const;
137  virtual bool is_string() const;
138  virtual bool is_list() const;
139  virtual size_t get_list_size() const;
140 
141  virtual float get_float() const;
142  virtual unsigned int get_uint() const;
143  virtual int get_int() const;
144  virtual bool get_bool() const;
145  virtual std::string get_string() const;
146  virtual std::vector<float> get_floats() const;
147  virtual std::vector<unsigned int> get_uints() const;
148  virtual std::vector<int> get_ints() const;
149  virtual std::vector<bool> get_bools() const;
150  virtual std::vector<std::string> get_strings() const;
151  virtual std::string get_as_string() const;
152 
153  virtual std::string get_comment() const;
154 
155  virtual bool is_default() const;
156 
157  private:
158  bool first_;
159  std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes_;
160  std::map<std::string, std::shared_ptr<YamlConfigurationNode>>::iterator current_;
161  };
162 
163 private:
164  /// @cond INTERNALS
165  class LoadQueueEntry
166  {
167  public:
168  LoadQueueEntry(std::string fn, bool im, bool id = false)
169  : filename(fn), ignore_missing(im), is_dir(id)
170  {
171  }
172 
173  std::string filename;
174  bool ignore_missing;
175  bool is_dir;
176  };
177  /// @endcond
178 
179  std::shared_ptr<YamlConfigurationNode> query(const char *path) const;
180  void
181  read_meta_doc(YAML::Node &doc, std::queue<LoadQueueEntry> &load_queue, std::string &host_file);
182  std::shared_ptr<YamlConfigurationNode> read_config_doc(const YAML::Node &doc);
183  std::shared_ptr<YamlConfigurationNode> read_yaml_file(std::string filename,
184  bool ignore_missing,
185  std::queue<LoadQueueEntry> &load_queue,
186  std::string & host_file);
187  void read_yaml_config(std::string filename,
188  std::string & host_file,
189  std::shared_ptr<YamlConfigurationNode> &root,
190  std::shared_ptr<YamlConfigurationNode> &host_root,
191  std::list<std::string> & files,
192  std::list<std::string> & dirs);
193  void write_host_file();
194 
195  std::string config_file_;
196  std::string host_file_;
197 
198  std::shared_ptr<YamlConfigurationNode> root_;
199  std::shared_ptr<YamlConfigurationNode> host_root_;
200 
201  bool write_pending_;
202  Mutex *write_pending_mutex_;
203 
204 private:
205  Mutex *mutex;
206 
207  typedef std::map<std::string, YAML::Node *> DocMap;
208  mutable DocMap documents_;
209 
210  char *sysconfdir_;
211  char *userconfdir_;
212 
213  FamThread *fam_thread_;
214 };
215 
216 } // end namespace fawkes
217 
218 #endif
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
Definition: yaml.cpp:930
virtual std::vector< int > get_ints() const
Get list of values from configuration which is of type int.
Definition: yaml.cpp:255
virtual bool is_float() const
Check if current value is a float.
Definition: yaml.cpp:113
virtual bool is_list() const
Check if a value is a list.
Definition: yaml.cpp:158
virtual std::string get_string() const
Get string value.
Definition: yaml.cpp:215
ValueIterator * search(const char *path)
Iterator with search results.
Definition: yaml.cpp:1137
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
Definition: yaml.cpp:841
virtual int get_int() const
Get int value.
Definition: yaml.cpp:197
virtual float get_float() const
Get float value.
Definition: yaml.cpp:179
ValueIterator * iterator()
Iterator for all values.
Definition: yaml.cpp:1129
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
Definition: yaml.cpp:1022
virtual std::vector< float > get_floats() const
Get list of values from configuration which is of type float.
Definition: yaml.cpp:237
YamlConfiguration()
Constructor.
Definition: yaml.cpp:302
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
Definition: yaml.cpp:1083
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:921
virtual bool is_list(const char *path)
Check if a value is a list.
Definition: yaml.cpp:874
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
Definition: yaml.cpp:795
Fawkes library namespace.
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
Definition: yaml.cpp:999
virtual bool valid() const
Check if the current element is valid.
Definition: yaml.cpp:89
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
Definition: yaml.cpp:957
void lock()
Lock the config.
Definition: yaml.cpp:1093
virtual bool is_bool(const char *path)
Check if a value is of type bool.
Definition: yaml.cpp:862
virtual bool is_uint() const
Check if current value is a unsigned int.
Definition: yaml.cpp:122
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
Definition: yaml.cpp:1071
virtual bool is_int(const char *path)
Check if a value is of type int.
Definition: yaml.cpp:856
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
Definition: yaml.cpp:813
virtual bool is_default(const char *path)
Check if a value was read from the default config.
Definition: yaml.cpp:890
virtual bool is_float(const char *path)
Check if a value is of type float.
Definition: yaml.cpp:835
virtual bool is_bool() const
Check if current value is a bool.
Definition: yaml.cpp:140
virtual std::vector< unsigned int > get_uints() const
Get list of values from configuration which is of type unsigned int.
Definition: yaml.cpp:246
virtual std::string get_comment() const
Get comment of value.
Definition: yaml.cpp:282
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
Definition: yaml.cpp:896
Iterator for YAML config trees.
Definition: yaml.h:118
virtual int get_int(const char *path)
Get value from configuration which is of type int.
Definition: yaml.cpp:771
virtual void load(const char *file_path)
Load configuration.
Definition: yaml.cpp:367
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
Definition: yaml.cpp:801
virtual std::vector< bool > get_bools() const
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:264
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: yaml.cpp:783
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: yaml.cpp:765
virtual const char * path() const
Path of value.
Definition: yaml.cpp:95
virtual std::string get_type(const char *path)
Get type of value at given path.
Definition: yaml.cpp:706
Configuration store using YAML documents.
Definition: yaml.h:42
virtual std::string get_as_string() const
Get value as string.
Definition: yaml.cpp:224
virtual size_t get_list_size() const
Get number of elements in list value.
Definition: yaml.cpp:167
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
Definition: yaml.cpp:939
File Alteration Monitor Listener.
Definition: fam.h:35
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
Definition: yaml.cpp:1041
void unlock()
Unlock the config.
Definition: yaml.cpp:1112
virtual bool get_bool() const
Get bool value.
Definition: yaml.cpp:206
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
Definition: yaml.cpp:990
virtual ~YamlConfiguration()
Destructor.
Definition: yaml.cpp:346
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
Definition: yaml.cpp:912
virtual unsigned int get_uint() const
Get unsigned int value.
Definition: yaml.cpp:188
virtual void try_dump()
Try to dump configuration.
Definition: yaml.cpp:1124
bool try_lock()
Try to lock the config.
Definition: yaml.cpp:1103
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
Definition: yaml.cpp:963
virtual bool next()
Check if there is another element and advance to this if possible.
Definition: yaml.cpp:78
virtual std::string get_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:717
virtual bool is_string(const char *path)
Check if a value is of type string.
Definition: yaml.cpp:868
virtual bool is_default() const
Check if current value was read from the default config.
Definition: yaml.cpp:288
virtual bool exists(const char *path)
Check if a given value exists.
Definition: yaml.cpp:695
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
Definition: yaml.cpp:1065
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
Definition: yaml.cpp:981
Iterator interface to iterate over config values.
Definition: config.h:71
virtual std::vector< std::string > get_strings() const
Get list of values from configuration which is of type string.
Definition: yaml.cpp:273
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
Definition: yaml.cpp:777
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:972
virtual void erase(const char *path)
Erase the given value from the configuration.
Definition: yaml.cpp:1027
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
Definition: yaml.cpp:1053
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
Definition: yaml.cpp:1035
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
Definition: yaml.cpp:1047
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:884
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
Definition: yaml.cpp:789
virtual bool is_string() const
Check if current value is a string.
Definition: yaml.cpp:149
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: yaml.cpp:528
virtual float get_float(const char *path)
Get value from configuration which is of type float.
Definition: yaml.cpp:759
virtual bool is_int() const
Check if current value is a int.
Definition: yaml.cpp:131
virtual void copy(Configuration *copyconf)
Copies all values from the given configuration.
Definition: yaml.cpp:689
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:807
Interface for configuration handling.
Definition: config.h:64
virtual const char * type() const
Type of value.
Definition: yaml.cpp:104