1
0

refactor shared

This commit is contained in:
2026-01-08 19:23:19 +08:00
parent 07cc58fb36
commit 64368b7837
23 changed files with 400 additions and 234 deletions

View File

@@ -3,8 +3,11 @@ target_sources(BasaltShared
PRIVATE
# Sources
basalt/pipe_operator.cpp
basalt/kernel.cpp
basalt/math.cpp
basalt/engine.cpp
basalt/deliver.cpp
basalt/object_loader.cpp
basalt/anime_loader.cpp
)
target_sources(BasaltShared
@@ -15,8 +18,12 @@ FILES
basalt/char_types.hpp
basalt/export_macro.hpp
basalt/pipe_operator.hpp
basalt/kernel.hpp
basalt/math.hpp
basalt/guid.hpp
basalt/engine.hpp
basalt/deliver.hpp
basalt/object_loader.hpp
basalt/anime_loader.hpp
)
target_include_directories(BasaltShared
PUBLIC

View File

@@ -0,0 +1,13 @@
#include "anime_loader.hpp"
namespace basalt::shared::anime_loader {
IAnimeLoader::IAnimeLoader() {
}
IAnimeLoader::~IAnimeLoader() {
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
namespace basalt::shared::anime_loader {
class IAnimeLoader {
public:
IAnimeLoader();
virtual ~IAnimeLoader();
protected:
};
} // namespace basalt::shared::anime_loader

View File

@@ -12,9 +12,9 @@
#define BSTEXT(x) x
#endif
namespace Basalt::Shared::Char {
namespace basalt::shared::char_types {
using BSString = std::basic_string<BSCHAR>;
using BSStringView = std::basic_string_view<BSCHAR>;
}; // namespace Basalt::Shared::Char
}; // namespace basalt::shared::char_types

View File

@@ -0,0 +1,29 @@
#include "deliver.hpp"
#include <stdexcept>
namespace basalt::shared::deliver {
IDeliver::IDeliver() {}
IDeliver::~IDeliver() {}
guid::Guid IDeliver::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IDeliver::Startup(DeliverConfig &&config) {
if (this->status != DeliverStatus::Ready) throw std::runtime_error("unexpected deliver status");
this->config = std::move(config);
this->status = DeliverStatus::Running;
}
void IDeliver::Transmit() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
}
void IDeliver::Shutdown() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
this->status = DeliverStatus::Stop;
}
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include "guid.hpp"
namespace basalt::shared::deliver {
struct DeliverConfig {
guid::Guid engine; ///< The GUID of render engine.
};
enum class DeliverStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IDeliver {
public:
IDeliver();
virtual ~IDeliver();
public:
virtual guid::Guid GetGuid() const;
virtual void Startup(DeliverConfig&& config);
virtual void Transmit();
virtual void Shutdown();
protected:
DeliverConfig config;
DeliverStatus status;
};
}

View File

@@ -0,0 +1,34 @@
#include "engine.hpp"
#include <stdexcept>
namespace basalt::shared::engine {
IEngine::IEngine() : config(), status(EngineStatus::Ready) {}
IEngine::~IEngine() {
if (this->status != EngineStatus::Stop) {
this->Shutdown();
}
}
guid::Guid IEngine::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IEngine::Startup(EngineConfig &&config) {
if (this->status != EngineStatus::Ready) throw std::runtime_error("unexpected engine status");
this->config = std::move(config);
this->status = EngineStatus::Running;
}
bool IEngine::Tick() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
return false;
}
void IEngine::Shutdown() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
this->status = EngineStatus::Stop;
}
}

View File

@@ -0,0 +1,41 @@
#pragma once
#include "guid.hpp"
#include <string>
#include <cinttypes>
namespace basalt::shared::engine {
struct EngineConfig {
bool headless; ///< Whether enable headless mode (No Window created).
std::uint32_t width; ///< Window width.
std::uint32_t height; ///< Window height.
guid::Guid deliver; ///< The GUID of deliver.
};
enum class EngineStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IEngine {
public:
IEngine();
virtual ~IEngine();
public:
virtual guid::Guid GetGuid() const;
virtual void Startup(EngineConfig&& config);
/**
* @brief
* @return True for active exit.
*/
virtual bool Tick();
virtual void Shutdown();
protected:
EngineConfig config;
EngineStatus status;
};
} // namespace basalt::shared::engine

View File

@@ -0,0 +1,50 @@
#pragma once
#include <cinttypes>
#include <compare>
namespace basalt::shared::guid {
struct Guid {
std::uint32_t d1, d2;
constexpr Guid(std::uint32_t gd1 = 0, std::uint32_t gd2 = 0) : d1(gd1), d2(gd2) {}
Guid(const Guid& rhs) : d1(rhs.d1), d2(rhs.d2) {}
Guid(Guid&& rhs) noexcept : d1(rhs.d1), d2(rhs.d2) {}
Guid& operator=(const Guid& rhs) {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
Guid& operator=(Guid&& rhs) noexcept {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
auto operator<=>(const Guid& rhs) const {
if (auto cmp = this->d1 <=> rhs.d1; cmp != 0) return cmp;
return this->d2 <=> rhs.d2;
}
bool operator==(const Guid& rhs) const { return ((this->d1 == rhs.d1) && (this->d2 == rhs.d2)); }
};
constexpr Guid DIRECTX8_ENGINE{0x0, 0x1};
constexpr Guid DIRECTX9_ENGINE{0x0, 0x2};
constexpr Guid DIRECTX11_ENGINE{0x0, 0x3};
constexpr Guid DIRECTX12_ENGINE{0x0, 0x4};
constexpr Guid OPENGL_ENGINE{0x0, 0x5};
constexpr Guid VULKAN_ENGINE{0x0, 0x6};
constexpr Guid CUDA_DELIVER{0x1, 0x1};
constexpr Guid ROCM_DELIVER{0x1, 0x2};
constexpr Guid PIPE_DELIVER{0x1, 0x3};
constexpr Guid TCP_DELIVER{0x1, 0x4};
constexpr Guid MMAP_DELIVER{0x1, 0x5};
constexpr Guid TINYOBJLOADER_OBJECT_LOADER{0x2, 0x1};
constexpr Guid CGLTF_OBJECT_LOADER{0x2, 0x2};
constexpr Guid ASSIMP_OBJECT_LOADER{0x2, 0x3};
constexpr Guid HOMEBREW_ANIME_LOADER{0x3, 0x1};
}

View File

@@ -1,65 +0,0 @@
#include "kernel.hpp"
#include <stdexcept>
namespace Basalt::Shared::Kernel {
#pragma region Engine
IEngine::IEngine() : config(), status(EngineStatus::Ready) {}
IEngine::~IEngine() {
if (this->status != EngineStatus::Stop) {
this->Shutdown();
}
}
Guid IEngine::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IEngine::Startup(EngineConfig &&config) {
if (this->status != EngineStatus::Ready) throw std::runtime_error("unexpected engine status");
this->config = std::move(config);
this->status = EngineStatus::Running;
}
bool IEngine::Tick() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
return false;
}
void IEngine::Shutdown() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
this->status = EngineStatus::Stop;
}
#pragma endregion
#pragma region Deliver
IDeliver::IDeliver() {}
IDeliver::~IDeliver() {}
Guid IDeliver::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IDeliver::Startup(DeliverConfig &&config) {
if (this->status != DeliverStatus::Ready) throw std::runtime_error("unexpected deliver status");
this->config = std::move(config);
this->status = DeliverStatus::Running;
}
void IDeliver::Transmit() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
}
void IDeliver::Shutdown() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
this->status = DeliverStatus::Stop;
}
#pragma endregion
} // namespace Basalt::Shared::Kernel

View File

@@ -1,123 +0,0 @@
#pragma once
#include "char_types.hpp"
#include <string>
#include <cinttypes>
namespace Basalt::Shared::Kernel {
#pragma region Guid & Predefined Guid
struct Guid {
std::uint32_t d1, d2;
constexpr Guid(std::uint32_t gd1 = 0, std::uint32_t gd2 = 0) : d1(gd1), d2(gd2) {}
Guid(const Guid& rhs) : d1(rhs.d1), d2(rhs.d2) {}
Guid(Guid&& rhs) noexcept : d1(rhs.d1), d2(rhs.d2) {}
Guid& operator=(const Guid& rhs) {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
Guid& operator=(Guid&& rhs) noexcept {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
auto operator<=>(const Guid& rhs) const {
if (auto cmp = this->d1 <=> rhs.d1; cmp != 0) return cmp;
return this->d2 <=> rhs.d2;
}
bool operator==(const Guid& rhs) const { return ((this->d1 == rhs.d1) && (this->d2 == rhs.d2)); }
};
constexpr Guid DIRECTX8_ENGINE{0x0, 0x1};
constexpr Guid DIRECTX9_ENGINE{0x0, 0x2};
constexpr Guid DIRECTX11_ENGINE{0x0, 0x3};
constexpr Guid DIRECTX12_ENGINE{0x0, 0x4};
constexpr Guid OPENGL_ENGINE{0x0, 0x5};
constexpr Guid VULKAN_ENGINE{0x0, 0x6};
constexpr Guid CUDA_DELIVER{0x1, 0x1};
constexpr Guid ROCM_DELIVER{0x1, 0x2};
constexpr Guid PIPE_DELIVER{0x1, 0x3};
constexpr Guid TCP_DELIVER{0x1, 0x4};
constexpr Guid MMAP_DELIVER{0x1, 0x5};
constexpr Guid TINYOBJLOADER_OBJECT_LOADER{0x2, 0x1};
constexpr Guid CGLTF_OBJECT_LOADER{0x2, 0x2};
constexpr Guid ASSIMP_OBJECT_LOADER{0x2, 0x3};
constexpr Guid HOMEBREW_ANIME_LOADER{0x3, 0x1};
#pragma endregion
#pragma region Engine
struct EngineConfig {
bool headless; ///< Whether enable headless mode (No Window created).
std::basic_string<BSCHAR> title; ///< Window title.
std::uint32_t width; ///< Window width.
std::uint32_t height; ///< Window height.
Guid deliver; ///< The GUID of deliver.
};
enum class EngineStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IEngine {
public:
IEngine();
virtual ~IEngine();
public:
virtual Guid GetGuid() const;
virtual void Startup(EngineConfig&& config);
/**
* @brief
* @return True for active exit.
*/
virtual bool Tick();
virtual void Shutdown();
protected:
EngineConfig config;
EngineStatus status;
};
#pragma endregion
#pragma region Deliver
struct DeliverConfig {
Guid engine; ///< The GUID of render engine.
};
enum class DeliverStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IDeliver {
public:
IDeliver();
virtual ~IDeliver();
public:
virtual Guid GetGuid() const;
virtual void Startup(DeliverConfig&& config);
virtual void Transmit();
virtual void Shutdown();
protected:
DeliverConfig config;
DeliverStatus status;
};
#pragma endregion
} // namespace Basalt::Shared::Kernel

View File

@@ -1,5 +1,82 @@
#include "math.hpp"
#include <stdexcept> // Include for std::out_of_range
namespace Basalt::Shared::Math {
}
#pragma region Vector3
FloatPoint& Vector3::operator[](size_t index) {
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw std::out_of_range("Vector3 index out of range");
}
}
const FloatPoint& Vector3::operator[](size_t index) const {
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw std::out_of_range("Vector3 index out of range");
}
}
#pragma endregion
#pragma region Vector4
FloatPoint& Vector4::operator[](size_t index) {
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
throw std::out_of_range("Vector4 index out of range");
}
}
const FloatPoint& Vector4::operator[](size_t index) const {
switch (index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
default:
throw std::out_of_range("Vector4 index out of range");
}
}
#pragma endregion
#pragma region Matrix4x4
Vector4& Matrix4x4::operator[](size_t index) {
return data.at(index);
}
const Vector4& Matrix4x4::operator[](size_t index) const {
return data.at(index);
}
#pragma endregion
} // namespace Basalt::Shared::Math

View File

@@ -1,5 +1,6 @@
#pragma once
#include <stdexcept>
#include <array> // Include array for std::array::at
namespace Basalt::Shared::Math {
@@ -7,19 +8,26 @@ namespace Basalt::Shared::Math {
struct Vector3 {
FloatPoint x, y, z;
};
struct Vector4 {
FloatPoint x, y, z, w;
};
struct Matrix4x4 {
Vector4 data[4];
FloatPoint& operator[](size_t index);
const FloatPoint& operator[](size_t index) const;
};
//template<typename TEle, size_t VCnt>
// requires(std::integral<TEle> || std::floating_point<TEle>)
//struct Vector {
// TEle factor[VCnt];
//};
struct Vector4 {
FloatPoint x, y, z, w;
FloatPoint& operator[](size_t index);
const FloatPoint& operator[](size_t index) const;
};
struct Matrix4x4 {
private:
std::array<Vector4, 4> data; // Use std::array instead of raw array for .at() method
public:
Vector4& operator[](size_t index);
const Vector4& operator[](size_t index) const;
};
#define NOT_IMPLEMENTED throw std::logic_error("not implemented function");
@@ -31,4 +39,4 @@ namespace Basalt::Shared::Math {
#undef NOT_IMPLEMENTED
} // namespace Basalt::Shared::Math
} // namespace Basalt::Shared::Math

View File

@@ -0,0 +1,13 @@
#include "object_loader.hpp"
namespace basalt::shared::object_loader {
IObjectLoader::IObjectLoader() {
}
IObjectLoader::~IObjectLoader() {
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
namespace basalt::shared::object_loader {
class IObjectLoader {
public:
IObjectLoader();
virtual ~IObjectLoader();
protected:
};
} // namespace basalt::shared::object_loader

View File

@@ -9,9 +9,9 @@
#include <sys/stat.h>
#endif
namespace Basalt::Shared {
namespace basalt::shared::pipe_operator {
PipeOperator::PipeOperator(const std::basic_string_view<BSCHAR> name) {
PipeOperator::PipeOperator(const char_types::BSStringView& name) {
#if defined(BASALT_OS_WINDOWS)
// Create Windows pipe name from given name
auto fullname = std::format(BSTEXT("\\\\.\\pipe\\{}"), name);

View File

@@ -1,13 +1,15 @@
#pragma once
#include "char_types.hpp"
#include <string>
#include <string_view>
#if defined(BASALT_OS_WINDOWS)
#include <Windows.h>
#else
// Nothing was included in POSIX
#endif
namespace Basalt::Shared {
namespace basalt::shared::pipe_operator {
class PipeOperator {
public:
@@ -20,7 +22,7 @@ namespace Basalt::Shared {
#endif
public:
PipeOperator(const std::basic_string_view<BSCHAR> name);
PipeOperator(const char_types::BSStringView& name);
~PipeOperator();
// Delete copy ctor
PipeOperator(const PipeOperator&) = delete;
@@ -29,10 +31,30 @@ namespace Basalt::Shared {
PipeOperator(PipeOperator&&) noexcept;
PipeOperator& operator=(PipeOperator&& other) noexcept;
public:
void Read(void* buffer, size_t size);
void Write(const void* buffer, size_t size);
template<typename TPod>
void ReadPod(TPod& buffer) {
Read(&buffer, sizeof(TPod));
}
template<typename TPod>
void WritePod(const TPod& buffer) {
Write(&buffer, sizeof(TPod));
}
void ReadString(std::string& buffer) {
size_t length = 0;
ReadPod(length);
buffer.resize(length);
Read(buffer.data(), length);
}
void WriteString(std::string_view& buffer) {
WritePod(buffer.size());
Write(buffer.data(), buffer.size());
}
private:
Handle m_Handle;
};
} // namespace Basalt::Shared
} // namespace basalt::shared::pipe_operator