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

c++ 通用类型

2016-04-14 20:28 453 查看

c++ 通用类型 类似any

修复了很多bug,现在应该基本没问题了。使用智能指针需要c++11的支持。

virtual_type之间赋值,传递的是引用。virtual_type与其他类型间赋值会产生一个拷贝。

带类型验证,但是只能在运行时报错,不能在编译阶段实现。

virtual_type v1,v2,v3;

int c = 100;

v1 = v2 = v3 = c;

cout << v1.cast() << endl;

v1.modify(45);

//v1.modify(4.0); wrong!

c = 145;

cout << v2.cast() << endl;

cout << v3.cast() << endl;

实现:

/*!
* \file basic.h
*
* \author erow
* \date 四月 2016
*
*   定义基本数据类型
*/

#pragma once
#include <memory>
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
using std::shared_ptr;
using std::string;
using std::cout;
class __base_type {
public:
virtual const char* get_id_name() = 0;//验证类型名称
};
/*!
* \class real_type
*
* \brief 数据实体:需要使用类模板来创建。
*
* \author erow
* \date 四月 2016
*/
template<class T>
class real_type :public __base_type
{
public:
const T&  get_value() {
return m_data;
}
const char* get_id_name() { return typeid(T).name(); }

void assign(const T& data) { m_data = data; }
real_type() {}
~real_type() {}

private:
T m_data;
};
/*!
* \class virtual_type
*
* \ingroup microcloud
*
* \brief boost:any.can accept any type
*
* USEAGE:
string str="asd";
virtual_type vs = str, vs1;
vs1 = vs;
cout << vs.cast<string>() << endl;
cout << vs1.cast<string>();
*
* \note There are maybe some mistakes
*
* \author erow
*
* \version 1.0
*
* \date 四月 2016
*
* Contact: clouderow@gmail.com
*
*/
class virtual_type {
public:
virtual_type() :policyPtr(nullptr) {}
virtual_type(const virtual_type& x)
{
policyPtr = x.policyPtr;
}

const virtual_type& operator=(const virtual_type& x)
{
policyPtr = x.policyPtr;
return *this;
}
template<typename T>
virtual_type(const T& x) {
policyPtr = std::make_shared<real_type<T> >();
(static_cast<real_type<T>*>(policyPtr.get()))->assign(x);
}
template<typename T>
const virtual_type& operator=(const T& x) {

policyPtr = std::make_shared<real_type<T> >() ;
(static_cast<real_type<T>*>(policyPtr.get()))->assign(x);
return *this;
}
/*!
* \brief cast 返回的是一个拷贝
*/
template<typename T>
T cast()
{
//cout << typeid(T).name() << "\n" << policyPtr->get_id_name();
if (policyPtr!=nullptr)
{
assert(strcmp(typeid(T).name(), policyPtr->get_id_name()) == 0);//确保类型相同
return (static_cast<real_type<T>*>(policyPtr.get()))->get_value();
}
throw std::exception("nullptr");
return T();
}
/*!
* \brief modify 修改内容
*/
template<typename T>
void modify(const T& data)
{
if (policyPtr != nullptr)
{
assert(strcmp(typeid(T).name(), policyPtr->get_id_name()) == 0);
(static_cast<real_type<T>*>(policyPtr.get()))->assign(data);
}
else        throw std::exception("nullptr");

}
bool is_empty() {
return policyPtr == nullptr;
}

~virtual_type() {

}
void print_type()
{
cout << policyPtr->get_id_name() << std::endl;
}
private:

shared_ptr<__base_type> policyPtr;

};


参考代码

http://www.2cto.com/kf/201211/170363.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ any