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