/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_collector_hh
#define _uds_collector_hh


// includes

#include <uds/flex_func.hh>
#include <uds/stdexcpt.hh>
#include <algorithm>

using namespace std;


namespace uds
{

template < class Cont, class Value = typename Cont::value_type >
class Collector
{	
public:
	typedef Cont container_type;
	
	/** \param o function that destroys an instance */
	template < class Op >
	Collector( Op o ) : op( o ) {}
	
	/** destructor */
	virtual
	~Collector() { for_each( cont.begin(), cont.end(), op ); }
	
	Cont&
	operator () () { return cont; }
	
private:
	/// container that holds pointers to the instances
	Cont						cont;
	/// function that destroys an instance
	flex_func< void, Value >	op;
};


/// one might want to throw this exception when one value is inserted twice
_uds_ExceptionClass( DoubleInstance, runtime_error,
	"instance is not unique\n", NoDiag );

}

#endif // _uds_collector_hh
