From: Baptiste L. <bl...@us...> - 2004-08-05 20:52:56
|
Update of /cvsroot/cpptool/CppParser/include/cpputtools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20897/include/cpputtools Added Files: configuration.h convertor.h ctti.h refcountptr.h value.h valuebinder.h Log Message: * upgraded to current cppunit 2 cvs --- NEW FILE: value.h --- #ifndef CPPUTTOOLS_VALUE_H_INCLUDED # define CPPUTTOOLS_VALUE_H_INCLUDED # include "ctti.h" # include <stdexcept> namespace CppUTTools { class ValueBadCast : public std::bad_cast { public: ValueBadCast() : std::bad_cast( "CppUTTools::ValueBadCast" ) { } }; namespace Impl { class ValueHolder { public: virtual ~ValueHolder() {} virtual TypeId type() const = 0; virtual ValueHolder *clone() const = 0; virtual void *pointer() = 0; }; template<class ValueType> class ValueHolderImpl : public ValueHolder { public: ValueHolderImpl( const ValueType &value ) : value_( value ) { } const ValueType &value() const { return value_; } public: // overridden from ValueHolder TypeId type() const { return typeId<ValueType>(); } void *pointer() { return &value_; } ValueHolder *clone() const { return new ValueHolderImpl<ValueType>( value_ ); } private: ValueType value_; }; } // namespace Impl class Value { public: Value(); Value( char value ); Value( bool value ); Value( short value ); Value( int value ); Value( long value ); Value( unsigned short value ); Value( unsigned int value ); Value( unsigned long value ); Value( float value ); Value( double value ); Value( long double value ); Value( const std::string &value ); Value( const std::wstring &value ); Value( const Value &other ); Value &operator =( const Value &other ); TypeId type() const; void swap( Value &other ); bool hasSameType( const Value &other ) const; bool isEmpty() const; const void *valuePointer() const; void *valuePointer(); void copyValueTo( void *storage ); //private: // not private for access by get/set/cast function Value( Impl::ValueHolder *holder ); Impl::ValueHolder *holder_; }; template<class ValueType> Value value( const ValueType &newValue ) { return Value( new Impl::ValueHolderImpl<ValueType>( newValue ) ); } // class Value // /////////////////////////////////////////////////////////////// inline Value::Value() : holder_( 0 ) { } inline Value::Value( bool value ) : holder_( new Impl::ValueHolderImpl<bool>( value ) ) { } inline Value::Value( char value ) : holder_( new Impl::ValueHolderImpl<char>( value ) ) { } inline Value::Value( short value ) : holder_( new Impl::ValueHolderImpl<short>( value ) ) { } inline Value::Value( int value ) : holder_( new Impl::ValueHolderImpl<int>( value ) ) { } inline Value::Value( long value ) : holder_( new Impl::ValueHolderImpl<long>( value ) ) { } inline Value::Value( unsigned short value ) : holder_( new Impl::ValueHolderImpl<unsigned short>( value ) ) { } inline Value::Value( unsigned int value ) : holder_( new Impl::ValueHolderImpl<unsigned int>( value ) ) { } inline Value::Value( unsigned long value ) : holder_( new Impl::ValueHolderImpl<unsigned long>( value ) ) { } inline Value::Value( float value ) : holder_( new Impl::ValueHolderImpl<float>( value ) ) { } inline Value::Value( double value ) : holder_( new Impl::ValueHolderImpl<double>( value ) ) { } inline Value::Value( long double value ) : holder_( new Impl::ValueHolderImpl<long double>( value ) ) { } inline Value::Value( const std::string &value ) : holder_( new Impl::ValueHolderImpl<std::string>( value ) ) { } inline Value::Value( const std::wstring &value ) : holder_( new Impl::ValueHolderImpl<std::wstring>( value ) ) { } inline Value::Value( const Value &other ) : holder_( other.holder_ ? other.holder_->clone() : 0 ) { } inline Value & Value::operator =( const Value &other ) { Value tmp( other ); swap( tmp ); return *this; } inline TypeId Value::type() const { return holder_ ? holder_->type() : typeId<void>(); } inline void Value::swap( Value &other ) { Impl::ValueHolder *tmp = holder_; holder_ = other.holder_; other.holder_ = tmp; } inline bool Value::hasSameType( const Value &other ) const { return type() == other.type(); } inline bool Value::isEmpty() const { return holder_ == 0; } inline Value::Value( Impl::ValueHolder *holder ) : holder_( holder ) { } inline const void * Value::valuePointer() const { // this const cast avoid extra virtual function Impl::ValueHolder *holder = const_cast<Impl::ValueHolder *>( holder_ ); return holder ? holder->pointer() : 0; } inline void * Value::valuePointer() { return holder_ ? holder_->pointer() : 0; } } // namespace CppUTTools // Those function are imported in the global namespace since all compiler don't have // argument dependent look-up. They all take type 'Value' in parameter which is in a namespace. template<class ValueType> CppUTTools::Value &set( CppUTTools::Value &value, const ValueType &newValue ) { return value = CppUTTools::Value( new Impl::ValueHolderImpl<ValueType>( newValue ) ); } template<class ValueType> const ValueType &get( const CppUTTools::Value &value ) { if ( value.type() != CppUTTools::typeId<ValueType>() ) throw CppUTTools::ValueBadCast(); return static_cast<CppUTTools::Impl::ValueHolderImpl<ValueType> *>( value.holder_ )->value(); } template<class ValueType> const ValueType *get( const CppUTTools::Value *value ) { if ( value.type() != CppUTTools::typeId<ValueType>() ) return 0; return static_cast<CppUTTools::Impl::ValueHolderImpl<ValueType> *>( value.holder_ )->value(); } #endif // CPPUTTOOLS_VALUE_H_INCLUDED --- NEW FILE: configuration.h --- #ifndef COMMANDLINE_OPTIONS_H_INCUDED # define COMMANDLINE_OPTIONS_H_INCUDED # include "configuration/parser.h" #endif // COMMANDLINE_OPTIONS_H_INCUDED --- NEW FILE: ctti.h --- #ifndef CPPUTTOOLS_CONFIGURATION_CTTI_H_INCUDED # define CPPUTTOOLS_CONFIGURATION_CTTI_H_INCUDED #include <string> // ///////////////////////////////////////////////////// // ///////////////////////////////////////////////////// // CTTI using std::type_info & typeid // ///////////////////////////////////////////////////// // ///////////////////////////////////////////////////// # ifndef CPPUTTOOLS_CTTI_NO_TYPEINFO # include <typeinfo> namespace CppUTTools { class TypeId { public: TypeId( const std::type_info &typeId ) : type_( typeId ) { } bool operator ==( const TypeId &other ) const { return type_ == other.type_; } bool operator !=( const TypeId &other ) const { return !( *this == other ); } bool operator <( const TypeId &other ) const { return type_.before( other.type_ ); } private: const std::type_info &type_; }; // Compile Time Type Information functions // ////////////////////////////////////////////////////////////////// template<class Type> TypeId typeId() { return typeid( Type ); } } // namespace CommandLineOptions // ///////////////////////////////////////////////////// // ///////////////////////////////////////////////////// // CTTI using hande-made mecanism (template overload) // ///////////////////////////////////////////////////// // ///////////////////////////////////////////////////// # else // ifndef CPPUTTOOLS_CTTI_NO_TYPEINFO class TypeId { public: TypeId( const char *type ) : type_( type ) { } bool operator <( const TypeId &other ) const { return strcmp( type_, other.type_) < 0; } bool operator ==( const TypeId &other ) const { return strcmp( type_, other.type_ ) == 0; } bool operator !=( const TypeId &other ) const { return !( *this == other ); } private: const char *type_; }; template<class Type> TypeId typeId<Type>() { return __error__typeId_function_not_overloaded; } #define CPPUTTOOLS_DECLARE_CTTI( type ) \ template<> \ TypeId typeId<type>() \ { \ return TypeId( #type ); \ } CPPUTTOOLS_DECLARE_CTTI( char ); CPPUTTOOLS_DECLARE_CTTI( signed char ); CPPUTTOOLS_DECLARE_CTTI( unsigned char ); CPPUTTOOLS_DECLARE_CTTI( short ); CPPUTTOOLS_DECLARE_CTTI( unsigned short ); CPPUTTOOLS_DECLARE_CTTI( int ); CPPUTTOOLS_DECLARE_CTTI( unsigned int ); CPPUTTOOLS_DECLARE_CTTI( long ); CPPUTTOOLS_DECLARE_CTTI( unsigned long ); CPPUTTOOLS_DECLARE_CTTI( float ); CPPUTTOOLS_DECLARE_CTTI( double ); CPPUTTOOLS_DECLARE_CTTI( long double ); CPPUTTOOLS_DECLARE_CTTI( const char * ); CPPUTTOOLS_DECLARE_CTTI( const wchar_t * ); CPPUTTOOLS_DECLARE_CTTI( std::string ); CPPUTTOOLS_DECLARE_CTTI( std::wstring ); # endif #endif // CPPUTTOOLS_CONFIGURATION_CTTI_H_INCUDED --- NEW FILE: convertor.h --- #ifndef CPPUTTOOLS_CONVERTOR_H_INCLUDED # define CPPUTTOOLS_CONVERTOR_H_INCLUDED # include "ctti.h" # include <stdexcept> # include <map> class Convertor { public: virtual ~Convertor() {} virtual void convert( const void *source, void *target ) = 0; }; template<class SourceType ,class TargetType> class ConstructorConvertor : public Convertor { public: // overridden from Convertor void convert( const void *source, void *target ) { *( static_cast<TargetType *>(target) ) = TargetType( static_cast<SourceType *>(source) ); } }; template<class SourceType ,class TargetType> class CStrConvertor : public Convertor { public: // overridden from Convertor void convert( const void *source, void *target ) { *( static_cast<TargetType *>(target) ) = static_cast<SourceType *>(source)->c_str(); } } class ConvertorRegistry { public: static ConvertorRegistry &instance(); ConvertorRegistry(); ~ConvertorRegistry(); void add( TypeId sourceType, TypeId targetType, Convertor &convertor ); bool isConvertible( TypeId sourceType, TypeId targetType ); bool convert( TypeId sourceType, TypeId targetType, const void *source, void *target ) const; private: typedef std::map<TypeId,Convertor *> TargetConvertors; typedef std::map<TypeId, TargetConvertors> SourceConvertors; SourceConvertors convertors_; }; #define CPPUTTOOLS_REGISTER_CONVERTOR( SourceType, TargetType, convertor ) \ ConvertorRegistry::instance().add( typeId<sourceType>(), \ typeId<targetType>(), \ convertor ) #define CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( SourceType, TargetType ) \ CPPUTTOOLS_REGISTER_CONVERTOR( sourceType, targetType, \ new ConstructorConvertor<SourceType,TargetType>() ) #define CPPUTTOOLS_REGISTER_CSTR_CONVERTOR( SourceType, TargetType ) \ CPPUTTOOLS_REGISTER_CONVERTOR( sourceType, targetType, \ new CStrConvertor<SourceType,TargetType>() ) inline void ConvertorRegistry::add( TypeId sourceType, TypeId targetType, Convertor &convertor ) { convertors_[sourceType][targetType] = &convertor; } inline bool ConvertorRegistry::isConvertible( TypeId sourceType, TypeId targetType ) { SourceConvertors::const_iterator it = convertors_.find( sourceType ); return it != convertors_.end() && it->second.count( targetType ) > 0; } inline bool ConvertorRegistry::convert( TypeId sourceType, TypeId targetType, const void *source, void *target ) const { SourceConvertors::const_iterator it = convertors_.find( sourceType ); TargetConvertors::const_iterator itTarget = it->second.find( targetType ); if ( itTarget == it->second.end() ) return false; itTarget->second->convert( sourceType, target ); return true; } inline ConvertorRegistry::ConvertorRegistry() { CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( short, int ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( short, long ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( short, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( short, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( short, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( int, long ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( int, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( int, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( int, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( long, int ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( long, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( long, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( long, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( float, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( float, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( double, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned short, unsigned int ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned short, unsigned long ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned short, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned short, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned short, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned int, long ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned int, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned int, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned int, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned long, float ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned long, double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( unsigned long, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( double, long double ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( char *, std::string ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( const char *, std::string ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( wchar_t *, std::wstring ); CPPUTTOOLS_REGISTER_CONSTRUCTOR_CONVERTOR( const wchar_t *, std::wstring ); CPPUTTOOLS_REGISTER_CSTR_CONVERTOR( std::string, const char * ); CPPUTTOOLS_REGISTER_CSTR_CONVERTOR( std::wstring, const wchar_t * ); } ConvertorRegistry::~ConvertorRegistry() { SourceConvertors::iterator it = convertors_.begin(); for ( ; it != convertors_.end; ++it ) { TargetConvertors::iterator itTarget = it->second.begin(); for ( ; itTarget != it->second.end(); ++itTarget ) delete itTarget->second; } } } // namespace CppUTTools #endif // CPPUTTOOLS_CONVERTOR_H_INCLUDED --- NEW FILE: valuebinder.h --- #ifndef CPPUTTOOLS_VALUEBINDER_H_INCLUDED # define CPPUTTOOLS_VALUEBINDER_H_INCLUDED # include "value.h" namespace CppUTTools { namespace Impl { class ValueBinderImpl { public: virtual ~ValueBinderImpl() {} virtual void set( void *storage, const Value &value ) const = 0; virtual Value get( const void *storage ) const = 0; virtual TypeId type() const = 0; }; template<class ValueType> class SpecificValueBinderImpl : public ValueBinderImpl { public: static const ValueBinderImpl &instance() { static SpecificValueBinderImpl dummy; return dummy; } public: // overridden from ValueBinderImpl void set( void *storage, const Value &value ) const { if ( !value.isEmpty() ) *( static_cast<ValueType *>( storage ) )= ::get<ValueType>( value ); } Value get( const void *storage ) const { return value( *( static_cast<const ValueType *>( storage ) ) ); } TypeId type() const { return typeId<ValueType>(); } }; class VoidValueBinderImpl : public ValueBinderImpl { public: static const ValueBinderImpl &instance() { static VoidValueBinderImpl dummy; return dummy; } public: // overridden from ValueBinderImpl void set( void *storage, const Value &value ) const { } Value get( const void *storage ) const { return Value(); } TypeId type() const { return typeId<void>(); } }; } // namespace Impl class ValueBinder { public: ValueBinder(); void set( const Value &value ) const; Value get() const; TypeId type() const; bool isEmpty() const; public: // private, but friendly template function ValueBinder( void *storage, const Impl::ValueBinderImpl &impl ); private: void *storage_; const Impl::ValueBinderImpl *impl_; }; template<class ValueType> ValueBinder valueBinder( ValueType &storage ) { return ValueBinder( &storage, Impl::SpecificValueBinderImpl<ValueType>::instance() ); } // Implementation // //////////////////////////////////////////////////////////// inline ValueBinder::ValueBinder() : storage_( 0 ) , impl_( &Impl::VoidValueBinderImpl::instance() ) { } inline ValueBinder::ValueBinder( void *storage, const Impl::ValueBinderImpl &impl ) : storage_( storage ) , impl_( &impl ) { } inline void ValueBinder::set( const Value &value ) const { if ( value.type() != type() ) throw ValueBadCast(); impl_->set( storage_, value ); } inline Value ValueBinder::get() const { if ( isEmpty() ) return Value(); return impl_->get( storage_ ); } inline TypeId ValueBinder::type() const { return impl_->type(); } inline bool ValueBinder::isEmpty() const { return storage_ == 0; } } // namespace CppUTTools #endif // CPPUTTOOLS_VALUEBINDER_H_INCLUDED --- NEW FILE: refcountptr.h --- #ifndef CPPUTTOOLS_REFCOUNTPTR_H_INCLUDED # define CPPUTTOOLS_REFCOUNTPTR_H_INCLUDED # include <cpput/config.h> namespace CppUTTools { class ReferenceCounted { public: virtual ~ReferenceCounted() {} virtual void incrementReferenceCount() = 0; virtual bool decrementReferenceCount() = 0; virtual unsigned int getReferenceCount() const = 0; }; // A ReferenceCount which does not provide thread-safety. class ReferenceCountedNoThread : public ReferenceCounted { public: ReferenceCountedNoThread() : count_( 0 ) { } public: // overridden from ReferenceCounted void incrementReferenceCount() { ++count_; } bool decrementReferenceCount() { return --count_ == 0; } unsigned int getReferenceCount() const { return count_; } private: unsigned int count_; }; namespace Impl { /// ReferenceCountPtr base class, implementation common to all pointers... class ReferenceCountPtrBase { public: long use_count() const { if ( p_ ) return p_->getReferenceCount(); return 0; } bool unique() const { return use_count() == 1; } operator bool() const { return p_ != 0; } bool operator !() const { return p_ == 0; } void reset() { ReferenceCountPtrBase tmp; tmp.swap( *this ); } protected: ReferenceCountPtrBase() : p_( 0 ) { } ReferenceCountPtrBase( ReferenceCounted *p ) : p_( p ) { if ( p_ ) p_->incrementReferenceCount(); } ReferenceCountPtrBase( const ReferenceCountPtrBase &other ) : p_( other.p_ ) { if ( p_ ) p_->incrementReferenceCount(); } ~ReferenceCountPtrBase() { if ( p_ && p_->decrementReferenceCount() ) delete p_; } void swap( ReferenceCountPtrBase &other ) { ReferenceCounted *tmpP = p_; p_ = other.p_; other.p_ = tmpP; } ReferenceCounted *get() const { return p_; } private: ReferenceCounted *p_; }; } // namespace Impl template<class PointeeType> class RefCountPtr : public Impl::ReferenceCountPtrBase { public: typedef RefCountPtr<PointeeType> ThisType; typedef Impl::ReferenceCountPtrBase SuperClass; RefCountPtr() { } RefCountPtr( PointeeType *p ) : Impl::ReferenceCountPtrBase( p ) { } RefCountPtr( const ThisType &other ) : Impl::ReferenceCountPtrBase( other ) { } void reset( PointeeType *p ) { RefCountPtr tmp( p ); tmp.swap( *this ); } PointeeType *get() const { return static_cast<PointeeType *>( SuperClass::get() ); } void swap( RefCountPtr &other ) { SuperClass::swap( other ); } ThisType &operator =( const RefCountPtr &other ) { ThisType tmp( other ); swap( tmp ); return *this; } PointeeType &operator *() const { // assert( SuperClass::get() != 0 ) return *( static_cast<PointeeType *>( SuperClass::get() ) ); } PointeeType *operator ->() const { return static_cast<PointeeType *>( SuperClass::get() ); } }; template<class TargetType, class SourceType> RefCountPtr<TargetType> staticPointerCast( const RefCountPtr<SourceType> &p ) { TargetType *target = static_cast<TargetType *>( p.get() ); return RefCountPtr<TargetType>( target ); } } // namespace CppUT #endif // CPPUTTOOLS_REFCOUNTPTR_H_INCLUDED |