Sender: Flier (Little Sea (: Good days :)), the letter: programming Title: Boost :: Any Send Station: BBS Shuimu Tsinghua Station (Mon May 13 12:02:24 2002)
I am coming to throw, some people follow up? :)
Boost :: Any
Boost :: Any is a very interesting class, just started me, I thought it was a variant type, and it was able to save any type values and read it in any type of value, but I was wrong: (Boost :: Any author It is believed that the so-called Generic Type has three levels of interpretation methods: 1. Type of transitions like the Variant type, you can save one (int) 5 goes in, reading one (string "5" out. Win The following Variant Type is one The similar functionality of huge union implements, the use of flexible but efficiency 2. Difference to the type containing the value, save one (int) 5, will not be implicitly converted to (String) '5' or (double) 5.0, Such efficiency is high and safe, do not worry about Ambiguous Conversions 3. If the included value type is not distinguished, such as forcing all saved values to void * Save Read, there is a programmer to determine its type. This efficiency is highest but Unable to guarantee the type of security boost :: any, select the second level of design ideas, which allows users to save any type value into an ANY type variable, but inside does not change the type of value, and provide methods to make users actively / Passive Type Judgment.
In terms of implementation, Boost :: Any uses two internal class PlaceHolder and Holder to save the actual type of value. Class PlaceHold is just an interface, template class Holder is a specific type of implementation. The type () method obtains the actual value type, namely TypeId (Valuetype); Clone () method Gets the copy return new Holder (HELD); Virtual const st: type_info & type () Const Virtual PlaceHolder * Clone () Const The type of value is not as specifically fields like WIN, but is static in the form of template parameters. This is more efficient (only in the compile),
Strong versatility (any type can, real ANY), but flexibility is not as good as Variant, replace the entire PlaceHolder directly when copying construction / assignment / swap, which ensures the continuation of the value type.
In terms of use, a method of active / passive type detection is provided. You can use any :: type () method to actively detect value type BOOL IS_INT (const boost :: any "{RETURN OPERAND.TYPE () == TypeId (int);} can also be detected via an ANY_CAST function. This function has the same syntax specification as the * _cast family keyword in C , attempts to make type conversion, if the type does not match, the pointer conversion returns NULL, throws Boost :: BAD_ANE_CAST exception, Boost :: Any uses two internal class PlaceHolder and Holder to save the actual type of value. Class PlaceHold is just an interface, template class Holder is a specific type of implementation. The type () method obtains the actual value type, namely TypeId (Valuetype); Clone () method Gets the copy return new Holder (HELD); Virtual const st: type_info & type () Const Virtual PlaceHolder * Clone () Const The type of value is not as specifically fields like WIN, but is static in the form of template parameters. Such efficiency is higher (only in the compile period), more versatility (any type can, real ANY), but flexibility is not as good as Variant to copy the entire PlaceHolder, so that it can be guaranteed The continuation of the value type.
In terms of use, a method of active / passive type detection is provided. You can use any :: type () method to actively detect value type BOOL IS_INT (const boost :: any "{RETURN OPERAND.TYPE () == TypeId (int);} can also be detected via an ANY_CAST function. This function has the same syntax specification as the * _cast family keyword in C , attempts to make type conversion, if the type does not match, the pointer is converted back to NULL, and the reference conversion throws boost :: Bad_Any_cast exception boost :: any str = string ("12345"); try {cout << Boost :: any_cast
In terms of application, the ANY type is suitable for a different type but uses the associated value. Such as C ... Formal Function Parameters This is not type safe, can be transformed through Vector
// What: Variant Type Boost :: Any // Who: Contributed by Kevlin Henney, // with features contributed and bugs found by // Ed Brey, Mark Rodgers, Peter Dimov, And James Curran // When: July 2001 // WHERE: TESTED WITH BCC 5.5, MSVC 6.0, And G 2.95
#include
#include "boost / config.hpp"
~ any () {Delete Content;} public: // modifiers
Any & Swap (any & r Hs) {std :: swap (content, rhs.content); return * this;}
Template
Any & Operator = (const any () {~ any () {delete content;}
Public: // modifier
Any & Swap (any & r Hs) {std :: swap (content, rhs.content); return * this;}
Template
Any & Operator = (const any (rhs) {any (rhs) .swap (* this); return * this;}
Public: // queries
Bool Empty () const {return!
Const std :: type_info & type () const {return content? content-> type (): typeid (void);
#ifndef boost_no_member_template_friends private: // types # else public: // types (public so any_cast can be non--friend) #ENDIF
ANY (RHS) .swap (* this); return * this;}
Public: // queries
Bool Empty () const {return!
Const std :: type_info & type () const {return content? content-> type (): typeid (void);
#ifndef boost_no_member_template_friends private: // types # else public: // types (public so any_cast can be non--friend) #ENDIF
Holder (Const ValueType & Value): Held (Value) {}
Public: // queriesvirtual const st: type_info & type () const {return type;}
Virtual PlaceHolder * Clone () const {
Return New Holder (HELD);
Public: // representation
ValueType Held;
Holder (Const ValueType & Value): Held (Value) {}
Public: // queries
Virtual const st :: type_info & type () const {return typeid (value);
Virtual PlaceHolder * Clone () const {
Return New Holder (HELD);
Public: // representation
ValueType Held;
Virtual const char * what () const throw () {return "boost :: bad_any_cast:" Failed conversion use boost :: any_cast ";}}
template
Template
Template
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.//// Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // Permissions Notice Appear in all copies and derivatives.//// this Software is provided "As IS" without express or implied warranty.
#ENDIF
-. The meaning of life is // ____ / // _ / // _ /. Hope / / ___ / _ /// / // _ / __ __ _ _ ★. Work / / ____ // / / / / / '__ `/ //`' _ /. Love you / / / ___ / / / / ___ / //// __ /// //.. And you love people / / ___ / / / / / / ____ / / _ // ___ / / / / / / // ____ /// __ /// ____ / /// _ / @ 126.com. ※ Modification: · Flier in May 13 12:03:25 Modified this article · [From: 202.114.32.217] ※ Source: · BBS Shuimu Tsinghua Station bbs.edu.cn · [From: 202.114.32.217]