F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Delegate.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Delegate.hpp
3 // \brief helper functions to ease correct getDelegate implementations
4 // ======================================================================
5 #include <new>
6 #include <type_traits>
7 #include "Fw/Types/Assert.hpp"
8 #include "Os/Os.hpp"
9 #ifndef OS_DELEGATE_HPP_
10 #define OS_DELEGATE_HPP_
11 namespace Os {
12 namespace Delegate {
13 
45 template <class Interface, class Implementation>
46 inline Interface* makeDelegate(HandleStorage& aligned_new_memory) {
47  // Ensure prerequisites before performing placement new
48  static_assert(std::is_base_of<Interface, Implementation>::value, "Implementation must derive from Interface");
49  static_assert(sizeof(Implementation) <= FW_HANDLE_MAX_SIZE, "Handle size not large enough");
50  static_assert((FW_HANDLE_ALIGNMENT % alignof(Implementation)) == 0, "Handle alignment invalid");
51  // Placement new the object and ensure non-null result
52  Implementation* interface = new (aligned_new_memory) Implementation;
53  FW_ASSERT(interface != nullptr);
54  return interface;
55 }
56 
88 template <class Interface, class Implementation>
89 inline Interface* makeDelegate(HandleStorage& aligned_new_memory, const Interface* to_copy) {
90  const Implementation* copy_me = reinterpret_cast<const Implementation*>(to_copy);
91  // Ensure prerequisites before performing placement new
92  static_assert(std::is_base_of<Interface, Implementation>::value, "Implementation must derive from Interface");
93  static_assert(sizeof(Implementation) <= sizeof(aligned_new_memory), "Handle size not large enough");
94  static_assert((FW_HANDLE_ALIGNMENT % alignof(Implementation)) == 0, "Handle alignment invalid");
95  // Placement new the object and ensure non-null result
96  Implementation* interface = nullptr;
97  if (to_copy == nullptr) {
98  interface = new (aligned_new_memory) Implementation;
99  } else {
100  interface = new (aligned_new_memory) Implementation(*copy_me);
101  }
102  FW_ASSERT(interface != nullptr);
103  return interface;
104 }
105 } // namespace Delegate
106 } // namespace Os
107 #endif
#define FW_ASSERT(...)
Definition: Assert.hpp:14
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:378
#define FW_HANDLE_MAX_SIZE
Maximum size of a handle for OS resources (files, queues, locks, etc.)
Definition: FpConfig.h:374
U8 HandleStorage[FW_HANDLE_MAX_SIZE]
Storage type for OSAL handles.
Definition: Os.hpp:10
Interface * makeDelegate(HandleStorage &aligned_new_memory)
Make a delegate of type Interface using Implementation without copy-constructor support (generic func...
Definition: Delegate.hpp:46