📄 anyarray.h
字号:
// -*- C++ -*-
//##############################################################################
// anyarray.h
// The dnc Module
// Copyright (c) Dreamsoft Mark Zhao
// Create Date:2007-3-15 10:04:09
//##############################################################################
#ifndef __DNC_ANYARRAY_H__
#define __DNC_ANYARRAY_H__
#ifndef __DNC_TYPELIST_H__
#include "typelist.h"
#endif
#ifndef __DNC_TYPETRAITS_H__
#include "typetraits.h"
#endif
namespace dnc{
//AnyArrayImpl 比 AnyArray 多了一个记数器i,对于生成的所有的基类,都有一个唯一
//的索引,这样每一个基类都是唯一的,防止了向下转换时的不确定性.
template <class List, template <class> class Unit,int i>
class AnyArrayImpl;
template <class T1, class T2, template <class> class Unit,int i>
class AnyArrayImpl<TypeList<T1, T2>, Unit,i>
: public AnyArrayImpl<T1, Unit,i>
, public AnyArrayImpl<T2, Unit,i+1>
{
public:
typedef AnyArrayImpl<T1, Unit,i> LeftBase;
typedef AnyArrayImpl<T2, Unit,i+1> RightBase;
template <typename T> struct Rebind{
typedef Unit<T> Result;
};
template<class T>
AnyArrayImpl(T *vals) : LeftBase(vals),RightBase(vals+1){}
AnyArrayImpl(){}
};
template <class AtomicType, template <class> class Unit,int i>
class AnyArrayImpl : public Unit<AtomicType>
{
public:
typedef Unit<AtomicType> LeftBase;
template <typename T> struct Rebind{
typedef Unit<T> Result;
};
template<class T>
AnyArrayImpl(T *vals) : LeftBase(*vals){}
AnyArrayImpl(){}
};
template <template <class> class Unit,int i>
class AnyArrayImpl<NullType, Unit,i>{
public:
template<class T>
AnyArrayImpl(T *vals){}
AnyArrayImpl(){}
};
//帮助AnyArray::at()完成索引访问操作
template <class A,int i,class E> struct AnyArrayAtHelper;
template<class A,class E>
struct AnyArrayAtHelper<A,0,E>{
typedef typename A::template Rebind<E>::Result UnitType;
typedef typename A::LeftBase LeftBase;
static UnitType& at(A &obj){
return static_cast<LeftBase&>(obj);
}
static const UnitType& at(const A &obj){
return static_cast<const LeftBase&>(obj);
}
};
template<class A,int i,class E>
struct AnyArrayAtHelper{
typedef typename A::template Rebind<E>::Result UnitType;
typedef typename A::RightBase RightBase;
static UnitType& at(A &obj){
return AnyArrayAtHelper<RightBase,i-1,E>::at(obj);
}
static const UnitType& at(const A &obj){
return AnyArrayAtHelper<RightBase,i-1,E>::at(obj);
}
};
/** AnyArray的默认单元结构
* @param T anything type
*/
template <class T>
struct AnyArrayUnit{
typedef typename remove_reference<T>::type RealType;
AnyArrayUnit(const RealType &val) : value(val){}
AnyArrayUnit(RealType &val) : value(val){}
AnyArrayUnit(){}
T value;
};
/** 任意数组,用它我们可以产生每个元素的类型都不一样的数组,然后通过索引可以访
* 问数组中的元素.
* @param List the TypeList
* @param Unit same AnyArrayUnit
* @note If List have reference type,then provide initialization values for
* the controctor.
* <code>
* int initVals[] = {90,30};
* dnc::AnyArray<dnc::TypeLine<int,int>::types>
* vals(initVals);
* vals.at<0>() == 70;
* vals.at<1>() == 90;
* </code>
*/
template <class List, template <class> class Unit = AnyArrayUnit>
class AnyArray:public AnyArrayImpl<List,Unit,0>
{
public:
typedef AnyArrayImpl<List,Unit,0> Base;
template<class T>
AnyArray(T *vals):Base(vals){}
AnyArray(){}
template<int i>
typename remove_reference<typename TL::TypeAt<List,i>::Result>::type&
at(){
return AnyArrayAtHelper<AnyArray,i,
typename TL::TypeAt<List,i>::Result>::at(*this).value;
}
template<int i>
const typename remove_reference<typename TL::TypeAt<List,i>::Result>::type&
at() const{
return AnyArrayAtHelper<AnyArray,i,
typename TL::TypeAt<List,i>::Result>::at(*this).value;
}
};
}
#endif //__DNC_ANYARRAY_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -