nlib
ExiAllocator.h
[詳解]
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
4 #define INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
5 
6 #include <new>
7 
8 #include "nn/nlib/Config.h"
9 #include "nn/nlib/exi/Types.h"
10 #include "nn/nlib/Swap.h"
11 
12 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace exi {
19 
20 struct ExiAllocatorInfo;
21 class ExiAllocator;
23  public:
26  ExiAllocatorEx(const ExiAllocatorEx& rhs) NLIB_NOEXCEPT : m_Ptr(rhs.m_Ptr) {}
28  m_Ptr = rhs.m_Ptr;
29  return *this;
30  }
31  NLIB_MOVE_MEMBER_HELPER_1(ExiAllocatorEx, m_Ptr);
33  using std::swap;
34  swap(rhs.m_Ptr, m_Ptr);
35  }
36 
37  errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
38  void* Finalize() NLIB_NOEXCEPT;
39  void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
40  void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
41  void Dealloc(void* p) NLIB_NOEXCEPT;
42  void Reset() NLIB_NOEXCEPT;
43  bool Test(size_t nbytes) NLIB_NOEXCEPT;
44  size_t GetPos() NLIB_NOEXCEPT;
45  bool IsOutOfMemory() NLIB_NOEXCEPT;
46 
47  bool IsValid() const NLIB_NOEXCEPT { return m_Ptr != NULL; }
48  NLIB_SAFE_BOOL(ExiAllocatorEx, IsValid())
49 
50  private:
51  explicit ExiAllocatorEx(ExiAllocatorInfo* p) NLIB_NOEXCEPT : m_Ptr(p) {}
52  ExiAllocatorInfo* m_Ptr;
53  friend class ExiAllocator;
54 };
55 
57  public:
58  static errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
59  static void* Finalize() NLIB_NOEXCEPT;
60  static void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
61  static void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
62  static void Dealloc(void* p) NLIB_NOEXCEPT;
63  static void Reset() NLIB_NOEXCEPT;
64  static bool Test(size_t nbytes) NLIB_NOEXCEPT;
65  static size_t GetPos() NLIB_NOEXCEPT;
66  static bool IsOutOfMemory() NLIB_NOEXCEPT;
67  static ExiAllocatorEx GetAllocator() NLIB_NOEXCEPT;
68  static void Swap(ExiAllocatorEx& rhs) NLIB_NOEXCEPT;
69 
70  private:
71  ExiAllocator();
73 };
74 
75 class NLIB_VIS_PUBLIC ExiAllocatorSwapper NLIB_FINAL {
76  public:
77  explicit ExiAllocatorSwapper(ExiAllocatorEx& rhs) NLIB_NOEXCEPT // NOLINT
78  : m_Al(rhs),
79  m_Swapped(rhs) {
80  if (m_Al) ExiAllocator::Swap(rhs);
81  }
82  ~ExiAllocatorSwapper() NLIB_NOEXCEPT {
83  if (m_Swapped) ExiAllocator::Swap(m_Al);
84  }
85 
86  private:
87  ExiAllocatorEx& m_Al;
88  bool m_Swapped;
89  NLIB_DISALLOW_COPY_AND_ASSIGN(ExiAllocatorSwapper);
90 };
91 
92 #ifdef NN_PLATFORM_CTR
93 // NOTE: ARMCC does not support placement delete
94 #define NEXI_DEF_NEW_DELETE(typeName) \
95  static void* operator new(size_t size) throw() { \
96  NLIB_ASSERT(size == sizeof(typeName)); \
97  return ExiAllocator::Alloc(size); \
98  } \
99  static void* operator new(size_t size, const std::nothrow_t&) throw() { \
100  NLIB_ASSERT(size == sizeof(typeName)); \
101  return ExiAllocator::Alloc(size); \
102  } \
103  static void* operator new(size_t size, void* ptr) throw() { \
104  return ::operator new(size, ptr); \
105  } \
106  static void operator delete(void* p) throw() { ExiAllocator::Dealloc(p); } \
107  static void* operator new[](size_t size, void* p) throw(); \
108  static void operator delete[](void* p) throw()
109 
110 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
111 public: \
112  static void* operator new(size_t size, void* ptr) throw() { \
113  return ::operator new(size, ptr); \
114  } \
115 private: \
116  static void* operator new(size_t size) throw(); \
117  static void* operator new(size_t size, const std::nothrow_t&) throw(); \
118  static void operator delete(void* p) throw(); \
119  static void* operator new[](size_t size, void* p) throw(); \
120  static void* operator new[](size_t size, const std::nothrow_t&) throw(); \
121  static void operator delete[](void* p) throw()
122 #else
123 #define NEXI_DEF_NEW_DELETE(typeName) \
124 private: \
125  static void* operator new[](size_t size); \
126  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
127  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
128  static void operator delete[](void* p) NLIB_NOEXCEPT; \
129  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
130  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT; \
131 public: \
132  static void* operator new(size_t size) { \
133  NLIB_ASSERT(size == sizeof(typeName)); \
134  return ExiAllocator::Alloc(size); \
135  } \
136  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
137  NLIB_ASSERT(size == sizeof(typeName)); \
138  return ExiAllocator::Alloc(size); \
139  } \
140  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
141  return ::operator new(size, ptr); \
142  } \
143  static void operator delete(void* p) NLIB_NOEXCEPT { ExiAllocator::Dealloc(p); } \
144  static void operator delete(void* p, const std::nothrow_t&) NLIB_NOEXCEPT { \
145  ExiAllocator::Dealloc(p); \
146  } \
147  static void operator delete(void* p, void* ptr) NLIB_NOEXCEPT { \
148  return ::operator delete(p, ptr); \
149  }
150 
151 #ifndef CAFE
152 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
153 public: \
154  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
155  return ::operator new(size, ptr); \
156  } \
157  static void operator delete(void* p, void* ptr) NLIB_NOEXCEPT { \
158  return ::operator delete(p, ptr); \
159  } \
160  static void operator delete(void* p) NLIB_NOEXCEPT { \
161  NLIB_UNUSED(p); \
162  NLIB_ASSUME(0); \
163  } \
164 private: \
165  static void* operator new(size_t size); \
166  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
167  static void operator delete(void* p, const std::nothrow_t&) NLIB_NOEXCEPT; \
168  static void* operator new[](size_t size); \
169  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
170  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
171  static void operator delete[](void* p) NLIB_NOEXCEPT; \
172  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
173  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT
174 #else
175 #define NEXI_DISALLOW_NEW_DELETE(typeName) NLIB_STATIC_ASSERT(true)
176 #endif
177 #endif
178 
179 } // namespace exi
180 NLIB_NAMESPACE_END
181 
182 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
183 #undef NLIB_VIS_PUBLIC
184 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
185 #endif
186 
187 #endif // INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
XMLパーサーが内部で利用するアロケータです。ユーザーが利用することもできます。
Definition: ExiAllocator.h:56
文字列型のtypedefやユーティリティマクロ等が定義されています。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:160
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:61
bool IsValid() const noexcept
アロケータが初期化されていればtrueを返します。
Definition: ExiAllocator.h:47
void swap(ExiAllocatorEx &rhs) noexcept
オブジェクトの内容をスワップします。
Definition: ExiAllocator.h:32
constexpr ExiAllocatorEx() noexcept
デフォルトコンストラクタです。
Definition: ExiAllocator.h:24
XmlStreamReader, XmlStreamWriter の各インスタンス毎に設定できるアロケータです。
Definition: ExiAllocator.h:22
ExiAllocatorEx & operator=(const ExiAllocatorEx &rhs) noexcept
代入演算子です。
Definition: ExiAllocator.h:27
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:80
開発環境別の設定が書かれるファイルです。
~ExiAllocatorEx() noexcept
デストラクタです。
Definition: ExiAllocator.h:25
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:211
ExiAllocatorEx(const ExiAllocatorEx &rhs) noexcept
コピーコンストラクタです。
Definition: ExiAllocator.h:26
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24