2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2015-06-23 12:33:45 +02:00
|
|
|
|
2024-02-26 13:01:12 +01:00
|
|
|
#pragma once
|
2015-06-23 12:33:45 +02:00
|
|
|
|
2021-01-21 21:08:50 +01:00
|
|
|
#include <cstddef>
|
|
|
|
|
2015-06-23 12:33:45 +02:00
|
|
|
/**
|
|
|
|
* Simple OO wrapper for a const string pointer.
|
|
|
|
*/
|
|
|
|
template<typename T=char>
|
|
|
|
class StringPointer {
|
|
|
|
public:
|
2020-04-03 15:24:43 +02:00
|
|
|
using value_type = T;
|
|
|
|
using reference = T &;
|
|
|
|
using const_reference = const T &;
|
|
|
|
using pointer = T *;
|
|
|
|
using const_pointer = const T *;
|
2015-06-23 12:33:45 +02:00
|
|
|
|
2015-11-11 15:13:46 +01:00
|
|
|
static constexpr value_type SENTINEL = '\0';
|
|
|
|
|
2015-06-23 12:33:45 +02:00
|
|
|
private:
|
2020-01-03 15:49:29 +01:00
|
|
|
const_pointer value;
|
2015-06-23 12:33:45 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
StringPointer() = default;
|
2020-04-03 15:25:23 +02:00
|
|
|
constexpr StringPointer(const_pointer _value) noexcept
|
2015-06-23 12:33:45 +02:00
|
|
|
:value(_value) {}
|
|
|
|
|
2021-01-21 21:08:50 +01:00
|
|
|
constexpr bool operator==(std::nullptr_t) const noexcept {
|
|
|
|
return value == nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-23 12:33:45 +02:00
|
|
|
/**
|
|
|
|
* Check if this is a "nulled" instance. A "nulled" instance
|
|
|
|
* must not be used.
|
|
|
|
*/
|
2020-04-03 15:25:23 +02:00
|
|
|
constexpr bool IsNull() const noexcept {
|
2015-06-23 12:33:45 +02:00
|
|
|
return value == nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:25:23 +02:00
|
|
|
constexpr const_pointer c_str() const noexcept {
|
2015-06-23 12:33:45 +02:00
|
|
|
return value;
|
|
|
|
}
|
2015-11-11 15:13:46 +01:00
|
|
|
|
2020-04-03 15:25:23 +02:00
|
|
|
bool empty() const noexcept {
|
2015-11-11 15:13:46 +01:00
|
|
|
return *value == SENTINEL;
|
|
|
|
}
|
2015-06-23 12:33:45 +02:00
|
|
|
};
|