Runtime Concept 的模拟(5)
简化使用方法!
用链式的单继承解决多继承方法的调用不明确的问题。
支持auto concept和non-auto concept。
支持引用类型concept和值类型concept。
已测试支持的编译器:
VC 7.1 (VC 2003)
VC 8.0 (VC 2005)
VC 9.0 (VC 2008)
GCC 3.4.2
GCC 4.2.3
基本原理:
1. 用类继承的方法生成vtable。
2. 用链式的单继承来组合concept。
缺陷:
1. 成员函数的调用方法不知道是不是符合标准,不过估计移植性没什么问题。
2. static应该改为多线程安全的Singleton。
3. 没考虑异常安全。
另:
1. 关于非虚成员函数指针可以指向虚函数并正常调用的问题:
i) VC 如果指向的是没有基类的类的虚函数就可以。
ii) GCC 总是可以调用的。
2. VC6 不支持偏特化,不支持类模板参数的成员类模板。
// main.cpp
//
main.cpp
#include
<
stdio.h
>
#include
<
vector
>
#include
<
string
>
#include
"
shape.hpp
"
#include
"
concept.hpp
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
CONCEPT_MEMBER_FUNCTION(c_set_value, set_value,
void
, (
int
Value), (Value));
CONCEPT_MEMBER_FUNCTION(c_set_value1, set_value1,
void
, (
int
Value), (Value));
CONCEPT_MEMBER_FUNCTION(c_set_value2, set_value2,
void
, (
int
Value), (Value));
CONCEPT_MEMBER_FUNCTION(c_set_value3, set_value3,
void
, (
int
Value), (Value));
CONCEPT_MEMBER_FUNCTION(c_set_value4, set_value4,
void
, (
int
Value), (Value));
CONCEPT_MEMBER_FUNCTION(c_draw, draw,
void
, (monitor device)
const
, (device));
CONCEPT_FUNCTION(g_set_value, set_value,
void
, (ObjectType
&
t,
int
Value), (t, Value));
CONCEPT_FUNCTION(g_drawShape, drawShape,
void
, (
const
ObjectType
&
t, monitor device), (t, device));
typedef concept_list
<
g_drawShape,
c_draw,
c_set_value,
c_set_value1,
c_set_value2,
c_set_value3,
c_set_value4
>
Shape;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
template
<>
struct
concept_map
<
Shape, Rectangle
>
...
{}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
////
typedef auto_concept
<
Shape
>
auto_Shape;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
DrawShapes(monitor device, std::vector
<
auto_Shape
>
const
&
g)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
std::vector<auto_Shape>::const_iterator b(g.begin()), e(g.end());
for(; b != e; ++b)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
b->draw(device);
auto_Shape::drawShape(*b, device);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
T
>
void
DrawShape(monitor device,
const
T
&
t)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (0) concept<Shape> tmp = t; //static non-auto concept
t.draw(device);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main(
int
argc,
char
*
argv[])
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
auto_Shape as = Rectangle();
int sizeofShape = sizeof(Shape), sizeofconceptShape = sizeof(auto_Shape);
as.draw(stdout);
as.set_value(1);
as.draw(stdout);
as.set_value1(sizeofShape);
as.draw(stdout);
as.set_value2(sizeofconceptShape);
as.draw(stdout);
as.set_value3(1);
as.draw(stdout);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
std::vector<auto_Shape> vg;
vg.push_back(Rectangle());
vg.push_back(Ellipse());
vg.push_back(vg[0]);
vg.push_back(vg[2]);
vg[2] = vg[0];
vg[3] = vg[1];
//vg.push_back(Triangle()); //错误,不符合Shape concept // OK in GCC
//vg.push_back(std::string("xxx")); //错误,不符合Shape concept
DrawShapes(stdout, vg);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Rectangle rect;
Ellipse elli;
EllipseChild ellichild;
DrawShape(stdout, rect);
//DrawShape(stdout, elli); //错误,不符合Shape static concept
//DrawShape(stdout, ellichild); //错误,不符合Shape static concept
ref_concept<Shape> ref_non_auto_Shape = rect;
ref_non_auto_Shape = rect;
//ref_non_auto_Shape = elli; //错误,不符合Shape non-auto concept
printf("rect.value = %d ", rect.value);
ref_non_auto_Shape.set_value(1);
printf("rect.value = %d ", rect.value);
ref_auto_concept<c_set_value>(rect).set_value(2);
ref_auto_concept<g_set_value> x(rect);
ref_auto_concept<g_set_value>::set_value(x, 2);
printf("rect.value = %d ", rect.value);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return 0;
}
// shape.hpp
//
shape.hpp
#include
<
stdio.h
>
typedef FILE
*
monitor;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
ShapeBase
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int value;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
ShapeBase() : value(0) ...{}
void set_value(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value1(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value2(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value3(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value4(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
Rectangle :
public
ShapeBase
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
void draw(monitor device) const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
fprintf(device, "Rectangle.draw: %d ", value);
}
void draw() const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
printf("Rectangle.draw ?: %d ", value);
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
Ellipse
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int value;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Ellipse() : value(0) ...{}
void set_value(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value1(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value2(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value3(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
void set_value4(int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
value = Value;
}
virtual void draw(monitor device) const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
fprintf(device, "Ellipse.draw: %d ", value);
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
EllipseChild :
public
Ellipse
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual void draw(monitor device) const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
fprintf(device, "EllipseChild.draw?: %d ", value);
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
Triangle :
public
ShapeBase
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual void draw(monitor device) const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
fprintf(device, "Triangle.draw virtual: %d ", value);
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
drawShape(
const
Rectangle
&
refShape, monitor device)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
refShape.draw(device);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
drawShape(
const
Ellipse
&
refShape, monitor device)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
refShape.draw(device);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
drawShape(
const
Triangle
&
refShape, monitor device)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
refShape.draw(device);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
T
>
void
set_value(T
&
refShape,
int
Value)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
refShape.value = Value;
}
// concept.hpp
//
concept.hpp
#ifdef _MSC_VER
#define
MEMFUNCCAST(A) reinterpret_cast<A>
#else
#define
MEMFUNCCAST(A)
#endif
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
CONCEPT_FUNCTION(CONCEPTNAME, FNAME, RTYPE, FTYPE, PARA)
struct
CONCEPTNAME
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
template <class BaseFrom = concept_base, bool isvtable = true, class ObjectType = concept_object>
struct table : public concept_bind_with<typename BaseFrom::Base, typename BaseFrom::DeriveFrom, isvtable, ObjectType>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef CONCEPTNAME Base;
typedef BaseFrom DeriveFrom;
typedef typename get_type<RTYPE (*)FTYPE>::type function_type;
function_type p##FNAME;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
table() : p##FNAME(&::FNAME) ...{}
};
template <class BaseFrom, class ObjectType>
struct table<BaseFrom, false, ObjectType> : public concept_bind_with<typename BaseFrom::Base, typename BaseFrom::DeriveFrom, false, ObjectType>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef CONCEPTNAME Base;
typedef BaseFrom DeriveFrom;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
const void* CONCEPTNAME##_index() const ...{return this;}
static RTYPE FNAME FTYPE
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return (*t.ptable->p##FNAME)PARA;
}
};
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
CONCEPT_MEMBER_FUNCTION(CONCEPTNAME, FNAME, RTYPE, FTYPE, PARA)
struct
CONCEPTNAME
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
template <class BaseFrom = concept_base, bool isvtable = true, class ObjectType = concept_object>
struct table : public concept_bind_with<typename BaseFrom::Base, typename BaseFrom::DeriveFrom, isvtable, ObjectType>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef CONCEPTNAME Base;
typedef BaseFrom DeriveFrom;
typedef typename get_type<RTYPE (ObjectType::*)FTYPE>::type function_type;
function_type p##FNAME;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
table() : p##FNAME(MEMFUNCCAST(function_type)(&ObjectType::FNAME)) ...{}
};
template <class BaseFrom, class ObjectType>
struct table<BaseFrom, false, ObjectType> : public concept_bind_with<typename BaseFrom::Base, typename BaseFrom::DeriveFrom, false, ObjectType>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef CONCEPTNAME Base;
typedef BaseFrom DeriveFrom;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
const void* CONCEPTNAME##_index() const ...{return this;}
RTYPE FNAME FTYPE
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef typename ObjectType::TableType TableType;
typedef typename TableType::conceptType conceptType;
typedef concept<conceptType> concept_Type;
ObjectType* This = reinterpret_cast<ObjectType*>(
reinterpret_cast<size_t>(this) -
reinterpret_cast<size_t>(
reinterpret_cast<concept_Type*>(0)->CONCEPTNAME##_index()));
return (This->object_pointer->*This->ptable->p##FNAME)PARA;
}
};
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
template
<
class
T
>
struct
get_type
...
{typedef T type;}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
concept_base;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
struct
concept_object
...
{}
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
template
<
class
conceptT
=
int
,
class
T
=
int
>
struct
concept_map
...
{int t[2];}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT,
class
BaseFrom
=
concept_base,
bool
isvtable
=
true
,
class
ObjectType
=
concept_object
>
struct
concept_bind_with
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
typedef typename conceptT::template table<BaseFrom, isvtable, ObjectType> type;
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
concept_base
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
typedef concept_base Base;
typedef concept_base DeriveFrom;
template <class BaseFrom = concept_base, bool isvtable = true, class ObjectType = concept_object>
struct table : public concept_bind_with<typename BaseFrom::Base, typename BaseFrom::DeriveFrom, isvtable, ObjectType>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef concept_base Base;
typedef BaseFrom DeriveFrom;
};
template <bool isvtable, class ObjectType>
struct table<concept_base, isvtable, ObjectType>
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef concept_base Base;
typedef concept_base DeriveFrom;
};
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
C0
=
concept_base,
class
C1
=
concept_base,
class
C2
=
concept_base,
class
C3
=
concept_base,
class
C4
=
concept_base,
class
C5
=
concept_base,
class
C6
=
concept_base,
class
C7
=
concept_base,
class
C8
=
concept_base,
class
C9
=
concept_base,
class
C10
=
concept_base,
class
C11
=
concept_base,
class
C12
=
concept_base,
class
C13
=
concept_base,
class
C14
=
concept_base,
class
C15
=
concept_base,
class
C16
=
concept_base,
class
C17
=
concept_base,
class
C18
=
concept_base,
class
C19
=
concept_base
>
struct
concept_list
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
template <class BaseFrom = concept_base, bool isvtable = true, class T = concept_object>
struct table : public
concept_bind_with< C0,typename concept_bind_with< C1,typename concept_bind_with< C2,typename concept_bind_with< C3,typename concept_bind_with< C4,
typename concept_bind_with< C5,typename concept_bind_with< C6,typename concept_bind_with< C7,typename concept_bind_with< C8,typename concept_bind_with< C9,
typename concept_bind_with<C10,typename concept_bind_with<C11,typename concept_bind_with<C12,typename concept_bind_with<C13,typename concept_bind_with<C14,
typename concept_bind_with<C15,typename concept_bind_with<C16,typename concept_bind_with<C17,typename concept_bind_with<C18,typename concept_bind_with<C19,
BaseFrom
>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type
>::type>::type>::type>::type>::type>::type>::type>::type>::type, isvtable, T>::type
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
typedef concept_list Base;
typedef BaseFrom DeriveFrom;
};
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
TableT
>
struct
concept_class_base
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
typedef TableT TableType;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
concept_object *object_pointer;
TableT *ptable;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
operator const concept_object&() const ...{ return *object_pointer; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
operator concept_object&() ...{ return *object_pointer; }
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT,
class
T
=
concept_object
>
struct
concept_table :
public
concept_bind_with
<
conceptT, concept_base,
true
, T
>
::type
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
typedef conceptT conceptType;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
typedef T* (*tcloneT)(const T *p);
typedef void (*tdeleteT)(T *p);
tcloneT pcloneT;
tdeleteT pdeleteT;
concept_table() :
pcloneT(reinterpret_cast<tcloneT>(&concept_table::cloneT)),
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
pdeleteT(reinterpret_cast<tdeleteT>(&concept_table::deleteT))...{}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
static concept_table* vtable()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
static concept_table static_table;
return &static_table;
}
static T *cloneT(const T *p)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return new T(*p);
}
static void deleteT(T *p)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
delete p;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT
>
class
auto_concept;
template
<
class
conceptT
>
class
ref_concept;
template
<
class
conceptT
>
class
ref_auto_concept;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT,
bool
isauto
=
false
,
bool
isref
=
false
>
class
concept :
public
concept_class_base
<
concept_table
<
conceptT
>
>
,
public
concept_bind_with
<
conceptT, concept_base,
false
, concept_class_base
<
concept_table
<
conceptT
>
>
>
::type
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public:
using concept_class_base<concept_table<conceptT> >::ptable;
using concept_class_base<concept_table<conceptT> >::object_pointer;
typedef concept_table<conceptT> TableType;
private:
void init_with(concept_object* p, TableType* pTable)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ptable = pTable;
if (isref)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
object_pointer = p;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
object_pointer = (*ptable->pcloneT)(p);
}
}
concept_object *cloneT() const
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return (*ptable->pcloneT)(object_pointer);
}
void deleteT()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
(*ptable->pdeleteT)(object_pointer);
}
public:
template <class T>
concept(const T& t)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (0) char static_assert[isauto || sizeof(concept_map<>) != sizeof(concept_map<conceptT, T>) ? 1 : -1];
typedef concept_table<conceptT, T> cTable;
init_with(const_cast<concept_object *>(reinterpret_cast<const concept_object *>(&t)),
(reinterpret_cast<TableType*>(cTable::vtable())));
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const concept& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const concept<conceptT, isauto, !isref>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const concept<conceptT, !isauto, isref>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const concept<conceptT, !isauto, !isref>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const auto_concept<conceptT>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const ref_concept<conceptT>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
concept(const ref_auto_concept<conceptT>& rhs) ...{ init_with(rhs.object_pointer, rhs.ptable); }
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
~concept()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!isref) deleteT();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
concept& operator = (const concept& rhs)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
concept(rhs).swap(*this);
return *this;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void swap(concept& rhs)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
concept_object *pointer0 = object_pointer;
TableType *ptable0 = ptable;
object_pointer = rhs.object_pointer;
ptable = rhs.ptable;
rhs.object_pointer = pointer0;
rhs.ptable = ptable0;
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT
>
class
auto_concept :
public
concept
<
conceptT,
true
,
false
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public:
template <class T>
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
auto_concept(const T& t) : concept<conceptT, true, false>(t) ...{}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT
>
class
ref_concept :
public
concept
<
conceptT,
false
,
true
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public:
template <class T>
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
ref_concept(const T& t) : concept<conceptT, false, true>(t) ...{}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
template
<
class
conceptT
>
class
ref_auto_concept :
public
concept
<
conceptT,
true
,
true
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public:
template <class T>
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
ref_auto_concept(const T& t) : concept<conceptT, true, true>(t) ...{}
}
;