siplasplas
A library for C++ reflection and introspection
fixedsize.hpp
1 #ifndef SIPLASPLAS_TYPEERASURE_ANYSTORAGE_FIXEDSIZE_HPP
2 #define SIPLASPLAS_TYPEERASURE_ANYSTORAGE_FIXEDSIZE_HPP
3 
4 #include <siplasplas/utility/memory_manip.hpp>
5 #include <siplasplas/typeerasure/typeinfo.hpp>
6 #include <array>
7 #include <type_traits>
8 
9 namespace cpp
10 {
11 
19 template<std::size_t Size, std::size_t Alignment = alignof(std::uint8_t)>
21 {
22 protected:
26  const char* begin() const
27  {
28  return reinterpret_cast<const char*>(&_storage);
29  }
30 
34  char* begin()
35  {
36  return reinterpret_cast<char*>(&_storage);
37  }
38 
42  const char* end() const
43  {
44  return begin() + size();
45  }
46 
50  char* end()
51  {
52  return begin() + size();
53  }
54 
58  constexpr std::size_t size() const
59  {
60  return Size;
61  }
62 
72  const void* storage(cpp::typeerasure::TypeInfo typeInfo) const
73  {
74  return cpp::detail::aligned_ptr(begin(), typeInfo.alignment());
75  }
76 
87  {
88  return cpp::detail::aligned_ptr(begin(), typeInfo.alignment());
89  }
90 
96  template<typename T>
97  bool objectFitsInStorage() const
98  {
99  return reinterpret_cast<const char*>(storage(cpp::typeerasure::TypeInfo::get<T>())) + sizeof(T) <= end();
100  }
101 
102 private:
103  std::aligned_storage_t<Size, Alignment> _storage;
104 };
105 
106 }
107 
108 #endif // SIPLASPLAS_TYPEERASURE_ANYSTORAGE_FIXEDSIZE_HPP
Definition: canary_allocator.hpp:7
Implements fixed-size storage.
Definition: fixedsize.hpp:20
const char * begin() const
Returns a pointer to the beginning of the storage.
Definition: fixedsize.hpp:26
SIPLASPLAS_UTILITY_EXPORT char * aligned_ptr(char *pointer, std::size_t alignment)
Returns an address aligned to an specific boundary.
constexpr std::size_t size() const
Returns the size in bytes of the storage.
Definition: fixedsize.hpp:58
const void * storage(cpp::typeerasure::TypeInfo typeInfo) const
Returns a pointer to the storage memory space.
Definition: fixedsize.hpp:72
std::size_t alignment() const
returns the alignment of the type
Definition: typeinfo.hpp:260
char * end()
Returns a pointer to the end of the storage.
Definition: fixedsize.hpp:50
void * storage(cpp::typeerasure::TypeInfo typeInfo)
Returns a pointer to the storage memory space.
Definition: fixedsize.hpp:86
char * begin()
Returns a pointer to the beginning of the storage.
Definition: fixedsize.hpp:34
const char * end() const
Returns a pointer to the end of the storage.
Definition: fixedsize.hpp:42
Contains minimal information to execute the value semantics operations of a type. ...
Definition: typeinfo.hpp:108
bool objectFitsInStorage() const
Checks if an object can be hosted in the storage space.
Definition: fixedsize.hpp:97