andorid sp

来源:互联网 发布:人工智能之父西蒙 编辑:程序博客网 时间:2024/04/28 22:42

template <typename TYPE>
class ANDROID_API Singleton   模板单例类
{
public:
    static TYPE& getInstance() {//static 构成单例类很重要
        Mutex::Autolock _l(sLock);
        TYPE* instance = sInstance;
        if (instance == 0) {
            instance = new TYPE();
            sInstance = instance;
        }
        return *instance;
    }

class ComposerService : public Singleton<ComposerService>

getService(name, &mComposerService)

template<typename INTERFACE>
status_t getService(const String16& name, sp<INTERFACE>* outService)
{
    const sp<IServiceManager> sm = defaultServiceManager();
    if (sm != NULL) {
        *outService = interface_cast<INTERFACE>(sm->getService(name));
        if ((*outService) != NULL) return NO_ERROR;
    }
    return NAME_NOT_FOUND;
}

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}

template <typename T>
class sp
{
public:
    inline sp() : m_ptr(0) { }

    sp(T* other);
 template<typename U> sp(U* other);
}

template<typename T>
sp<T>::sp(T* other)
: m_ptr(other)
  {
    if (other) other->incStrong(this);
  }

template<typename T> template<typename U>
sp<T>::sp(U* other) : m_ptr(other)
{
    if (other) ((T*)other)->incStrong(this);
}

原创粉丝点击