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 #include <vector>
23
24 #include "android-base/macros.h"
25
26 #include "androidfw/StringPiece.h"
27
28 namespace android {
29 namespace util {
30
31 /**
32 * Makes a std::unique_ptr<> with the template parameter inferred by the
33 * compiler.
34 * This will be present in C++14 and can be removed then.
35 */
36 template <typename T, class... Args>
make_unique(Args &&...args)37 std::unique_ptr<T> make_unique(Args&&... args) {
38 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
39 }
40
41 // Based on std::unique_ptr, but uses free() to release malloc'ed memory
42 // without incurring the size increase of holding on to a custom deleter.
43 template <typename T>
44 class unique_cptr {
45 public:
46 using pointer = typename std::add_pointer<T>::type;
47
unique_cptr()48 constexpr unique_cptr() : ptr_(nullptr) {}
unique_cptr(std::nullptr_t)49 constexpr explicit unique_cptr(std::nullptr_t) : ptr_(nullptr) {}
unique_cptr(pointer ptr)50 explicit unique_cptr(pointer ptr) : ptr_(ptr) {}
unique_cptr(unique_cptr && o)51 unique_cptr(unique_cptr&& o) noexcept : ptr_(o.ptr_) { o.ptr_ = nullptr; }
52
~unique_cptr()53 ~unique_cptr() { std::free(reinterpret_cast<void*>(ptr_)); }
54
55 inline unique_cptr& operator=(unique_cptr&& o) noexcept {
56 if (&o == this) {
57 return *this;
58 }
59
60 std::free(reinterpret_cast<void*>(ptr_));
61 ptr_ = o.ptr_;
62 o.ptr_ = nullptr;
63 return *this;
64 }
65
66 inline unique_cptr& operator=(std::nullptr_t) {
67 std::free(reinterpret_cast<void*>(ptr_));
68 ptr_ = nullptr;
69 return *this;
70 }
71
release()72 pointer release() {
73 pointer result = ptr_;
74 ptr_ = nullptr;
75 return result;
76 }
77
get()78 inline pointer get() const { return ptr_; }
79
80 void reset(pointer ptr = pointer()) {
81 if (ptr == ptr_) {
82 return;
83 }
84
85 pointer old_ptr = ptr_;
86 ptr_ = ptr;
87 std::free(reinterpret_cast<void*>(old_ptr));
88 }
89
swap(unique_cptr & o)90 inline void swap(unique_cptr& o) { std::swap(ptr_, o.ptr_); }
91
92 inline explicit operator bool() const { return ptr_ != nullptr; }
93
94 inline typename std::add_lvalue_reference<T>::type operator*() const { return *ptr_; }
95
96 inline pointer operator->() const { return ptr_; }
97
98 inline bool operator==(const unique_cptr& o) const { return ptr_ == o.ptr_; }
99
100 inline bool operator!=(const unique_cptr& o) const { return ptr_ != o.ptr_; }
101
102 inline bool operator==(std::nullptr_t) const { return ptr_ == nullptr; }
103
104 inline bool operator!=(std::nullptr_t) const { return ptr_ != nullptr; }
105
106 private:
107 DISALLOW_COPY_AND_ASSIGN(unique_cptr);
108
109 pointer ptr_;
110 };
111
112 void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out);
113
114 // Converts a UTF-8 string to a UTF-16 string.
115 std::u16string Utf8ToUtf16(const StringPiece& utf8);
116
117 // Converts a UTF-16 string to a UTF-8 string.
118 std::string Utf16ToUtf8(const StringPiece16& utf16);
119
120 std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
121
122 } // namespace util
123 } // namespace android
124
125 #endif /* UTIL_H_ */
126