Conteneurs stl polmorphiques

Contenu du snippet

poly_container permet de construire des listes, vecteurs et autres conteneurs STL polymorphiques et transparents.

Source / Exemple :


template<class C, typename T>
	class const_poly_iterator
	{
		typedef const_poly_iterator<C,T> _Self;

		//! mimic inheritage from the regular const_iterator
		//! because we cannot simply derive from it since some iterators are regular pointers
		typedef typename C::const_iterator _Mimic;		
	protected:
		_Mimic i; 
	public:
		operator _Mimic&() {return i;}	//!<needed to use poly_iterator in algorithms transparently
	public:
		_Self() {};
		_Self(const _Mimic& p):i(p) {}

		const T& operator*() const {return **i;}
		const T* operator->() const {return &**i;}
				
	public:	// compatibility with _Mimic iterator
		_Self& operator++() {++i; return *this;}
		_Self& operator--() {--i; return *this;}

		bool operator==(const _Mimic& other) const {return i==other;}
		bool operator!=(const _Mimic& other) const {return i!=other;}

		bool operator==(const _Self& other) const {return i==other.i;}
		bool operator!=(const _Self& other) const {return i!=other.i;}
	};

	template<class C, typename T>
		class poly_iterator:public const_poly_iterator<C,T>
	{
		typedef poly_iterator<C,T> _Self;
		typedef const_poly_iterator<C,T> _Parent;

		//! mimic inheritage from the regular iterator
		//! because we cannot simply derive from it since some iterators are regular pointers
		typedef typename C::iterator _Mimic;		
	public:
		operator _Mimic&() {return (_Mimic&)i;}	//!<needed to use poly_iterator in algorithms transparently

	public:
		_Self() {};
		_Self(const _Mimic& p):_Parent(p) {};
		_Self(const _Parent& p):_Parent(p) {};

	public:	// compatibility with _Mimic iterator
		_Self& operator++() {++i; return *this;}
		_Self& operator--() {--i; return *this;}
	};

	template<typename T, typename R=boost::shared_ptr<T> >
	struct poly_factory
	{
		inline static R factory(const T& p) {return R(T::Factory(p));}
	};

	template<class C, typename T, typename F=poly_factory<T,C::value_type> >
	class poly_container : public C
	{
		typedef C _Parent;
		typedef poly_container<C,T,F> _Self;
		typedef typename C::value_type value_type;	// (smart) pointer to T
	protected:
		typedef T& reference;
		typedef const T& const_reference;
	public:
		typedef _Parent::size_type size_type;
		typedef poly_iterator<C,T> iterator;
		typedef const_poly_iterator<C,T> const_iterator;

		_Self() {};
		_Self(const _Parent& p):_Parent(p) {};

		const_iterator begin() const {return _Parent::begin();}
		iterator begin() {return _Parent::begin();}
		const_iterator end() const {return _Parent::end();}
		iterator end() {return _Parent::end();}

		iterator insert(iterator i, const value_type& f) {return _Parent::insert(i, f);}
		void push_front(const value_type& f) {insert(begin(), f);}
		void push_back(const value_type& f) {insert(end(), f);}

		const_reference front() const {return *_Parent::front();}
		const_reference back() const {return *_Parent::back();}

		void push_front(const_iterator& f) {copy(begin(), *_Parent::const_iterator(f));}
		void push_back(const_iterator& f) {insert(end(), *_Parent::const_iterator(f));}

		iterator insert(iterator i, const T& f) {return _Parent::insert(i, F::factory(f));}
		void push_front(const T& f) {insert(begin(), F::factory(f));}
		void push_back(const T& f) {insert(end(), F::factory(f));}
	};

Conclusion :


ce code fait partie de ma librairie DYL, en open source, qui contient plein d'autres choses intéressantes. Allez voir sur http://www.dynabits.com/dyl/

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.