diff options
| author | 2017-01-20 21:30:11 +0200 | |
|---|---|---|
| committer | 2017-03-01 23:30:57 +0200 | |
| commit | 8a8c0f348b0da4fd91c846211ede568a1f0f11c8 (patch) | |
| tree | f80158deb9edba170bf73ddfe93326d02976ad09 /src | |
| parent | Merge pull request #2603 from wwylele/please-signal (diff) | |
| download | yuzu-8a8c0f348b0da4fd91c846211ede568a1f0f11c8.tar.gz yuzu-8a8c0f348b0da4fd91c846211ede568a1f0f11c8.tar.xz yuzu-8a8c0f348b0da4fd91c846211ede568a1f0f11c8.zip | |
Common: add ParamPackage
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/param_package.cpp | 120 | ||||
| -rw-r--r-- | src/common/param_package.h | 40 | ||||
| -rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/tests/common/param_package.cpp | 25 |
5 files changed, 188 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 8a6170257..13277a5c2 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -35,6 +35,7 @@ set(SRCS | |||
| 35 | memory_util.cpp | 35 | memory_util.cpp |
| 36 | microprofile.cpp | 36 | microprofile.cpp |
| 37 | misc.cpp | 37 | misc.cpp |
| 38 | param_package.cpp | ||
| 38 | scm_rev.cpp | 39 | scm_rev.cpp |
| 39 | string_util.cpp | 40 | string_util.cpp |
| 40 | symbols.cpp | 41 | symbols.cpp |
| @@ -66,6 +67,7 @@ set(HEADERS | |||
| 66 | memory_util.h | 67 | memory_util.h |
| 67 | microprofile.h | 68 | microprofile.h |
| 68 | microprofileui.h | 69 | microprofileui.h |
| 70 | param_package.h | ||
| 69 | platform.h | 71 | platform.h |
| 70 | quaternion.h | 72 | quaternion.h |
| 71 | scm_rev.h | 73 | scm_rev.h |
diff --git a/src/common/param_package.cpp b/src/common/param_package.cpp new file mode 100644 index 000000000..3a6ef8c27 --- /dev/null +++ b/src/common/param_package.cpp | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <vector> | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "common/param_package.h" | ||
| 9 | #include "common/string_util.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | constexpr char KEY_VALUE_SEPARATOR = ':'; | ||
| 14 | constexpr char PARAM_SEPARATOR = ','; | ||
| 15 | constexpr char ESCAPE_CHARACTER = '$'; | ||
| 16 | const std::string KEY_VALUE_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '0'}; | ||
| 17 | const std::string PARAM_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '1'}; | ||
| 18 | const std::string ESCAPE_CHARACTER_ESCAPE{ESCAPE_CHARACTER, '2'}; | ||
| 19 | |||
| 20 | ParamPackage::ParamPackage(const std::string& serialized) { | ||
| 21 | std::vector<std::string> pairs; | ||
| 22 | Common::SplitString(serialized, PARAM_SEPARATOR, pairs); | ||
| 23 | |||
| 24 | for (const std::string& pair : pairs) { | ||
| 25 | std::vector<std::string> key_value; | ||
| 26 | Common::SplitString(pair, KEY_VALUE_SEPARATOR, key_value); | ||
| 27 | if (key_value.size() != 2) { | ||
| 28 | LOG_ERROR(Common, "invalid key pair %s", pair.c_str()); | ||
| 29 | continue; | ||
| 30 | } | ||
| 31 | |||
| 32 | for (std::string& part : key_value) { | ||
| 33 | part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR}); | ||
| 34 | part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR}); | ||
| 35 | part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER}); | ||
| 36 | } | ||
| 37 | |||
| 38 | Set(key_value[0], key_value[1]); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {} | ||
| 43 | |||
| 44 | std::string ParamPackage::Serialize() const { | ||
| 45 | if (data.empty()) | ||
| 46 | return ""; | ||
| 47 | |||
| 48 | std::string result; | ||
| 49 | |||
| 50 | for (const auto& pair : data) { | ||
| 51 | std::array<std::string, 2> key_value{{pair.first, pair.second}}; | ||
| 52 | for (std::string& part : key_value) { | ||
| 53 | part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE); | ||
| 54 | part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE); | ||
| 55 | part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE); | ||
| 56 | } | ||
| 57 | result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR; | ||
| 58 | } | ||
| 59 | |||
| 60 | result.pop_back(); // discard the trailing PARAM_SEPARATOR | ||
| 61 | return result; | ||
| 62 | } | ||
| 63 | |||
| 64 | std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const { | ||
| 65 | auto pair = data.find(key); | ||
| 66 | if (pair == data.end()) { | ||
| 67 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 68 | return default_value; | ||
| 69 | } | ||
| 70 | |||
| 71 | return pair->second; | ||
| 72 | } | ||
| 73 | |||
| 74 | int ParamPackage::Get(const std::string& key, int default_value) const { | ||
| 75 | auto pair = data.find(key); | ||
| 76 | if (pair == data.end()) { | ||
| 77 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 78 | return default_value; | ||
| 79 | } | ||
| 80 | |||
| 81 | try { | ||
| 82 | return std::stoi(pair->second); | ||
| 83 | } catch (const std::logic_error&) { | ||
| 84 | LOG_ERROR(Common, "failed to convert %s to int", pair->second.c_str()); | ||
| 85 | return default_value; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | float ParamPackage::Get(const std::string& key, float default_value) const { | ||
| 90 | auto pair = data.find(key); | ||
| 91 | if (pair == data.end()) { | ||
| 92 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 93 | return default_value; | ||
| 94 | } | ||
| 95 | |||
| 96 | try { | ||
| 97 | return std::stof(pair->second); | ||
| 98 | } catch (const std::logic_error&) { | ||
| 99 | LOG_ERROR(Common, "failed to convert %s to float", pair->second.c_str()); | ||
| 100 | return default_value; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | void ParamPackage::Set(const std::string& key, const std::string& value) { | ||
| 105 | data[key] = value; | ||
| 106 | } | ||
| 107 | |||
| 108 | void ParamPackage::Set(const std::string& key, int value) { | ||
| 109 | data[key] = std::to_string(value); | ||
| 110 | } | ||
| 111 | |||
| 112 | void ParamPackage::Set(const std::string& key, float value) { | ||
| 113 | data[key] = std::to_string(value); | ||
| 114 | } | ||
| 115 | |||
| 116 | bool ParamPackage::Has(const std::string& key) const { | ||
| 117 | return data.find(key) != data.end(); | ||
| 118 | } | ||
| 119 | |||
| 120 | } // namespace Common | ||
diff --git a/src/common/param_package.h b/src/common/param_package.h new file mode 100644 index 000000000..c4c11b221 --- /dev/null +++ b/src/common/param_package.h | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <initializer_list> | ||
| 8 | #include <string> | ||
| 9 | #include <unordered_map> | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | /// A string-based key-value container supporting serializing to and deserializing from a string | ||
| 14 | class ParamPackage { | ||
| 15 | public: | ||
| 16 | using DataType = std::unordered_map<std::string, std::string>; | ||
| 17 | |||
| 18 | ParamPackage() = default; | ||
| 19 | explicit ParamPackage(const std::string& serialized); | ||
| 20 | ParamPackage(std::initializer_list<DataType::value_type> list); | ||
| 21 | ParamPackage(const ParamPackage& other) = default; | ||
| 22 | ParamPackage(ParamPackage&& other) = default; | ||
| 23 | |||
| 24 | ParamPackage& operator=(const ParamPackage& other) = default; | ||
| 25 | ParamPackage& operator=(ParamPackage&& other) = default; | ||
| 26 | |||
| 27 | std::string Serialize() const; | ||
| 28 | std::string Get(const std::string& key, const std::string& default_value) const; | ||
| 29 | int Get(const std::string& key, int default_value) const; | ||
| 30 | float Get(const std::string& key, float default_value) const; | ||
| 31 | void Set(const std::string& key, const std::string& value); | ||
| 32 | void Set(const std::string& key, int value); | ||
| 33 | void Set(const std::string& key, float value); | ||
| 34 | bool Has(const std::string& key) const; | ||
| 35 | |||
| 36 | private: | ||
| 37 | DataType data; | ||
| 38 | }; | ||
| 39 | |||
| 40 | } // namespace Common | ||
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index b47156ca4..d1144ba77 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | set(SRCS | 1 | set(SRCS |
| 2 | glad.cpp | 2 | glad.cpp |
| 3 | tests.cpp | 3 | tests.cpp |
| 4 | common/param_package.cpp | ||
| 4 | core/file_sys/path_parser.cpp | 5 | core/file_sys/path_parser.cpp |
| 5 | ) | 6 | ) |
| 6 | 7 | ||
diff --git a/src/tests/common/param_package.cpp b/src/tests/common/param_package.cpp new file mode 100644 index 000000000..efec2cc86 --- /dev/null +++ b/src/tests/common/param_package.cpp | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <catch.hpp> | ||
| 6 | #include <math.h> | ||
| 7 | #include "common/param_package.h" | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | TEST_CASE("ParamPackage", "[common]") { | ||
| 12 | ParamPackage original{ | ||
| 13 | {"abc", "xyz"}, {"def", "42"}, {"jkl", "$$:1:$2$,3"}, | ||
| 14 | }; | ||
| 15 | original.Set("ghi", 3.14f); | ||
| 16 | ParamPackage copy(original.Serialize()); | ||
| 17 | REQUIRE(copy.Get("abc", "") == "xyz"); | ||
| 18 | REQUIRE(copy.Get("def", 0) == 42); | ||
| 19 | REQUIRE(std::abs(copy.Get("ghi", 0.0f) - 3.14f) < 0.01f); | ||
| 20 | REQUIRE(copy.Get("jkl", "") == "$$:1:$2$,3"); | ||
| 21 | REQUIRE(copy.Get("mno", "uvw") == "uvw"); | ||
| 22 | REQUIRE(copy.Get("abc", 42) == 42); | ||
| 23 | } | ||
| 24 | |||
| 25 | } // namespace Common | ||