// SPDX-License-Identifier: 0BSD


#pragma once

#include <dirent.h>


namespace vore::file {
	namespace {
		class DIR {
		public:
			constexpr DIR() noexcept = default;

			DIR(const char * path) noexcept {
				this->stream = opendir(path);
				this->opened = this->stream;
			}

			DIR(const DIR &) = delete;
			constexpr DIR(DIR && oth) noexcept : stream(oth.stream), opened(oth.opened) { oth.opened = false; }

			DIR & operator=(DIR && oth) noexcept {
				this->swap(oth);
				return *this;
			}

			~DIR() {
				if(this->opened)
					closedir(this->stream);
			}

			constexpr operator ::DIR *() const noexcept { return this->stream; }


			void swap(DIR & oth) noexcept {
				std::swap(this->stream, oth.stream);
				std::swap(this->opened, oth.opened);
			}

		private:
			::DIR * stream = nullptr;
			bool opened    = false;
		};
	}
}
