Support Class Library
A set of tools providing classes and utility
RawStorage.h
Go to the documentation of this file.
1 #pragma once
2 #include <scl/macros.h>
5 
6 namespace scl{
7  namespace utils{
13  template <class T>
14  class RawStorage{
15  protected:
16  using storage_type = META::aligned_storage_t<sizeof(T), alignof(T)>;
17 
22  bool init;
23 
29 
31  return &storage;
32  }
33 
34  T* ptr(){
35  return reinterpret_cast<T*>(this->rawPtr());
36  }
37 
38  public:
39  RawStorage() : init{false}, storage{} {
40  }
41 
43  this->destructor();
44  }
45 
47  *this = std::move(other);
48  }
49 
51  this->init = other.init;
52  this->constructor(std::move(other.get()));
53  other.init = false; //set the correct move semantics
54  return *this;
55  }
56 
66  template <class... Args>
67  T& constructor(Args&&... args){
68  this->destructor();
69  new( rawPtr() )T{std::forward<Args>(args)...};
70  this->init = true;
71  return this->get();
72  }
73 
78  void destructor(){
79  if(this->init) {
80  this->ptr()->~T();
81  this->init = false;
82  }
83  }
84 
88  template <class... Args>
89  T& construct(Args&&... args){
90  return this->constructor(std::forward<Args&&>(args)...);
91  }
92 
96  void destroy(){
97  this->destructor();
98  }
99 
105  this->destroy();
106  return *this;
107  }
108 
114  T& get(){
115  if(!this->init)
117 
118  return *ptr();
119  }
120 
126  T& operator*(){
127  return this->get();
128  }
129 
136  return &(this->get());
137  }
138 
143  const T& get() const{
144  if(!this->init)
146 
147  return *reinterpret_cast<const T*>(&this->storage);
148  }
149 
154  const T& operator*() const{
155  return this->get();
156  }
157 
162  realConst(T*) operator->() const{
163  return &(this->get());
164  }
165 
170  bool hasValue() const{
171  return this->init;
172  }
173 
178  operator bool() const{
179  return this->hasValue();
180  }
181  };
182  }
183 }
bool hasValue() const
Determine whether or not the storage holds a value.
Definition: RawStorage.h:170
void destroy()
Alias for RawStorage::destructor.
Definition: RawStorage.h:96
Global namespace of the SCL.
Definition: alias.hpp:3
#define realConst(type)
Definition: macros.h:3
bool init
Determines whether or not the object has been constructed.
Definition: RawStorage.h:22
Exception class used when trying to access uninitialized memory.
RawStorage & operator=(RawStorage &&other)
Definition: RawStorage.h:50
storage_type storage
The actual memory that can hold the variable.
Definition: RawStorage.h:28
const T & operator*() const
Get the value from a constant RawStorage (e.g.
Definition: RawStorage.h:154
RawStorage(RawStorage &&other)
Definition: RawStorage.h:46
Class that handles raw storage (and manual memory management) for a variable type.
Definition: RawStorage.h:14
T * operator->()
Get a pointer to the underlying data.
Definition: RawStorage.h:135
scl::tools::meta ::aligned_storage_t< sizeof(Lhs), alignof(Lhs)> storage_type
Definition: RawStorage.h:16
storage_type * rawPtr()
Definition: RawStorage.h:30
T & constructor(Args &&... args)
Construct the variable in the storage.
Definition: RawStorage.h:67
T & operator*()
Access the underlying data.
Definition: RawStorage.h:126
T & construct(Args &&... args)
Alias for RawStorage::constructor.
Definition: RawStorage.h:89
RawStorage & reset()
Alias for RawStorage::destroy.
Definition: RawStorage.h:104
void destructor()
Call the destructor on the allocated object.
Definition: RawStorage.h:78
const T *const operator->() const
Get a pointer to the value from a constant RawStorage (e.g.
Definition: RawStorage.h:162