blob: 6c9eee0b88354c898f098940a73c1086ffb375cc [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080025#include "androidfw/StringPiece.h"
26
Adam Lesinski7ad11102016-10-28 16:39:15 -070027namespace android {
28namespace util {
29
30/**
31 * Makes a std::unique_ptr<> with the template parameter inferred by the
32 * compiler.
33 * This will be present in C++14 and can be removed then.
34 */
35template <typename T, class... Args>
36std::unique_ptr<T> make_unique(Args&&... args) {
37 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
38}
39
40// Based on std::unique_ptr, but uses free() to release malloc'ed memory
41// without incurring the size increase of holding on to a custom deleter.
42template <typename T>
43class unique_cptr {
44 public:
45 using pointer = typename std::add_pointer<T>::type;
46
47 constexpr unique_cptr() : ptr_(nullptr) {}
48 constexpr unique_cptr(std::nullptr_t) : ptr_(nullptr) {}
49 explicit unique_cptr(pointer ptr) : ptr_(ptr) {}
Chih-Hung Hsiehfc816262018-09-25 12:01:21 -070050 unique_cptr(unique_cptr&& o) noexcept : ptr_(o.ptr_) { o.ptr_ = nullptr; }
Adam Lesinski7ad11102016-10-28 16:39:15 -070051
52 ~unique_cptr() { std::free(reinterpret_cast<void*>(ptr_)); }
53
Chih-Hung Hsiehfc816262018-09-25 12:01:21 -070054 inline unique_cptr& operator=(unique_cptr&& o) noexcept {
Adam Lesinski7ad11102016-10-28 16:39:15 -070055 if (&o == this) {
56 return *this;
57 }
58
59 std::free(reinterpret_cast<void*>(ptr_));
60 ptr_ = o.ptr_;
61 o.ptr_ = nullptr;
62 return *this;
63 }
64
65 inline unique_cptr& operator=(std::nullptr_t) {
66 std::free(reinterpret_cast<void*>(ptr_));
67 ptr_ = nullptr;
68 return *this;
69 }
70
71 pointer release() {
72 pointer result = ptr_;
73 ptr_ = nullptr;
74 return result;
75 }
76
77 inline pointer get() const { return ptr_; }
78
79 void reset(pointer ptr = pointer()) {
80 if (ptr == ptr_) {
81 return;
82 }
83
84 pointer old_ptr = ptr_;
85 ptr_ = ptr;
86 std::free(reinterpret_cast<void*>(old_ptr));
87 }
88
89 inline void swap(unique_cptr& o) { std::swap(ptr_, o.ptr_); }
90
91 inline explicit operator bool() const { return ptr_ != nullptr; }
92
93 inline typename std::add_lvalue_reference<T>::type operator*() const { return *ptr_; }
94
95 inline pointer operator->() const { return ptr_; }
96
97 inline bool operator==(const unique_cptr& o) const { return ptr_ == o.ptr_; }
98
Adam Lesinski0c405242017-01-13 20:47:26 -080099 inline bool operator!=(const unique_cptr& o) const { return ptr_ != o.ptr_; }
100
Adam Lesinski7ad11102016-10-28 16:39:15 -0700101 inline bool operator==(std::nullptr_t) const { return ptr_ == nullptr; }
102
Adam Lesinski0c405242017-01-13 20:47:26 -0800103 inline bool operator!=(std::nullptr_t) const { return ptr_ != nullptr; }
104
Adam Lesinski7ad11102016-10-28 16:39:15 -0700105 private:
106 DISALLOW_COPY_AND_ASSIGN(unique_cptr);
107
108 pointer ptr_;
109};
110
Adam Lesinskida431a22016-12-29 16:08:16 -0500111void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out);
112
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800113// Converts a UTF-8 string to a UTF-16 string.
114std::u16string Utf8ToUtf16(const StringPiece& utf8);
115
116// Converts a UTF-16 string to a UTF-8 string.
117std::string Utf16ToUtf8(const StringPiece16& utf16);
118
Adam Lesinski7ad11102016-10-28 16:39:15 -0700119} // namespace util
120} // namespace android
121
122#endif /* UTIL_H_ */