开发者

C++ Design question: Passing functions via templates?

Thanks in advance for reading. This is a picky design question in that I have an adequate solution, but question if there isn't a better way via templates, which I'm not very experienced with.

I have a series of types of data tables (derived from abstract class DataTable) which I need to store. I wrote a "DataTableIndex" abstract class which stores a vector DataTable*'s and which handles the general work common to all DataTableIndexes -- doing lookups, implementing a Proxy pattern so tables are only loaded if needed, error checking, etc.

I then subclass开发者_如何转开发 it for each table type, the only reason I need to do so is because each table type has a specific function to be called to load it.

I'd like to avoid this subclassing of DataTableIndex somehow via templates if possible, because there are lots of subclasses of DataTable.

class DataTableIndex
{  
  // various functions to implement lookup, Proxy and error checking
  //   functionality common to all DataTableIndexes.
  // This code needs access to _lookupTable and _theTables

  DataTable* getTable( int tableNum );

private:
  // These functions call the appropriate user interface function for loading
  //   a table of the subclass' type.
  //   They are called by more general non-virtual public functions
  virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable ) = 0;
  virtual DataTable* loadTable( int tableNum ) = 0;

  vector<LookupEntry*> _lookupTable;
  vector<DataTable*>   _theTables;

  UserInterface* UI;
};  

This class has then very simple subclasses, which basically point to functions in the User Interface class that actually open and parse the file of the data table.

class TableTypeA_Index : public DataTableIndex
{
  virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable )
  {
     return UI->loadTableAIndex( _lookupTable );
  }

  virtual DataTable* loadTable( int tableNum )  
  { 
     return UI->loadTableTypeA( _lookupTable[ tableNum ] );
  }

};

This works adequately. But I feel like I should be able to pass "loadTableTypeA" for example to DataTableIndex via a template parameter, so I don't have to subclass it.

Another reason why I would like to use templates is that I don't want to have to cast DataTable* all of the time to the actual table type. Even though I know at compile time what type of table it should be, I feel like I should be using dynamic_cast<> to error check, but I don't want the caller of getTable() to have to do this every time (it is called often).

My ideal solution would: 1) generalize the DataTableIndex class to a template, with template parameters replacing LookupEntry* and DataTable* in _lookupTable and _theTables. This would eliminate casting.

2) map the appropriate UI functions to load the right table type, without subclassing.

So basically I'd like to the use of this class to look like this (somehow)

DataTableIndex< LookupEntryTypeAMatlab, 
                TableTypeA, 
                loadTableAIndex(),
                loadTableTypeA() > theTypeAIndex;

I gave some thought to policy classes, but my impression of that approach was that, in this case, I'd just be moving the subclassing to something else.


In general, this could be done by using the strategy pattern. This can be implemented using simple composition, so there are no templates needed. However, you will still have to define a special strategy for each of the different table types. This will require subclassing:

class LoadTableStrategy {
public:
    virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable ) = 0;
    virtual DataTable* loadTable( int tableNum ) = 0;
};

This class will have to be subclasses for each table-type. DataTableIndex would then receive an instance of LoadTableStrategy in the constructor, and use that to load the data, instead of the private virtual functions.

You can of course pass the strategy-type as template parameter, but be aware, that this as drawbacks. Two DataTableIndex instantiated with different template parameters will be different types for the compiler. You will not be able to define a function that can handle both, unless you create specific overloads, or make the function a function template itself.


If you want to stick with templates you could turn TableTypeA_Index into a template, however you would have to pass pointers to the member functions of UserInterface in to the constructor. E.g.:

typedef bool (UserInterface::*LoadTableIndexFP) (vector<LookupEntry*>&);
typedef DataTable* (UserInterface::*LoadTableTypeFP) (LookupEntry*);

template<class TABLE>
class TableType_Index : public DataTableIndex
{
public:
    TableType_Index (LoadTableIndexFP loadTableIndexFP, LoadTableTypeFP loadTableTypeFP)
        : loadTableIndexFP (loadTableIndexFP)
        , loadTableTypeFP (loadTableTypeFP)
    {
    }

    virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable )
    {
        return (UI->*loadTableIndexFP) (returnLookupTable);
    }

    virtual DataTable* loadTable( int tableNum )
    {
        return (UI->*loadTableTypeFP) (_lookupTable[ tableNum ]);
    }

private:
    LoadTableIndexFP loadTableIndexFP;
    LoadTableTypeFP loadTableTypeFP;
};

int main (int argc, char* argv[])
{
    TableType_Index<TableA> (&UserInterface::loadTableAIndex, &UserInterface::loadTableTypeA);

    return 0;
}

I haven't added LookupEntryTypeAMatlab as a template parameter as it isn't clear from your TableTypeA_Index definition what its role would be.

Note, an alternative to passing ptr-to-mem-funs into the ctor would be to have a traits class per table type:

template<typename T>
struct TableTraits
{
};

template<>
struct TableTraits<TableA>
{
    static LoadTableIndexFP loadTableIndexFP;
    static LoadTableTypeFP loadTableTypeFP;
};

LoadTableIndexFP TableTraits<TableA>::loadTableIndexFP = &UserInterface::loadTableAIndex;
LoadTableTypeFP TableTraits<TableA>::loadTableTypeFP = &UserInterface::loadTableTypeA;

template<class TABLE>
class TableType_Index : public DataTableIndex
{
public:
    virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable )
    {
        return (UI->*TableTraits<TableA>::loadTableIndexFP) (returnLookupTable);
    }

    virtual DataTable* loadTable( int tableNum )
    {
        return (UI->*TableTraits<TableA>::loadTableTypeFP) (_lookupTable[ tableNum ]);
    }
};

Neither approach is ideal though...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜