From: <z-...@us...> - 2008-02-19 18:53:06
|
Revision: 8010 http://armagetronad.svn.sourceforge.net/armagetronad/?rev=8010&view=rev Author: z-man Date: 2008-02-19 10:47:35 -0800 (Tue, 19 Feb 2008) Log Message: ----------- Got basic std::vector wrapper working. You can't do much with it yet, of course. Modified Paths: -------------- private/z-man/clio/Makefile private/z-man/clio/ichained.cpp private/z-man/clio/tests/container.test/test.cpp Modified: private/z-man/clio/Makefile =================================================================== --- private/z-man/clio/Makefile 2008-02-19 17:21:21 UTC (rev 8009) +++ private/z-man/clio/Makefile 2008-02-19 18:47:35 UTC (rev 8010) @@ -1,5 +1,5 @@ -#IOINCLUDE=/usr/local/include/io -IOINCLUDE=/home/moos/usr/include/io +IOINCLUDE=/usr/local/include/io +#IOINCLUDE=/home/moos/usr/include/io #CXX = g++-4.1.1 #CXX = gcc-40-g++ CXX = g++ Modified: private/z-man/clio/ichained.cpp =================================================================== --- private/z-man/clio/ichained.cpp 2008-02-19 17:21:21 UTC (rev 8009) +++ private/z-man/clio/ichained.cpp 2008-02-19 18:47:35 UTC (rev 8010) @@ -55,7 +55,8 @@ ObjectWrapper * wrapper = GetObjectWrapper(); if ( wrapper ) { - wrapper->RegisterGCMarker( *this ); + // not required; the automatic GC marker finder will take care of that later. + // wrapper->RegisterGCMarker( *this ); // IChained Objects are always owned by IO wrapper->SetOwnership( ObjectWrapper::OwnedByIo ); Modified: private/z-man/clio/tests/container.test/test.cpp =================================================================== --- private/z-man/clio/tests/container.test/test.cpp 2008-02-19 17:21:21 UTC (rev 8009) +++ private/z-man/clio/tests/container.test/test.cpp 2008-02-19 18:47:35 UTC (rev 8010) @@ -1,128 +1,209 @@ #include "helptest.hpp" #include "typetraits.hpp" +#include "ichained.hpp" // **************************** // std:: containers // **************************** -template< class Container > class ContainerWrapper +class A +{}; + +class TestVector { public: + A & GetA() + { + static A a; + return a; + }; + + std::vector< int > & GetInts() + { + return ints_; + } +private: + std::vector< int > ints_; +}; + +namespace clio +{ + +// proxy objects. ORIGINAL should be the raw type of the wrapped object. +template< class ORIGINAL > +class Proxy: public IChained +{ +public: + // returns the original object + ORIGINAL & Get() const + { + if ( !original_ ) + { + std::ostringstream err; + err << "Proxy for " << GetTypeName< ORIGINAL >() << + " is broken, the wrapped object may have gone out of scope."; + throw Exception( "Broken Proxy", err.str() ); + } + + return *original_; + } + + // creates proxy for original + explicit Proxy( ORIGINAL * original ) + : original_( original ) + { + // register with object wrappers + assert( original ); + ObjectWrapper::RegisterChained( this, original ); + } + + // creates proxy for original + explicit Proxy( ORIGINAL & original ) + : original_( &original ) + { + // register with object wrappers + ObjectWrapper::RegisterChained( this, &original ); + } +protected: + // called when the object this thing is chained to gets destroyed + virtual void OnBreak() CLIO_NOTHROW + { + // remove reference to original + original_ = 0; + } +private: +private: + // poiter to the original wrapped object + ORIGINAL * original_; +}; + +template< class Container > class VectorProxy: public Proxy< Container > +{ +public: typedef typename Container::value_type value_type; typedef typename Container::reference reference; typedef typename Container::const_reference const_reference; - explicit ContainerWrapper( Container & container ) - : container_( container ) + explicit VectorProxy( Container & container ) + : Proxy< Container >( container ) {} + explicit VectorProxy( Container * container ) + : Proxy< Container >( container ) + {} + const_reference at( int index ) const { - return container_[index]; + return this->Get()[index]; } void append( const_reference value ) { - container_.push_back(value); + this->Get().push_back(value); } unsigned int size() const { - return container_.size(); + return this->Get().size(); } -private: - Container & container_; }; +} -class A -{}; +CLIO_TEMPLATE_SET_DEFAULTCONSTRUCTOR( clio::VectorProxy, (1,(class)), false ); -class B +CLIO_TEMPLATE_CLASS( clio::VectorProxy, (1,(class))) { -public: - B( A & ){} -}; + typedef T1 CONTAINER; + CLIO_METHOD(append); + CLIO_METHOD(size); + CLIO_METHOD( at ); +} -class TestVector +namespace clio { + +namespace dataconversion +{ + +// Data conversion via proxy classes. The proxy class is supposed to have this +// public interface: +// class PROXY{ +// PROXY( ORIGINAL ); +// ORIGINAL Get(); +// }; +// and is supposed to be derived from IChained. + +template< class ORIGINAL, class PROXY > class DataConverterProxy: public DataConverterDefs< ORIGINAL > +{ public: - A const & GetA() - { - static A a; - return a; - }; + typedef DataConverterDefs< ORIGINAL > Base; - std::vector< int > & GetInts() + typedef typename Base::TArg TArg; + typedef typename Base::Converted Converted; + + typedef DataConverterReference< PROXY &, DataConverterPointerImplementation< PROXY > > IMPL; + + static Converted FromIo( IoObject * from, DataConversionCall const & call, IExceptionThrower const & thrower ) { - return ints_; + PROXY & proxy = IMPL::FromIo( from, call, thrower ); + return proxy.Get(); } - ContainerWrapper< std::vector< int > > GetInts2() + static void CheckType( IChained const & ){} + + static inline IoObject * ToIo( IoState * state, TArg from, DataConversionCall const & call ) { - return ContainerWrapper< std::vector< int > >( ints_ ); + // create + typename IMPL::TArg arg = *(new PROXY(from)); + + // READ BELOW IF THIS LINE GIVES AN ERROR + DataConverterProxy::CheckType( arg ); + // If the above line gives an error, that means you tried to register + // a proxy object for data conversion that is not derived from clio::IChained. + // Let all your proxies inherit from clio::IChained to correct this error. It's + // best if you let them inherit from clio::Proxy< ORIGINAL >. + + return IMPL::ToIo( state, arg, call ); } -private: - std::vector< int > ints_; }; -namespace clio +class B: public clio::IChained { -namespace dataconversion -{ +public: + B( A & ){} +}; template<> struct DataConverterManager< A >: public DataConverterManager< B > { }; -template<> struct DataConverterManager< A & >: public DataConverterValue< B > +template<> struct DataConverterManager< A const & >: public DataConverterManager< B > { }; -template<> struct DataConverterManager< A const & >: public DataConverterManager< B > +template<> struct DataConverterManager< A & >: public DataConverterProxy< A &, Proxy<A> > { }; -template< class T > struct DataConverterManager< std::vector<T> & >: public DataConverterManager< ContainerWrapper< std::vector<T> > > +template< class T > +struct DataConverterManager< std::vector<T> & > + : public DataConverterProxy< std::vector<T> &, + VectorProxy< std::vector<T> > > { - typedef DataConverterManager< ContainerWrapper< std::vector<T> > & > BASE; +}; -/* - static IoObject * ToIo( IoState * state, std::vector<T> & vec, const clio::DataConversionCall & call ) - { - return BASE::ToIo( state, ContainerWrapper< std::vector<T> >( vec ), call );} -*/ -}; } + } -CLIO_TEMPLATE_SET_DEFAULTCONSTRUCTOR( ContainerWrapper, (1,(class)), false ); - CLIO_TEMPLATE_CLASS( std::vector, (1,(class))) { } -CLIO_TEMPLATE_CLASS( ContainerWrapper, (1,(class))) -{ - typedef T1 CONTAINER; - CLIO_METHOD(append); - // CLIO_METHOD(pop_back); - CLIO_METHOD(size); - // typedef T1 VECTOR::ACCESSOR(int); - // CLIO_METHOD(operator[]); - // CLIO_METHOD_EX(operator[],(VECTOR::const_reference (VECTOR::*)(VECTOR::size_type) const),"squareBrackets"); - //CLIO_METHOD((void (VECTOR::*)(T1 const &)) &VECTOR::push_back); -//CLIO_METHOD((const T1 & (VECTOR::*)() const)&front); - //CLIO_METHOD(back); - // CLIO_METHOD(((GETTER)&front)); - // CLIO_METHOD_EX( front, GETTER, "front" ); - // CLIO_METHOD_EX( front, (T1 const & (VECTOR::*)() const), "front" ); - CLIO_METHOD( at ); -} - CLIO_CLASS( TestVector ) { CLIO_METHOD( GetA ); - // CLIO_METHOD( GetInts ); + CLIO_METHOD( GetInts ); } void Test() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |