Mana
Stack.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "common/Platform.h"
10 #include "common/Noncopyable.h"
11 #include "common/OutputStream.h"
12 #include <memory>
13 
14 namespace mana
15 {
16  class Actor;
17 
22  class Stack final : Noncopyable
23  {
24  public:
25  Stack();
26  Stack(const Stack&) = delete;
27  Stack(Stack&&) noexcept = delete;
28  Stack& operator=(const Stack&) = delete;
29  Stack& operator=(Stack&&) noexcept = delete;
30  ~Stack() = default;
31 
32  void Serialize(const std::shared_ptr<OutputStream>& stream) const;
33  void Deserialize(const std::shared_ptr<OutputStream>& stream);
34 
38  void Clear();
39 
44  void Duplicate();
45 
50  void Remove(const address_t size);
51 
57  template<typename T>
58  void Push(T value);
59 
65  void Push(const void* buffer, const address_t size);
66 
72  template<typename T>
73  T Pop();
74 
80  void PopData(void* buffer, const address_t size);
81 
88  template<typename T>
89  T Get(const address_t index) const;
90 
96  void* GetAddress(const address_t index) const;
97 
104  template<typename T>
105  void Set(const address_t index, T value);
106 
111  address_t GetSize() const;
112 
118  void SetSize(const address_t size);
119 
125  bool operator==(const Stack& other) const;
126 
127  private:
128  static address_t GetAlignmentSize(const address_t size);
129  void AllocateBegin(const address_t gainSize);
130  void AllocateEnd(const address_t gainSize);
131  void Deallocate(const address_t releaseSize);
132 
133  private:
134  union Buffer
135  {
136  Actor* mActorPointer;
137  const char* mStringPointer;
138  void* mVoidPointer;
139  int_t mInteger;
140  float_t mFloat;
141  bool mBool;
142 
143  explicit operator Actor*() const { return mActorPointer; }
144  explicit operator const char*() const { return mStringPointer; }
145  explicit operator void*() const { return mVoidPointer; }
146  explicit operator int32_t() const { return static_cast<int32_t>(mInteger); }
147  explicit operator int64_t() const { return static_cast<int64_t>(mInteger); }
148  explicit operator float() const { return static_cast<float>(mFloat); }
149  explicit operator double() const { return static_cast<double>(mFloat); }
150  explicit operator bool() const { return mBool; }
151 
152  void Set(Actor* actorPointer) { mActorPointer = actorPointer; }
153  void Set(const char* stringPointer) { mStringPointer = stringPointer; }
154  void Set(void* voidPointer) { mVoidPointer = voidPointer; }
155  void Set(const int32_t value) { mInteger = static_cast<int_t>(value); }
156  void Set(const int64_t value) { mInteger = static_cast<int_t>(value); }
157  void Set(const float value) { mFloat = static_cast<float_t>(value); }
158  void Set(const double value) { mFloat = static_cast<float_t>(value); }
159  void Set(bool const value) { mBool = value; }
160  };
161  std::unique_ptr<Buffer, decltype(&std::free)> mBuffer;
162  address_t mAllocatedSize = 0;
163  address_t mUsedSize = 0;
164  };
165 }
166 
167 #include "Stack.inl"
Definition: Actor.h:29
Definition: Buffer.h:17
Definition: OutputStream.h:15
Definition: Stack.h:23
void Duplicate()
Definition: Stack.inl:39
Stack(const Stack &)=delete
void SetSize(const address_t size)
Definition: Stack.inl:110
Stack()
Definition: Stack.inl:12
void Set(const address_t index, T value)
Definition: Stack.inl:97
void * GetAddress(const address_t index) const
Definition: Stack.inl:89
void Push(T value)
Definition: Stack.inl:51
void Remove(const address_t size)
Definition: Stack.inl:44
void PopData(void *buffer, const address_t size)
Definition: Stack.inl:74
void Clear()
Definition: Stack.inl:34
T Get(const address_t index) const
Definition: Stack.inl:81
T Pop()
Definition: Stack.inl:67
Stack(Stack &&) noexcept=delete
void Serialize(const std::shared_ptr< OutputStream > &stream) const
Definition: Stack.inl:17
void Deserialize(const std::shared_ptr< OutputStream > &stream)
Definition: Stack.inl:23
address_t GetSize() const
Definition: Stack.inl:105
Definition: CodeBuffer.cpp:12
std::uint32_t address_t
Definition: Type.h:31
std::int32_t int_t
Definition: Type.h:29
float float_t
Definition: Type.h:28
Definition: Noncopyable.h:18