DxLibEx
helper.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2015-2017 DxLibEx project
3  https://github.com/Nagarei/DxLibEx/
4 
5  Distributed under the Boost Software License, Version 1.0.
6  (See http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef DXLE_INC_HELPER_H_
9 #define DXLE_INC_HELPER_H_
10 
11 //実装用ファイルです。
12 //開発者以外がここの機能を使うのはお勧めできません
14 #include <cassert>
15 #include "dxlibex/config/defines.h"
16 
17 namespace dxle{
19 namespace impl{
20 
21  template<typename T>
26  {
27  private:
29  public:
30  //ここのpublic関数はUIに直結するので破壊的変更は可能な限り避けるように
31 
33 
35  inline const T& GetInt()const DXLE_NOEXCEPT_OR_NOTHROW{ return count(); }
37  inline const T& count()const DXLE_NOEXCEPT_OR_NOTHROW{ return value; }
39  explicit inline operator T()const DXLE_NOEXCEPT_OR_NOTHROW{ return count(); }
40 
41  inline const This_T& operator+()const DXLE_NOEXCEPT_OR_NOTHROW{ return *this; }
42  inline This_T operator-()const DXLE_NOEXCEPT_OR_NOTHROW{ return This_T(-value); }
43 
44  inline This_T operator+(const T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return This_T(value + other); }
45  inline This_T operator-(const T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return This_T(value - other); }
46  inline friend This_T operator+(const T& other, const This_T& bace) DXLE_NOEXCEPT_OR_NOTHROW{ assert(bace.value != -1); return This_T(other + bace.value); }
47  inline friend This_T operator-(const T& other, const This_T& bace) DXLE_NOEXCEPT_OR_NOTHROW{ assert(bace.value != -1); return This_T(other - bace.value); }
48  inline This_T& operator+=(const T& other) DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); value += other; return *this; }
49  inline This_T& operator-=(const T& other) DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); value -= other; return *this; }
50 
51  inline T operator-(const This_T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return value - other.value; }
52 
53  inline bool operator< (const This_T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return value < other.value; }
54  inline bool operator> (const This_T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return value > other.value; }
55  inline bool operator<=(const This_T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return value <= other.value; }
56  inline bool operator>=(const This_T& other)const DXLE_NOEXCEPT_OR_NOTHROW{ assert(value != -1); return value >= other.value; }
57 
58  protected:
59  explicit Counter_template(T param)DXLE_NOEXCEPT_OR_NOTHROW : value(param){}
60 
61  template<typename U>
62  struct Cast : public Counter_template<U>{
63  explicit Cast(const U& param) :Counter_template<U>(param){}
65  inline static Counter_template<U> Do(const U& value)DXLE_NOEXCEPT_OR_NOTHROW
66  {
67  return Counter_template<U>(Cast<U>(value));
68  }
69  };
70  private:
71  T value;
72  };
73 
74 
75  template<typename Child>
78  {
79  protected:
81  private:
82  //コピー禁止
83  Unique_Handle_Bace(const Bace_T&) = delete;
84  Unique_Handle_Bace& operator=(const Bace_T&) = delete;
85  public:
87  :handle(-1)
88  {}
89 
90  //所有権の譲渡
92  : handle(other.handle)
93  {
94  other.handle = -1;
95  SetDeleteHandleFlag(handle, &handle);
96  }
97  //所有権の譲渡
99  {
100  if (this == &other) { return *this; }
101  handle = other.handle;
102  other.handle = -1;
103  SetDeleteHandleFlag(handle, &handle);
104  return *this;
105  }
106 
108  //リソース解放
109  static_cast<Child*>(this)->Delete();
110  }
111  protected:
112  //間違えて他の種類のハンドルを持たないようにprotectedにしておく
114  : handle(param_handle)
115  {
116  SetDeleteHandleFlag(handle, &handle);
117  }
118 
122  int temp = GetHandle();
123  SetDeleteHandleFlag(handle, nullptr);
124  handle = -1;
125  return temp;
126  }
127  int GetHandle()const DXLE_NOEXCEPT_OR_NOTHROW{ return handle; }
128  private:
129  int handle;
130  };
131  template<typename Child>
134  {
135  protected:
137  private:
138  //コピー禁止
139  Unique_HandledObject_Bace(const Bace_T&) = delete;
140  Unique_HandledObject_Bace& operator=(const Bace_T&) = delete;
141  protected:
143  : handle(-1)
144  {}
145 
146  //所有権の譲渡
148  : handle(std::move(other.handle))
149  {
150  other.handle = -1;
151  }
152  //所有権の譲渡
154  {
155  if (this == &other) { return *this; }
156  handle = other.handle;
157  DxLib::SetDeleteHandleFlag(handle, &handle);
158  other.handle = -1;
159  return *this;
160  }
161 
162  protected:
163  //間違えて他の種類のハンドルを持たないようにprotectedにしておく
165  : handle(param_handle)
166  {
167  DxLib::SetDeleteHandleFlag(handle, &handle);
168  }
170  //リソース解放
171  static_cast<Child*>(this)->delete_this();
172  }
173  DXLE_CONSTEXPR int GetHandle()const DXLE_NOEXCEPT_OR_NOTHROW{ return handle; }
174  void SetHandle_IMPL(int new_handle) { handle = (new_handle); DxLib::SetDeleteHandleFlag(handle, &handle); }
175  private:
176  int handle;
177  };
178 
179 }//namespace impl
180 }//namespace dxle
181 
182 #endif
friend This_T operator+(const T &other, const This_T &bace) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:46
bool operator<=(const This_T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:55
const T & count() const DXLE_NOEXCEPT_OR_NOTHROW
整数型の値を取得する
Definition: helper.hpp:37
static Counter_template< U > Do(const U &value) DXLE_NOEXCEPT_OR_NOTHROW
Counter_template<T>からCounter_template<U>にキャストする
Definition: helper.hpp:65
Unique_HandledObject_Bace(Bace_T &&other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:147
#define DXLE_CONSTEXPR
Definition: suffix.hpp:21
Counter_template(T param) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:59
bool operator<(const This_T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:53
int GetHandle() const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:127
Unique_Handle_Bace & operator=(Bace_T &&other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:98
Unique_Handle_Bace(int param_handle) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:113
friend This_T operator-(const T &other, const This_T &bace) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:47
virtual ~Unique_HandledObject_Bace() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:169
const This_T & operator+() const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:41
ハンドルの指すオブジェクト実装用
Definition: helper.hpp:133
~Unique_Handle_Bace() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:107
Unique_HandledObject_Bace(int param_handle) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:164
This_T & operator-=(const T &other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:49
Definition: point2d.hpp:672
Definition: cast_if.hpp:12
Counter_template() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:32
DXLE_CONSTEXPR int GetHandle() const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:173
bool operator>=(const This_T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:56
Unique_HandledObject_Bace< Child > Bace_T
Definition: helper.hpp:136
void SetHandle_IMPL(int new_handle)
Definition: helper.hpp:174
bool operator>(const This_T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:54
const T & GetInt() const DXLE_NOEXCEPT_OR_NOTHROW
整数型の値を取得する
Definition: helper.hpp:35
This_T operator+(const T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:44
Unique_Handle_Bace(Bace_T &&other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:91
This_T operator-() const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:42
Unique_HandledObject_Bace & operator=(Bace_T &&other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:153
Unique_Handle_Bace< Child > Bace_T
Definition: helper.hpp:80
DXLE_CONSTEXPR Unique_HandledObject_Bace() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:142
#define DXLE_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:94
This_T operator-(const T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:45
ハンドル自動削除実装用
Definition: helper.hpp:77
Unique_Handle_Bace() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:86
T operator-(const This_T &other) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:51
This_T & operator+=(const T &other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:48
int ReleaseRun() DXLE_NOEXCEPT_OR_NOTHROW
Definition: helper.hpp:121