mpd/src/util/StringPointer.hxx

50 lines
969 B
C++
Raw Normal View History

// 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
#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:
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:
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) {}
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
};