util/AllocatedString: rename to BasicAllocatedString

To make things simpler, AllocatedString is now a non-template class.
This commit is contained in:
Max Kellermann
2021-01-14 12:39:45 +01:00
committed by Max Kellermann
parent 8d80280ab9
commit cfb7f8ab84
20 changed files with 73 additions and 64 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2015-2021 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -41,8 +41,8 @@
*
* Unlike std::string, this object can hold a "nullptr" special value.
*/
template<typename T=char>
class AllocatedString {
template<typename T>
class BasicAllocatedString {
public:
using value_type = typename StringPointer<T>::value_type;
using reference = typename StringPointer<T>::reference;
@@ -57,41 +57,41 @@ public:
private:
pointer value;
explicit AllocatedString(pointer _value) noexcept
explicit BasicAllocatedString(pointer _value) noexcept
:value(_value) {}
public:
AllocatedString(std::nullptr_t n) noexcept
BasicAllocatedString(std::nullptr_t n) noexcept
:value(n) {}
AllocatedString(AllocatedString &&src) noexcept
BasicAllocatedString(BasicAllocatedString &&src) noexcept
:value(src.Steal()) {}
~AllocatedString() noexcept {
~BasicAllocatedString() noexcept {
delete[] value;
}
static AllocatedString Donate(pointer value) noexcept {
return AllocatedString(value);
static BasicAllocatedString Donate(pointer value) noexcept {
return BasicAllocatedString(value);
}
static AllocatedString Null() noexcept {
static BasicAllocatedString Null() noexcept {
return nullptr;
}
static AllocatedString Empty() {
static BasicAllocatedString Empty() {
auto p = new value_type[1];
p[0] = SENTINEL;
return Donate(p);
}
static AllocatedString Duplicate(string_view src) {
static BasicAllocatedString Duplicate(string_view src) {
auto p = new value_type[src.size() + 1];
*std::copy_n(src.data(), src.size(), p) = SENTINEL;
return Donate(p);
}
AllocatedString &operator=(AllocatedString &&src) noexcept {
BasicAllocatedString &operator=(BasicAllocatedString &&src) noexcept {
std::swap(value, src.value);
return *this;
}
@@ -136,9 +136,17 @@ public:
return std::exchange(value, nullptr);
}
AllocatedString Clone() const {
BasicAllocatedString Clone() const {
return Duplicate(*this);
}
};
class AllocatedString : public BasicAllocatedString<char> {
public:
using BasicAllocatedString::BasicAllocatedString;
AllocatedString(BasicAllocatedString<value_type> &&src) noexcept
:BasicAllocatedString(std::move(src)) {}
};
#endif

View File

@@ -23,7 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
AllocatedString<>
AllocatedString
FormatStringV(const char *fmt, std::va_list args) noexcept
{
std::va_list tmp;
@@ -37,10 +37,10 @@ FormatStringV(const char *fmt, std::va_list args) noexcept
char *buffer = new char[length + 1];
vsnprintf(buffer, length + 1, fmt, args);
return AllocatedString<>::Donate(buffer);
return AllocatedString::Donate(buffer);
}
AllocatedString<>
AllocatedString
FormatString(const char *fmt, ...) noexcept
{
std::va_list args;

View File

@@ -24,20 +24,20 @@
#include <cstdarg>
template<typename T> class AllocatedString;
class AllocatedString;
/**
* Format into an #AllocatedString.
*/
gcc_nonnull_all
AllocatedString<char>
AllocatedString
FormatStringV(const char *fmt, std::va_list args) noexcept;
/**
* Format into an #AllocatedString.
*/
gcc_nonnull(1) gcc_printf(1,2)
AllocatedString<char>
AllocatedString
FormatString(const char *fmt, ...) noexcept;
#endif