aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Model/Workflow/Values
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-30 21:38:53 -0700
committerrtk0c <[email protected]>2022-06-30 21:38:53 -0700
commit7fe47a9d5b1727a61dc724523b530762f6d6ba19 (patch)
treee95be6e66db504ed06d00b72c579565bab873277 /app/source/Cplt/Model/Workflow/Values
parent2cf952088d375ac8b2f45b144462af0953436cff (diff)
Restructure project
Diffstat (limited to 'app/source/Cplt/Model/Workflow/Values')
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Basic.cpp111
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Basic.hpp67
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Database.cpp88
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Database.hpp51
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Dictionary.cpp49
-rw-r--r--app/source/Cplt/Model/Workflow/Values/Dictionary.hpp25
-rw-r--r--app/source/Cplt/Model/Workflow/Values/List.cpp100
-rw-r--r--app/source/Cplt/Model/Workflow/Values/List.hpp50
-rw-r--r--app/source/Cplt/Model/Workflow/Values/fwd.hpp17
9 files changed, 558 insertions, 0 deletions
diff --git a/app/source/Cplt/Model/Workflow/Values/Basic.cpp b/app/source/Cplt/Model/Workflow/Values/Basic.cpp
new file mode 100644
index 0000000..198387c
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Basic.cpp
@@ -0,0 +1,111 @@
+#include "Basic.hpp"
+
+#include <charconv>
+#include <cmath>
+#include <limits>
+
+bool NumericValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_Numeric;
+}
+
+NumericValue::NumericValue()
+ : BaseValue(BaseValue::KD_Numeric)
+{
+}
+
+template <class T, int kMaxSize>
+static std::string NumberToString(T value)
+{
+ char buf[kMaxSize];
+ auto res = std::to_chars(buf, buf + kMaxSize, value);
+ if (res.ec == std::errc()) {
+ return std::string(buf, res.ptr);
+ } else {
+ return "<err>";
+ }
+}
+
+std::string NumericValue::GetTruncatedString() const
+{
+ constexpr auto kMaxSize = std::numeric_limits<int64_t>::digits10;
+ return ::NumberToString<int64_t, kMaxSize>((int64_t)mValue);
+}
+
+std::string NumericValue::GetRoundedString() const
+{
+ constexpr auto kMaxSize = std::numeric_limits<int64_t>::digits10;
+ return ::NumberToString<int64_t, kMaxSize>((int64_t)std::round(mValue));
+}
+
+std::string NumericValue::GetString() const
+{
+ constexpr auto kMaxSize = std::numeric_limits<double>::max_digits10;
+ return ::NumberToString<double, kMaxSize>(mValue);
+}
+
+int64_t NumericValue::GetInt() const
+{
+ return static_cast<int64_t>(mValue);
+}
+
+double NumericValue::GetValue() const
+{
+ return mValue;
+}
+
+void NumericValue::SetValue(double value)
+{
+ mValue = value;
+}
+
+bool TextValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_Text;
+}
+
+TextValue::TextValue()
+ : BaseValue(BaseValue::KD_Text)
+{
+}
+
+const std::string& TextValue::GetValue() const
+{
+ return mValue;
+}
+
+void TextValue::SetValue(const std::string& value)
+{
+ mValue = value;
+}
+
+bool DateTimeValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_DateTime;
+}
+
+DateTimeValue::DateTimeValue()
+ : BaseValue(BaseValue::KD_DateTime)
+{
+}
+
+std::string DateTimeValue::GetString() const
+{
+ namespace chrono = std::chrono;
+ auto t = chrono::system_clock::to_time_t(mValue);
+
+ char data[32];
+ std::strftime(data, sizeof(data), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
+
+ return std::string(data);
+}
+
+const std::chrono::time_point<std::chrono::system_clock>& DateTimeValue::GetValue() const
+{
+ return mValue;
+}
+
+void DateTimeValue::SetValue(const std::chrono::time_point<std::chrono::system_clock>& value)
+{
+ mValue = value;
+}
diff --git a/app/source/Cplt/Model/Workflow/Values/Basic.hpp b/app/source/Cplt/Model/Workflow/Values/Basic.hpp
new file mode 100644
index 0000000..820fb13
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Basic.hpp
@@ -0,0 +1,67 @@
+#pragma once
+
+#include <Cplt/Model/Workflow/Value.hpp>
+
+#include <chrono>
+#include <cstdint>
+#include <string>
+
+class NumericValue : public BaseValue
+{
+private:
+ double mValue;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ NumericValue();
+
+ NumericValue(const NumericValue&) = delete;
+ NumericValue& operator=(const NumericValue&) = delete;
+ NumericValue(NumericValue&&) = default;
+ NumericValue& operator=(NumericValue&&) = default;
+
+ std::string GetTruncatedString() const;
+ std::string GetRoundedString() const;
+ std::string GetString() const;
+
+ int64_t GetInt() const;
+ double GetValue() const;
+ void SetValue(double value);
+};
+
+class TextValue : public BaseValue
+{
+private:
+ std::string mValue;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ TextValue();
+
+ TextValue(const TextValue&) = delete;
+ TextValue& operator=(const TextValue&) = delete;
+ TextValue(TextValue&&) = default;
+ TextValue& operator=(TextValue&&) = default;
+
+ const std::string& GetValue() const;
+ void SetValue(const std::string& value);
+};
+
+class DateTimeValue : public BaseValue
+{
+private:
+ std::chrono::time_point<std::chrono::system_clock> mValue;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ DateTimeValue();
+
+ DateTimeValue(const DateTimeValue&) = delete;
+ DateTimeValue& operator=(const DateTimeValue&) = delete;
+ DateTimeValue(DateTimeValue&&) = default;
+ DateTimeValue& operator=(DateTimeValue&&) = default;
+
+ std::string GetString() const;
+ const std::chrono::time_point<std::chrono::system_clock>& GetValue() const;
+ void SetValue(const std::chrono::time_point<std::chrono::system_clock>& value);
+};
diff --git a/app/source/Cplt/Model/Workflow/Values/Database.cpp b/app/source/Cplt/Model/Workflow/Values/Database.cpp
new file mode 100644
index 0000000..25b77e9
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Database.cpp
@@ -0,0 +1,88 @@
+#include "Database.hpp"
+
+#include <Cplt/Model/Database.hpp>
+#include <Cplt/Model/Workflow/ValueInternals.hpp>
+
+#include <limits>
+
+TableKind DatabaseRowIdValue::GetTable() const
+{
+ return mTable;
+}
+
+int64_t DatabaseRowIdValue::GetRowId() const
+{
+ return mRowId;
+}
+
+bool DatabaseRowIdValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_DatabaseRowId;
+}
+
+DatabaseRowIdValue::DatabaseRowIdValue()
+ : BaseValue(KD_DatabaseRowId)
+ , mTable{ TableKind::Sales }
+ , mRowId{ std::numeric_limits<int64_t>::max() }
+{
+}
+
+bool SaleDatabaseRowValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_SaleDatabaseRow;
+}
+
+SaleDatabaseRowValue::SaleDatabaseRowValue()
+ : BaseObjectValue(KD_SaleDatabaseRow)
+{
+}
+
+const BaseValue* SaleDatabaseRowValue::GetProperty(int idx) const
+{
+ switch (idx) {
+ case 0: return &mCustomerName;
+ case 1: return &mDeadline;
+ case 2: return &mDeliveryTime;
+ default: return nullptr;
+ }
+}
+
+bool SaleDatabaseRowValue::SetProperty(int idx, std::unique_ptr<BaseValue> value)
+{
+ switch (idx) {
+ case 0: return false;
+ case 1: CHECK_VALUE_TYPE_AND_MOVE(DateTimeValue, mDeadline, value.get()); break;
+ case 2: CHECK_VALUE_TYPE_AND_MOVE(DateTimeValue, mDeliveryTime, value.get()); break;
+ }
+ return true;
+}
+
+bool PurchaseDatabaseRowValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_PurchaseDatabaseRow;
+}
+
+PurchaseDatabaseRowValue::PurchaseDatabaseRowValue()
+ : BaseObjectValue(KD_PurchaseDatabaseRow)
+{
+}
+
+const BaseValue* PurchaseDatabaseRowValue::GetProperty(int idx) const
+{
+ switch (idx) {
+ case 0: return &mFactoryName;
+ case 1: return &mOrderTime;
+ case 2: return &mDeliveryTime;
+ default: return nullptr;
+ }
+}
+
+bool PurchaseDatabaseRowValue::SetProperty(int idx, std::unique_ptr<BaseValue> value)
+{
+ switch (idx) {
+ case 0: return false;
+ case 1: CHECK_VALUE_TYPE_AND_MOVE(DateTimeValue, mOrderTime, value.get()); break;
+ case 2: CHECK_VALUE_TYPE_AND_MOVE(DateTimeValue, mDeliveryTime, value.get()); break;
+ }
+ return true;
+}
diff --git a/app/source/Cplt/Model/Workflow/Values/Database.hpp b/app/source/Cplt/Model/Workflow/Values/Database.hpp
new file mode 100644
index 0000000..f1c1571
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Database.hpp
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <Cplt/Model/Workflow/Value.hpp>
+#include <Cplt/Model/Workflow/Values/Basic.hpp>
+#include <Cplt/fwd.hpp>
+
+class DatabaseRowIdValue : public BaseValue
+{
+private:
+ TableKind mTable;
+ int64_t mRowId;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ DatabaseRowIdValue();
+
+ TableKind GetTable() const;
+ int64_t GetRowId() const;
+};
+
+class SaleDatabaseRowValue : public BaseObjectValue
+{
+private:
+ int mCustomerId;
+ TextValue mCustomerName;
+ DateTimeValue mDeadline;
+ DateTimeValue mDeliveryTime;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ SaleDatabaseRowValue();
+
+ virtual const BaseValue* GetProperty(int idx) const;
+ virtual bool SetProperty(int idx, std::unique_ptr<BaseValue> value);
+};
+
+class PurchaseDatabaseRowValue : public BaseObjectValue
+{
+private:
+ int mFactoryId;
+ TextValue mFactoryName;
+ DateTimeValue mOrderTime;
+ DateTimeValue mDeliveryTime;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ PurchaseDatabaseRowValue();
+
+ virtual const BaseValue* GetProperty(int idx) const;
+ virtual bool SetProperty(int idx, std::unique_ptr<BaseValue> value);
+};
diff --git a/app/source/Cplt/Model/Workflow/Values/Dictionary.cpp b/app/source/Cplt/Model/Workflow/Values/Dictionary.cpp
new file mode 100644
index 0000000..97bf509
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Dictionary.cpp
@@ -0,0 +1,49 @@
+#include "Dictionary.hpp"
+
+#include <Cplt/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<BaseValue>& 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<BaseValue> 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/app/source/Cplt/Model/Workflow/Values/Dictionary.hpp b/app/source/Cplt/Model/Workflow/Values/Dictionary.hpp
new file mode 100644
index 0000000..6eff308
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/Dictionary.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <Cplt/Model/Workflow/Value.hpp>
+
+#include <tsl/array_map.h>
+#include <memory>
+#include <string>
+#include <string_view>
+
+class DictionaryValue : public BaseValue
+{
+private:
+ tsl::array_map<char, std::unique_ptr<BaseValue>> 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<BaseValue>& value);
+ BaseValue& InsertOrReplace(std::string_view key, std::unique_ptr<BaseValue> value);
+ void Remove(std::string_view key);
+};
diff --git a/app/source/Cplt/Model/Workflow/Values/List.cpp b/app/source/Cplt/Model/Workflow/Values/List.cpp
new file mode 100644
index 0000000..9fd6bfd
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/List.cpp
@@ -0,0 +1,100 @@
+#include "List.hpp"
+
+#include <utility>
+
+BaseValue* ListValue::Iterator::operator*() const
+{
+ return mIter->get();
+}
+
+BaseValue* ListValue::Iterator::operator->() const
+{
+ return mIter->get();
+}
+
+ListValue::Iterator& ListValue::Iterator::operator++()
+{
+ ++mIter;
+ return *this;
+}
+
+ListValue::Iterator ListValue::Iterator::operator++(int) const
+{
+ return Iterator(mIter + 1);
+}
+
+ListValue::Iterator& ListValue::Iterator::operator--()
+{
+ --mIter;
+ return *this;
+}
+
+ListValue::Iterator ListValue::Iterator::operator--(int) const
+{
+ return Iterator(mIter - 1);
+}
+
+bool operator==(const ListValue::Iterator& a, const ListValue::Iterator& b)
+{
+ return a.mIter == b.mIter;
+}
+
+ListValue::Iterator::Iterator(decltype(mIter) iter)
+ : mIter{ iter }
+{
+}
+
+bool ListValue::IsInstance(const BaseValue* value)
+{
+ return value->GetKind() == KD_List;
+}
+
+ListValue::ListValue()
+ : BaseValue(KD_List)
+{
+}
+
+int ListValue::GetCount() const
+{
+ return mElements.size();
+}
+
+BaseValue* ListValue::GetElement(int i) const
+{
+ return mElements[i].get();
+}
+
+void ListValue::Append(std::unique_ptr<BaseValue> element)
+{
+ mElements.push_back(std::move(element));
+}
+
+void ListValue::Insert(int i, std::unique_ptr<BaseValue> element)
+{
+ mElements.insert(mElements.begin() + i, std::move(element));
+}
+
+void ListValue::Insert(Iterator iter, std::unique_ptr<BaseValue> element)
+{
+ mElements.insert(iter.mIter, std::move(element));
+}
+
+void ListValue::Remove(int i)
+{
+ mElements.erase(mElements.begin() + i);
+}
+
+void ListValue::Remove(Iterator iter)
+{
+ mElements.erase(iter.mIter);
+}
+
+ListValue::Iterator ListValue::begin()
+{
+ return Iterator(mElements.begin());
+}
+
+ListValue::Iterator ListValue::end()
+{
+ return Iterator(mElements.end());
+}
diff --git a/app/source/Cplt/Model/Workflow/Values/List.hpp b/app/source/Cplt/Model/Workflow/Values/List.hpp
new file mode 100644
index 0000000..cc8e061
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/List.hpp
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <Cplt/Model/Workflow/Value.hpp>
+
+#include <memory>
+#include <vector>
+
+class ListValue : public BaseValue
+{
+public:
+ class Iterator
+ {
+ private:
+ std::vector<std::unique_ptr<BaseValue>>::iterator mIter;
+
+ public:
+ BaseValue* operator*() const;
+ BaseValue* operator->() const;
+
+ Iterator& operator++();
+ Iterator operator++(int) const;
+ Iterator& operator--();
+ Iterator operator--(int) const;
+
+ friend bool operator==(const Iterator& a, const Iterator& b);
+
+ private:
+ friend class ListValue;
+ Iterator(decltype(mIter) iter);
+ };
+
+private:
+ std::vector<std::unique_ptr<BaseValue>> mElements;
+
+public:
+ static bool IsInstance(const BaseValue* value);
+ ListValue();
+
+ int GetCount() const;
+ BaseValue* GetElement(int i) const;
+
+ void Append(std::unique_ptr<BaseValue> element);
+ void Insert(int i, std::unique_ptr<BaseValue> element);
+ void Insert(Iterator iter, std::unique_ptr<BaseValue> element);
+ void Remove(int i);
+ void Remove(Iterator iter);
+
+ Iterator begin();
+ Iterator end();
+};
diff --git a/app/source/Cplt/Model/Workflow/Values/fwd.hpp b/app/source/Cplt/Model/Workflow/Values/fwd.hpp
new file mode 100644
index 0000000..51a04e9
--- /dev/null
+++ b/app/source/Cplt/Model/Workflow/Values/fwd.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+// Basic.hpp
+class NumericValue;
+class TextValue;
+class DateTimeValue;
+
+// Database.hpp
+class DatabaseRowIdValue;
+class SaleDatabaseRowValue;
+class PurchaseDatabaseRowValue;
+
+// Dictionary.hpp
+class DictionaryValue;
+
+// List.hpp
+class ListValue;