Util.h revision 0c40524953f3d36a880f91183302a2ea5c722930
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 UTIL_H_ 18#define UTIL_H_ 19 20#include <cstdlib> 21#include <memory> 22 23#include "android-base/macros.h" 24 25namespace android { 26namespace util { 27 28/** 29 * Makes a std::unique_ptr<> with the template parameter inferred by the 30 * compiler. 31 * This will be present in C++14 and can be removed then. 32 */ 33template <typename T, class... Args> 34std::unique_ptr<T> make_unique(Args&&... args) { 35 return std::unique_ptr<T>(new T{std::forward<Args>(args)...}); 36} 37 38// Based on std::unique_ptr, but uses free() to release malloc'ed memory 39// without incurring the size increase of holding on to a custom deleter. 40template <typename T> 41class unique_cptr { 42 public: 43 using pointer = typename std::add_pointer<T>::type; 44 45 constexpr unique_cptr() : ptr_(nullptr) {} 46 constexpr unique_cptr(std::nullptr_t) : ptr_(nullptr) {} 47 explicit unique_cptr(pointer ptr) : ptr_(ptr) {} 48 unique_cptr(unique_cptr&& o) : ptr_(o.ptr_) { o.ptr_ = nullptr; } 49 50 ~unique_cptr() { std::free(reinterpret_cast<void*>(ptr_)); } 51 52 inline unique_cptr& operator=(unique_cptr&& o) { 53 if (&o == this) { 54 return *this; 55 } 56 57 std::free(reinterpret_cast<void*>(ptr_)); 58 ptr_ = o.ptr_; 59 o.ptr_ = nullptr; 60 return *this; 61 } 62 63 inline unique_cptr& operator=(std::nullptr_t) { 64 std::free(reinterpret_cast<void*>(ptr_)); 65 ptr_ = nullptr; 66 return *this; 67 } 68 69 pointer release() { 70 pointer result = ptr_; 71 ptr_ = nullptr; 72 return result; 73 } 74 75 inline pointer get() const { return ptr_; } 76 77 void reset(pointer ptr = pointer()) { 78 if (ptr == ptr_) { 79 return; 80 } 81 82 pointer old_ptr = ptr_; 83 ptr_ = ptr; 84 std::free(reinterpret_cast<void*>(old_ptr)); 85 } 86 87 inline void swap(unique_cptr& o) { std::swap(ptr_, o.ptr_); } 88 89 inline explicit operator bool() const { return ptr_ != nullptr; } 90 91 inline typename std::add_lvalue_reference<T>::type operator*() const { return *ptr_; } 92 93 inline pointer operator->() const { return ptr_; } 94 95 inline bool operator==(const unique_cptr& o) const { return ptr_ == o.ptr_; } 96 97 inline bool operator!=(const unique_cptr& o) const { return ptr_ != o.ptr_; } 98 99 inline bool operator==(std::nullptr_t) const { return ptr_ == nullptr; } 100 101 inline bool operator!=(std::nullptr_t) const { return ptr_ != nullptr; } 102 103 private: 104 DISALLOW_COPY_AND_ASSIGN(unique_cptr); 105 106 pointer ptr_; 107}; 108 109inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) { 110 return resid | (static_cast<uint32_t>(package_id) << 24); 111} 112 113inline uint8_t get_package_id(uint32_t resid) { 114 return static_cast<uint8_t>((resid >> 24) & 0x000000ffu); 115} 116 117// The type ID is 1-based, so if the returned value is 0 it is invalid. 118inline uint8_t get_type_id(uint32_t resid) { 119 return static_cast<uint8_t>((resid >> 16) & 0x000000ffu); 120} 121 122inline uint16_t get_entry_id(uint32_t resid) { return static_cast<uint16_t>(resid & 0x0000ffffu); } 123 124inline bool is_internal_resid(uint32_t resid) { 125 return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0; 126} 127 128inline bool is_valid_resid(uint32_t resid) { 129 return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0; 130} 131 132void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out); 133 134} // namespace util 135} // namespace android 136 137#endif /* UTIL_H_ */ 138