dimanche 6 novembre 2016

why Android's NuCachedSource2 complete the handler by reflector instead of by multiple inheritance

Dears:

I am wondering why Android's NuCachedSource2 complete the handler by reflector instead of by multiple inheritance?

It is,

NuCachedSource2::NuCachedSource2(
    const sp<DataSource> &source,
    const char *cacheConfig,
    bool disconnectAtHighwatermark)
: mSource(source),
  mReflector(new AHandlerReflector<NuCachedSource2>(this)), ...




template<class T>
struct AHandlerReflector : public AHandler {
    AHandlerReflector(T *target)
        : mTarget(target) {
    }

protected:
    virtual void onMessageReceived(const sp<AMessage> &msg) {
        sp<T> target = mTarget.promote();
        if (target != NULL) {
            target->onMessageReceived(msg);
        }
    }

private:
    wp<T> mTarget;

    AHandlerReflector(const AHandlerReflector<T> &);
    AHandlerReflector<T> &operator=(const AHandlerReflector<T> &);
};

by the definition you could see NuCachedSource2 inherits DataSource as

struct NuCachedSource2 : public DataSource {

but adopt a method names "Reflector" which uses template to complete the necessary interface "onMessageReceived()".

However, why NuCachedSource2 does NOT inherit both DataSource & AHandler directly but uses this template way instead?

Is there anyone knows the root cause?

Thanks!

Aucun commentaire:

Enregistrer un commentaire