0

我在将模板类存储在两个对象的基础上时遇到问题。

假设我有一个名为 ObjectManager 的广义类定义为:

template<typename T>
class ObjectManager
{}

我有一个像这样的基类:

class MediaSample
{
public:
    MediaSample(ObjectManager<?>* manager){}
private:
    ObjectManager<?> mMediaSampleManager;
}

现在我从 MediaSample 声明另外两个驱动类,比如:

class AudioSample : public MediaSample
{
public:
    AudioSample() : MediaSample(new ObjectManager<AudioSample>()){}
    //...
}
public class VideoSample : public MediaSample
{
public:
    VideoSample() : MediaSample(new ObjectManager<VideoSample>())
    //...
}

我的问题是经理的类型,类的输入参数MediaSample

必须放置哪种类型而不是问号字符?(音频、视频还是什么?)

为了更清楚,我发布了 ObjectManager 的完整来源,请注意对象管理器是媒体样本对象的轮询,媒体样本可以是音频或视频

class BaseMediaSampleManager
{
public:
    ~BaseMediaSampleManager(){}

    virtual INT initialize(INT objectCount) = 0;

protected:
private:
};

template<typename T>
class MediaSamppleManager : public BaseMediaSampleMnager //(based on user2079303 advise)
{
protected:
    MediaSamppleManager(void);

    MediaSamppleManager(const MediaSamppleManager& manager);

    MediaSamppleManager& operator = (const MediaSamppleManager& rhs);

public: 

    virtual ~MediaSamppleManager(void);

    static MediaSamppleManager<T>* instance();

    INT initialize(INT objectCount);

    // take existing object from object pool and return that object
    INT aquireObject(T** object);

    // take back in use object to object pool
    INT release(T* object);

private:
    std::list<T*> mPooledSamples;
    INT32 mPooledObjectCount;
    INT32 mInUseObjects;
    Mutex mPooledSamplesMutex;
    static Mutex mSampleManagerObjectMutex;
    static MediaSamppleManager<T>* mSampleManagerObject;
};

template<typename T>
Mutex MediaSamppleManager<T>::mSampleManagerObjectMutex;

template<typename T>
MediaSamppleManager<T>* MediaSamppleManager<T>::mSampleManagerObject = NULL;

template<typename T>
MediaSamppleManager<T>* MediaSamppleManager<T>::instance()
{
    if (mSampleManagerObject == NULL)
    {
        ScopedLock<Mutex> guard(mSampleManagerObjectMutex);
        if (mSampleManagerObject == NULL)
        {
            MediaSamppleManager<T>* temp = new MediaSamppleManager<T>();
            mSampleManagerObject = temp;
        }
    }
    return mSampleManagerObject;
}

template<typename T>
MediaSamppleManager<T>& MediaSamppleManager<T>::operator=(const MediaSamppleManager<T>& rhs)
{

}

template<typename T>
MediaSamppleManager<T>::MediaSamppleManager(const MediaSamppleManager<T>& manager)
{

}

template<typename T>
INT MediaSamppleManager<T>::release(T* object)
{
    ScopedLock<Mutex> guard(mPooledSamplesMutex);
    mPooledSamples.push_back(object);
    mInUseObjects--;
}

template<typename T>
INT MediaSamppleManager<T>::aquireObject(T** object)
{
    ScopedLock<Mutex> guard(mPooledSamplesMutex);
    if (mInUseObjects == mPooledObjectCount)
    {
        // do we need waiting until new sample become available? or 
        // is it required to create new sample when no free sample exist in pool? 
        return 2;
    } 
    else
    {
        T* temp = 0;
        temp = mPooledSamples.front();
        *object = temp;
        mPooledSamples.pop_front();
        mInUseObjects++;
    }
    return 1;
}

template<typename T>
INT MediaSamppleManager<T>::initialize(INT objectCount)
{
    if (objectCount<=0)
    {
        return -1;
    }
    mPooledObjectCount = objectCount;
    mPooledSamples.resize(objectCount);
    for (int i = 0 ; i< objectCount ; i++)
    {
        T* temp = new T(this);
        mPooledSamples.push_back(temp);
    }
    return 1;
}

template<typename T>
MediaSamppleManager<T>::~MediaSamppleManager(void)
{
    std::cout << "MediaSampleManager Destroyed \n";
}

template<typename T>
MediaSamppleManager<T>::MediaSamppleManager(void)
    :mPooledObjectCount(0)
    ,mInUseObjects(0)
{

}

类媒体样本:

class MediaSample
{
public:
    // create empty media sample
    MediaSample(BaseMediaSampleManager* manager);

    virtual ~MediaSample(void);

    virtual INT32 rate() = 0;

    virtual double frameSize() = 0;

    // data size of media sample
    virtual INT size();

    UINT* data();

    ULONG samplingTime();

    INT32 addRef();

    INT32 release();    

private:
    UINT8* mMediaSampleBuffer;
    // The time sample captured
    ULONG mTimestamp;
    // length of data
    INT mDataLength;

    INT mRefCount;

    BaseMediaSampleManager* mMediaSampleManager;
};

类 AudioSample :

class PMCAPI AudioSample 
    :public MediaSample/*<AudioSample>*/
{
public:
    AudioSample(MediaSamppleManager<AudioSample>* manager);

    ~AudioSample();

    virtual INT32 rate();

    virtual double frameSize();

    virtual INT size();

protected:
private:
    // in 8,16 KHZ
    INT32 mSampleRate;
    // Mono or Stereo
    INT mChannels;
    // Used format ex: PCM,G729,G723,G711A,G711U
    INT mCodec;
    // ex : 8 bit , 16 bit
    INT mBitsPerSample;
};
4

3 回答 3

2

混合编译时间(模板)和运行时多态性(继承)可能会变得非常困难。你可以做什么:

a) 使基类成为模板

template <class T>
class MediaSample {
    ObjectManager<T> mMediaSampleManager;
};

class AudioSample : public MediaSample<AudioSample>

在这种情况下AudioSample并不VideoSample会有一个通用的基类,这可能不是你想要的。或者,您可以

b) 给 ObjectManager 一个非模板基类(一个接口)

class BaseManager{
    virtual ~BaseManager(){}
    // pure virtual functions that ObjectManager implements
};

template<typename T>
class ObjectManager: public BaseManager{
    // implementation
};

class MediaSample {
    BaseManager mMediaSampleManager;
    // ...
};

class AudioSample: public MediaSample {
public:
    AudioSample() : MediaSample(new ObjectManager<AudioSample>()){}

另外,请注意,如果您的MediaSample构造函数接受一个指向新构造的指针ObjectManager并且您可能将其复制到成员对象,您必须记住删除该指针,否则它会泄漏。您可能应该按值传递。

于 2014-07-31T11:35:20.790 回答
0

如何让 MediaSample 成为模板:

template <typename T>
class MediaSample
{
public:
    MediaSample(ObjectManager<T>* manager){}
private:
    ObjectManager<T> mMediaSampleManager;
};

这样您就可以拥有不同类型的 MediaSample,而无需派生类 AudioSample 和 VideoSample。

于 2014-07-31T11:30:39.487 回答
0

在这种情况下MediaSample需要是一个模板

template<typename T>
class MediaSample
{
public:
    MediaSample(ObjectManager<T>* manager): mMediaSampleManager(manager) {}
private:
    ObjectManager<T>* mMediaSampleManager;
}

并且XxxSample必须从专业继承MediaSample

class AudioSample : public MediaSample<AudioSample>
{
public:
    AudioSample() : MediaSample(new ObjectManager<AudioSample>()){}
    ...
}
于 2014-07-31T11:30:56.413 回答