HidlInternal.h revision dda25621dd0549fa7b058f553b20f3a6eca735c8
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ANDROID_HIDL_INTERNAL_H 18#define ANDROID_HIDL_INTERNAL_H 19 20#include <cstdint> 21#include <utility> 22 23namespace android { 24namespace hardware { 25namespace details { 26 27// hidl_log_base is a base class that templatized 28// classes implemented in a header can inherit from, 29// to avoid creating dependencies on liblog. 30struct hidl_log_base { 31 void logAlwaysFatal(const char *message) const; 32}; 33 34// HIDL client/server code should *NOT* use this class. 35// 36// hidl_pointer wraps a pointer without taking ownership, 37// and stores it in a union with a uint64_t. This ensures 38// that we always have enough space to store a pointer, 39// regardless of whether we're running in a 32-bit or 64-bit 40// process. 41template<typename T> 42struct hidl_pointer { 43 hidl_pointer() 44 : _pad(0) { 45 } 46 hidl_pointer(T* ptr) 47 : mPointer(ptr) { 48 } 49 hidl_pointer(const hidl_pointer<T>& other) { 50 mPointer = other.mPointer; 51 } 52 hidl_pointer(hidl_pointer<T>&& other) { 53 *this = std::move(other); 54 } 55 56 hidl_pointer &operator=(const hidl_pointer<T>& other) { 57 mPointer = other.mPointer; 58 return *this; 59 } 60 hidl_pointer &operator=(hidl_pointer<T>&& other) { 61 mPointer = other.mPointer; 62 other.mPointer = nullptr; 63 return *this; 64 } 65 hidl_pointer &operator=(T* ptr) { 66 mPointer = ptr; 67 return *this; 68 } 69 70 operator T*() const { 71 return mPointer; 72 } 73 explicit operator void*() const { // requires explicit cast to avoid ambiguity 74 return mPointer; 75 } 76 T& operator*() const { 77 return *mPointer; 78 } 79 T* operator->() const { 80 return mPointer; 81 } 82 T &operator[](size_t index) { 83 return mPointer[index]; 84 } 85 const T &operator[](size_t index) const { 86 return mPointer[index]; 87 } 88 89private: 90 union { 91 T* mPointer; 92 uint64_t _pad; 93 }; 94}; 95 96} // namespace details 97} // namespace hardware 98} // namespace android 99 100#endif // ANDROID_HIDL_INTERNAL_H 101