zoukankan      html  css  js  c++  java
  • boost::any 用法

    boost::any可以存放任何类型的C++类型,也可以是用户自定义的类型。非常方便,可以很方便的满足在运行过程中判断数据类型,从而进行相关的操作。

    函数原型:

    // In header: <boost/any.hpp>
    
    
    class any {
    public:
      // construct/copy/destruct
      any();
      any(const any &);
      template<typename ValueType> any(const ValueType &);
      any & operator=(const any &);
      template<typename ValueType> any & operator=(const ValueType &);
      ~any();
    
      // modifiers
      any & swap(any &);
    
      // queries
      bool empty() const;
      const std::type_info & type() const;
    };

    成员函数说明:

       Boost::Any的实现比较简单,Any拥有一个模版构造函数,这使他可以接受任何类型的对象。真正的变量内容被封装在嵌套类类型的成员变量中,并且在嵌套类中使用typeid来记录真正的类型信息。

      1) any& swap(any& other);交换存在两个 any 对象中的值。
      2) any& operator=(const any& other);如果any实例非空,则丢弃所存放的值,并存入other值的拷贝。
      3)template<typename ValueType>  any& operator=(const ValueType& value);如果any实例非空,则丢弃所存放的值,并存入 value 的一份拷贝,value可以是任意符合any要求的类型。
      4) bool empty() const;给出any实例当前是否有值,不管是什么值。因而,当any持有一个指针时,即使该指针值为空,则 empty也返回 false 。
      5) const std::type_info& type() const;给出所存值的类型。如果 any 为空,则类型为 void.
      6) any_cast()将any类型转化为真实的类型,如果是指针返回的可能是空指针,如果非指针,则可能会抛出异常。

    三 实例

    1)简单实例以及调用常用的成员函数:

    #include <iostream>
    #include <string>
    #include <utility>
    #include <vector>
    #include "boost/any.hpp"
    
    class A 
    {
    public:  
        void some_function() 
        { 
            std::cout << "A::some_function()
    "; 
        }
    };
    class B
    {
    public:  
        void some_function() 
        { 
            std::cout << "B::some_function()
    "; 
        }
    };
    
    void print_any(boost::any& a)
    {  
        if (A* pA=boost::any_cast<A>(&a)) 
        {   
            pA->some_function();  
        }  
        else if (B* pB=boost::any_cast<B>(&a))
        {   
            pB->some_function();  
        }      
        else
        {   
            try 
            {     
                std::cout << boost::any_cast<std::string>(a) << '
    ';   
            }   
            catch(boost::bad_any_cast&) 
            {     
                std::cout << "Oops!
    ";   
            }  
        }
    }
    
    int main() 
    {  
        std::cout << "Example of using any.
    
    "; 
        std::vector<boost::any> store_anything;  
        store_anything.push_back(A());  
        store_anything.push_back(B());  
        // 我们再来,再加一些别的东西  
        store_anything.push_back(std::string("This is fantastic! "));  
        store_anything.push_back(3);  
        store_anything.push_back(std::make_pair(true, 7.92));     
        std::for_each(  store_anything.begin(),  store_anything.end(),  print_any);
    
    
         std::cout << "Example of using any member functions
    
    "; 
         boost::any a1(100); 
         boost::any a2(std::string("200"));  
         boost::any a3; 
         std::cout << "a3 is "; 
         if (!a3.empty()) 
         {    
             std::cout << "not empty
     "; 
         }  
         std::cout << "empty
    ";  
         a1.swap(a2); 
         try 
         {   
             std::string s=boost::any_cast<std::string>(a1); 
             std::cout << "a1 contains a string: " << s << "
    "; 
         } 
         catch(boost::bad_any_cast& e) 
         {  
             std::cout << "I guess a1 doesn't contain a string!
    "; 
         } 
         if (int* p=boost::any_cast<int>(&a2)) 
         {  
             std::cout << "a2 seems to have swapped contents with a1: "      << *p << "
    "; 
         } 
         else
         {    
             std::cout << "Nope, no int in a2
    "; 
         }  
         if (typeid(int)==a2.type()) 
         {    
             std::cout << "a2's type_info equals the type_info of int
    ";  
         }
    
    }
     
    
    2)解决类似与map但是可以映射到各种不同的类型的问题:
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include "boost/any.hpp"
    class property
    {  
        boost::any value_;
        std::string name_;
    public:  
        property(const std::string& name,const boost::any& value)  
            : name_(name),value_(value) {}  
        std::string name() const { return name_; } 
        boost::any& value() { return value_; } 
        friend bool operator<(const property& lhs, const property& rhs)
        {return lhs.name_<rhs.name_;}
    };
    void print_names(const property& p) 
    { 
        std::cout << p.name() << "
    ";
    }
    int main() 
    {  
        std::cout << "Example of using any for storing properties.
    ";
        std::vector<property> properties; 
        properties.push_back(  property("B", 30));
        properties.push_back(  property("A", std::string("Thirty something"))); 
        properties.push_back(property("C", 3.1415));  
        std::sort(properties.begin(),properties.end());
        std::for_each(properties.begin(),  properties.end(),  print_names);
        std::cout << "
    ";  
        std::cout <<  boost::any_cast<std::string>(properties[0].value()) << "
    "; 
        std::cout <<  boost::any_cast<int>(properties[1].value()) << "
    "; 
        std::cout <<  boost::any_cast<double>(properties[2].value()) << "
    ";    
    }

    四 注意

    1)Any中如果是指针,要注意指针的最后的释放,最好使用shared_ptr来管理。
    2)Any中如果是指针,如果Any.isempty()返回false,但是any所包含的指针仍可能是无效的。
    3)Any中如果是指针,则在调用any_cast()转化的过程中不会抛出异常,但是如果是一般变量或引用的话,类型不正确会抛出boost::bad_any_cast异常。

    五 参考

    1)Beyond the C++ Standard Library: An Introduction to Boost
    2)boost在线document

    参考:http://www.cppblog.com/mzty/archive/2007/08/16/30156.html

    any类实现:

    boost::any类并不是一个模板类,这可以大大的方便上层应用的使用,它会自动化的类型转换。核心就是any类中,包含一个模板类holder的基类placeholder指针,而placeholder却不是模板类,这样就可以被any类使用了。

    具体的如下面所示:

    http://www.cnblogs.com/wuerping/articles/116414.html

    更多:http://blog.csdn.net/hityct1/article/details/4186962

  • 相关阅读:
    Tiburon 支持 Unicode 的 LoadFromFile, SaveToFile
    截图:截取当前程序的界面,并保存到bmp图片中。
    List和string
    Can/CanOpen资料
    C语言的字节对齐及#pragma pack的使用
    Delphi操作Excel(2) Stringgrid导出到Excel
    zt:16进制Motorola Srecords 文件格式
    Python核心教程(第二版)读书笔记(二)
    Python核心教程(第二版)读书笔记(一)
    Python核心教程(第二版)读书笔记(三)
  • 原文地址:https://www.cnblogs.com/youxin/p/4451950.html
Copyright © 2011-2022 走看看