Home | History | Annotate | Download | only in androidfw
      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 
     25 #include "androidfw/StringPiece.h"
     26 
     27 namespace android {
     28 namespace 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  */
     35 template <typename T, class... Args>
     36 std::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.
     42 template <typename T>
     43 class 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) {}
     50   unique_cptr(unique_cptr&& o) : ptr_(o.ptr_) { o.ptr_ = nullptr; }
     51 
     52   ~unique_cptr() { std::free(reinterpret_cast<void*>(ptr_)); }
     53 
     54   inline unique_cptr& operator=(unique_cptr&& o) {
     55     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 
     99   inline bool operator!=(const unique_cptr& o) const { return ptr_ != o.ptr_; }
    100 
    101   inline bool operator==(std::nullptr_t) const { return ptr_ == nullptr; }
    102 
    103   inline bool operator!=(std::nullptr_t) const { return ptr_ != nullptr; }
    104 
    105  private:
    106   DISALLOW_COPY_AND_ASSIGN(unique_cptr);
    107 
    108   pointer ptr_;
    109 };
    110 
    111 void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out);
    112 
    113 // Converts a UTF-8 string to a UTF-16 string.
    114 std::u16string Utf8ToUtf16(const StringPiece& utf8);
    115 
    116 // Converts a UTF-16 string to a UTF-8 string.
    117 std::string Utf16ToUtf8(const StringPiece16& utf16);
    118 
    119 }  // namespace util
    120 }  // namespace android
    121 
    122 #endif /* UTIL_H_ */
    123