diff options
-rw-r--r-- | include/cru/base/PropertyTree.h | 47 | ||||
-rw-r--r-- | src/base/PropertyTree.cpp | 30 | ||||
-rw-r--r-- | test/base/PropertyTreeTest.cpp | 22 |
3 files changed, 46 insertions, 53 deletions
diff --git a/include/cru/base/PropertyTree.h b/include/cru/base/PropertyTree.h index 54e185b9..8eecc3b9 100644 --- a/include/cru/base/PropertyTree.h +++ b/include/cru/base/PropertyTree.h @@ -1,8 +1,9 @@ #pragma once #include "Base.h" -#include "String.h" +#include <string> +#include <string_view> #include <unordered_map> namespace cru { @@ -10,54 +11,44 @@ class PropertyTree; class CRU_BASE_API PropertySubTreeRef { public: - static String CombineKey(StringView left, StringView right); + static std::string CombineKey(std::string_view left, std::string_view right); - explicit PropertySubTreeRef(PropertyTree* tree, String path = {}); - - CRU_DEFAULT_COPY(PropertySubTreeRef); - CRU_DEFAULT_MOVE(PropertySubTreeRef); - - CRU_DEFAULT_DESTRUCTOR(PropertySubTreeRef); + explicit PropertySubTreeRef(PropertyTree* tree, std::string path = {}); public: PropertyTree* GetTree() const { return tree_; } - String GetPath() const { return path_; } - void SetPath(String path) { path_ = std::move(path); } + std::string GetPath() const { return path_; } + void SetPath(std::string path) { path_ = std::move(path); } PropertySubTreeRef GetParent() const; - PropertySubTreeRef GetChild(const String& key) const; + PropertySubTreeRef GetChild(const std::string& key) const; - String GetValue(const String& key) const; - void SetValue(const String& key, String value); - void DeleteValue(const String& key); + std::string GetValue(const std::string& key) const; + void SetValue(const std::string& key, std::string value); + void DeleteValue(const std::string& key); private: PropertyTree* tree_; - String path_; + std::string path_; }; -class CRU_BASE_API PropertyTree { +class CRU_BASE_API PropertyTree : public Object2 { public: - static String CombineKey(StringView left, StringView right); + static std::string CombineKey(std::string_view left, std::string_view right); PropertyTree() = default; - explicit PropertyTree(std::unordered_map<String, String> values); - - CRU_DELETE_COPY(PropertyTree); - CRU_DELETE_MOVE(PropertyTree); - - CRU_DEFAULT_DESTRUCTOR(PropertyTree); + explicit PropertyTree(std::unordered_map<std::string, std::string> values); public: - String GetValue(const String& key) const; - void SetValue(const String& key, String value); - void DeleteValue(const String& key); + std::string GetValue(const std::string& key) const; + void SetValue(const std::string& key, std::string value); + void DeleteValue(const std::string& key); - PropertySubTreeRef GetSubTreeRef(const String& path); + PropertySubTreeRef GetSubTreeRef(const std::string& path); private: - std::unordered_map<String, String> values_; + std::unordered_map<std::string, std::string> values_; }; } // namespace cru diff --git a/src/base/PropertyTree.cpp b/src/base/PropertyTree.cpp index 8303a706..db17f84f 100644 --- a/src/base/PropertyTree.cpp +++ b/src/base/PropertyTree.cpp @@ -1,13 +1,13 @@ #include "cru/base/PropertyTree.h" -#include <unordered_map> #include "cru/base/Exception.h" namespace cru { -String PropertySubTreeRef::CombineKey(StringView left, StringView right) { +std::string PropertySubTreeRef::CombineKey(std::string_view left, + std::string_view right) { return PropertyTree::CombineKey(left, right); } -PropertySubTreeRef::PropertySubTreeRef(PropertyTree* tree, String path) +PropertySubTreeRef::PropertySubTreeRef(PropertyTree* tree, std::string path) : tree_(tree), path_(std::move(path)) { Expects(tree); } @@ -22,30 +22,32 @@ PropertySubTreeRef PropertySubTreeRef::GetParent() const { return PropertySubTreeRef(tree_, {}); } -PropertySubTreeRef PropertySubTreeRef::GetChild(const String& key) const { +PropertySubTreeRef PropertySubTreeRef::GetChild(const std::string& key) const { return PropertySubTreeRef(tree_, CombineKey(path_, key)); } -String PropertySubTreeRef::GetValue(const String& key) const { +std::string PropertySubTreeRef::GetValue(const std::string& key) const { return tree_->GetValue(CombineKey(path_, key)); } -void PropertySubTreeRef::SetValue(const String& key, String value) { +void PropertySubTreeRef::SetValue(const std::string& key, std::string value) { tree_->SetValue(CombineKey(path_, key), std::move(value)); } -void PropertySubTreeRef::DeleteValue(const String& key) { +void PropertySubTreeRef::DeleteValue(const std::string& key) { tree_->DeleteValue(CombineKey(path_, key)); } -String PropertyTree::CombineKey(StringView left, StringView right) { - return String(left) + String(left.empty() ? u"" : u".") + String(right); +std::string PropertyTree::CombineKey(std::string_view left, + std::string_view right) { + return std::string(left) + std::string(left.empty() ? "" : ".") + + std::string(right); } -PropertyTree::PropertyTree(std::unordered_map<String, String> values) +PropertyTree::PropertyTree(std::unordered_map<std::string, std::string> values) : values_(std::move(values)) {} -String PropertyTree::GetValue(const String& key) const { +std::string PropertyTree::GetValue(const std::string& key) const { auto it = values_.find(key); if (it == values_.end()) { throw Exception(u"Property tree has no value."); @@ -53,18 +55,18 @@ String PropertyTree::GetValue(const String& key) const { return it->second; } -void PropertyTree::SetValue(const String& key, String value) { +void PropertyTree::SetValue(const std::string& key, std::string value) { values_[key] = std::move(value); } -void PropertyTree::DeleteValue(const String& key) { +void PropertyTree::DeleteValue(const std::string& key) { auto it = values_.find(key); if (it != values_.end()) { values_.erase(it); } } -PropertySubTreeRef PropertyTree::GetSubTreeRef(const String& path) { +PropertySubTreeRef PropertyTree::GetSubTreeRef(const std::string& path) { return PropertySubTreeRef(this, path); } diff --git a/test/base/PropertyTreeTest.cpp b/test/base/PropertyTreeTest.cpp index 24d7ca9e..644265a9 100644 --- a/test/base/PropertyTreeTest.cpp +++ b/test/base/PropertyTreeTest.cpp @@ -7,20 +7,20 @@ TEST_CASE("PropertyTree", "[property_tree]") { using cru::PropertyTree; PropertyTree tree({ - {u"k1", u"v1"}, - {u"k2", u"v2"}, - {u"k3.sub", u"v3"}, + {"k1", "v1"}, + {"k2", "v2"}, + {"k3.sub", "v3"}, }); - REQUIRE(tree.GetValue(u"k1") == u"v1"); - REQUIRE(tree.GetValue(u"k2") == u"v2"); - REQUIRE(tree.GetValue(u"k3.sub") == u"v3"); + REQUIRE(tree.GetValue("k1") == "v1"); + REQUIRE(tree.GetValue("k2") == "v2"); + REQUIRE(tree.GetValue("k3.sub") == "v3"); - PropertySubTreeRef sub_tree = tree.GetSubTreeRef(u"k3"); - REQUIRE(sub_tree.GetValue(u"sub") == u"v3"); + PropertySubTreeRef sub_tree = tree.GetSubTreeRef("k3"); + REQUIRE(sub_tree.GetValue("sub") == "v3"); PropertySubTreeRef root_tree = sub_tree.GetParent(); - REQUIRE(root_tree.GetValue(u"k1") == u"v1"); - REQUIRE(root_tree.GetValue(u"k2") == u"v2"); - REQUIRE(root_tree.GetValue(u"k3.sub") == u"v3"); + REQUIRE(root_tree.GetValue("k1") == "v1"); + REQUIRE(root_tree.GetValue("k2") == "v2"); + REQUIRE(root_tree.GetValue("k3.sub") == "v3"); } |