|
| 1 | +/* |
| 2 | +Copyright (c) 2019 Artem Chernyshev |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "imstring.h" |
| 24 | +#include "imgui.h" |
| 25 | +#include "imgui_internal.h" |
| 26 | + |
| 27 | +ImString::ImString() |
| 28 | + : mData(0) |
| 29 | + , mRefCount(0) |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +ImString::ImString(size_t len) |
| 34 | + : mData(0) |
| 35 | + , mRefCount(0) |
| 36 | +{ |
| 37 | + reserve(len); |
| 38 | +} |
| 39 | + |
| 40 | +ImString::ImString(char* string) |
| 41 | + : mData(string) |
| 42 | + , mRefCount(0) |
| 43 | +{ |
| 44 | + ref(); |
| 45 | +} |
| 46 | + |
| 47 | +ImString::ImString(const char* string) |
| 48 | + : mData(0) |
| 49 | + , mRefCount(0) |
| 50 | +{ |
| 51 | + if(string) { |
| 52 | + mData = ImStrdup(string); |
| 53 | + ref(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +ImString::ImString(const ImString& other) |
| 58 | +{ |
| 59 | + mRefCount = other.mRefCount; |
| 60 | + mData = other.mData; |
| 61 | + ref(); |
| 62 | +} |
| 63 | + |
| 64 | +ImString::~ImString() |
| 65 | +{ |
| 66 | + unref(); |
| 67 | +} |
| 68 | + |
| 69 | +char& ImString::operator[](size_t pos) { |
| 70 | + return mData[pos]; |
| 71 | +} |
| 72 | + |
| 73 | +ImString::operator char* () { return mData; } |
| 74 | + |
| 75 | +bool ImString::operator==(const char* string) |
| 76 | +{ |
| 77 | + return strcmp(string, mData) == 0; |
| 78 | +} |
| 79 | + |
| 80 | +bool ImString::operator!=(const char* string) |
| 81 | +{ |
| 82 | + return strcmp(string, mData) != 0; |
| 83 | +} |
| 84 | + |
| 85 | +bool ImString::operator==(const ImString& string) |
| 86 | +{ |
| 87 | + return strcmp(string.c_str(), mData) == 0; |
| 88 | +} |
| 89 | + |
| 90 | +bool ImString::operator!=(const ImString& string) |
| 91 | +{ |
| 92 | + return strcmp(string.c_str(), mData) != 0; |
| 93 | +} |
| 94 | + |
| 95 | +ImString& ImString::operator=(const char* string) |
| 96 | +{ |
| 97 | + if(mData) |
| 98 | + unref(); |
| 99 | + mData = ImStrdup(string); |
| 100 | + ref(); |
| 101 | + return *this; |
| 102 | +} |
| 103 | + |
| 104 | +ImString& ImString::operator=(const ImString& other) |
| 105 | +{ |
| 106 | + if(mData && mData != other.mData) |
| 107 | + unref(); |
| 108 | + mRefCount = other.mRefCount; |
| 109 | + mData = other.mData; |
| 110 | + ref(); |
| 111 | + return *this; |
| 112 | +} |
| 113 | + |
| 114 | +void ImString::reserve(size_t len) |
| 115 | +{ |
| 116 | + if(mData) |
| 117 | + unref(); |
| 118 | + mData = (char*)ImGui::MemAlloc(len + 1); |
| 119 | + mData[len] = '\0'; |
| 120 | + ref(); |
| 121 | +} |
| 122 | + |
| 123 | +char* ImString::get() { |
| 124 | + return mData; |
| 125 | +} |
| 126 | + |
| 127 | +const char* ImString::c_str() const { |
| 128 | + return mData; |
| 129 | +} |
| 130 | + |
| 131 | +bool ImString::empty() const { |
| 132 | + return mData == 0 || mData[0] == '\0'; |
| 133 | +} |
| 134 | + |
| 135 | +int ImString::refcount() const { |
| 136 | + return *mRefCount; |
| 137 | +} |
| 138 | + |
| 139 | +void ImString::ref() { |
| 140 | + if(!mRefCount) { |
| 141 | + mRefCount = new int(); |
| 142 | + (*mRefCount) = 0; |
| 143 | + } |
| 144 | + (*mRefCount)++; |
| 145 | +} |
| 146 | + |
| 147 | +void ImString::unref() |
| 148 | +{ |
| 149 | + if(mRefCount) { |
| 150 | + (*mRefCount)--; |
| 151 | + if(*mRefCount == 0) { |
| 152 | + ImGui::MemFree(mData); |
| 153 | + mData = 0; |
| 154 | + delete mRefCount; |
| 155 | + } |
| 156 | + mRefCount = 0; |
| 157 | + } |
| 158 | +} |
0 commit comments