From d1b61b02b1a6bdaa337ff060eda3d3d5fb67827d Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Thu, 6 Feb 2025 16:34:23 +0100 Subject: refactor: move containers into separate directory --- lib/nes/common/containers/path-builder.hh | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/nes/common/containers/path-builder.hh (limited to 'lib/nes/common/containers/path-builder.hh') diff --git a/lib/nes/common/containers/path-builder.hh b/lib/nes/common/containers/path-builder.hh new file mode 100644 index 0000000..2991a83 --- /dev/null +++ b/lib/nes/common/containers/path-builder.hh @@ -0,0 +1,55 @@ +#pragma once + +#include "nes/common/containers/span.hh" +#include "nes/common/containers/path-view.hh" +#include "nes/common/types.hh" +#include "nes/common/status.hh" +#include + +namespace nes +{ + /// A mutable path with a fixed-length buffer. + class path_builder + { + template + friend class path_buffer; + + span buffer_; + u32 length_{ 0 }; + + public: + explicit path_builder(span); + + auto get_components() const -> path_view { return path_view{ get_path() }; } + auto get_path() const -> std::string_view { return std::string_view{ buffer_.get_data(), length_ }; } + + auto push(std::string_view) -> status; + auto pop() -> status; + }; + + /// A path builder with a buffer embedded into it. + template + class path_buffer : public path_builder + { + char buffer_[Capacity]; + + public: + explicit path_buffer() + : path_builder{ buffer_ } + { + } + + path_buffer(path_buffer const& other) + : path_builder{ buffer_ } + { + std::copy_n(other.buffer_, Capacity, buffer_); + length_ = other.length_; + } + + auto operator=(path_buffer const& other) + { + std::copy_n(other.buffer_, Capacity, buffer_); + length_ = other.length_; + } + }; +} // namespace nes \ No newline at end of file -- cgit v1.3.1