Added version by Fernando Cacciola (to be removed on release of next version of Boost).

[SVN r944]
This commit is contained in:
Eric Friedman
2003-02-11 05:00:19 +00:00
parent 2ded07a196
commit ff8afc877c

View File

@@ -1,189 +1,316 @@
//----------------------------------------------------------------------------- // (C) 2003, Fernando Luis Cacciola Carballal.
// boost optional.hpp header file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
// //
// Copyright (c) 2002 // This material is provided "as is", with absolutely no warranty expressed
// Eric Friedman // or implied. Any use is at your own risk.
// //
// Permission to use, copy, modify, distribute and sell this software // Permission to use or copy this software for any purpose is hereby granted
// and its documentation for any purpose is hereby granted without fee, // without fee, provided the above notices are retained on all copies.
// provided that the above copyright notice appears in all copies and // Permission to modify the code and to distribute modified code is granted,
// that both the copyright notice and this permission notice appear in // provided the above notices are retained, and a notice that the code was
// supporting documentation. No representations are made about the // modified is included with the above copyright notice.
// suitability of this software for any purpose. It is provided "as is" //
// without express or implied warranty. // See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// fernando_cacciola@hotmail.com
//
#ifndef BOOST_OPTIONAL_FLC_19NOV2002_HPP
#define BOOST_OPTIONAL_FLC_19NOV2002_HPP
#ifndef BOOST_OPTIONAL_HPP #include<new>
#define BOOST_OPTIONAL_HPP #include<algorithm>
#include <new> // for placement-new #include "boost/config.hpp"
#include "boost/assert.hpp"
#include "boost/type_traits/alignment_of.hpp"
#include "boost/type_traits/type_with_alignment.hpp"
#include "boost/aligned_storage.hpp" #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
#include "boost/move.hpp" // VC6.0 has the following bug:
// When a templated assignment operator exist, an implicit conversion
// constructing an optional<T> is used when assigment of the form:
// optional<T> opt ; opt = T(...);
// is compiled.
// However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
// Therefore, for VC6.0 templated assignment is disabled.
//
#define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
#endif
namespace boost { #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
// VC7.0 has the following bug:
// When both a non-template and a template copy-ctor exist
// and the templated version is made 'explicit', the explicit is also
// given to the non-templated version, making the class non-implicitely-copyable.
//
#define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
#endif
template <typename T> namespace boost
class optional
: public moveable< optional<T> >
{ {
typedef aligned_storage< namespace optional_detail
sizeof(T) {
, alignment_of<T>::value template <class T>
> storage_t; class aligned_storage
bool empty_;
storage_t storage_;
public: // structors
~optional()
{ {
clear(); // Borland ICEs if unnamed unions are used for this!
union dummy_u
{
char data[ sizeof(T) ];
type_with_alignment< ::boost::alignment_of<T>::value > aligner_;
} dummy_ ;
public:
void const* address() const { return &dummy_.data[0]; }
void * address() { return &dummy_.data[0]; }
} ;
}
template<class T>
class optional
{
typedef optional<T> this_type ;
typedef optional_detail::aligned_storage<T> storage_type ;
typedef void (this_type::*unspecified_bool_type)();
public :
typedef T value_type ;
// Creates an optional<T> uninitialized.
// No-throw
optional ()
:
m_initialized(false) {}
// Creates an optional<T> initialized with 'val'.
// Can throw if T::T(T const&) does
explicit optional ( T const& val )
:
m_initialized(false)
{
construct(val);
} }
optional() #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
: empty_(true) // NOTE: MSVC needs templated versions first
// Creates a deep copy of another convertible optional<U>
// Requires a valid conversion from U to T.
// Can throw if T::T(U const&) does
template<class U>
explicit optional ( optional<U> const& rhs )
:
m_initialized(false)
{ {
if ( rhs )
construct(*rhs);
}
#endif
// Creates a deep copy of another optional<T>
// Can throw if T::T(T const&) does
optional ( optional const& rhs )
:
m_initialized(false)
{
if ( rhs )
construct(*rhs);
} }
optional(const optional& operand) // No-throw (assuming T::~T() doesn't)
: empty_(operand.empty_) ~optional() { destroy() ; }
{
if (!empty_)
new(storage_.address()) T(operand.get());
}
optional(move_source<optional> source) #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
: empty_(source.get().empty_) // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
{ // Requires a valid conversion from U to T.
if (!empty_) // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
template<class U>
optional& operator= ( optional<U> const& rhs )
{
destroy(); // no-throw
if ( rhs )
{ {
optional& operand = source.get(); // An exception can be thrown here.
new(storage.address()) T( move(operand.get()) ); // It it happens, THIS will be left uninitialized.
construct(*rhs);
} }
} return *this ;
}
#endif
optional(const T& operand) // Assigns from another optional<T> (deep-copies the rhs value)
: empty_(false) // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
{ optional& operator= ( optional const& rhs )
new(storage_.address()) T(operand); {
} destroy(); // no-throw
optional(move_source<T> source) if ( rhs )
: empty_(false)
{
T& operand = source.get();
new(storage_.address()) T( move(operand) );
}
public: // modifiers
optional& operator=(const optional& rhs)
{
// If rhs is empty...
if (rhs.empty_)
{ {
// ...then simply clear *this and leave: // An exception can be thrown here.
clear(); // It it happens, THIS will be left uninitialized.
return *this; construct(*rhs);
} }
return *this ;
}
// Otherwise, assign rhs's content to *this: // Destroys the current value, if any, leaving this UNINITIALIZED
return (*this = rhs.get()); // No-throw (assuming T::~T() doesn't)
} void reset()
{
destroy();
}
optional& operator=(move_source<optional> source) // Replaces the current value -if any- with 'val'
{ // Basic Guarantee: If T::T( T const& ) throws this is left UNINITIALIZED.
// If rhs is empty... void reset ( T const& val )
if (rhs.empty_) {
{ destroy();
// ...then simply clear *this and leave: construct(val);
clear(); }
return *this;
}
optional& rhs = source.get(); // Returns a pointer to the value if this is initialized, otherwise,
// returns NULL.
// No-throw
T const* get() const { return m_initialized ? static_cast<T const*>(m_storage.address()) : 0 ; }
T* get() { return m_initialized ? static_cast<T*> (m_storage.address()) : 0 ; }
// Otherwise, move rhs's content to *this: // Returns a pointer to the value if this is initialized, otherwise,
return (*this = move(rhs.get())); // the behaviour is UNDEFINED
} // No-throw
T const* operator->() const { BOOST_ASSERT(m_initialized) ; return get() ; }
T* operator->() { BOOST_ASSERT(m_initialized) ; return get() ; }
optional& operator=(const T& rhs) // Returns a reference to the value if this is initialized, otherwise,
{ // the behaviour is UNDEFINED
// If *this is empty... // No-throw
if (empty_) T const& operator *() const { BOOST_ASSERT(m_initialized) ; return *get() ; }
{ T& operator *() { BOOST_ASSERT(m_initialized) ; return *get() ; }
// ...then copy rhs to *this's storage:
new(storage_.address()) T(rhs);
empty_ = false;
}
else
{
// ...otherwise, assign rhs to *this's content:
get() = rhs;
}
return *this; // implicit conversion to "bool"
} // No-throw
operator unspecified_bool_type() const { return m_initialized ? &this_type::destroy : 0 ; }
optional& operator=(move_source<T> source) // This is provided for those compilers which don't like the conversion to bool
{ // on some contexts.
T& rhs = source.get(); bool operator!() const { return !m_initialized ; }
// If *this is empty... private :
if (empty_)
{
// ...then move-construct rhs to *this's storage:
new(storage_.address()) T( move(rhs) );
empty_ = false;
}
else
{
// ...otherwise, move-assign rhs to *this's content:
get() = move(rhs);
}
return *this; void construct ( T const& val )
} {
new (m_storage.address()) T(val) ;
m_initialized = true ;
}
void swap(optional& operand) void destroy()
{ {
// Move *this into temporary storage... if ( m_initialized )
optional temp( move(get()) ); {
get()->~T() ;
m_initialized = false ;
}
}
// ...move operand into *this... bool m_initialized ;
*this = move(operand); storage_type m_storage ;
} ;
// ...and move temporary into operand: // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
operand = move(temp); // No-throw
} template<class T>
inline
T const* get_pointer ( optional<T> const& opt )
{
return opt.get() ;
}
void clear() template<class T>
{ inline
if (!empty_) T* get_pointer ( optional<T>& opt )
{ {
get().~T(); return opt.get() ;
empty_ = true; }
}
}
public: // queries // template<class OP> bool equal_pointees(OP const& x, OP const& y);
bool empty() const //
{ // Being OP a model of OptionalPointee (either a pointer or an optional):
return empty_; //
} // If both x and y have valid pointees, returns the result of (*x == *y)
// If only one has a valid pointee, returns false.
// If none have valid pointees, returns true.
// No-throw
template<class OptionalPointee>
inline
bool equal_pointees ( OptionalPointee const& x, OptionalPointee const& y )
{
return (!x) != (!y) ? false : ( !x ? true : (*x) == (*y) ) ;
}
T& get() // optional's operator == and != have deep-semantics (compare values).
{ // WARNING: This is UNLIKE pointers. Use equal_pointees() in generic code instead.
return *static_cast<T*>(storage_.address()); template<class T>
} inline
bool operator == ( optional<T> const& x, optional<T> const& y )
{ return equal_pointees(x,y); }
template<class T>
inline
bool operator != ( optional<T> const& x, optional<T> const& y )
{ return !( x == y ) ; }
//
// The following swap implementation follows the GCC workaround as found in
// "boost/detail/compressed_pair.hpp"
//
namespace optional_detail {
#ifdef __GNUC__
// workaround for GCC (JM):
using std::swap;
#endif
// optional's swap:
// If both are initialized, calls swap(T&, T&), with whatever exception guarantess are given there.
// If only one is initialized, calls I.reset() and U.reset(*I), with the Basic Guarantee
// If both are uninitialized, do nothing (no-throw)
template<class T>
inline
void optional_swap ( optional<T>& x, optional<T>& y )
{
if ( !x && !!y )
{
x.reset(*y); // Basic guarantee.
y.reset();
}
else if ( !!x && !y )
{
y.reset(*x); // Basic guarantee.
x.reset();
}
else if ( !!x && !!y )
{
#ifndef __GNUC__
using std::swap ;
#endif
swap(*x,*y);
}
}
} // namespace optional_detail
template<class T> inline void swap ( optional<T>& x, optional<T>& y )
{
optional_detail::optional_swap(x,y);
}
const T& get() const
{
return *static_cast<const T*>(storage_.address());
}
};
} // namespace boost } // namespace boost
#endif // BOOST_OPTIONAL_HPP #endif