SDXFrameWork
0.13
SDXFrameWork
Main Page
Related Pages
Classes
All
Classes
Namespaces
Functions
Variables
Enumerations
Enumerator
Pages
Utility
Any.h
1
//Copyright © 2014 SDXFramework
2
//[License]GNU Affero General Public License, version 3
3
//[Contact]http://sourceforge.jp/projects/dxframework/
4
#pragma once
5
#include <vector>
6
7
namespace
SDX
8
{
9
template
<
class
TBase,
int
MaxSize >
10
/*静的にメモリを確保する動的型.*/
11
/*共通基底クラスと最大型サイズをテンプレートに入れる*/
12
/*Holderとか実装してない分、メモリ効率は良い*/
13
class
Any
14
{
15
private
:
16
char
buff[MaxSize];
17
18
public
:
19
template
<
typename
T >
20
Any
(
const
T& src)
21
{
22
static_assert(
sizeof
(T) <= MaxSize,
"Any<> MaxSize Over"
);
23
//継承チェック
24
//static_assert(std::is_base_of<TBase, T>(), "KEISYOU SITENAI");
25
new
(buff) T(src);
26
}
27
28
~
Any
()
29
{
30
((TBase*)
this
)->~TBase();
31
}
32
33
Any
& operator = (
Any
const
& src)
34
{
35
if
(
this
!= & src)
36
{
37
for
(
int
a = 0;a < MaxSize;++a)
38
{
39
buff[a] = src.buff[a];
40
}
41
}
42
return
*
this
;
43
}
44
45
template
<
typename
T >
46
Any
& operator = (
const
T& src)
47
{
48
static_assert(
sizeof
(T) <= MaxSize,
"Any<> MaxSize Over"
);
49
*
this
=
new
(buff) T(src);
50
return
*
this
;
51
}
52
53
TBase* operator->()
54
{
55
return
(TBase*)
this
;
56
}
57
};
58
59
template
<
class
TBase ,
int
MaxSize >
60
/*静的にメモリを確保する動的型.*/
61
/*共通スーパークラスと最大型サイズをテンプレートに入れる*/
62
/*Holderとかの分も考えて、MaxSizeを指定する必要がありメモリ効率が下がる*/
63
/*色々安全にした分、メモリ効率が若干悪く、速度も若干遅い*/
64
class
SafeAny
65
{
66
private
:
67
char
buff[MaxSize];
68
69
class
IHolder
70
{
71
public
:
72
virtual
~IHolder() {}
73
virtual
IHolder * clone(
char
* buff) = 0;
74
virtual
TBase* Get(
void
) = 0;
75
};
76
77
IHolder* content;
78
79
template
<
typename
T >
80
class
Holder :
public
IHolder
81
{
82
public
:
83
84
explicit
Holder(T
const
& src) :
85
held(src)
86
{}
87
88
89
explicit
Holder(T
const
&& src) :
90
held(src)
91
{}
92
93
virtual
IHolder *clone(
char
* buff )
94
{
95
return
new
(buff) Holder(held);
96
}
97
98
TBase* Get()
99
{
100
return
&held;
101
}
102
103
private
:
104
T held;
105
};
106
107
public
:
108
template
<
typename
T >
109
SafeAny
(T
const
& src) :
110
content(
new
(buff) Holder<T>(src))
111
{
112
static_assert(
sizeof
(Holder<T>) <= MaxSize,
"SafeAny<> MaxSize Over"
);
113
}
114
115
SafeAny
(
SafeAny
const
& other) :
116
content(other.content ? other.content->clone(buff) : 0)
117
{ }
118
119
~
SafeAny
()
120
{
121
content->~IHolder();
122
}
123
124
SafeAny
& operator = (
SafeAny
const
& src)
125
{
126
if
(
this
!= &src)
127
{
128
content = src.content->clone(buff);
129
}
130
return
*
this
;
131
}
132
133
template
<
typename
T >
134
SafeAny
& operator = ( T
const
& src)
135
{
136
static_assert(
sizeof
(Holder<T>) <= MaxSize,
"SafeAny<> MaxSize Over"
);
137
content =
new
(buff) Holder<T>(src);
138
return
*
this
;
139
}
140
141
TBase* operator->()
142
{
143
content->Get();
144
}
145
146
};
147
}
SDX::Any
Definition:
Any.h:13
SDX::SafeAny
Definition:
Any.h:64
Generated on Sun Jun 28 2015 09:15:35 for SDXFrameWork by
1.8.7