• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <memory>
20 #include <string>
21 #include <type_traits>
22 
23 namespace unwindstack {
24 
25 // Ref-counted read-only string.  Used to avoid string allocations/copies.
26 // It is intended to be transparent std::string replacement in most cases.
27 class SharedString {
28  public:
SharedString()29   SharedString() : data_() {}
SharedString(std::string && s)30   SharedString(std::string&& s) : data_(std::make_shared<const std::string>(std::move(s))) {}
SharedString(const std::string & s)31   SharedString(const std::string& s) : SharedString(std::string(s)) {}
SharedString(const char * s)32   SharedString(const char* s) : SharedString(std::string(s)) {}
33 
clear()34   void clear() { data_.reset(); }
is_null()35   bool is_null() const { return data_.get() == nullptr; }
empty()36   bool empty() const { return is_null() ? true : data_->empty(); }
c_str()37   const char* c_str() const { return is_null() ? "" : data_->c_str(); }
38 
39   operator const std::string&() const {
40     [[clang::no_destroy]] static const std::string empty;
41     return data_ ? *data_.get() : empty;
42   }
43 
string_view()44   operator std::string_view() const { return static_cast<const std::string&>(*this); }
45 
46  private:
47   std::shared_ptr<const std::string> data_;
48 };
49 
50 template <typename T, typename = std::enable_if_t<std::is_same_v<T, SharedString>>>
51 static inline bool operator==(const T& a, const T& b) {
52   return static_cast<std::string_view>(a) == static_cast<std::string_view>(b);
53 }
54 static inline bool operator==(const SharedString& a, std::string_view b) {
55   return static_cast<std::string_view>(a) == b;
56 }
57 static inline bool operator==(std::string_view a, const SharedString& b) {
58   return a == static_cast<std::string_view>(b);
59 }
60 template <typename T, typename = std::enable_if_t<std::is_same_v<T, SharedString>>>
61 static inline bool operator!=(const T& a, const T& b) {
62   return !(a == b);
63 }
64 static inline bool operator!=(const SharedString& a, std::string_view b) {
65   return !(a == b);
66 }
67 static inline bool operator!=(std::string_view a, const SharedString& b) {
68   return !(a == b);
69 }
70 static inline std::string operator+(const SharedString& a, const char* b) {
71   return static_cast<const std::string&>(a) + b;
72 }
73 static inline std::string operator+(const char* a, const SharedString& b) {
74   return a + static_cast<const std::string&>(b);
75 }
76 
77 }  // namespace unwindstack
78