您的位置:首页 > 编程语言 > C语言/C++

仿基因编程的C++源码

2008-04-08 23:58 399 查看
#ifndef __GWL_H__
#define __GWL_H__
//GWL名字空间的开始
#define GWL_BEGIN namespace gwl{
//GWL名字空间的结束
#define GWL_END }
//使用名字空间GWL
#define _GWL using namespace gwl;
#include
#include
#include
#include
#include
GWL_BEGIN
using namespace std;
typedef basic_string,
 allocator > gstring;
#ifdef _UNICODE
#define gout wcout
#else
#define gout cout
#endif
//构建基本错误描述
enum {OK,NOTIMPL,MATCHERR,TYPEERR,INPUTERR,OUTPUTERR,BINDERR,DEBINDERR,NULLBIND,UNKNOW};
typedef vector gerrortype;
class gwlErrorInfo
{
private:
 static const gerrortype& error()
{
 static gerrortype errors(UNKNOW+1);
 errors[OK] = _T("没有错误");
 errors[UNKNOW] =_T("不确定原因的错误");
 errors[NOTIMPL] =_T("这个处理没有被真正实现");
 errors[MATCHERR] =_T("处理匹配器不能构建");
 errors[TYPEERR] = _T("不能处理的数据类型");
 errors[INPUTERR] = _T("不能输入这个类型数据");
 errors[OUTPUTERR] = _T("不能输出这个类型数据");
 errors[BINDERR] = _T("数据不支持绑定操作");
 errors[DEBINDERR] = _T("数据不支持解除绑定操作");
 errors[NULLBIND] = _T("绑定操作不能针对空指针");
 return errors;
}
public:
 static const gerrortype& gwlErrors(){return error();}
};
#define errs gwlErrorInfo::gwlErrors()
//////////////////////////////////////////////////////////////
//为子类型识别定义一个宏.它把对象本身当做一个异常抛出
#define WHO virtual void Who(){throw this;}
//类Element,是编程元素的抽象,被抽象得只有一个特性:被认出身分
class Element
{
public:
 virtual ~Element(){}
 virtual void Who() =0;
 Element(){}
protected:
 explicit Element(const Element&){}
 Element& operator = (const Element&){return *this;}
};
//////////////////////////////////////////////////////////////
//类Company,用来把一些有关联的处理机组织到一起
//它相当于现实中的公司或机构
class Company:public Element
{
public:
 static const gstring& Name()
 {
  static gstring name = _T("company");
  return name;
 }
 WHO
};
//////////////////////////////////////////////////////////////
//所有数据的基类型,但它没有任何数据内容.
//它是对数据的高度抽象, 抽象到只有它是能被处理这一点特性了.
class Data:public Element{ };
//////////////////////////////////////////////////////////////
//版本信息
class VersionInfo:public Data
{
public:
 VersionInfo(unsigned int _major =1, unsigned int _minor =0,gstring _name = Company::Name() ):
   major(_major)
    ,minor(_minor)
    ,companyName(_name){}
   bool operator == (const VersionInfo& right)
   {
    return major == right.major
     && minor == right.minor
     && companyName == right.companyName;
   }
   unsigned int major;
   unsigned int minor;
   gstring  companyName;
   WHO
};
//////////////////////////////////////////////////////////////
//错误信息的携带类型.规定ID为零表示没有错误.
class ErrorInfo:public Data
{
public:
 ErrorInfo(unsigned int _id, gstring _info):id(_id),info(_info){}
   ErrorInfo(unsigned int _id = OK){
    id = _id%(UNKNOW+1);
    info = errs[id];
   }
   ErrorInfo& operator =( ErrorInfo& right){
    id = right.id;
    info= right.info;
    return *this;
   }
   unsigned int id;
   gstring  info;
   WHO
private:
};
//////////////////////////////////////////////////////////////
//对数据进行分类的模板.
template
class LocalData:public Data
{
public:
 typedef LocalData localType;
};
//////////////////////////////////////////////////////////////
//对错误类型分类的模板.
template
class LocalError:public ErrorInfo{
public:
 typedef LocalError localType;
 LocalError(unsigned int _id, gstring _info): ErrorInfo(_id, _info){}
 LocalError(unsigned int _id):ErrorInfo(_id){}
 WHO
};
//////////////////////////////////////////////////////////////
//数据群落
//通过数据群落中处理, 用户可以方便的实现自己想要的相关数据处理.
template
class DataCommunity:public Data
{
public:
 virtual void Set(PROCESSOR& match){}
 virtual void Get(PROCESSOR& match){}
};
//////////////////////////////////////////////////////////////
//处理机的基类型, processor
//它是系统中"数据处理块"的高度抽象. 抽象到只能处理数据了.
class Processor:public Element
{
public:
 Processor(){}
 virtual void Set(Data& data) {throw ErrorInfo(INPUTERR);} //数据流入
 virtual void Get(Data& data) {throw ErrorInfo(OUTPUTERR);} //数据流出
 virtual void Bind(Data* data) {throw ErrorInfo(BINDERR);}
 virtual void Debind(Data* data) {throw ErrorInfo(DEBINDERR);}
 virtual const ErrorInfo& LastError(){
  static ErrorInfo err(NOTIMPL);
  return err;
 };
protected:
 //防止因赋值操作和拷贝构造所带来的混乱
  Processor& operator =(Processor&){return *this;}
 explicit Processor(Processor& o){}//这是也防止混乱的.
};

//////////////////////////////////////////////////////////////
//宏CENOZOIC,用来新生一个处理机
#define CENOZOIC(NEW_PROCESSOR_NAME)/
class NEW_PROCESSOR_NAME:public Processor/
{/
public:/
 WHO/
};
////////////////////////////////
//定义一个从windows动态链接库进行处理机真正实现的建立方针
/////
template
class Windll
{
public:
 typedef PROCESSOR* (*CREATE)(PROCESSOR&);
 Windll():LibHandle(0){}
 ~Windll(){if (LibHandle) FreeLibrary(LibHandle);}
 PROCESSOR* Create(TCHAR* path)
 {
  CREATE exCreate;
  if(!path) return 0;
  LibHandle = LoadLibrary(path);
  if(!LibHandle)
   return 0;
  exCreate = (CREATE)GetProcAddress(LibHandle, "Create");
  if(!exCreate)
   return 0;
  return exCreate(PROCESSOR());
 }
private:
 HMODULE LibHandle;//处理机工厂所在的库的句柄
};
//////////////////////////////////////////////////////////////
//处理机匹配器.它有以下功能
//功能1:对处理机名柄进行智能包装
//功能2:对处理所能处理的数据进行匹配
template class ENVIONMENT = Windll>
class Match
{
public:
 typedef Match MatchType;//, ENVIONMENT> MatchType;//自己的类型
 typedef LocalData datatype;//所能处理的数据类型
 typedef DataCommunity datacomm;//所能处理的数据群类型
 Match(TCHAR* loc)
 {
  handle = envionment.Create(loc);
 }
 ~Match()
 {
  if(handle)
  {
  delete handle;
  handle = 0;
  }
 }
 operator bool(){
  return handle?true:false;
 }
 //单项数据输入
 MatchType& operator >> (datatype& data)
 {
  if(!handle) throw ErrorInfo(MATCHERR);
  handle->Get(data);
  return *this;
 }
 //单项数据输出
 MatchType& operator << (datatype& data)
 {
  if(!handle) throw ErrorInfo(MATCHERR);
  handle->Set(data);
  return *this;
 }
 //数据群落输出
 MatchType& operator<< (datacomm& dataSet)
 {
  if(!handle) throw ErrorInfo(MATCHERR);
  dataSet.Set(*handle);
  return *this;
 }
 // 数据群落输入
 MatchType& operator >>(datacomm& dataSet)
 {
  if(!handle) throw ErrorInfo(MATCHERR);
  dataSet.Get(*handle);
  return *this;
 }
 const ErrorInfo& LastError()
 {
    if(!handle) throw ErrorInfo(MATCHERR);
  return handle->LastError();
 }
private:
 PROCESSOR *handle;
 ENVIONMENT envionment;
};
GWL_END
#endif //__GWL_H__ 
 
//QQ:707351736
//
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: