Move constructing is now allowed for the scoped buffer, copy assignment disallowed.

Copy assignment isn't allowed because T cannot be guaranteed to be copyable and I don't want this class to involve type traits
v1
Brett 2023-07-24 02:52:11 -04:00
parent 31855dd0a4
commit 85fdc4fa65
2 changed files with 17 additions and 9 deletions

View File

@ -11,6 +11,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <blt/math/math.h> #include <blt/math/math.h>
#include <blt/std/memory.h>
namespace blt::string { namespace blt::string {
@ -63,14 +64,13 @@ namespace blt::string {
} }
struct utf8_string { struct utf8_string {
char* characters; blt::scoped_buffer<char> characters;
unsigned int size;
}; };
// taken from java, adapted for c++. // taken from java, adapted for c++.
static inline utf8_string createUTFString(const std::string& str) { static inline utf8_string createUTFString(const std::string& str) {
const unsigned int strlen = (unsigned int) str.size(); const auto strlen = (unsigned int) str.size();
unsigned int utflen = strlen; unsigned int utflen = strlen;
for (unsigned int i = 0; i < strlen; i++) { for (unsigned int i = 0; i < strlen; i++) {
@ -82,9 +82,7 @@ namespace blt::string {
if (utflen > 65535 || /* overflow */ utflen < strlen) if (utflen > 65535 || /* overflow */ utflen < strlen)
throw "UTF Error"; throw "UTF Error";
utf8_string chars{}; utf8_string chars{scoped_buffer<char>{utflen + 2}};
chars.size = utflen + 2;
chars.characters = new char[chars.size];
int count = 0; int count = 0;
chars.characters[count++] = (char) ((utflen >> 0) & 0xFF); chars.characters[count++] = (char) ((utflen >> 0) & 0xFF);

View File

@ -73,13 +73,23 @@ namespace blt {
_buffer = new T[size]; _buffer = new T[size];
} }
scoped_buffer(scoped_buffer& copy) = delete; scoped_buffer(const scoped_buffer& copy) = delete;
scoped_buffer(scoped_buffer&& move) = delete; scoped_buffer(scoped_buffer&& move) noexcept {
_buffer = move._buffer;
_size = move.size();
move._buffer = nullptr;
}
scoped_buffer operator=(scoped_buffer& copyAssignment) = delete; scoped_buffer operator=(scoped_buffer& copyAssignment) = delete;
scoped_buffer operator=(scoped_buffer&& moveAssignment) = delete; scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept {
_buffer = moveAssignment._buffer;
_size = moveAssignment.size();
moveAssignment._buffer = nullptr;
return *this;
}
inline T& operator[](unsigned long index) { inline T& operator[](unsigned long index) {
return _buffer[index]; return _buffer[index];