GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
DataObject.h
1 
29 #pragma once
30 
31 #include <any>
32 #include <typeindex>
33 #include <typeinfo>
34 
35 #include "core/GpgFrontendCoreExport.h"
36 #include "core/utils/MemoryUtils.h"
37 
38 namespace GpgFrontend {
39 
40 class DataObject;
41 using DataObjectPtr = std::shared_ptr<DataObject>;
42 
43 class GPGFRONTEND_CORE_EXPORT DataObject {
44  public:
45  DataObject();
46 
47  DataObject(std::initializer_list<std::any>);
48 
49  ~DataObject();
50 
51  DataObject(DataObject&&) noexcept;
52 
53  auto operator[](size_t index) const -> std::any;
54 
55  void AppendObject(std::any);
56 
57  [[nodiscard]] auto GetParameter(size_t index) const -> std::any;
58 
59  [[nodiscard]] auto GetObjectSize() const -> size_t;
60 
61  void Swap(DataObject& other) noexcept;
62 
63  void Swap(DataObject&& other) noexcept;
64 
65  template <typename... Args>
66  auto Check() -> bool {
67  if (sizeof...(Args) != GetObjectSize()) return false;
68 
69  std::vector<std::type_info const*> type_list = {&typeid(Args)...};
70  for (size_t i = 0; i < type_list.size(); ++i) {
71  if (std::type_index(*type_list[i]) !=
72  std::type_index((*this)[i].type())) {
73  GF_CORE_LOG_ERROR(
74  "value of index {} in data object is type: {}, "
75  "not expected type: {}",
76  i, ((*this)[i]).type().name(), type_list[i]->name());
77  return false;
78  }
79  }
80  return true;
81  }
82 
83  private:
84  class Impl;
85  SecureUniquePtr<Impl> p_;
86 };
87 
88 template <typename... Args>
89 auto TransferParams(Args&&... args) -> std::shared_ptr<DataObject> {
90  return GpgFrontend::SecureCreateSharedObject<DataObject>(
91  DataObject{std::forward<Args>(args)...});
92 }
93 
94 template <typename T>
95 auto ExtractParams(const std::shared_ptr<DataObject>& d_o, int index) -> T {
96  if (!d_o) {
97  throw std::invalid_argument("nullptr provided for DataObjectPtr");
98  }
99  return std::any_cast<T>(d_o->GetParameter(index));
100 }
101 
102 void swap(DataObject& a, DataObject& b) noexcept;
103 
104 } // namespace GpgFrontend
Definition: DataObject.cpp:35
Definition: DataObject.h:43
Definition: app.cpp:39