2005/12/01

boost::any

boost::any 是一个可以存放多种型别数据的容器,并且保留了原数据的型别。
any 可存放的型别 ValueType 最少要满足如下条件:
  • CopyConstructible
  • optionally Assignable. 所有形式的 assignment 都要求有强异常安全保证。
  • destructor 不能抛出异常。

头文件 <boost/any.hpp>

namespace boost {
  class bad_any_cast;
  class any;
  template<typename ValueType> ValueType any_cast(const any &);
  template<typename ValueType> const ValueType * any_cast(const any *);
  template<typename ValueType> ValueType * any_cast(any *);
}
对一个 any 进行 any_cast 失败将抛出 boost::bad_any_cast 异常。

Synopsis

class
bad_any_cast : public std::bad_cast { public: virtual const char *
what() const; };
 
boost::any 是这个库的主要组件。

Synopsis

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;
};

Description

any construct/copy/destruct

  1. any();

    Postconditions: this->empty()

  2. any(const any & other);

    Effects: Copy constructor 将 other 中的内容复制到新的 instance 中,新的 instance 中的内容与 other 中是相等的,不论是型别还是值。如果 other 是 empty ,那么新的 instance 也是 empty 。

  3. Throws: 可能会抛出 std::bad_alloc 异常或者是任何来自被包含型别的 copy constructor 中的异常。

  4. template<typename ValueType> any(const ValueType & value);

    Effects: 制造一个 value 的副本,新的 instance 中的内容与 value 相等,不论是型别还是值。 Throws: std::bad_alloc 或者任何来自被包含型别的 copy constructor 中的异常。

  5. any & operator=(const any & rhs);

    Effects: 复制 rhs 中的内容到当前 instance ,放弃以前的内容, instance 中的新内容与 rhs 中是相等的,不论是型别还是值。如果 rhs 是 empty ,那么 instance 也是 empty 。 Throws: std::bad_alloc 或者任何来自被包含型别的 copy constructor 中的异常。 Assignment 满足强异常安全保证。

  6. template<typename ValueType> any & operator=(const ValueType & rhs);

    Effects: 制造一个 rhs 的副本,放弃以前的内容, 新内容与 rhs 相等,不论是型别还是值。 Throws: std::bad_alloc 或者任何来自被包含型别的 copy constructor 中的异常。 Assignment 满足强异常安全保证。

  7. ~any();

    Effects: 释放 instance 管理的所有被使用的资源。 Throws: Nothing.

any modifiers

  1. any & swap(any & rhs);

    Effects: 交换 *this 和 rhs 的内容。 Returns: *this Throws: Nothing.

any queries

  1. bool empty() const;

    Returns: true if instance is empty, otherwise false. Throws: Will not throw.

  2. const std::type_info & type() const;

    Returns: 如果 instance 非空,返回被包含值的 typeid 。否则返回 typeid(void). Notes: Useful for querying against types known either at compile time or only at runtime.

要取得 any 中的值,就必须使用 boost::any_cast 函数。

Synopsis

template<typename ValueType> ValueType any_cast(const any & operand);
template<typename ValueType> const ValueType * any_cast(const any * operand);
template<typename ValueType> ValueType * any_cast(any * operand);

Description

Returns: 如果传入一个 pointer , 操作成功则返回一个指向 operand 的内容 value 的指针,它有着与 value 相同的受限条件;否则返回 null 。如果传入一个值或引用,操作成功则返回内容 value 的一个副本。 Throws: 指针的重载版本不抛出异常;接受值或引用的重载版本如果操作失败就抛出 bad_any_castRationale: 值/引用版本返回一个副本,这是因为 C++ 内部转换运算返回的是副本。

值得注意的是 boost::any 是保留所包含的值的型别的, any_cast 在实现的时候先判断要转换的目标型别与原本所持有的型别的 typeid ,两者必须相等。因此 any 不能用来将数据在各种型别间转换。

没有评论: