Loading libs/binder/ndk/include_ndk/android/binder_auto_utils.h +37 −37 Original line number Diff line number Diff line Loading @@ -34,39 +34,39 @@ #include <cstddef> namespace android { namespace ndk { /** * Represents one strong pointer to an AIBinder object. */ class AutoAIBinder { class SpAIBinder { public: /** * Takes ownership of one strong refcount of binder. */ explicit AutoAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} explicit SpAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} /** * Convenience operator for implicitly constructing an AutoAIBinder from nullptr. This is not * Convenience operator for implicitly constructing an SpAIBinder from nullptr. This is not * explicit because it is not taking ownership of anything. */ AutoAIBinder(std::nullptr_t) : AutoAIBinder() {} SpAIBinder(std::nullptr_t) : SpAIBinder() {} /** * This will delete the underlying object if it exists. See operator=. */ AutoAIBinder(const AutoAIBinder& other) { *this = other; } SpAIBinder(const SpAIBinder& other) { *this = other; } /** * This deletes the underlying object if it exists. See set. */ ~AutoAIBinder() { set(nullptr); } ~SpAIBinder() { set(nullptr); } /** * This takes ownership of a binder from another AIBinder object but it does not affect the * ownership of that other object. */ AutoAIBinder& operator=(const AutoAIBinder& other) { SpAIBinder& operator=(const SpAIBinder& other) { AIBinder_incStrong(other.mBinder); set(other.mBinder); return *this; Loading @@ -82,7 +82,7 @@ public: /** * This returns the underlying binder object for transactions. If it is used to create another * AutoAIBinder object, it should first be incremented. * SpAIBinder object, it should first be incremented. */ AIBinder* get() const { return mBinder; } Loading @@ -92,7 +92,7 @@ public: * ownership to the object that is put in here. * * Recommended use is like this: * AutoAIBinder a; // will be nullptr * SpAIBinder a; // will be nullptr * SomeInitFunction(a.getR()); // value is initialized with refcount * * Other usecases are discouraged. Loading @@ -108,17 +108,17 @@ private: * This baseclass owns a single object, used to make various classes RAII. */ template <typename T, void (*Destroy)(T*)> class AutoA { class ScopedA { public: /** * Takes ownership of t. */ explicit AutoA(T* t = nullptr) : mT(t) {} explicit ScopedA(T* t = nullptr) : mT(t) {} /** * This deletes the underlying object if it exists. See set. */ ~AutoA() { set(nullptr); } ~ScopedA() { set(nullptr); } /** * Takes ownership of t. Loading @@ -144,7 +144,7 @@ public: * ownership to the object that is put in here. * * Recommended use is like this: * AutoA<T> a; // will be nullptr * ScopedA<T> a; // will be nullptr * SomeInitFunction(a.getR()); // value is initialized with refcount * * Other usecases are discouraged. Loading @@ -153,12 +153,12 @@ public: T** getR() { return &mT; } // copy-constructing, or move/copy assignment is disallowed AutoA(const AutoA&) = delete; AutoA& operator=(const AutoA&) = delete; AutoA& operator=(AutoA&&) = delete; ScopedA(const ScopedA&) = delete; ScopedA& operator=(const ScopedA&) = delete; ScopedA& operator=(ScopedA&&) = delete; // move-constructing is okay AutoA(AutoA&&) = default; ScopedA(ScopedA&&) = default; private: T* mT; Loading @@ -167,27 +167,27 @@ private: /** * Convenience wrapper. See AParcel. */ class AutoAParcel : public AutoA<AParcel, AParcel_delete> { class ScopedAParcel : public ScopedA<AParcel, AParcel_delete> { public: /** * Takes ownership of a. */ explicit AutoAParcel(AParcel* a = nullptr) : AutoA(a) {} ~AutoAParcel() {} AutoAParcel(AutoAParcel&&) = default; explicit ScopedAParcel(AParcel* a = nullptr) : ScopedA(a) {} ~ScopedAParcel() {} ScopedAParcel(ScopedAParcel&&) = default; }; /** * Convenience wrapper. See AStatus. */ class AutoAStatus : public AutoA<AStatus, AStatus_delete> { class ScopedAStatus : public ScopedA<AStatus, AStatus_delete> { public: /** * Takes ownership of a. */ explicit AutoAStatus(AStatus* a = nullptr) : AutoA(a) {} ~AutoAStatus() {} AutoAStatus(AutoAStatus&&) = default; explicit ScopedAStatus(AStatus* a = nullptr) : ScopedA(a) {} ~ScopedAStatus() {} ScopedAStatus(ScopedAStatus&&) = default; /** * See AStatus_isOk. Loading @@ -198,36 +198,36 @@ public: /** * Convenience wrapper. See AIBinder_DeathRecipient. */ class AutoAIBinder_DeathRecipient : public AutoA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> { class ScopedAIBinder_DeathRecipient : public ScopedA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> { public: /** * Takes ownership of a. */ explicit AutoAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : AutoA(a) {} ~AutoAIBinder_DeathRecipient() {} AutoAIBinder_DeathRecipient(AutoAIBinder_DeathRecipient&&) = default; explicit ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : ScopedA(a) {} ~ScopedAIBinder_DeathRecipient() {} ScopedAIBinder_DeathRecipient(ScopedAIBinder_DeathRecipient&&) = default; }; /** * Convenience wrapper. See AIBinder_Weak. */ class AutoAIBinder_Weak : public AutoA<AIBinder_Weak, AIBinder_Weak_delete> { class ScopedAIBinder_Weak : public ScopedA<AIBinder_Weak, AIBinder_Weak_delete> { public: /** * Takes ownership of a. */ explicit AutoAIBinder_Weak(AIBinder_Weak* a = nullptr) : AutoA(a) {} ~AutoAIBinder_Weak() {} AutoAIBinder_Weak(AutoAIBinder_Weak&&) = default; explicit ScopedAIBinder_Weak(AIBinder_Weak* a = nullptr) : ScopedA(a) {} ~ScopedAIBinder_Weak() {} ScopedAIBinder_Weak(ScopedAIBinder_Weak&&) = default; /** * See AIBinder_Weak_promote. */ AutoAIBinder promote() { return AutoAIBinder(AIBinder_Weak_promote(get())); } SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); } }; } // namespace android } // namespace ndk #endif // __cplusplus Loading libs/binder/ndk/include_ndk/android/binder_interface_utils.h +13 −13 Original line number Diff line number Diff line Loading @@ -35,7 +35,7 @@ #include <memory> #include <mutex> namespace android { namespace ndk { // analog using std::shared_ptr for RefBase-like semantics class SharedRefBase { Loading Loading @@ -68,7 +68,7 @@ public: virtual ~ICInterface() {} // This either returns the single existing implementation or creates a new implementation. virtual AutoAIBinder asBinder() = 0; virtual SpAIBinder asBinder() = 0; }; // wrapper analog to BnInterface Loading @@ -78,36 +78,36 @@ public: BnCInterface() {} virtual ~BnCInterface() {} AutoAIBinder asBinder() override; SpAIBinder asBinder() override; protected: // This function should only be called by asBinder. Otherwise, there is a possibility of // multiple AIBinder* objects being created for the same instance of an object. virtual AutoAIBinder createBinder() = 0; virtual SpAIBinder createBinder() = 0; private: std::mutex mMutex; // for asBinder AutoAIBinder_Weak mWeakBinder; ScopedAIBinder_Weak mWeakBinder; }; // wrapper analog to BpInterfae template <typename INTERFACE> class BpCInterface : public INTERFACE { public: BpCInterface(const AutoAIBinder& binder) : mBinder(binder) {} BpCInterface(const SpAIBinder& binder) : mBinder(binder) {} virtual ~BpCInterface() {} AutoAIBinder asBinder() override; SpAIBinder asBinder() override; private: AutoAIBinder mBinder; SpAIBinder mBinder; }; template <typename INTERFACE> AutoAIBinder BnCInterface<INTERFACE>::asBinder() { SpAIBinder BnCInterface<INTERFACE>::asBinder() { std::lock_guard<std::mutex> l(mMutex); AutoAIBinder binder; SpAIBinder binder; if (mWeakBinder.get() != nullptr) { binder.set(AIBinder_Weak_promote(mWeakBinder.get())); } Loading @@ -120,12 +120,12 @@ AutoAIBinder BnCInterface<INTERFACE>::asBinder() { } template <typename INTERFACE> AutoAIBinder BpCInterface<INTERFACE>::asBinder() { SpAIBinder BpCInterface<INTERFACE>::asBinder() { return mBinder; } #endif // __cplusplus } // namespace ndk } // namespace android #endif // __cplusplus /** @} */ libs/binder/ndk/include_ndk/android/binder_parcel_utils.h +4 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ #include <string> namespace ndk { /** * Takes a std::string and reallocates it to the specified length. For use with AParcel_readString. * See use below in AParcel_readString. Loading Loading @@ -66,6 +68,8 @@ static inline binder_status_t AParcel_readString(const AParcel* parcel, std::str &stringData); } } // namespace ndk #endif // __cplusplus /** @} */ Loading
libs/binder/ndk/include_ndk/android/binder_auto_utils.h +37 −37 Original line number Diff line number Diff line Loading @@ -34,39 +34,39 @@ #include <cstddef> namespace android { namespace ndk { /** * Represents one strong pointer to an AIBinder object. */ class AutoAIBinder { class SpAIBinder { public: /** * Takes ownership of one strong refcount of binder. */ explicit AutoAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} explicit SpAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} /** * Convenience operator for implicitly constructing an AutoAIBinder from nullptr. This is not * Convenience operator for implicitly constructing an SpAIBinder from nullptr. This is not * explicit because it is not taking ownership of anything. */ AutoAIBinder(std::nullptr_t) : AutoAIBinder() {} SpAIBinder(std::nullptr_t) : SpAIBinder() {} /** * This will delete the underlying object if it exists. See operator=. */ AutoAIBinder(const AutoAIBinder& other) { *this = other; } SpAIBinder(const SpAIBinder& other) { *this = other; } /** * This deletes the underlying object if it exists. See set. */ ~AutoAIBinder() { set(nullptr); } ~SpAIBinder() { set(nullptr); } /** * This takes ownership of a binder from another AIBinder object but it does not affect the * ownership of that other object. */ AutoAIBinder& operator=(const AutoAIBinder& other) { SpAIBinder& operator=(const SpAIBinder& other) { AIBinder_incStrong(other.mBinder); set(other.mBinder); return *this; Loading @@ -82,7 +82,7 @@ public: /** * This returns the underlying binder object for transactions. If it is used to create another * AutoAIBinder object, it should first be incremented. * SpAIBinder object, it should first be incremented. */ AIBinder* get() const { return mBinder; } Loading @@ -92,7 +92,7 @@ public: * ownership to the object that is put in here. * * Recommended use is like this: * AutoAIBinder a; // will be nullptr * SpAIBinder a; // will be nullptr * SomeInitFunction(a.getR()); // value is initialized with refcount * * Other usecases are discouraged. Loading @@ -108,17 +108,17 @@ private: * This baseclass owns a single object, used to make various classes RAII. */ template <typename T, void (*Destroy)(T*)> class AutoA { class ScopedA { public: /** * Takes ownership of t. */ explicit AutoA(T* t = nullptr) : mT(t) {} explicit ScopedA(T* t = nullptr) : mT(t) {} /** * This deletes the underlying object if it exists. See set. */ ~AutoA() { set(nullptr); } ~ScopedA() { set(nullptr); } /** * Takes ownership of t. Loading @@ -144,7 +144,7 @@ public: * ownership to the object that is put in here. * * Recommended use is like this: * AutoA<T> a; // will be nullptr * ScopedA<T> a; // will be nullptr * SomeInitFunction(a.getR()); // value is initialized with refcount * * Other usecases are discouraged. Loading @@ -153,12 +153,12 @@ public: T** getR() { return &mT; } // copy-constructing, or move/copy assignment is disallowed AutoA(const AutoA&) = delete; AutoA& operator=(const AutoA&) = delete; AutoA& operator=(AutoA&&) = delete; ScopedA(const ScopedA&) = delete; ScopedA& operator=(const ScopedA&) = delete; ScopedA& operator=(ScopedA&&) = delete; // move-constructing is okay AutoA(AutoA&&) = default; ScopedA(ScopedA&&) = default; private: T* mT; Loading @@ -167,27 +167,27 @@ private: /** * Convenience wrapper. See AParcel. */ class AutoAParcel : public AutoA<AParcel, AParcel_delete> { class ScopedAParcel : public ScopedA<AParcel, AParcel_delete> { public: /** * Takes ownership of a. */ explicit AutoAParcel(AParcel* a = nullptr) : AutoA(a) {} ~AutoAParcel() {} AutoAParcel(AutoAParcel&&) = default; explicit ScopedAParcel(AParcel* a = nullptr) : ScopedA(a) {} ~ScopedAParcel() {} ScopedAParcel(ScopedAParcel&&) = default; }; /** * Convenience wrapper. See AStatus. */ class AutoAStatus : public AutoA<AStatus, AStatus_delete> { class ScopedAStatus : public ScopedA<AStatus, AStatus_delete> { public: /** * Takes ownership of a. */ explicit AutoAStatus(AStatus* a = nullptr) : AutoA(a) {} ~AutoAStatus() {} AutoAStatus(AutoAStatus&&) = default; explicit ScopedAStatus(AStatus* a = nullptr) : ScopedA(a) {} ~ScopedAStatus() {} ScopedAStatus(ScopedAStatus&&) = default; /** * See AStatus_isOk. Loading @@ -198,36 +198,36 @@ public: /** * Convenience wrapper. See AIBinder_DeathRecipient. */ class AutoAIBinder_DeathRecipient : public AutoA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> { class ScopedAIBinder_DeathRecipient : public ScopedA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> { public: /** * Takes ownership of a. */ explicit AutoAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : AutoA(a) {} ~AutoAIBinder_DeathRecipient() {} AutoAIBinder_DeathRecipient(AutoAIBinder_DeathRecipient&&) = default; explicit ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : ScopedA(a) {} ~ScopedAIBinder_DeathRecipient() {} ScopedAIBinder_DeathRecipient(ScopedAIBinder_DeathRecipient&&) = default; }; /** * Convenience wrapper. See AIBinder_Weak. */ class AutoAIBinder_Weak : public AutoA<AIBinder_Weak, AIBinder_Weak_delete> { class ScopedAIBinder_Weak : public ScopedA<AIBinder_Weak, AIBinder_Weak_delete> { public: /** * Takes ownership of a. */ explicit AutoAIBinder_Weak(AIBinder_Weak* a = nullptr) : AutoA(a) {} ~AutoAIBinder_Weak() {} AutoAIBinder_Weak(AutoAIBinder_Weak&&) = default; explicit ScopedAIBinder_Weak(AIBinder_Weak* a = nullptr) : ScopedA(a) {} ~ScopedAIBinder_Weak() {} ScopedAIBinder_Weak(ScopedAIBinder_Weak&&) = default; /** * See AIBinder_Weak_promote. */ AutoAIBinder promote() { return AutoAIBinder(AIBinder_Weak_promote(get())); } SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); } }; } // namespace android } // namespace ndk #endif // __cplusplus Loading
libs/binder/ndk/include_ndk/android/binder_interface_utils.h +13 −13 Original line number Diff line number Diff line Loading @@ -35,7 +35,7 @@ #include <memory> #include <mutex> namespace android { namespace ndk { // analog using std::shared_ptr for RefBase-like semantics class SharedRefBase { Loading Loading @@ -68,7 +68,7 @@ public: virtual ~ICInterface() {} // This either returns the single existing implementation or creates a new implementation. virtual AutoAIBinder asBinder() = 0; virtual SpAIBinder asBinder() = 0; }; // wrapper analog to BnInterface Loading @@ -78,36 +78,36 @@ public: BnCInterface() {} virtual ~BnCInterface() {} AutoAIBinder asBinder() override; SpAIBinder asBinder() override; protected: // This function should only be called by asBinder. Otherwise, there is a possibility of // multiple AIBinder* objects being created for the same instance of an object. virtual AutoAIBinder createBinder() = 0; virtual SpAIBinder createBinder() = 0; private: std::mutex mMutex; // for asBinder AutoAIBinder_Weak mWeakBinder; ScopedAIBinder_Weak mWeakBinder; }; // wrapper analog to BpInterfae template <typename INTERFACE> class BpCInterface : public INTERFACE { public: BpCInterface(const AutoAIBinder& binder) : mBinder(binder) {} BpCInterface(const SpAIBinder& binder) : mBinder(binder) {} virtual ~BpCInterface() {} AutoAIBinder asBinder() override; SpAIBinder asBinder() override; private: AutoAIBinder mBinder; SpAIBinder mBinder; }; template <typename INTERFACE> AutoAIBinder BnCInterface<INTERFACE>::asBinder() { SpAIBinder BnCInterface<INTERFACE>::asBinder() { std::lock_guard<std::mutex> l(mMutex); AutoAIBinder binder; SpAIBinder binder; if (mWeakBinder.get() != nullptr) { binder.set(AIBinder_Weak_promote(mWeakBinder.get())); } Loading @@ -120,12 +120,12 @@ AutoAIBinder BnCInterface<INTERFACE>::asBinder() { } template <typename INTERFACE> AutoAIBinder BpCInterface<INTERFACE>::asBinder() { SpAIBinder BpCInterface<INTERFACE>::asBinder() { return mBinder; } #endif // __cplusplus } // namespace ndk } // namespace android #endif // __cplusplus /** @} */
libs/binder/ndk/include_ndk/android/binder_parcel_utils.h +4 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ #include <string> namespace ndk { /** * Takes a std::string and reallocates it to the specified length. For use with AParcel_readString. * See use below in AParcel_readString. Loading Loading @@ -66,6 +68,8 @@ static inline binder_status_t AParcel_readString(const AParcel* parcel, std::str &stringData); } } // namespace ndk #endif // __cplusplus /** @} */