|
19 | 19 | #ifndef CONFIGURATION_HPP_ |
20 | 20 | #define CONFIGURATION_HPP_ |
21 | 21 |
|
| 22 | +#include <algorithm> |
| 23 | +#include <iostream> |
22 | 24 | #include <memory> |
| 25 | +#include <optional> |
23 | 26 | #include <string> |
24 | 27 | #include <variant> |
25 | 28 | #include <vector> |
26 | 29 |
|
27 | 30 | /// @brief Namespace defining Data Containers |
28 | 31 | namespace Data { |
29 | 32 |
|
| 33 | + using ReturnType = |
| 34 | + std::variant<int, double, float, std::string, bool, char>; |
| 35 | + |
30 | 36 | /// @brief Declaration of wrapper class for property tree parser |
31 | 37 | class PTree; |
32 | 38 |
|
| 39 | + class IGettable { |
| 40 | + public: |
| 41 | + /// @brief Default template for getting parameters from the config |
| 42 | + /// @param str String key to search for |
| 43 | + /// @return The value searched for |
| 44 | + virtual ReturnType get(std::string, ReturnType) = 0; |
| 45 | + |
| 46 | + /// @brief Template for the optional parameters |
| 47 | + /// @param str String key to search for |
| 48 | + /// @return The optional value searched for |
| 49 | + virtual std::shared_ptr<ReturnType> get_optional(std::string, |
| 50 | + ReturnType) = 0; |
| 51 | + virtual std::vector<ReturnType> getVector(std::string) = 0; |
| 52 | + |
| 53 | + [[deprecated("This function is not intended to be permanent. getVector " |
| 54 | + "will recieve future focus.")]] virtual std::vector<int> |
| 55 | + getIntVector(std::string) = 0; |
| 56 | + [[deprecated("This function is not intended to be permanent. getVector " |
| 57 | + "will recieve future focus.")]] virtual std::vector<double> |
| 58 | + getDoubleVector(std::string) = 0; |
| 59 | + [[deprecated( |
| 60 | + "This function is not intended to be permanent. getVector will " |
| 61 | + "recieve future focus.")]] virtual std::vector<std::string> |
| 62 | + getStringVector(std::string) = 0; |
| 63 | + }; |
| 64 | + |
| 65 | + class IParseable { |
| 66 | + public: |
| 67 | + /// @brief Parse a single string to a vector |
| 68 | + /// @param string to parse |
| 69 | + /// @return vector containing parsed string |
| 70 | + virtual std::vector<ReturnType> parse(std::string) = 0; |
| 71 | + |
| 72 | + /// @brief Parse a single string to a vector |
| 73 | + /// @param string to parse |
| 74 | + /// @param delimiter string is being parsed with |
| 75 | + /// @return vector containing parsed string |
| 76 | + virtual std::vector<ReturnType> parse(std::string, std::string) = 0; |
| 77 | + }; |
| 78 | + |
33 | 79 | /// @brief Interface used for Configuration, provides guaranteed methods |
34 | | - class IConfiguration { |
| 80 | + class IConfigable : public virtual IGettable, public virtual IParseable { |
35 | 81 | public: |
36 | | - virtual ~IConfiguration() = default; |
37 | | - virtual std::vector<std::string> |
38 | | - parseString2VectorOfStrings(std::string st) = 0; |
39 | | - virtual std::vector<int> parseString2VectorOfInts(std::string st) = 0; |
40 | | - virtual std::vector<std::string> getStringVector(std::string str) = 0; |
41 | | - virtual std::vector<int> getIntVector(std::string str) = 0; |
42 | 82 | virtual std::vector<std::string> |
43 | 83 | getSectionCategories(std::string section) = 0; |
44 | 84 | }; |
45 | 85 |
|
46 | 86 | /// @brief Type definition for an easy to use pointer to a configuration |
47 | 87 | /// interface |
48 | | - using IConfigurationPtr = std::shared_ptr<Data::IConfiguration>; |
| 88 | + using IConfigablePtr = std::shared_ptr<Data::IConfigable>; |
49 | 89 |
|
50 | 90 | /// @brief Class describing a standard configuration file |
51 | | - class Configuration : public IConfiguration { |
| 91 | + class Config : public IConfigable { |
52 | 92 | private: |
53 | 93 | std::unique_ptr<PTree> dmTree; |
| 94 | + ReturnType convert_type(std::string); |
| 95 | + std::string trim(const std::string &str); |
54 | 96 |
|
55 | 97 | public: |
56 | 98 | /// @brief Default constructor with no file |
57 | | - Configuration(); |
| 99 | + Config(); |
58 | 100 |
|
59 | 101 | /// @brief Primary Constructor used with a file |
60 | 102 | /// @param configFile string path to the config file |
61 | | - Configuration(std::string configFile); |
| 103 | + Config(std::string configFile); |
62 | 104 |
|
63 | 105 | /// @brief Default Destructor |
64 | | - ~Configuration(); |
| 106 | + ~Config(); |
65 | 107 |
|
66 | | - /// @brief Default template for getting parameters from the config |
67 | | - /// @param T Type to return |
68 | | - /// @param str String key to search for |
69 | | - /// @return The value searched for of type T |
70 | | - template <typename T> T get(std::string str); |
| 108 | + ReturnType get(std::string str, ReturnType default_value) override; |
71 | 109 |
|
72 | | - /// @brief Template for the optional parameters |
73 | | - /// @param T Type to return |
74 | | - /// @param str String key to search for |
75 | | - /// @return The optional value searched for of type T |
76 | | - template <typename T> std::shared_ptr<T> optional(std::string str); |
77 | | - |
78 | | - /// @brief Helper function to parse a string to a vector of strings |
79 | | - /// @param st string to parse |
80 | | - /// @return vector of parsed strings |
81 | | - std::vector<std::string> |
82 | | - parseString2VectorOfStrings(std::string st) override; |
| 110 | + std::vector<ReturnType> getVector(std::string str) override; |
83 | 111 |
|
84 | | - /// @brief Helper function to parse a string to a vector of ints |
85 | | - /// @param st string to parse |
86 | | - /// @return vector of parsed ints |
87 | | - std::vector<int> parseString2VectorOfInts(std::string st) override; |
| 112 | + std::shared_ptr<ReturnType> |
| 113 | + get_optional(std::string str, ReturnType default_value) override; |
88 | 114 |
|
89 | | - /// @brief Wrapper function around `optional` and |
90 | | - /// `parseString2VectorofStrings`. |
91 | | - /// @param str string to parse |
92 | | - /// @return vector of parsed strings |
93 | | - std::vector<std::string> getStringVector(std::string str) override { |
94 | | - std::shared_ptr<std::string> value = |
95 | | - this->optional<std::string>(str); |
96 | | - if (value) { |
97 | | - return this->parseString2VectorOfStrings(*value); |
98 | | - } else { |
99 | | - return {}; |
100 | | - } |
| 115 | + std::vector<ReturnType> parse(std::string str) override { |
| 116 | + return this->parse(str, ","); |
101 | 117 | } |
102 | 118 |
|
103 | | - /// @brief Wrapper function around `optional` and |
104 | | - /// `parseString2VectorofInts`. |
105 | | - /// @param str string to parse |
106 | | - /// @return vector of parsed ints |
107 | | - std::vector<int> getIntVector(std::string str) override { |
108 | | - std::shared_ptr<std::string> value = |
109 | | - this->optional<std::string>(str); |
110 | | - if (value) { |
111 | | - return this->parseString2VectorOfInts(*value); |
112 | | - } else { |
113 | | - return {}; |
114 | | - } |
115 | | - } |
| 119 | + /// @brief Helper function to parse a string to a vector of strings |
| 120 | + /// @param st string to parse |
| 121 | + /// @return vector of parsed strings |
| 122 | + std::vector<ReturnType> parse(std::string str, |
| 123 | + std::string delimiter) override; |
116 | 124 |
|
117 | 125 | /// @brief Helper function to return all the categories belonging to the |
118 | 126 | /// provided section |
119 | 127 | /// @param section String describing the section to search |
120 | 128 | /// @return A vector of the categories avaliable |
121 | 129 | std::vector<std::string> |
122 | 130 | getSectionCategories(std::string section) override; |
| 131 | + |
| 132 | + std::vector<int> getIntVector(std::string str) override { |
| 133 | + std::string numbers = ""; |
| 134 | + numbers = std::get<std::string>(this->get(str, numbers)); |
| 135 | + std::vector<ReturnType> returned = this->parse(numbers); |
| 136 | + std::vector<int> casted_returned = {}; |
| 137 | + std::for_each(returned.begin(), returned.end(), [&](ReturnType &n) { |
| 138 | + casted_returned.push_back(std::get<int>(n)); |
| 139 | + }); |
| 140 | + return casted_returned; |
| 141 | + } |
| 142 | + |
| 143 | + std::vector<double> getDoubleVector(std::string str) override { |
| 144 | + std::string numbers = ""; |
| 145 | + numbers = std::get<std::string>(this->get(str, numbers)); |
| 146 | + std::vector<ReturnType> returned = this->parse(numbers); |
| 147 | + std::vector<double> casted_returned = {}; |
| 148 | + std::for_each(returned.begin(), returned.end(), [&](ReturnType &n) { |
| 149 | + casted_returned.push_back(std::get<double>(n)); |
| 150 | + }); |
| 151 | + return casted_returned; |
| 152 | + } |
| 153 | + |
| 154 | + std::vector<std::string> getStringVector(std::string str) override { |
| 155 | + std::string strings = ""; |
| 156 | + strings = std::get<std::string>(this->get(str, strings)); |
| 157 | + std::vector<ReturnType> returned = this->parse(strings); |
| 158 | + std::vector<std::string> casted_returned; |
| 159 | + std::for_each( |
| 160 | + returned.begin(), returned.end(), [&](Data::ReturnType &n) { |
| 161 | + casted_returned.push_back(std::get<std::string>(n)); |
| 162 | + }); |
| 163 | + return casted_returned; |
| 164 | + } |
123 | 165 | }; |
124 | 166 | } // namespace Data |
125 | 167 | #endif |
0 commit comments