|
#include "DataReaderTestFactory.h"
#include "DerivedReader.h"
// uncomment the next string if you have boost library
//#include <boost/shared_ptr.hpp>
//comment the next string if you have boost lib
#include "CountedPtr.h"
#include <iostream>
#include <map>
//------------------------------------------------------------------
// class DataReaderTestFactory -- clonebale factory for test clone objects
//------------------------------------------------------------------
class DataReaderTestFactoryImpl
{
// uncomment the next string if you have boost library
//typedef boost::shared_ptr<IDataReader> IDataReaderSPtr;
//comment the next string if you have boost lib
typedef CountedPtr<IDataReader> IDataReaderSPtr;
typedef std::map<std::string,IDataReaderSPtr> ReaderMap;
typedef IDataReader::IDataReaderPtr IDataReaderPtr;
public:
static DataReaderTestFactoryImpl& instance();
DataReaderTestFactoryImpl() {};
~DataReaderTestFactoryImpl();
void init();
IDataReaderPtr create(const std::string& name) const;
private:
// !!! Not implemented
DataReaderTestFactoryImpl(const DataReaderTestFactoryImpl&);
// !!! Not implemented
DataReaderTestFactoryImpl& operator=(const DataReaderTestFactoryImpl&);
//keep clonebale objects
ReaderMap m_map;
};
// ---------------------------------------------------------------------------
// class CFTIValidFactoryCreatorImpl
// ---------------------------------------------------------------------------
//static
DataReaderTestFactoryImpl& DataReaderTestFactoryImpl::instance()
{
static DataReaderTestFactoryImpl impl;
return impl;
}
// ---------------------------------------------------------------------------
DataReaderTestFactoryImpl::~DataReaderTestFactoryImpl()
{
m_map.clear();
}
// ---------------------------------------------------------------------------
void
DataReaderTestFactoryImpl::init()
{
IDataReaderSPtr ptr(new Derived1Reader());
m_map.insert(ReaderMap::value_type("Derived1Reader", ptr));
IDataReaderSPtr ptr1(new Derived2Reader());
m_map.insert(ReaderMap::value_type("Derived2Reader", ptr1 ));
}
// ---------------------------------------------------------------------------
IDataReader::IDataReaderPtr
DataReaderTestFactoryImpl::create(const std::string& name) const
{
ReaderMap::const_iterator it = m_map.find(name);
if(it == m_map.end())
{
std::cout << "ERROR: " << name << " not found in DataReaderFactory" << std::endl;
//through exception or do something smart
}
return (it->second)->clone();
}
//------------------------------------------------------------------
// struct DataReaderTestFactory
//-----------------------------------------------------------------
//static
void
DataReaderTestFactory::init()
{
DataReaderTestFactoryImpl::instance().init();
}
//static
DataReaderTestFactory::IDataReaderPtr
DataReaderTestFactory::create(const std::string& name)
{
return DataReaderTestFactoryImpl::instance().create(name);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.