#pragma once

#pragma once

#include <string>
#include <cstddef>

#ifdef _WIN32
#include <windows.h>
class mapped_file_win {
public:
    mapped_file_win();
    ~mapped_file_win();

    // 禁用拷贝
    mapped_file_win(const mapped_file_win&) = delete;
    mapped_file_win& operator=(const mapped_file_win&) = delete;

    // 允许移动
    mapped_file_win(mapped_file_win&& other) noexcept;
    mapped_file_win& operator=(mapped_file_win&& other) noexcept;

    bool map_file(const std::wstring& filename);
    void unmap();

    [[nodiscard]] const void* get_data() const { return data; }
    [[nodiscard]] void* get_data() { return data; }
    [[nodiscard]] size_t get_size() const { return size; }
    [[nodiscard]] bool is_mapped() const { return data != nullptr; }

private:
    void cleanup();

    void* data;
    size_t size;
    HANDLE file_handle;
    HANDLE mapping_handle;
};
using mapped_file = mapped_file_win;

#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

class mapped_file_unix {
public:
    mapped_file_unix();
    ~mapped_file_unix();

    // 禁用拷贝
    mapped_file_unix(const mapped_file_unix&) = delete;
    mapped_file_unix& operator=(const mapped_file_unix&) = delete;

    // 允许移动
    mapped_file_unix(mapped_file_unix&& other) noexcept;
    mapped_file_unix& operator=(mapped_file_unix&& other) noexcept;

    bool map_file(const std::wstring& filename);
    void unmap();

    [[nodiscard]] const void* get_data() const { return data; }
    [[nodiscard]] void* get_data() { return data; }
    [[nodiscard]] size_t get_size() const { return size; }
    [[nodiscard]] bool is_mapped() const { return data != nullptr; }

private:
    void cleanup();

    void* data;
    size_t size;
    int fd;
};
using mapped_file = mapped_file_unix;
#endif
