tags:

views:

48

answers:

2

Is it possible to store a templated class like

template <typename rtn, typename arg>
class BufferAccessor {
  public:
    int ThreadID;
    virtual rtn do_work(arg) = 0;  
}; 

BufferAccessor<void,int> access1;
BufferAccessor<int,void> access2;

in the same container like a vector or list

edit: The purpose for this is I am trying to make a circular buffer where the objects that want to use the buffer need to register with the buffer. The buffer will store a boost::shared_ptr to the accessor objects and generate a callback to there functions that will push or pull data to/from the buffer. The callback will be used in a generic thread worker function that I have created similar to a thread pool with the fact that they need to access a shared memory object. Below is some code I have typed up that might help illustrate what I am trying to do, but it hasn't been compiled it yet and this is also my first time using bind, function, multi-threading

 typedef boost::function<BUF_QObj (void)> CallbackT_pro;
        typedef boost::function<void (BUF_QObj)> CallbackT_con;
        typedef boost::shared_ptr<BufferAccessor> buf_ptr;

        // Register the worker object
            int register_consumer(BufferAccesser &accessor) {
                mRegCons[mNumConsumers] = buf_ptr(accessor);
                return ++mNumConsumers;
            }

            int register_producer(BufferAccesser &accessor) {
                mRegPros[mNumProducers] = buf_ptr(accessor);
                return ++mNumProducers;
            }
                // Dispatch consumer threads
                for(;x<mNumConsumers; ++x) {
                    CallBack_Tcon callback_con = boost::bind(&BufferAccessor::do_work, mRegCons[x]);
                    tw = new boost:thread(boost::bind(&RT_ActiveCircularBuffer::consumerWorker, this, callback_con));
                    consumers.add(tw);
                }

                // Dispatch producer threads
                for(x=0;x<mNumProducers; ++x) {
                    CallBack_Tpro callback_pro = boost::bind(&BufferAccessor::do_work, mRegPros[x], _1);
                    tw = new boost:thread(boost::bind(&RT_ActiveCircularBuffer::producerWorker, this, callback_pro));
                    producers.add(tw);
                }
        // Thread Template Workers - Consumer
            void consumerWorker(CallbackT_con worker) {
                struct BUF_QObj *qData;

                while(!mRun)
                    cond.wait(mLock);

                while(!mTerminate) {
                    // Set interruption point so that thread can be interrupted
                    boost::thread::interruption_point();
                    { // Code Block
                        boost::mutex::scoped_lock lock(mLock);
                        if(buf.empty()) {
                            cond.wait(mLock)

                        qData = mBuf.front();
                        mBuf.pop_front(); // remove the front element
                    } // End Code Block

                    worker(qData); // Process data

                    // Sleep that thread for 1 uSec
                    boost::thread::sleep(boost::posix_time::nanoseconds(1000));
                } // End of while loop
            }

            // Thread Template Workers - Producer
            void producerWorker(CallbackT_pro worker) {
                struct BUF_QObj *qData;
                boost::thread::sleep(boost::posix_time::nanoseconds(1000));

                while(!mRun)
                    cond.wait(mLock);

                while(!mTerminate) {
                    // Set interruption point so that thread can be interrupted
                    boost::thread::interruption_point();

                    qData = worker(); // get data to be processed

                    { // Code Block
                        boost::mutex::scoped_lock lock(mLock);
                        buf.push_back(qData);
                        cond.notify_one(mLock);
                    } // End Code Block

                    // Sleep that thread for 1 uSec
                    boost::thread::sleep(boost::posix_time::nanoseconds(1000));
                } // End of while loop
            }
+3  A: 

No it's not, because STL containers are homogenous, and access1 and access2 have completely different unrelated types. But you could make the class BufferAccessor non-template one but the do-work member as a template, like this:

class BufferAccessor
{
   template<class R, class A>
   R doWork(A arg) {...}
};

In this case you could store BufferAccessors in a container, but you can't make a member template function virtual.

Armen Tsirunyan
What is the best way to inherit from this class when define a void return and some kind of object return for the dowork function
Talguy
@Talguy: I am afraid you will have to rephrase your question. I didn't understand it, sorry
Armen Tsirunyan
I want to inherit from the BufferAccessor class so that an object will be defined as a producer or consumer for my circular. would I just leave the functionality in the base class dowork function empty and then define the functionality in the derived classes. or should I write some filler code for the base class like cout << "Hello world".
Talguy
@Talguy: Honestly? I have no idea :)
Armen Tsirunyan
A: 

Yes, you can use vector<BufferAccessor<void,int> > to store BufferAccessor<void,int> objects and vector<BufferAccessor<int,void> > to store BufferAccessor<int,void> objects.

What you cant do is use same vector to store both BufferAccessor<int,void> and BufferAccessor<void,int> object The reason it doesnt work is because BufferAccessor<void,int>, and BufferAccessor<int,void> are two different classes

Note: it is possible to use same vector to store both BufferAccessor<int,void> and BufferAccessor<void,int> but you would have to either store them as void * using shared_ptr<void>. Or better yet you can use a boost::variant

Yogesh Arora