Boost logo

Boost Users :

From: Stephen torri (storri_at_[hidden])
Date: 2004-09-04 15:20:24


In my example data is passed between objects using a Data_Source. I want
to hide the method data is passed from what the value is of the data. So
a object receiving it does not need to be concerned with whether the
data is coming from a file or database.

In my situation I have three data sources for information: a file,
memory pointer or database. Each of these data sources can handle a
std::string, a Memory_Object, and a Graph_Object type as its internal
data structure. The question I want to understand is how to create a
generic interface to create the appropriate factory.

Here is the table of the possible data source combinations (3 types * 3
sources = 9 possible). So more types and more sources can make creating
the right Data_Source an ugly experience for maintaining the code. I
don't want to have to have 9 separate functions.

            -----------------------------------------------------------
            | File_Data_Src | Memory_Data_Src | Graph_Data_Src
-----------------------------------------------------------------------
std::string | x | x | x
------------------------------------------------------------------------
Memory Obj | x | x | x
------------------------------------------------------------------------
Graph Obj | x | x | x
------------------------------------------------------------------------

At initialization I give a interger the Data_Source_Factory_Creator to
represent what kind of factory I want. When a call is made to the
Data_Source_Factory_Creator::get_Factory I want the apporpriate factory
returned based upon the integer value given during initialization. Right
now I get the right compilor error for
Data_Source_Factory_Creator::get_Factory() because given
Data_Source_Factory is a template class I need to instantiate this
template correctly. I don't know how to make this function return a
generic type so that I can do:

Data_Source_Factory<File_Data_Source>* m_factory =
Data_Source_Factory_Creator::get_Factory();

Any ideas?

Stephen

-- 
Email: storri_at_[hidden]


#ifndef DATA_SOURCE_FACTORY_H_
#define DATA_SOURCE_FACTORY_H_

#include "infrastructure/data_source/Data_Source.hpp"
#include <inttypes.h>

namespace infrastructure {

  template <typename Data_Source_Type>
  class Data_Source_Factory {
  public:

    static Data_Source_Factory<Data_Source_Type>* get_Factory ();

    Data_Source_Type* get_String_Source ();

    Data_Source_Type* get_Memory_Source ();

    Data_Source_Type* get_Graph_Source ();

  private:

    Data_Source_Factory (){}

    static Data_Source_Factory<Data_Source_Type>* m_instance;
  };

  class Data_Source_Factory_Creator {
  public:

    /* The Data_Source produce by the factory will be of the same type
       (memory passing, file passing or database passing).
    */
    void init (const uint32_t type);

    /*
     * If type is FILE, return a Data_Source_Factory<File_Data_Source>
     * Else if type is MEMORY, return a Data_Source_Factory<Memory_Data_Source>
     * Else if type is DATABASE, return a Data_Source_Factory<Database_Data_Source>
     */
    static Data_Source_Factory* get_Factory ();

  private:

    Data_Source_Factory_Creator();

    static uint32_t m_type;
  };

} /* namespace infrastructure */

#endif /* DATA_SOURCE_FACTORY_H_ */



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net