common/param_package: Implement lists and nested data structures.

Implements the handling of arbitrarily complex data structures.
The following are supported:
- Lists (like `key:[val1|val2|...]`)
- Dictionaries/Maps (like `key:[k1:v1,k2:[v2a,v2b]]`)

Lists of Dictionaries/Maps look a bit different:
```
key:[k1:v1,k2:[v2a,v2b]|a:x,b:y]
     \_______________/  \_____/
           dict1         dict2
```
This commit is contained in:
adityaruplaha
2020-06-30 23:25:08 +05:30
parent c4a4b40b2d
commit eebb545b40
2 changed files with 248 additions and 1 deletions

View File

@@ -7,6 +7,7 @@
#include <initializer_list>
#include <map>
#include <string>
#include <vector>
namespace Common {
@@ -25,12 +26,30 @@ public:
ParamPackage& operator=(ParamPackage&& other) = default;
std::string Serialize() const;
// Getters
std::string Get(const std::string& key, const std::string& default_value) const;
std::vector<std::string> Get(const std::string& key,
const std::vector<std::string>& default_value) const;
int Get(const std::string& key, int default_value) const;
std::vector<int> Get(const std::string& key, const std::vector<int>& default_value) const;
float Get(const std::string& key, float default_value) const;
std::vector<float> Get(const std::string& key, const std::vector<float>& default_value) const;
ParamPackage Get(const std::string& key, const ParamPackage& default_value) const;
std::vector<ParamPackage> Get(const std::string& key,
const std::vector<ParamPackage>& default_value) const;
// Setters
void Set(const std::string& key, std::string value);
void Set(const std::string& key, std::vector<std::string> value);
void Set(const std::string& key, int value);
void Set(const std::string& key, std::vector<int> value);
void Set(const std::string& key, float value);
void Set(const std::string& key, std::vector<float> value);
void Set(const std::string& key, ParamPackage value);
void Set(const std::string& key, std::vector<ParamPackage> value);
// Other methods
bool Has(const std::string& key) const;
void Erase(const std::string& key);
void Clear();
@@ -43,6 +62,24 @@ public:
private:
DataType data;
/**
* Replace complex data structures with placeholders, and generate the lookup table.
* The lookup table is emptied before being used.
*/
static std::string PlaceholderifyData(const std::string& str, std::vector<std::string>& lookup);
/**
* Replace placeholders (in-place) with original data obtained from a lookup table.
*/
static void ReplacePlaceholders(std::string& str, const std::vector<std::string>& lookup);
/**
* Replace placeholders with original data obtained from a lookup table and returns the
* resultant string.
*/
static std::string ReplacePlaceholders(const std::string& str,
const std::vector<std::string>& lookup);
};
} // namespace Common