views:

646

answers:

2

Hi,

In my new project i am building a data management module.I want to give a simple template storage type to upper layers like

template<typename T>
class Data
{
 public:
  T getValue();
 private:

 boost::numeric::ublas::matrix<T> data;
}

My aim is, to change allocator of data with some different allocators like Boost.inter process allocator or Boost.pool allocator (Boost Ublas Matrix and vector classes takes allocator as a template parameter ).And give only a single class and Factory method to create appropriate allocator under cover.A virtual base class could be sweet but i couldn't handle how to use it with templates.What kind of design patterns or solutions do you offer?

Edit:

I will use boost.pool and boost.shared_memory_allocator.Briefly i want to have different classes with different allocation strategies.But my point is upper parts of program should have no knowledge about it.Real challenge for me is to collect different template classes with an identical base classes.

Edit: For one who want to use matrix class with custom allocator.

it is like this:

 using boost::numeric::ublas;

    template<typename T, class Allocator = boost::pool_allocator<T>>
    class
    {      
      public:
      matrix<T, row_major, std::vector<T,Allocator>> mData;
    }
A: 

Are you trying to swap allocators at compile time based on type? You'd need a if-else template and some allocator class (template) definitions.

If you want runtime allocators, then it's easier: you'd put the base class (interface definition class) in the template and pass appropriate subclasses based on whatever condition you need to fulfill.

dirkgently
A: 

It's not clear what you want, but as a shot in the dark, is the following helpful?

template<typename T>
class IData
{
 public:
  virtual T getValue() = 0;
  virtual ~IData() {}
};

template<typename T, typename Allocator=std::allocator<T> >
class Data : public IData<T>
{
 public:
  virtual T getValue();
 private:

 boost::numeric::ublas::matrix<T, Allocator> data;
}
sorry, i accept, it wasnt clear but your method is feasible for me.
Qubeuc
Cool :) glad to help
I put you back up :P
GMan