Skip to content
Open

Ztd #131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Checks: >
modernize-*,
performance-*,
readability-*,

-bugprone-easily-swappable-parameters,
-bugprone-exception-escape,
-bugprone-unchecked-optional-access,
Expand All @@ -18,6 +19,7 @@ Checks: >
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-pro-type-static-cast-downcast,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-init-variables,
-modernize-use-trailing-return-type,
-modernize-use-integer-sign-comparison,
-readability-magic-numbers,
Expand All @@ -28,10 +30,10 @@ Checks: >
-readability-else-after-return,
-readability-avoid-nested-conditional-operator,
-readability-math-missing-parentheses,
-readability-redundant-declaration,

-bugprone-macro-parentheses,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-init-variables,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-owning-memory,
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
include_directories(.)

set(ZTD_PRECOMPILE_HEADERS ON)
set(ZTD_CLANG_TIDY_ENABLED ${CLANG_TIDY_ENABLED})
add_subdirectory(ztd)

if (CLANG_TIDY_ENABLED)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-use-color;-extra-arg-before=-Wno-unknown-warning-option")
endif ()
Expand Down
19 changes: 4 additions & 15 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
add_library(hydra-common
platform.hpp
macros.hpp
type_aliases.hpp
types.hpp
range.hpp
traits.hpp
functions.hpp
atomic.hpp
literals.hpp
string.hpp
time.hpp
hash.hpp
linked_list.hpp
pool.hpp
static_pool.hpp
dynamic_pool.hpp
small_cache.hpp
filesystem.hpp
log.cpp
log.hpp
lz4.cpp
lz4.hpp
toml_helper.hpp
fmt_helper.hpp
config.cpp
config.hpp
common.hpp
io/stream.hpp
io/continuous_stream.hpp
io/memory_stream.hpp
Expand All @@ -33,11 +22,11 @@ add_library(hydra-common
io/sparse_stream.hpp
)

target_link_libraries(hydra-common PRIVATE hydra_compile_options)
target_link_libraries(hydra-common PUBLIC fmt::fmt toml11::toml11)

target_precompile_headers(hydra-common PUBLIC common.hpp)

target_link_libraries(hydra-common PRIVATE hydra_compile_options)
target_link_libraries(hydra-common PUBLIC fmt::fmt toml11::toml11 ztd::ztd)

if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
set_target_properties(hydra-common PROPERTIES
XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${IOS_DEPLOYMENT_TARGET}
Expand Down
9 changes: 1 addition & 8 deletions src/common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@

#include "common/atomic.hpp"
#include "common/config.hpp"
#include "common/dynamic_pool.hpp"
#include "common/filesystem.hpp"
#include "common/fmt_helper.hpp"
#include "common/functions.hpp"
#include "common/hash.hpp"
#include "common/io/iostream_stream.hpp"
#include "common/io/memory_stream.hpp"
#include "common/io/sparse_stream.hpp"
#include "common/io/stream_view.hpp"
#include "common/linked_list.hpp"
#include "common/literals.hpp"
#include "common/log.hpp"
#include "common/objc.hpp"
#include "common/platform.hpp"
#include "common/range.hpp"
#include "common/small_cache.hpp"
#include "common/static_pool.hpp"
#include "common/string.hpp"
#include "common/time.hpp"
#include "common/toml_helper.hpp"
#include "common/traits.hpp"
8 changes: 4 additions & 4 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct into<CustomResolution> {
namespace hydra {

Config::Config() {
#ifdef PLATFORM_APPLE
#ifdef ZTD_PLATFORM_APPLE
if (const char* home = std::getenv("HOME")) {
app_data_path =
fmt::format("{}/Library/Application Support/" APP_NAME, home);
Expand All @@ -72,7 +72,7 @@ Config::Config() {
} else {
LOG_FATAL(Other, "Failed to find HOME path");
}
#elifdef PLATFORM_WINDOWS
#elifdef ZTD_PLATFORM_WINDOWS
if (const char* app_data = std::getenv("APPDATA")) {
app_data_path = fmt::format("{}/" APP_NAME, app_data);
logs_path = fmt::format("{}/logs", app_data_path); // TODO
Expand All @@ -85,7 +85,7 @@ Config::Config() {
} else {
LOG_FATAL(Other, "Failed to find USERPROFILE path");
}
#elif defined(PLATFORM_LINUX)
#elifdef ZTD_PLATFORM_LINUX
if (const char* xdg_config = std::getenv("XDG_CONFIG_HOME")) {
app_data_path = fmt::format("{}/" APP_NAME, xdg_config);
logs_path = fmt::format("{}/logs", app_data_path);
Expand All @@ -111,7 +111,7 @@ Config::Config() {
std::filesystem::create_directories(app_data_path);
std::filesystem::create_directories(logs_path);
// HACK
#ifndef PLATFORM_IOS
#ifndef ZTD_PLATFORM_IOS
std::filesystem::create_directories(pictures_path);
#endif

Expand Down
8 changes: 5 additions & 3 deletions src/common/config.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include <cstdlib>

#include <fmt/ranges.h>

#include "common/fmt_helper.hpp"
#include "common/log.hpp"
#include "common/platform.hpp"
#include "common/types.hpp"

#define CONFIG_INSTANCE Config::GetInstance()
Expand Down Expand Up @@ -104,7 +106,7 @@ class Config {
static std::vector<LoaderPlugin> GetDefaultLoaderPlugins() { return {}; }
static std::vector<std::string> GetDefaultPatchPaths() { return {}; }
static InputBackend GetDefaultInputBackend() {
#ifdef PLATFORM_APPLE
#ifdef ZTD_PLATFORM_APPLE
return InputBackend::AppleGameController;
#else
return InputBackend::Sdl;
Expand All @@ -121,7 +123,7 @@ class Config {
#endif
}
static GpuRenderer GetDefaultGpuRenderer() {
#ifdef PLATFORM_APPLE
#ifdef ZTD_PLATFORM_APPLE
return GpuRenderer::Metal;
#else
return GpuRenderer::Null;
Expand Down
46 changes: 0 additions & 46 deletions src/common/dynamic_pool.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/common/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include <CoreFoundation/CoreFoundation.h>

#include "common/fmt_helper.hpp"
#include "common/log.hpp"
#include "common/platform.hpp"

namespace hydra {

#ifdef PLATFORM_APPLE
#ifdef ZTD_PLATFORM_APPLE
inline std::string GetBundleResourcePath(const std::string& filename) {
CFBundleRef main_bundle = CFBundleGetMainBundle();
if (main_bundle == nullptr) {
Expand Down
108 changes: 108 additions & 0 deletions src/common/fmt_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once

#include "ztd/ztd.hpp"

#include <cstdlib>
#include <fmt/core.h>

#define ENUM_FORMAT_CASE(type, c, name) \
case type::c: \
res = name; \
break;

#define ENABLE_ENUM_FORMATTING(type, ...) \
template <> \
struct fmt::formatter<type> : formatter<string_view> { \
template <typename FormatContext> \
auto format(type value, FormatContext& ctx) const { \
std::string_view res; \
switch (value) { \
ZTD_FOR_EACH_1_2(ENUM_FORMAT_CASE, type, __VA_ARGS__) \
default: \
return formatter<string_view>::format( \
fmt::format("unknown ({})", \
static_cast<hydra::u64>(value)), \
ctx); \
break; \
} \
return formatter<string_view>::format(res, ctx); \
} \
};

#define STRUCT_FORMAT_CASE(member, f, name) \
fmt::format(name ": {" f "}", value.member),

#define ENABLE_STRUCT_FORMATTING(type, ...) \
template <> \
struct fmt::formatter<type> : formatter<string_view> { \
template <typename FormatContext> \
auto format(const type& value, FormatContext& ctx) const { \
/* TODO: make this more efficient */ \
std::string res = fmt::format( \
"{}", fmt::join(std::array{ZTD_FOR_EACH_0_3( \
STRUCT_FORMAT_CASE, __VA_ARGS__)}, \
", ")); \
return formatter<string_view>::format(std::move(res), ctx); \
} \
};

#define ENUM_CAST_CASE(type, value, n) \
if (value_str == n) \
return type::value;

#define ENABLE_ENUM_CASTING(namespc, type, ...) \
namespace namespc { \
inline std::optional<type> To##type(std::string_view value_str) { \
ZTD_FOR_EACH_1_2(ENUM_CAST_CASE, type, __VA_ARGS__) \
return std::nullopt; \
} \
}

#define ENABLE_ENUM_FORMATTING_AND_CASTING(namespc, type, ...) \
ENABLE_ENUM_FORMATTING(namespc::type, __VA_ARGS__) \
ENABLE_ENUM_CASTING(namespc, type, __VA_ARGS__)

#define ENUM_BIT_TEST(type, c, n) \
if (any(value & type::c)) { \
if (added) \
name += " | "; \
else \
added = true; \
name += n; \
}

#define ENABLE_ENUM_FLAGS_FORMATTING(type, ...) \
template <> \
struct fmt::formatter<type> : formatter<string_view> { \
template <typename FormatContext> \
auto format(type value, FormatContext& ctx) const { \
std::string name; \
bool added = false; \
ZTD_FOR_EACH_1_2(ENUM_BIT_TEST, type, __VA_ARGS__) \
if (!added) \
name = "none"; \
return formatter<string_view>::format(name, ctx); \
} \
};

template <typename T>
struct fmt::formatter<ztd::Range<T>> : formatter<string_view> {
fmt::formatter<T> value_formatter;

constexpr auto parse(fmt::format_parse_context& ctx) {
return value_formatter.parse(ctx);
}

template <typename FormatContext>
auto format(const ztd::Range<T>& range, FormatContext& ctx) const {
auto out = ctx.out();

*out++ = '<';
out = value_formatter.format(range.getBegin(), ctx);
out = fmt::format_to(out, ", ");
out = value_formatter.format(range.getEnd(), ctx);
*out++ = ')';

return out;
}
};
4 changes: 2 additions & 2 deletions src/common/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ T ceil_divide(T dividend, T divisor) {
return (dividend + divisor - 1) / divisor;
}

inline constexpr u32 make_magic4(const char c0, const char c1, const char c2,
const char c3) {
constexpr u32 make_magic4(const char c0, const char c1, const char c2,
const char c3) {
return static_cast<u32>(c0) | static_cast<u32>(c1) << 8 |
static_cast<u32>(c2) << 16 | static_cast<u32>(c3) << 24;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/io/continuous_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class IContinuousStream : public IStream {
IContinuousStream() noexcept = default;
~IContinuousStream() noexcept = default;

MAKE_DEFAULT_COPYABLE(IContinuousStream);
MAKE_NON_MOVABLE(IContinuousStream);
ZTD_MAKE_DEFAULT_COPYABLE(IContinuousStream);
ZTD_MAKE_DEFAULT_MOVABLE(IContinuousStream);

u64 GetSeek() const override { return seek; }
void SeekTo(u64 seek_) override { seek = seek_; }
Expand Down
4 changes: 2 additions & 2 deletions src/common/io/memory_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class MemoryStream : public IContinuousStream {
public:
MemoryStream(std::span<u8> data_) : data{data_} {}

MAKE_DEFAULT_COPYABLE(MemoryStream);
MAKE_DEFAULT_MOVABLE(MemoryStream);
ZTD_MAKE_DEFAULT_COPYABLE(MemoryStream);
ZTD_MAKE_DEFAULT_MOVABLE(MemoryStream);

u64 GetSize() const override { return data.size(); }

Expand Down
Loading
Loading