免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2241 | 回复: 1
打印 上一主题 下一主题

[C++] 我自己简化的boost::any怎么能存取简单类型无法存取类? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-28 12:18 |只看该作者 |倒序浏览
自己简化了一下Any:
class Any {
        public:
                Any() : content(0){}
                template<typename ValType>
                Any(const ValType & value) : content(new holder<ValType>(value)){}

                Any(const Any & other) : content(other.content ? other.content->clone() : 0){}

                ~Any(){delete content;}

        public:
                template<typename ValType>
                Any & operator=(const ValType & rhs){
                        Any(rhs).swap(*this);        return *this;
                }
                bool empty() const{return !content;}
                const std::type_info & type() const        {
                        return content ? content->type() : typeid(void);
                }
        private:
                Any & swap(Any & rhs){
                        std::swap(content, rhs.content);
                        return *this;
                }

        private:
                class placeholder{
                public:
                        virtual ~placeholder(){}
                        virtual const std::type_info & type() const = 0;
                        virtual placeholder * clone() const = 0;
                };

                template<typename ValType>
                class holder : public placeholder{
                public:
                        holder(const ValType & value) : held(value){}
                public:
                        virtual const std::type_info & type() const        {return typeid(ValType);}

                        virtual placeholder * clone() const        {return new holder(held);}
                public:
                        ValType held;
                };

        private:
                template<typename ValType>
                friend ValType * any_cast(Any *);

                placeholder * content;

        };

        template<typename ValType>
        inline ValType * any_cast(Any * any){
                return any && any->type() == typeid(ValType)?
                        &static_cast<Any::holder<ValType> *>(any->content)->held : 0;
        }

        template<typename ValType>
        ValType any_cast(const Any &any){
                ValType * val = any_cast<ValType>(&any);
                if(val == 0)        throw "invalid type-casting";
                return *val;
        }

之后想用于map<string,Any>,往里存取double等简单类型无误,例如:
mp["ddd"]= val;
但放入类就有问题,例如:
class A
{
        public:
                enum ATYPE{AT_X,AT_Y};
                virtual ATYPE getType() = 0;
};
class X:public A
{
        public:
                ATYPE getType(){return AT_X;};
                void print();
        public:
                string x1;
                double x2;
};

一样:mp["XXX"] = val;注意这里可以用type()取类型。
取的时候:

        std::map<std::string,Any>::iterator it(mp.find(key));
        if(it == mp.end())        return Any(std::string(""));
        return it->second;
到it->second返回时就segment fault。在这里想用type()取类型也一样segment fault,不知错在哪儿。

论坛徽章:
0
2 [报告]
发表于 2011-10-28 17:34 |只看该作者
还是靠自己啊。做了两件事:
1、删除.o重新编译。
2、ValType any_cast(const Any &any){
                 ValType * val = any_cast<ValType>(&any);
                 if(val == 0)        throw "invalid type-casting";
                 return *val;
         }
把参数改为Any any,我的编译器不支持这玩意,只能牺牲效率了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP