F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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_
11namespace Os {
12namespace Delegate {
13
45template <class Interface, class Implementation, class StorageType>
46inline Interface* makeDelegate(StorageType& 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) <= sizeof(StorageType), "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
88template <class Interface, class Implementation, class StorageType>
89inline Interface* makeDelegate(StorageType& 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:440
Interface * makeDelegate(StorageType &aligned_new_memory)
Make a delegate of type Interface using Implementation without copy-constructor support (generic func...
Definition Delegate.hpp:46