parent
0871fd927f
commit
13c7266c9f
4 changed files with 264 additions and 39 deletions
@ -0,0 +1,152 @@ |
|||||||
|
/*! @file
|
||||||
|
|
||||||
|
@id $Id$ |
||||||
|
|
||||||
|
taken from boost.org boost::Any |
||||||
|
|
||||||
|
*/ |
||||||
|
// 1 2 3 4 5 6 7 8
|
||||||
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||||
|
|
||||||
|
// See http://www.boost.org/libs/any for Documentation.
|
||||||
|
|
||||||
|
#ifndef XML_CXX_ANY_INCLUDED |
||||||
|
#define XML_CXX_ANY_INCLUDED |
||||||
|
|
||||||
|
#include <algorithm> |
||||||
|
#include <typeinfo> |
||||||
|
#include <stdexcept> |
||||||
|
|
||||||
|
namespace xml { |
||||||
|
|
||||||
|
class Any { |
||||||
|
|
||||||
|
public: // structors
|
||||||
|
|
||||||
|
Any(): content(0) {} |
||||||
|
|
||||||
|
template<typename ValueType> Any(const ValueType& value): |
||||||
|
content(new holder<ValueType>(value)) { |
||||||
|
} |
||||||
|
|
||||||
|
Any(const Any& other): content(other.content?other.content->clone():0) {} |
||||||
|
|
||||||
|
~Any() { |
||||||
|
delete content; |
||||||
|
} |
||||||
|
|
||||||
|
public: // modifiers
|
||||||
|
|
||||||
|
Any& swap(Any& rhs) { |
||||||
|
std::swap(content, rhs.content); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
template<typename ValueType> Any& operator=(const ValueType& rhs) { |
||||||
|
Any(rhs).swap(*this); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
Any& operator=(const Any& rhs) { |
||||||
|
Any(rhs).swap(*this); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
public: // queries
|
||||||
|
|
||||||
|
bool empty() const { |
||||||
|
return !content; |
||||||
|
} |
||||||
|
|
||||||
|
const std::type_info& type() const { |
||||||
|
return content?content->type():typeid(void); |
||||||
|
} |
||||||
|
|
||||||
|
private: // types
|
||||||
|
|
||||||
|
class placeholder { |
||||||
|
public: // structors
|
||||||
|
virtual ~placeholder() {} |
||||||
|
virtual const std::type_info& type() const = 0; |
||||||
|
virtual placeholder* clone() const = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
template<typename ValueType> class holder: public placeholder { |
||||||
|
|
||||||
|
public: |
||||||
|
|
||||||
|
holder(const ValueType& value): held(value) {} |
||||||
|
|
||||||
|
virtual const std::type_info& type() const { |
||||||
|
return typeid(ValueType); |
||||||
|
} |
||||||
|
|
||||||
|
virtual placeholder* clone() const { |
||||||
|
return new holder(held); |
||||||
|
} |
||||||
|
|
||||||
|
ValueType held; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
private: // representation
|
||||||
|
|
||||||
|
template<typename ValueType> friend ValueType* any_cast(Any*); |
||||||
|
template<typename ValueType> friend ValueType& any_cast(Any&); |
||||||
|
|
||||||
|
placeholder* content; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
template<typename ValueType> ValueType* any_cast(Any* operand) { |
||||||
|
return operand && operand->type() == typeid(ValueType) |
||||||
|
? &static_cast<Any::holder<ValueType>*>(operand->content)->held |
||||||
|
: 0; |
||||||
|
} |
||||||
|
|
||||||
|
template<typename ValueType> const ValueType* any_cast(const Any* operand) { |
||||||
|
return any_cast<ValueType>(const_cast<Any*>(operand)); |
||||||
|
} |
||||||
|
|
||||||
|
template<typename ValueType> ValueType& any_cast(Any& operand) { |
||||||
|
if (operand.type()==typeid(ValueType)) |
||||||
|
return static_cast<Any::holder<ValueType>*>(operand.content)->held; |
||||||
|
throw std::bad_cast(); |
||||||
|
} |
||||||
|
|
||||||
|
template<typename ValueType> const ValueType& any_cast(const Any& operand) { |
||||||
|
return any_cast<ValueType>(const_cast<Any&>(operand)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
// Boost Software License - Version 1.0 - August 17th, 2003
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person or organization
|
||||||
|
// obtaining a copy of the software and accompanying documentation covered by
|
||||||
|
// this license (the "Software") to use, reproduce, display, distribute,
|
||||||
|
// execute, and transmit the Software, and to prepare derivative works of the
|
||||||
|
// Software, and to permit third-parties to whom the Software is furnished to
|
||||||
|
// do so, all subject to the following:
|
||||||
|
|
||||||
|
// The copyright notices in the Software and this entire statement, including
|
||||||
|
// the above license grant, this restriction and the following disclaimer,
|
||||||
|
// must be included in all copies of the Software, in whole or in part, and
|
||||||
|
// all derivative works of the Software, unless such copies or derivative
|
||||||
|
// works are solely in the form of machine-executable object code generated by
|
||||||
|
// a source language processor.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||||
|
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||||
|
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||||
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue