HEBench
engine_object.hpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef _HEBench_API_Bridge_EngineObject_H_7e5fa8c2415240ea93eff148ed73539b
6 #define _HEBench_API_Bridge_EngineObject_H_7e5fa8c2415240ea93eff148ed73539b
7 
8 #include <cstdint>
9 #include <memory>
10 
11 #include "error_handling.hpp"
12 
13 namespace hebench {
14 namespace cpp {
15 
16 class BaseEngine;
17 
38 {
39 public:
41  virtual ~ITaggedObject() {}
47  virtual std::int64_t classTag() const = 0;
48 
49  static constexpr std::int64_t MaskByteSet = 0xFF;
61  static constexpr std::int64_t MaskReservedBits = MaskByteSet << 56;
62 };
63 
102 class EngineObject final : public ITaggedObject
103 {
104 private:
106 
107 public:
112  static constexpr std::int64_t tag = 0x2000000000000000; // bit 61
113 
114  EngineObject(const BaseEngine &engine, std::shared_ptr<void> p_obj) :
115  m_engine(engine), m_p_obj(p_obj)
116  {
117  if (!p_obj)
118  throw std::invalid_argument(HEBERROR_MSG_CLASS("Invalid null pointer: p_obj"));
119  }
121  EngineObject(src.engine(), src.m_p_obj)
122  {
123  }
124  ~EngineObject() override {}
126  {
127  if (this != &src)
128  {
129  if (&m_engine != &src.m_engine)
130  throw std::runtime_error(HEBERROR_MSG_CLASS("Engine mismatch."));
131  if (!src.m_p_obj)
132  throw std::invalid_argument(HEBERROR_MSG_CLASS("Invalid null pointer: src.m_p_obj"));
133  this->m_p_obj = src.m_p_obj;
134  } // end if
135  return *this;
136  }
137 
138  const BaseEngine &engine() const { return m_engine; }
139 
140  template <class T>
141  T &get()
142  {
143  return *reinterpret_cast<T *>(m_p_obj.get());
144  }
145  template <class T>
146  const T &get() const
147  {
148  return *reinterpret_cast<T *>(m_p_obj.get());
149  }
150 
151  std::int64_t classTag() const override { return EngineObject::tag; }
152 
153 private:
154  const BaseEngine &m_engine;
155  std::shared_ptr<void> m_p_obj;
156 };
157 
158 } // namespace cpp
159 } // namespace hebench
160 
161 #endif // defined _HEBench_API_Bridge_EngineObject_H_7e5fa8c2415240ea93eff148ed73539b
Base class that encapsulates common behavior of backend engines.
Definition: engine.hpp:70
Helper class to encapsulate objects that will cross the boundary of the API Bridge.
EngineObject & operator=(const EngineObject &src)
std::int64_t classTag() const override
Retrieves the tag of the class to which this object belongs.
EngineObject(const BaseEngine &engine, std::shared_ptr< void > p_obj)
static constexpr std::int64_t tag
Used to identify this class when returned as a handle to Test Harness.
EngineObject(const EngineObject &src)
const BaseEngine & engine() const
Represents an object with a tag.
static constexpr std::int64_t MaskByteSet
static constexpr std::int64_t MaskReservedBits
Mask representing all bits reserved by C++ wrapper.
virtual std::int64_t classTag() const =0
Retrieves the tag of the class to which this object belongs.
#define HEBERROR_MSG_CLASS(message)
#define HEBERROR_DECLARE_CLASS_NAME(class_name)