tecu
tecu

Reputation: 550

C++ templated member function as an interface to another templated class

I would like to create a class that acts as an interface to a set of specialized template classes. For example:

template<typename T>
class ThingDoer {
    public:
        void Method()
        {
            // do something;
        }
};

class ThingDoerInterface {
    public:
        template<typename T>
        void Method()
        {
            // call ThingDoer<T>::Method(); somehow
        }
};

int main()
{
    ThingDoerInterface i;
    i.Method<int>();
    i.Method<char>();
    // etc.
    
    return 0;
}

My generic requirements for the object I'd like look something like this:

I have a working solution to my actual problem that's based on std::unordered_multimap, but I'm interested if something like this can be done with templates alone.

Edit:

This is a more specific example that I hope will illustrate what I am actually trying to do.

class ABase {
    public:
        virtual ~ABase() {}
};

class A1 : public ABase {};
class A2 : public ABase {};

class BBase {
    public:
        virtual ~BBase() {}
};
class B1 : public BBase {};
class B2 : public BBase {};


class ThingDoerInterface {
    public:
        template<typename T>
        void Store(BBase* b_ptr)
        {
            // store the B pointer with the type of T as a key
            // (T will be A1 or A2)
        }
        
        template<typename T>
        void Recall()
        {
            // call all the stored B pointers associated with the type of T
        }
};

int main()
{
    ThingDoerInterface i;
    
    B1* b_one_ptr = new B1;
    B2* b_two_ptr = new B2;
    
    i.Store<A1>(b_one_ptr);
    i.Store<A1>(b_two_ptr);
    
    i.Store<A2>(b_one_ptr);
    
    i.Recall<A1>(); // do something with b_one_ptr and b_two_ptr
    i.Recall<A2>(); // do something with b_one_ptr
    
    delete b_two_ptr;
    delete b_one_ptr;
    
    return 0;
}

And I have done this with an std::unordered_multimap, but what I want to know is if it is possible to store the association like this:

template<typename T>
class ThingDoer {
    public:
        void Store(BBase* b_ptr)
        {
            b_ptrs.push_back(b_ptr);
        }
        
        void Recall()
        {
            // do something with the b_ptrs associated with the type of T
        }
    private:
        std::vector<BBase*> b_ptrs;
};

but do so in the ThingDoerInterface somehow.

Upvotes: 3

Views: 698

Answers (1)

dspeyer
dspeyer

Reputation: 3026

I'm not necessarily saying it's worth the complexity, but...

#include<iostream>
#include<vector>
using namespace std;

class repo {
public:
  repo(){
    id = highestid++;
  }
  template<typename T>
  T& get() {
    vector<T>& vec = data<T>::vec;
    if (vec.size() <= id) {
      vec.resize(id+1);
    }
    return vec[id];
  }
private:
  template<typename T>
  struct data {
    static vector<T> vec;
  };
  int id;
  static int highestid;
};
/*static*/ int repo::highestid = 0;
template<typename T>
/*static*/ vector<T> repo::data<T>::vec;

main(){
  repo a, b;
  a.get<int>() = 42;
  b.get<int>() = 54;
  a.get<float>() = 4.2;
  b.get<float>() = 5.4;
  cout << a.get<int>() << ", " << b.get<int>() << ", " << a.get<float>() << ", " << b.get<float>() << endl;
}

I've written it for fast lookup. The first time you call get for a given repo, you invalidate all references returned by other repos, and destroying a repo doesn't clean up anything. But if you have a small number of repos whose lifetime is basically the whole program, that should be ok.

Upvotes: 3

Related Questions