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 #ifndef _LIBUNWINDSTACK_SHAREDSTRING_H
18 #define _LIBUNWINDSTACK_SHAREDSTRING_H
19 
20 #include <memory>
21 #include <string>
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 static inline bool operator==(const SharedString& a, SharedString& b) {
51   return static_cast<std::string_view>(a) == static_cast<std::string_view>(b);
52 }
53 static inline bool operator==(const SharedString& a, std::string_view b) {
54   return static_cast<std::string_view>(a) == b;
55 }
56 static inline bool operator==(std::string_view a, const SharedString& b) {
57   return a == static_cast<std::string_view>(b);
58 }
59 static inline bool operator!=(const SharedString& a, SharedString& b) {
60   return !(a == b);
61 }
62 static inline bool operator!=(const SharedString& a, std::string_view b) {
63   return !(a == b);
64 }
65 static inline bool operator!=(std::string_view a, const SharedString& b) {
66   return !(a == b);
67 }
68 static inline std::string operator+(const SharedString& a, const char* b) {
69   return static_cast<const std::string&>(a) + b;
70 }
71 static inline std::string operator+(const char* a, const SharedString& b) {
72   return a + static_cast<const std::string&>(b);
73 }
74 
75 }  // namespace unwindstack
76 #endif  // _LIBUNWINDSTACK_SHAREDSTRING_H
77