aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/thread/DataObject.h
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-10-16 20:19:26 +0000
committersaturneric <[email protected]>2023-10-16 20:19:26 +0000
commit5175b3ae6687839afa2cdfe01f2fd70d714024ed (patch)
tree96eef7e087749c9118d5da7a838fdd81b0e96354 /src/core/thread/DataObject.h
parentfeat: using piml tech on plugin system (diff)
downloadGpgFrontend-5175b3ae6687839afa2cdfe01f2fd70d714024ed.tar.gz
GpgFrontend-5175b3ae6687839afa2cdfe01f2fd70d714024ed.zip
refactor: use c++17 features and piml tech to rewrite DataObject and Task
Diffstat (limited to 'src/core/thread/DataObject.h')
-rw-r--r--src/core/thread/DataObject.h162
1 files changed, 59 insertions, 103 deletions
diff --git a/src/core/thread/DataObject.h b/src/core/thread/DataObject.h
index 885b09ec..af5f0f5f 100644
--- a/src/core/thread/DataObject.h
+++ b/src/core/thread/DataObject.h
@@ -28,110 +28,66 @@
#pragma once
-/**
- * @brief DataObject to be passed to the callback function.
- *
- */
- class GPGFRONTEND_CORE_EXPORT DataObject {
- public:
- struct Destructor {
- const void *p_obj;
- void (*destroy)(const void *);
- };
-
- /**
- * @brief Get the Objects Size
- *
- * @return size_t
- */
- size_t GetObjectSize();
-
- /**
- * @brief
- *
- * @tparam T
- * @param ptr
- */
- template <typename T>
- void AppendObject(T &&obj) {
- SPDLOG_TRACE("append object: {}", static_cast<void *>(this));
- auto *obj_dstr = this->get_heap_ptr(sizeof(T));
- new ((void *)obj_dstr->p_obj) T(std::forward<T>(obj));
-
- if (std::is_class_v<T>) {
- auto destructor = [](const void *x) {
- static_cast<const T *>(x)->~T();
- };
- obj_dstr->destroy = destructor;
- } else {
- obj_dstr->destroy = nullptr;
- }
-
- data_objects_.push(obj_dstr);
- }
+#include <stack>
- /**
- * @brief
- *
- * @tparam T
- * @param ptr
- */
- template <typename T>
- void AppendObject(T *obj) {
- SPDLOG_TRACE("called: {}", static_cast<void *>(this));
- auto *obj_dstr = this->get_heap_ptr(sizeof(T));
- auto *ptr_heap = new ((void *)obj_dstr->p_obj) T(std::move(*obj));
- if (std::is_class_v<T>) {
- SPDLOG_TRACE("is class");
- auto destructor = [](const void *x) {
- static_cast<const T *>(x)->~T();
- };
- obj_dstr->destroy = destructor;
- } else {
- obj_dstr->destroy = nullptr;
- }
- data_objects_.push(std::move(obj_dstr));
- }
+#include "core/GpgFrontendCoreExport.h"
+
+namespace GpgFrontend::Thread {
+
+class DataObject;
+using DataObjectPtr = std::shared_ptr<DataObject>; ///<
+
+class GPGFRONTEND_CORE_EXPORT DataObject {
+ public:
+ DataObject();
+
+ DataObject(std::initializer_list<std::any>);
+
+ ~DataObject();
+
+ DataObject(GpgFrontend::Thread::DataObject&&) noexcept;
+
+ std::any operator[](size_t index) const;
- /**
- * @brief
- *
- * @tparam T
- * @return std::shared_ptr<T>
- */
- template <typename T>
- T PopObject() {
- SPDLOG_TRACE("pop object: {}", static_cast<void *>(this));
- if (data_objects_.empty()) throw std::runtime_error("No object to pop");
- auto *obj_dstr = data_objects_.top();
- auto *heap_ptr = (T *)obj_dstr->p_obj;
- auto obj = std::move(*(T *)(heap_ptr));
- this->free_heap_ptr(obj_dstr);
- data_objects_.pop();
- return obj;
+ void AppendObject(std::any);
+
+ std::any GetParameter(size_t index) const;
+
+ size_t GetObjectSize() const;
+
+ void Swap(DataObject& other) noexcept;
+
+ void Swap(DataObject&& other) noexcept;
+
+ template <typename... Args>
+ bool Check() {
+ if (sizeof...(Args) != GetObjectSize()) return false;
+
+ std::vector<std::type_info const*> type_list = {&typeid(Args)...};
+ for (size_t i = 0; i < type_list.size(); ++i) {
+ if (type_list[i] != &((*this)[i]).type()) return false;
}
+ return true;
+ }
+
+ private:
+ class Impl;
+ std::unique_ptr<Impl> p_;
+};
+
+template <typename... Args>
+std::shared_ptr<DataObject> TransferParams(Args&&... args) {
+ return std::make_shared<DataObject>(DataObject{std::forward<Args>(args)...});
+}
+
+template <typename T>
+T ExtractParams(const std::shared_ptr<DataObject>& d_o, int index) {
+ if (!d_o) {
+ throw std::invalid_argument("nullptr provided for DataObjectPtr");
+ }
+ return std::any_cast<T>(d_o->GetParameter(index));
+}
+
+void swap(DataObject& a, DataObject& b) noexcept;
- /**
- * @brief Destroy the Data Object object
- *
- */
- ~DataObject();
-
- private:
- std::stack<Destructor *> data_objects_; ///<
-
- /**
- * @brief Get the heap ptr object
- *
- * @param bytes_size
- * @return void*
- */
- Destructor *get_heap_ptr(size_t bytes_size);
-
- /**
- * @brief
- *
- * @param heap_ptr
- */
- void free_heap_ptr(Destructor *);
- }; \ No newline at end of file
+} // namespace GpgFrontend::Thread \ No newline at end of file