Click here to Skip to main content
15,896,456 members
Articles / Programming Languages / C++

Using Class Templates with Fake Parameters in Derived Classes.

Rate me:
Please Sign up or sign in to vote.
3.70/5 (7 votes)
21 Aug 20079 min read 51.5K   121   14  
An article provide new C++ idom to avoid code duplication.
#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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions