From beb1f3969e13af72bd9098d484b693e397cf7235 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 3 Jun 2021 12:17:46 -0700 Subject: Dictionary value (string key) --- core/src/Model/Workflow/Values/Dictionary.cpp | 49 +++++++++++++++++++++++++++ core/src/Model/Workflow/Values/Dictionary.hpp | 25 ++++++++++++++ core/src/Model/Workflow/Values/fwd.hpp | 3 ++ 3 files changed, 77 insertions(+) create mode 100644 core/src/Model/Workflow/Values/Dictionary.cpp create mode 100644 core/src/Model/Workflow/Values/Dictionary.hpp (limited to 'core/src/Model/Workflow/Values') diff --git a/core/src/Model/Workflow/Values/Dictionary.cpp b/core/src/Model/Workflow/Values/Dictionary.cpp new file mode 100644 index 0000000..106e48d --- /dev/null +++ b/core/src/Model/Workflow/Values/Dictionary.cpp @@ -0,0 +1,49 @@ +#include "Dictionary.hpp" + +#include "Utils/Macros.hpp" + +bool DictionaryValue::IsInstance(const BaseValue* value) +{ + return value->GetKind() == KD_Dictionary; +} + +DictionaryValue::DictionaryValue() + : BaseValue(KD_Dictionary) +{ +} + +int DictionaryValue::GetCount() const +{ + return mElements.size(); +} + +BaseValue* DictionaryValue::Find(std::string_view key) +{ + auto iter = mElements.find(key); + if (iter != mElements.end()) { + return iter.value().get(); + } else { + return nullptr; + } +} + +BaseValue* DictionaryValue::Insert(std::string_view key, std::unique_ptr& value) +{ + auto [iter, success] = mElements.insert(key, std::move(value)); + if (success) { + return iter.value().get(); + } else { + return nullptr; + } +} + +BaseValue& DictionaryValue::InsertOrReplace(std::string_view key, std::unique_ptr value) +{ + auto [iter, DISCARD] = mElements.emplace(key, std::move(value)); + return *iter.value(); +} + +void DictionaryValue::Remove(std::string_view key) +{ + mElements.erase(mElements.find(key)); +} diff --git a/core/src/Model/Workflow/Values/Dictionary.hpp b/core/src/Model/Workflow/Values/Dictionary.hpp new file mode 100644 index 0000000..65ea82f --- /dev/null +++ b/core/src/Model/Workflow/Values/Dictionary.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "Model/Workflow/Value.hpp" + +#include +#include +#include +#include + +class DictionaryValue : public BaseValue +{ +private: + tsl::array_map> mElements; + +public: + static bool IsInstance(const BaseValue* value); + DictionaryValue(); + + int GetCount() const; + BaseValue* Find(std::string_view key); + + BaseValue* Insert(std::string_view key, std::unique_ptr& value); + BaseValue& InsertOrReplace(std::string_view key, std::unique_ptr value); + void Remove(std::string_view key); +}; diff --git a/core/src/Model/Workflow/Values/fwd.hpp b/core/src/Model/Workflow/Values/fwd.hpp index de26226..51a04e9 100644 --- a/core/src/Model/Workflow/Values/fwd.hpp +++ b/core/src/Model/Workflow/Values/fwd.hpp @@ -10,5 +10,8 @@ class DatabaseRowIdValue; class SaleDatabaseRowValue; class PurchaseDatabaseRowValue; +// Dictionary.hpp +class DictionaryValue; + // List.hpp class ListValue; -- cgit v1.2.3-70-g09d2