~aleteoryx/muditaos

749263bafde7a4125238a3cb1fa58ea69a6f4619 — Piotr Tanski 5 years ago 506f561
[EGD-4155] Created a code example of possible implementation of generic data pack for actions. (#957)

1 files changed, 52 insertions(+), 0 deletions(-)

M module-services/service-appmgr/doc/README.md
M module-services/service-appmgr/doc/README.md => module-services/service-appmgr/doc/README.md +52 -0
@@ 64,3 64,55 @@ The idea is to create a base for such generic data. It should be able to seriali
When sent, the data will be serialized into the data storage, and only that storage shall be sent along with the action request.

When received, the data will be re-created from the storage and used by a receiver object.

#### Interface proposal and code example

```
#include <string>
#include <cassert>

using DataPackage = std::string; // To be changed to chosen data format.

class Packageable {
public:
  virtual ~Packageable() noexcept = default;

  virtual DataPackage pack() const = 0;

  template <typename T> class Creator {
  public:
    virtual ~Creator() noexcept = default;

    virtual T unpack(const DataPackage &package) = 0;
  };
};

class ExampleData : public Packageable {
public:
  class ExampleCreator : public Packageable::Creator<ExampleData> {
  public:
    ExampleData unpack(const DataPackage &package) override {
      const auto x = std::stoi(package);
      return ExampleData{x};
    }
  };
  static inline ExampleCreator CREATOR;

  explicit ExampleData(int _x) : x{_x} {}

  DataPackage pack() const override { return std::to_string(x); }

  int getX() const noexcept { return x; }

private:
  int x{0};
};

int main() {
  ExampleData data{10};
  const auto package = data.pack();
  const auto received = ExampleData::CREATOR.unpack(package);
  assert(data.getX() == received.getX());
  return 0;
}
```