Skip to content

Commit

Permalink
cpp20
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Jan 23, 2021
1 parent cd76445 commit 46b2132
Show file tree
Hide file tree
Showing 80 changed files with 1,077 additions and 1,062 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.14.4)
project(UECS VERSION 0.15.0)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Ubpa_InitUCMake()
Ubpa_InitUCMake(VERSION 0.6.4)

Ubpa_InitProject()

Ubpa_AddDep(UContainer 0.0.9)
Ubpa_AddDep(UTemplate 0.4.9)
Ubpa_AddDep(UGraphviz 0.1.6)
Ubpa_AddDep(UTemplate 0.6.0)
Ubpa_AddDep(UGraphviz 0.2.0)

Ubpa_AddSubDirsRec(include)
Ubpa_AddSubDirsRec(src)
Expand Down
18 changes: 17 additions & 1 deletion cmake/InitUCMake.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
macro(Ubpa_InitUCMake)
cmake_parse_arguments(
"ARG" # prefix
"" #<options> # TRUE / FALSE
"VERSION" # <one_value_keywords>
"" #<multi_value_keywords> # list
${ARGN}
)
# 结果为 ARG_*
# - ARG_<option>
# - ARG_<one_value_keyword>
# - ARG_<multi_value_keyword>

if("${ARG_VERSION}" STREQUAL "")
message(FATAL_ERROR "not provide argument <VERSION>")
endif()

include(FetchContent)
set(UCMake_VERSION 0.6.3)
set(UCMake_VERSION ${ARG_VERSION})
message(STATUS "find package: UCMake ${UCMake_VERSION}")
find_package(UCMake ${UCMake_VERSION} EXACT QUIET)
if(NOT UCMake_FOUND)
Expand Down
16 changes: 14 additions & 2 deletions config/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
message(STATUS "config @PROJECT_NAME@ @PROJECT_VERSION@...")
message(STATUS "config @PROJECT_NAME@ @PROJECT_VERSION@ ...")

@PACKAGE_INIT@

@UBPA_PACKAGE_INIT@

include ( "${CMAKE_CURRENT_LIST_DIR}/@[email protected]" )
if(@Ubpa_USRefl_Build_AutoRefl@)
include("${CMAKE_CURRENT_LIST_DIR}/UbpaUSReflAutoRefl.cmake")
endif()

if(NOT @Ubpa_USRefl_UseNameof@)
CPMAddPackage(
NAME nameof
GITHUB_REPOSITORY Neargye/nameof
GIT_TAG 0e8bec0
)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

message(STATUS "config @PROJECT_NAME@ @PROJECT_VERSION@ done")
34 changes: 34 additions & 0 deletions include/UECS/AccessTypeID.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "CmptTag.h"

#include "detail/Util.h"

#include <UTemplate/TypeID.h>

#include <set>

namespace Ubpa::UECS {
// TypeID with AccessMode
class AccessTypeID : public TypeID {
public:
using TypeID::TypeID; // AccessMode::WRITE

constexpr AccessTypeID(std::size_t ID, AccessMode mode) noexcept : TypeID{ ID }, mode{ mode } {}
constexpr AccessTypeID(std::string_view type_name, AccessMode mode) noexcept : TypeID{ type_name }, mode{ mode } {}
constexpr AccessTypeID(TypeID ID, AccessMode mode = AccessMode::WRITE) noexcept : TypeID{ ID }, mode{ mode } {}
template<std::size_t N>
constexpr AccessTypeID(const char(&str)[N], AccessMode mode) noexcept : TypeID{ str }, mode{ mode } {}

constexpr AccessMode GetAccessMode() const noexcept { return mode; }
private:
AccessMode mode{ AccessMode::WRITE };
};

template<typename Cmpt>
static constexpr AccessTypeID AccessTypeID_of = { TypeID_of<RemoveTag_t<Cmpt>>, AccessMode_of<Cmpt> };

using AccessTypeIDSet = std::set<AccessTypeID, std::less<>>;
}

#include "detail/AccessTypeID.inl"
12 changes: 6 additions & 6 deletions include/UECS/ArchetypeFilter.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#pragma once

#include "CmptType.h"
#include "AccessTypeID.h"

#include <set>

namespace Ubpa::UECS {
// filter Archetype with all, any and none
struct ArchetypeFilter {
CmptAccessTypeSet all;
CmptAccessTypeSet any;
std::set<CmptType> none;
AccessTypeIDSet all;
AccessTypeIDSet any;
std::set<TypeID> none;

size_t HashCode() const noexcept;
std::size_t GetValue() const noexcept;

bool HaveWriteCmptType() const noexcept;
bool HaveWriteTypeID() const noexcept;

bool operator==(const ArchetypeFilter& rhs) const noexcept;
};
Expand Down
21 changes: 11 additions & 10 deletions include/UECS/ChunkView.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#pragma once

#include "CmptType.h"
#include "AccessTypeID.h"
#include "Entity.h"

#include <UContainer/Span.h>
#include <span>

namespace Ubpa::UECS {
class Archetype;

class ChunkView {
public:
ChunkView(Archetype* archetype, size_t chunkIdx) noexcept;
ChunkView(Archetype* archetype, std::size_t chunkIdx) noexcept;
ChunkView() noexcept = default;

bool Contains(CmptType) const;
size_t EntityNum() const noexcept { return entityNum; }
bool Contains(TypeID) const;
std::size_t EntityNum() const noexcept { return entityNum; }

// nullptr if not contain
void* GetCmptArray(CmptType) const;
void* GetCmptArray(TypeID) const;
template<typename Cmpt>
Span<Cmpt> GetCmptArray() const;
Span<const Entity> GetEntityArray() const;
std::span<Cmpt> GetCmptArray() const;
std::span<const Entity> GetEntityArray() const;

private:
Archetype* archetype{ nullptr };
size_t chunkIdx{ static_cast<size_t>(-1) };
size_t entityNum{ 0 };
std::size_t chunkIdx{ static_cast<std::size_t>(-1) };
std::size_t entityNum{ 0 };
};
}

Expand Down
21 changes: 10 additions & 11 deletions include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#pragma once

#include "CmptTag.h"
#include "AccessTypeID.h"

#include "CmptType.h"

#include <UContainer/Span.h>
#include <span>

namespace Ubpa::UECS {
// locate components in function's argument list for Archetype
// immutable
class CmptLocator {
public:
CmptLocator(Span<const CmptAccessType> types);
CmptLocator(CmptAccessTypeSet types);
CmptLocator(std::span<const AccessTypeID> types);
CmptLocator(AccessTypeIDSet types);

CmptLocator();

Expand All @@ -22,19 +21,19 @@ namespace Ubpa::UECS {
template<typename Func>
CmptLocator& Combine();

size_t HashCode() const noexcept { return hashCode; }
std::size_t GetValue() const noexcept { return hashCode; }

const CmptAccessTypeSet& CmptAccessTypes() const noexcept { return cmptTypes; }
const AccessTypeIDSet& AccessTypeIDs() const noexcept { return cmptTypes; }

bool operator==(const CmptLocator& rhs) const noexcept;

bool HasWriteCmptType() const noexcept;
bool HasWriteTypeID() const noexcept;
private:
void UpdateHashCode() noexcept;
void UpdateGetValue() noexcept;

CmptAccessTypeSet cmptTypes;
AccessTypeIDSet cmptTypes;

size_t hashCode;
std::size_t hashCode;
};
}

Expand Down
30 changes: 15 additions & 15 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
#pragma once

#include "CmptType.h"
#include "AccessTypeID.h"

#include <cassert>

namespace Ubpa::UECS {
// CmptType + void*
// TypeID + void*
class CmptPtr {
public:
constexpr CmptPtr(CmptType type, void* p) noexcept : type{ type }, p{ p } {}
constexpr CmptPtr(TypeID type, void* p) noexcept : type{ type }, p{ p } {}
template<typename Cmpt>
constexpr CmptPtr(Cmpt* p) noexcept : type{ CmptType::Of<Cmpt> }, p{ p } {}
constexpr CmptPtr(Cmpt* p) noexcept : type{ TypeID_of<Cmpt> }, p{ p } {}
constexpr CmptPtr() noexcept : CmptPtr{ Invalid() } {}

constexpr void* Ptr() const noexcept { return p; }

constexpr CmptType Type() const noexcept { return type; }
constexpr TypeID Type() const noexcept { return type; }

static constexpr CmptPtr Invalid() noexcept { return { CmptType::Invalid(), nullptr }; };
static constexpr CmptPtr Invalid() noexcept { return { TypeID{}, nullptr }; };
constexpr bool Valid() const noexcept { return p != nullptr && type.Valid(); }

template<typename Cmpt>
constexpr Cmpt* As() const noexcept { return reinterpret_cast<Cmpt*>(p); }
private:
CmptType type;
TypeID type;
void* p;
};

// CmptAccessType + void*
// AccessTypeID + void*
class CmptAccessPtr {
public:
constexpr CmptAccessPtr(CmptType type, void* p, AccessMode mode) noexcept : accessType{ type, mode }, p{ p } {}
constexpr CmptAccessPtr(CmptAccessType accessType, void* p) noexcept : accessType{ accessType }, p{ p } {}
constexpr CmptAccessPtr(TypeID type, void* p, AccessMode mode) noexcept : accessType{ type, mode }, p{ p } {}
constexpr CmptAccessPtr(AccessTypeID accessType, void* p) noexcept : accessType{ accessType }, p{ p } {}
constexpr CmptAccessPtr(CmptPtr p, AccessMode mode) noexcept : accessType{ p.Type(), mode }, p{ p.Ptr() } {}
template<typename TaggedCmpt>
constexpr CmptAccessPtr(TaggedCmpt p) noexcept : accessType{ CmptAccessType::Of<TaggedCmpt> }, p{ CastToVoidPointer(p) } {}
constexpr CmptAccessPtr(TaggedCmpt p) noexcept : accessType{ AccessTypeID_of<TaggedCmpt> }, p{ CastToVoidPointer(p) } {}
explicit constexpr CmptAccessPtr(CmptPtr p) noexcept : CmptAccessPtr{ p, AccessMode::LATEST } {}
explicit constexpr CmptAccessPtr() noexcept : CmptAccessPtr{ Invalid() } {}

explicit constexpr operator CmptPtr() const noexcept { return { CmptType{accessType}, p }; }
explicit constexpr operator CmptPtr() const noexcept { return { TypeID{accessType}, p }; }

constexpr void* Ptr() const noexcept { return p; }

constexpr CmptAccessType AccessType() const noexcept { return accessType; }
constexpr AccessTypeID AccessType() const noexcept { return accessType; }

static constexpr CmptAccessPtr Invalid() noexcept { return { CmptAccessType::Invalid(), nullptr }; };
static constexpr CmptAccessPtr Invalid() noexcept { return { AccessTypeID{}, nullptr }; };
constexpr bool Valid() const noexcept { return p != nullptr && accessType.Valid(); }

// check: type's access mode must be equal to <mode>
Expand All @@ -62,7 +62,7 @@ namespace Ubpa::UECS {
}
private:
friend class EntityMngr;
CmptAccessType accessType;
AccessTypeID accessType;
void* p;
};
}
4 changes: 3 additions & 1 deletion include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cstddef>

namespace Ubpa::UECS {
// read/write tag : LastFrame -> Write -> Latest
// singleton tag : Singleton
Expand All @@ -11,7 +13,7 @@ namespace Ubpa::UECS {
// 5. Write<Singleton<Cmpt>> == Singleton<Cmpt>
// 6. Latest<Singleton<Cmpt>>

enum class AccessMode : size_t {
enum class AccessMode : std::size_t {
LAST_FRAME = 0b000, // LastFrame<Cmpt>
WRITE = 0b001, // Write<Cmpt> / Cmpt*
LATEST = 0b010, // Latest<Cmpt> / const Cmpt*
Expand Down
Loading

0 comments on commit 46b2132

Please sign in to comment.