Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
MemoryHelper.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_MEMORYHELPER_HPP
8 #define NAZARA_MEMORYHELPER_HPP
9 
10 #if defined(NAZARA_COMPILER_MSVC) || defined(NAZARA_COMPILER_MINGW)
11 
12 #include <malloc.h>
13 
14 #define NAZARA_ALLOCA(size) _alloca(size)
15 #define NAZARA_ALLOCA_SUPPORT
16 
17 #elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
18 #include <alloca.h>
19 
20 #define NAZARA_ALLOCA(size) alloca(size)
21 #define NAZARA_ALLOCA_SUPPORT
22 
23 #endif
24 
25 #ifdef NAZARA_ALLOCA_SUPPORT
26  #define NazaraStackAllocation(T, size) Nz::StackArray<T>(static_cast<T*>(NAZARA_ALLOCA((size) * sizeof(T))), size)
27  #define NazaraStackAllocationNoInit(T, size) Nz::StackArray<T>(static_cast<T*>(NAZARA_ALLOCA((size) * sizeof(T))), size, Nz::NoInitTag())
28 #else
29  #define NazaraStackAllocation(T, size) Nz::StackArray<T>(static_cast<T*>(Nz::OperatorNew((size) * sizeof(T))), size)
30  #define NazaraStackAllocationNoInit(T, size) Nz::StackArray<T>(static_cast<T*>(Nz::OperatorNew((size) * sizeof(T))), size, Nz::NoInitTag())
31 #endif
32 
33 #include <cstddef>
34 #include <iterator>
35 
36 namespace Nz
37 {
38  void OperatorDelete(void* ptr);
39  void* OperatorNew(std::size_t size);
40 
41  template<typename T, typename... Args>
42  T* PlacementNew(T* ptr, Args&&... args);
43 
44  template<typename T>
45  void PlacementDestroy(T* ptr);
46 
47  struct NoInitTag {};
48 
49  template<typename T>
50  class StackArray
51  {
52  public:
53  using value_type = T;
54  using const_iterator = const value_type*;
55  using const_pointer = const value_type*;
56  using const_reference = const value_type&;
57  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
58  using difference_type = std::ptrdiff_t;
59  using iterator = value_type*;
60  using pointer = value_type*;
61  using reference = value_type&;
62  using reverse_iterator = std::reverse_iterator<iterator>;
63  using size_type = std::size_t;
64 
65  StackArray(T* stackMemory, std::size_t size);
66  StackArray(T* stackMemory, std::size_t size, NoInitTag);
67  ~StackArray();
68 
69  reference back();
70  const_reference back() const;
71 
72  iterator begin() noexcept;
73  const_iterator begin() const noexcept;
74 
75  const_iterator cbegin() const noexcept;
76  const_iterator cend() const noexcept;
77  const_reverse_iterator crbegin() const noexcept;
78  const_reverse_iterator crend() const noexcept;
79 
80  T* data() noexcept;
81  const T* data() const noexcept;
82 
83  bool empty() const noexcept;
84 
85  iterator end() noexcept;
86  const_iterator end() const noexcept;
87 
88  void fill(const T& value);
89 
90  reference front() noexcept;
91  const_reference front() const noexcept;
92 
93  size_type max_size() const noexcept;
94 
95  reverse_iterator rbegin() noexcept;
96  const_reverse_iterator rbegin() const noexcept;
97 
98  reverse_iterator rend() noexcept;
99  const_reverse_iterator rend() const noexcept;
100 
101  size_type size() const noexcept;
102 
103  reference operator[](size_type pos);
104  const_reference operator[](size_type pos) const;
105 
106  private:
107  std::size_t m_size;
108  T* m_ptr;
109  };
110 
111 }
112 
113 #include <Nazara/Core/MemoryHelper.inl>
114 
115 #endif // NAZARA_MEMORYHELPER_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
void PlacementDestroy(T *ptr)
Calls the object destructor explicitly.
Definition: MemoryHelper.inl:75
T * PlacementNew(T *ptr, Args &&... args)
Constructs the object inplace.
Definition: MemoryHelper.inl:64
void OperatorDelete(void *ptr)
Calls the operator delete on the pointer.
Definition: MemoryHelper.inl:33
void * OperatorNew(std::size_t size)
Calls the operator new on the pointer.
Definition: MemoryHelper.inl:47
Core class that represents a stack-allocated (if alloca is present) array.
Definition: MemoryHelper.hpp:50