Add tags for hidl interfaces.
IFoo -> i_tag
BsFoo -> bs_tag
BnFoo -> bnhw_tag
BpFoo -> bphw_tag
Also added enable_if. This will allow us to give better error
messages and add static_asserts in certain places.
Note, the tags are in HidlSupport instead of split between
HidlSupport/HidlPassthroughSupport/HidlBinderSupport because
of this loop in that case:
HidlBinderSupport.h --> HwBnBase.h --> HidlBinderSupport.h
Bug: 62807182
Test: compiles (these are all static checks)
Change-Id: I49a5ec824959484d237c8fa3c81eaa530be8e073
diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h
index 6c0d8df..9b9457f 100644
--- a/base/include/hidl/HidlInternal.h
+++ b/base/include/hidl/HidlInternal.h
@@ -28,6 +28,18 @@
namespace hardware {
namespace details {
+// tag for pure interfaces (e.x. IFoo)
+struct i_tag {};
+
+// tag for server interfaces (e.x. BnHwFoo)
+struct bnhw_tag {};
+
+// tag for proxy interfaces (e.x. BpHwFoo)
+struct bphw_tag {};
+
+// tag for server interfaces (e.x. BsFoo)
+struct bs_tag {};
+
//Templated classes can use the below method
//to avoid creating dependencies on liblog.
void logAlwaysFatal(const char *message);
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index 47ff581..fc834b9 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -306,7 +306,8 @@
// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
// and iface is of class IChild. BnChild will be used to wrapped the given iface.
// Return nullptr if iface is null or any failure.
-template <typename IType>
+template <typename IType,
+ typename = std::enable_if_t<std::is_same<details::i_tag, typename IType::_hidl_tag>::value>>
sp<IBinder> toBinder(sp<IType> iface) {
IType *ifacePtr = iface.get();
if (ifacePtr == nullptr) {
diff --git a/transport/include/hidl/HidlTransportSupport.h b/transport/include/hidl/HidlTransportSupport.h
index d116598..18a4036 100644
--- a/transport/include/hidl/HidlTransportSupport.h
+++ b/transport/include/hidl/HidlTransportSupport.h
@@ -62,7 +62,10 @@
bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
int policy, int priority);
-template <typename ILeft, typename IRight>
+template <typename ILeft,
+ typename IRight,
+ typename = std::enable_if_t<std::is_same<details::i_tag, typename ILeft::_hidl_tag>::value>,
+ typename = std::enable_if_t<std::is_same<details::i_tag, typename IRight::_hidl_tag>::value>>
bool interfacesEqual(sp<ILeft> left, sp<IRight> right) {
if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
return left == right;