liballofw
config.h
Go to the documentation of this file.
1 #ifndef ALLOFW_CONFIG_H
2 #define ALLOFW_CONFIG_H
3 
4 #include "common.h"
5 
6 #include <memory>
7 #include <string>
8 
9 namespace allofw {
10 
12  class Configuration {
13  public:
14 
16  class Node {
17  public:
18 
19  virtual bool isArray() const = 0;
20  virtual bool isDictionary() const = 0;
21  virtual bool isValue() const = 0;
22 
24  virtual void getString(char* output, size_t output_capacity, const char* fallback = "") const = 0;
26  virtual size_t getStringLength(const char* fallback = "") const = 0;
28  virtual int getInt32(int fallback = 0) const = 0;
30  virtual unsigned int getUInt32(unsigned int fallback = 0) const = 0;
32  virtual float getFloat(float fallback = 0.0f) const = 0;
34  virtual double getDouble(double fallback = 0.0) const = 0;
36  virtual bool getBoolean(bool fallback = 0.0) const = 0;
37 
39  std::string getString(const char* fallback = "") {
40  size_t len = getStringLength(fallback);
41  char* buffer = new char[len + 1];
42  getString(buffer, len + 1, fallback);
43  std::string result(buffer);
44  delete [] buffer;
45  return result;
46  }
47 
49  virtual Node* get(int index) const = 0;
51  virtual Node* get(const char* key) const = 0;
53  virtual size_t size() const = 0;
54 
55  protected:
56  virtual ~Node() { }
57  };
58 
60  virtual Node* getNode(const char* key) = 0;
62  virtual void destroyNode(Node* node) = 0;
63 
65  virtual void getString(const char* path, char* output, size_t output_capacity, const char* fallback = "") = 0;
67  virtual size_t getStringLength(const char* path, const char* fallback = "") = 0;
68 
70  virtual int getInt32(const char* path, int fallback = 0) = 0;
72  virtual unsigned int getUInt32(const char* path, unsigned int fallback = 0) = 0;
74  virtual float getFloat(const char* path, float fallback = 0.0f) = 0;
76  virtual double getDouble(const char* path, double fallback = 0.0) = 0;
78  virtual bool getBoolean(const char* path, bool fallback = 0.0) = 0;
79 
81  virtual void parseFile(const char* path) = 0;
83  virtual void parseFile(const char* path, const char* key) = 0;
84 
86  std::string getString(const char* path, const char* fallback = "") {
87  size_t len = getStringLength(path, fallback);
88  char* buffer = new char[len + 1];
89  getString(path, buffer, len + 1, fallback);
90  std::string result(buffer);
91  delete [] buffer;
92  return result;
93  }
94 
95  virtual ~Configuration() { }
96 
98  static Configuration* Create();
100  static Configuration* ParseMainArgs(int argc, char* argv[]);
102  static Configuration* CreateFromFile(const char* args = nullptr);
103  };
104 
105 }
106 
107 #endif
virtual bool getBoolean(bool fallback=0.0) const =0
Get boolean value.
virtual double getDouble(double fallback=0.0) const =0
Get double value.
virtual int getInt32(int fallback=0) const =0
Get int32 value.
virtual bool isDictionary() const =0
Is this an array?
virtual unsigned int getUInt32(unsigned int fallback=0) const =0
Get uint32 value.
virtual bool isArray() const =0
virtual Node * getNode(const char *key)=0
Get a node with a key (pair with destroyNode).
Definition: allofw.h:12
static Configuration * Create()
Create Configuration object.
Config node.
Definition: config.h:16
virtual float getFloat(float fallback=0.0f) const =0
Get float value.
virtual void getString(char *output, size_t output_capacity, const char *fallback="") const =0
Is this a value?
YAML-based configuration reader.
Definition: config.h:12
std::string getString(const char *path, const char *fallback="")
Get STL string at a given path.
Definition: config.h:86
std::string getString(const char *fallback="")
Get STL string value.
Definition: config.h:39
virtual bool isValue() const =0
Is this a dictionary?
virtual ~Node()
Definition: config.h:56
static Configuration * ParseMainArgs(int argc, char *argv[])
Create Configuration object by C++ main() arguments.
virtual size_t size() const =0
Get size of the node (for array and dictionary).
static Configuration * CreateFromFile(const char *args=nullptr)
Create Configuration object from file.
virtual size_t getStringLength(const char *fallback="") const =0
Get the length of the string.
virtual ~Configuration()
Definition: config.h:95
virtual void parseFile(const char *path)=0
Parse a YAML file.
virtual void destroyNode(Node *node)=0
Destroy the node.