diff options
Diffstat (limited to 'src/linda.cpp')
-rw-r--r-- | src/linda.cpp | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/src/linda.cpp b/src/linda.cpp index 48da1cf..1916629 100644 --- a/src/linda.cpp +++ b/src/linda.cpp | |||
@@ -79,10 +79,11 @@ template <bool OPT> | |||
79 | { | 79 | { |
80 | Linda* const _linda{ ToLinda<OPT>(L_, idx_) }; | 80 | Linda* const _linda{ ToLinda<OPT>(L_, idx_) }; |
81 | if (_linda != nullptr) { | 81 | if (_linda != nullptr) { |
82 | std::string_view const _lindaName{ _linda->getName() }; | ||
82 | char _text[128]; | 83 | char _text[128]; |
83 | int _len; | 84 | int _len; |
84 | if (_linda->getName()) | 85 | if (!_lindaName.empty()) |
85 | _len = sprintf(_text, "Linda: %.*s", (int) sizeof(_text) - 8, _linda->getName()); | 86 | _len = sprintf(_text, "Linda: %.*s", (int) sizeof(_text) - 8, _lindaName.data()); |
86 | else | 87 | else |
87 | _len = sprintf(_text, "Linda: %p", _linda); | 88 | _len = sprintf(_text, "Linda: %p", _linda); |
88 | lua_pushlstring(L_, _text, _len); | 89 | lua_pushlstring(L_, _text, _len); |
@@ -113,37 +114,37 @@ template <bool OPT> | |||
113 | // have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer | 114 | // have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer |
114 | static constexpr uintptr_t kPointerMagicShift{ 3 }; | 115 | static constexpr uintptr_t kPointerMagicShift{ 3 }; |
115 | 116 | ||
116 | Linda::Linda(Universe* U_, LindaGroup group_, char const* name_, size_t len_) | 117 | Linda::Linda(Universe* U_, LindaGroup group_, std::string_view const& name_) |
117 | : DeepPrelude{ LindaFactory::Instance } | 118 | : DeepPrelude{ LindaFactory::Instance } |
118 | , U{ U_ } | 119 | , U{ U_ } |
119 | , keeperIndex{ (group_ ? group_ : static_cast<int>(std::bit_cast<uintptr_t>(this) >> kPointerMagicShift)) % U_->keepers->nb_keepers } | 120 | , keeperIndex{ (group_ ? group_ : static_cast<int>(std::bit_cast<uintptr_t>(this) >> kPointerMagicShift)) % U_->keepers->nb_keepers } |
120 | { | 121 | { |
121 | setName(name_, len_); | 122 | setName(name_); |
122 | } | 123 | } |
123 | 124 | ||
124 | // ################################################################################################# | 125 | // ################################################################################################# |
125 | 126 | ||
126 | Linda::~Linda() | 127 | Linda::~Linda() |
127 | { | 128 | { |
128 | if (std::holds_alternative<AllocatedName>(nameVariant)) { | 129 | if (std::holds_alternative<std::string_view>(nameVariant)) { |
129 | AllocatedName& _name = std::get<AllocatedName>(nameVariant); | 130 | std::string_view& _name = std::get<std::string_view>(nameVariant); |
130 | U->internalAllocator.free(_name.name, _name.len); | 131 | U->internalAllocator.free(const_cast<char*>(_name.data()), _name.size()); |
131 | } | 132 | } |
132 | } | 133 | } |
133 | 134 | ||
134 | // ################################################################################################# | 135 | // ################################################################################################# |
135 | 136 | ||
136 | char const* Linda::getName() const | 137 | std::string_view Linda::getName() const |
137 | { | 138 | { |
138 | if (std::holds_alternative<AllocatedName>(nameVariant)) { | 139 | if (std::holds_alternative<std::string_view>(nameVariant)) { |
139 | AllocatedName const& _name = std::get<AllocatedName>(nameVariant); | 140 | std::string_view const& _name = std::get<std::string_view>(nameVariant); |
140 | return _name.name; | 141 | return _name; |
141 | } | 142 | } |
142 | if (std::holds_alternative<EmbeddedName>(nameVariant)) { | 143 | if (std::holds_alternative<EmbeddedName>(nameVariant)) { |
143 | char const* const _name{ std::get<EmbeddedName>(nameVariant).data() }; | 144 | char const* const _name{ std::get<EmbeddedName>(nameVariant).data() }; |
144 | return _name; | 145 | return std::string_view{ _name, strlen(_name) }; |
145 | } | 146 | } |
146 | return nullptr; | 147 | return std::string_view{}; |
147 | } | 148 | } |
148 | 149 | ||
149 | // ################################################################################################# | 150 | // ################################################################################################# |
@@ -182,22 +183,28 @@ int Linda::ProtectedCall(lua_State* L_, lua_CFunction f_) | |||
182 | 183 | ||
183 | // ################################################################################################# | 184 | // ################################################################################################# |
184 | 185 | ||
185 | void Linda::setName(char const* name_, size_t len_) | 186 | void Linda::setName(std::string_view const& name_) |
186 | { | 187 | { |
187 | // keep default | 188 | // keep default |
188 | if (!name_ || len_ == 0) { | 189 | if (name_.empty()) { |
189 | return; | 190 | return; |
190 | } | 191 | } |
191 | ++len_; // don't forget terminating 0 | 192 | size_t const _lenWithTZ{ name_.size() + 1 }; // don't forget terminating 0 |
192 | if (len_ < kEmbeddedNameLength) { | 193 | if (_lenWithTZ < kEmbeddedNameLength) { |
193 | nameVariant.emplace<EmbeddedName>(); | 194 | // grab our internal buffer |
194 | char* const _name{ std::get<EmbeddedName>(nameVariant).data() }; | 195 | EmbeddedName& _name = nameVariant.emplace<EmbeddedName>(); |
195 | memcpy(_name, name_, len_); | 196 | // copy the string in it |
197 | memcpy(_name.data(), name_.data(), name_.size()); | ||
198 | // don't forget terminating 0 | ||
199 | _name[_lenWithTZ] = 0; | ||
196 | } else { | 200 | } else { |
197 | AllocatedName& _name = std::get<AllocatedName>(nameVariant); | 201 | // allocate an external buffer |
198 | _name.name = static_cast<char*>(U->internalAllocator.alloc(len_)); | 202 | char* const _nameBuffer{ static_cast<char*>(U->internalAllocator.alloc(_lenWithTZ)) }; |
199 | _name.len = len_; | 203 | // copy the string in it |
200 | memcpy(_name.name, name_, len_); | 204 | memcpy(_nameBuffer, name_.data(), name_.size()); |
205 | // don't forget terminating 0 | ||
206 | _nameBuffer[_lenWithTZ] = 0; | ||
207 | nameVariant.emplace<std::string_view>(_nameBuffer, name_.size()); | ||
201 | } | 208 | } |
202 | } | 209 | } |
203 | 210 | ||