Add TinyString: SLE_STR compatible c-string wrapper
This commit is contained in:
@@ -784,6 +784,7 @@
|
|||||||
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp" />
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
||||||
<ClCompile Include="..\src\airport_gui.cpp" />
|
<ClCompile Include="..\src\airport_gui.cpp" />
|
||||||
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
||||||
|
@@ -1449,6 +1449,9 @@
|
|||||||
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
||||||
<Filter>Core Source Code</Filter>
|
<Filter>Core Source Code</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp">
|
||||||
|
<Filter>Core Source Code</Filter>
|
||||||
|
</ClInclude>
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp">
|
<ClCompile Include="..\src\aircraft_gui.cpp">
|
||||||
<Filter>GUI Source Code</Filter>
|
<Filter>GUI Source Code</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
@@ -784,6 +784,7 @@
|
|||||||
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp" />
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
||||||
<ClCompile Include="..\src\airport_gui.cpp" />
|
<ClCompile Include="..\src\airport_gui.cpp" />
|
||||||
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
||||||
|
@@ -1449,6 +1449,9 @@
|
|||||||
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
||||||
<Filter>Core Source Code</Filter>
|
<Filter>Core Source Code</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp">
|
||||||
|
<Filter>Core Source Code</Filter>
|
||||||
|
</ClInclude>
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp">
|
<ClCompile Include="..\src\aircraft_gui.cpp">
|
||||||
<Filter>GUI Source Code</Filter>
|
<Filter>GUI Source Code</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
@@ -784,6 +784,7 @@
|
|||||||
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
<ClInclude Include="..\src\core\smallstack_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
<ClInclude Include="..\src\core\smallvec_type.hpp" />
|
||||||
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
<ClInclude Include="..\src\core\string_compare_type.hpp" />
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp" />
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
<ClCompile Include="..\src\aircraft_gui.cpp" />
|
||||||
<ClCompile Include="..\src\airport_gui.cpp" />
|
<ClCompile Include="..\src\airport_gui.cpp" />
|
||||||
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
<ClCompile Include="..\src\autoreplace_gui.cpp" />
|
||||||
|
@@ -1449,6 +1449,9 @@
|
|||||||
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
<ClInclude Include="..\src\core\string_compare_type.hpp">
|
||||||
<Filter>Core Source Code</Filter>
|
<Filter>Core Source Code</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\src\core\tinystring_type.hpp">
|
||||||
|
<Filter>Core Source Code</Filter>
|
||||||
|
</ClInclude>
|
||||||
<ClCompile Include="..\src\aircraft_gui.cpp">
|
<ClCompile Include="..\src\aircraft_gui.cpp">
|
||||||
<Filter>GUI Source Code</Filter>
|
<Filter>GUI Source Code</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
@@ -489,6 +489,7 @@ core/smallmatrix_type.hpp
|
|||||||
core/smallstack_type.hpp
|
core/smallstack_type.hpp
|
||||||
core/smallvec_type.hpp
|
core/smallvec_type.hpp
|
||||||
core/string_compare_type.hpp
|
core/string_compare_type.hpp
|
||||||
|
core/tinystring_type.hpp
|
||||||
|
|
||||||
# GUI Source Code
|
# GUI Source Code
|
||||||
aircraft_gui.cpp
|
aircraft_gui.cpp
|
||||||
|
111
src/core/tinystring_type.hpp
Normal file
111
src/core/tinystring_type.hpp
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of OpenTTD.
|
||||||
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||||
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @file tinystring_type.hpp Functions related to the allocation of memory */
|
||||||
|
|
||||||
|
#ifndef TINYSTRING_TYPE_HPP
|
||||||
|
#define TINYSTRING_TYPE_HPP
|
||||||
|
|
||||||
|
#include "../string_func.h"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An SLE_STR compatible tiny c-string wrapper class.
|
||||||
|
*
|
||||||
|
* This is intended for the cases where the string is almost always empty,
|
||||||
|
* and the space overhead of std::string is undesirable.
|
||||||
|
*/
|
||||||
|
class TinyString {
|
||||||
|
char *storage = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline TinyString()
|
||||||
|
{
|
||||||
|
static_assert(offsetof(TinyString, storage) == 0, "offsetof(TinyString, storage) must be 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString(const TinyString &other)
|
||||||
|
{
|
||||||
|
if (other.storage != nullptr) this->storage = stredup(other.storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString(TinyString &&other) noexcept
|
||||||
|
{
|
||||||
|
this->storage = other.storage;
|
||||||
|
other.storage = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString(const std::string &other) noexcept
|
||||||
|
{
|
||||||
|
if (!other.empty()) this->storage = stredup(other.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString(const char *str) noexcept
|
||||||
|
{
|
||||||
|
if (str != nullptr && *str != 0) this->storage = stredup(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void clear()
|
||||||
|
{
|
||||||
|
if (this->storage != nullptr) {
|
||||||
|
free(this->storage);
|
||||||
|
this->storage = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ~TinyString()
|
||||||
|
{
|
||||||
|
if (this->storage != nullptr) free(this->storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString &operator=(const TinyString &other)
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
if (other.storage != nullptr) this->storage = stredup(other.storage);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString &operator=(TinyString &&other) noexcept
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
if (other.storage != nullptr) {
|
||||||
|
this->storage = other.storage;
|
||||||
|
other.storage = nullptr;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString &operator=(const std::string &other)
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
if (!other.empty()) this->storage = stredup(other.c_str());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TinyString &operator=(const char *str)
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
if (str != nullptr && *str != 0) this->storage = stredup(str);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator==(const char *str) const
|
||||||
|
{
|
||||||
|
if (str == nullptr) return this->empty();
|
||||||
|
if (this->storage == nullptr) return *str == 0;
|
||||||
|
return strcmp(this->storage, str) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool empty() const { return this->storage == nullptr || *this->storage == 0; }
|
||||||
|
inline const char *c_str() const { return this->storage; }
|
||||||
|
inline const char *data() const { return this->storage; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* TINYSTRING_TYPE_HPP */
|
Reference in New Issue
Block a user