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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性