From 727f057fa7774cb394f5ee795b2141a930d427ad Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Tue, 11 Jun 2019 15:58:44 -1000 Subject: [PATCH] GCC fixes and better CMake dependency handling --- CMakeLists.txt | 18 +- Example/CMakeLists.txt | 3 +- atdna/CMakeLists.txt | 53 +- extern/lzokay | 2 +- extern/yaml/CMakeLists.txt | 8 +- include/athena/Compression.hpp | 2 + include/athena/Utility.hpp | 10 +- include/athena/simd/simd_sse.hpp | 7 +- include/optional.hpp | 981 ------------------------------- src/athena/Compression.cpp | 4 + src/athena/FileReader.cpp | 17 +- 11 files changed, 64 insertions(+), 1041 deletions(-) delete mode 100644 include/optional.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c5317eb..9c0d617 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,7 @@ -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) cmake_minimum_required(VERSION 3.10 FATAL_ERROR) # because of c++17 project(athena) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -endif() ################## # Athena Version # @@ -14,7 +12,6 @@ set(ATHENA_MINOR_VERSION 3) set(ATHENA_PATCH_VERSION 0) set(ATHENA_VERSION ${ATHENA_MAJOR_VERSION}.${ATHENA_MINOR_VERSION}.${ATHENA_PATCH_VERSION}) -set(ATHENA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "Athena Include Path") ################ # Athena Build # @@ -22,8 +19,6 @@ set(ATHENA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "Athen add_subdirectory(extern) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${LZOKAY_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}) - if(WIN32) list(APPEND CORE_EXTRA src/win32_largefilewrapper.c include/win32_largefilewrapper.h src/athena/FileWriterWin32.cpp src/athena/FileReaderWin32.cpp) @@ -85,8 +80,9 @@ add_library(athena-core include/athena/YAMLDocWriter.hpp include/yaml.h include/utf8proc.h - include/optional.hpp ) +target_include_directories(athena-core PUBLIC $) +target_link_libraries(athena-core PUBLIC athena-libyaml) add_library(athena-sakura EXCLUDE_FROM_ALL src/athena/Sprite.cpp @@ -210,7 +206,7 @@ endif() ################## # Add all targets to the build-tree export set -export(TARGETS athena-core FILE "${CMAKE_CURRENT_BINARY_DIR}/AthenaTargets.cmake") +export(TARGETS athena-core athena-libyaml FILE "${CMAKE_CURRENT_BINARY_DIR}/AthenaTargets.cmake") # Export the package for use from the build-tree # (this registers the build-tree with a global CMake-registry) @@ -245,13 +241,13 @@ install(EXPORT AthenaTargets DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT athena) add_subdirectory(atdna) # Test target -atdna(atdna_test.cpp atdna/test.hpp) -add_executable(atdna-test atdna/test.cpp atdna_test.cpp atdna/test.hpp) +add_executable(atdna-test atdna/test.cpp atdna/test.hpp) +target_atdna(atdna-test atdna_test.cpp atdna/test.hpp) if (CMAKE_SYSTEM_NAME STREQUAL "Switch") set_target_properties(atdna-test PROPERTIES SUFFIX ".elf") - target_link_libraries(atdna-test athena-core athena-libyaml nx) + target_link_libraries(atdna-test athena-core nx) else() - target_link_libraries(atdna-test athena-core athena-libyaml) + target_link_libraries(atdna-test athena-core) endif() ######### diff --git a/Example/CMakeLists.txt b/Example/CMakeLists.txt index 20f247b..1a0d075 100644 --- a/Example/CMakeLists.txt +++ b/Example/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.0) project(Example) endif() -include_directories(${ATHENA_INCLUDE_DIR}) add_executable(Example main.cpp) @@ -11,6 +10,8 @@ if(GEKKO) include_directories(${LIBOGCDIR}/include) link_directories(${LIGOGCDIR}/lib/wii) target_link_libraries(Example athena-core z wiiuse bte ogc m) +target_include_directories(Example PRIVATE ${LIBOGCDIR}/include) +target_link_directories(Example PRIVATE ${LIBOGCDIR}/lib/wii) else() #TODO: add normal link libraries endif() diff --git a/atdna/CMakeLists.txt b/atdna/CMakeLists.txt index fabb514..46d9c95 100644 --- a/atdna/CMakeLists.txt +++ b/atdna/CMakeLists.txt @@ -11,8 +11,6 @@ if(MSVC) set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /O2 /Ob2 /DNDEBUG") set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "-debug -incremental:no") endif() -string(REPLACE "-stdlib=libc++ -fsanitize=memory -fsanitize-memory-track-origins" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) -string(REPLACE "-stdlib=libc++ -fsanitize=address" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Find dependencies include(FindLLVM.cmake) @@ -119,12 +117,12 @@ if(WIN32) endif() # ATDNA target -include_directories(${LLVM_INCLUDE_DIRS}) -link_directories(${LLVM_LIBRARY_DIRS}) add_executable(atdna main.cpp test.hpp ${PLAT_SRCS}) target_link_libraries(atdna ${LLVM_LIBS} ${PLAT_LIBS}) set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS "INSTALL_PREFIX=${ABS_INSTALL_BIN_DIR};__STDC_LIMIT_MACROS=1;__STDC_CONSTANT_MACROS=1") +target_include_directories(atdna PRIVATE ${LLVM_INCLUDE_DIRS}) +target_link_directories(atdna PRIVATE ${LLVM_LIBRARY_DIRS}) if(MSVC) set_target_properties(atdna PROPERTIES COMPILE_FLAGS "/GR-") @@ -183,36 +181,19 @@ endif() ####################### # Super handy macro for adding atdna target -function(atdna out) +function(atdna out incdirs cdefs) # Ninja wants invocations in root binary dir for DEPFILE application file(RELATIVE_PATH out_rel ${CMAKE_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/${out}") # Make input files source-relative - set(ins "") - set(ins_impdeps "") + unset(ins) + unset(ins_impdeps) foreach(arg ${ARGN}) list(APPEND ins ${CMAKE_CURRENT_SOURCE_DIR}/${arg}) list(APPEND ins_impdeps CXX) list(APPEND ins_impdeps ${CMAKE_CURRENT_SOURCE_DIR}/${arg}) endforeach() - # Get local include directories for atdna - get_property(incdirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES) - set(inccli "") - foreach(dir ${incdirs}) - list(APPEND inccli "-I${dir}") - endforeach() - - # Get local defines for atdna - get_property(cdefs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_DEFINITIONS) - set(cdefcli "") - foreach(def ${cdefs}) - if(def MATCHES "^WINAPI_FAMILY.*") - continue() # C++/CX gives clang a stomach ache - endif() - list(APPEND cdefcli "-D${def}") - endforeach() - # MS extra unset(extraargs) if(MSVC) @@ -242,21 +223,33 @@ function(atdna out) if(${CMAKE_GENERATOR} STREQUAL "Ninja") # Use Ninja's DEPFILE parser in cooperation with atdna add_custom_command(OUTPUT ${out} COMMAND $ - ARGS ${extraargs} -o ${out_rel} -MD -MT ${out_rel} -MF ${out_rel}.d ${cdefcli} ${inccli} - "-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins} + ARGS ${extraargs} -o ${out_rel} -MD -MT ${out_rel} -MF ${out_rel}.d + "$<$:-I$>" + "$<$:-D$>" + "-I${athena_SOURCE_DIR}/include" -isystem "${CLANG_INCLUDE_DIR}" ${ins} DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps} DEPFILE "${CMAKE_CURRENT_BINARY_DIR}/${out}.d" WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMENT "Generating DNA ${out_rel}") + COMMENT "Generating DNA ${out_rel}" + COMMAND_EXPAND_LISTS) else() # Use CMake's built-in dependency scanner for makefile targets add_custom_command(OUTPUT ${out} COMMAND $ - ARGS ${extraargs} -o ${out_rel} ${cdefcli} ${inccli} - "-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins} + ARGS ${extraargs} -o ${out_rel} + "$<$:-I$>" + "$<$:-D$>" + "-I${athena_SOURCE_DIR}/include" -isystem "${CLANG_INCLUDE_DIR}" ${ins} DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps} WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMENT "Generating DNA ${out_rel}") + COMMENT "Generating DNA ${out_rel}" + COMMAND_EXPAND_LISTS) endif() endfunction() +function(target_atdna target out) + atdna(${out} "$" + "$" ${ARGN}) + target_sources(${target} PRIVATE ${out} ${ARGN}) +endfunction() + endif() diff --git a/extern/lzokay b/extern/lzokay index f7da328..5589269 160000 --- a/extern/lzokay +++ b/extern/lzokay @@ -1 +1 @@ -Subproject commit f7da328248f3fea1a5e05d3f39598850383b1867 +Subproject commit 55892696bb35f640f9f22c465c216f221ac5c8ad diff --git a/extern/yaml/CMakeLists.txt b/extern/yaml/CMakeLists.txt index a7a2ffc..d1aaa15 100644 --- a/extern/yaml/CMakeLists.txt +++ b/extern/yaml/CMakeLists.txt @@ -5,7 +5,11 @@ set(YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERS file(GLOB SRC src/*.c) -include_directories(../../include) add_library(athena-libyaml STATIC ${SRC}) +target_include_directories(athena-libyaml PUBLIC $) -install(TARGETS athena-libyaml DESTINATION lib COMPONENT yaml) +install(TARGETS athena-libyaml DESTINATION lib EXPORT AthenaTargets COMPONENT yaml) + +if (NOT MSVC) + target_compile_options(athena-libyaml PRIVATE -Wno-unused-but-set-variable -Wno-unused-value) +endif() diff --git a/include/athena/Compression.hpp b/include/athena/Compression.hpp index 4793d02..b56eb7e 100644 --- a/include/athena/Compression.hpp +++ b/include/athena/Compression.hpp @@ -7,8 +7,10 @@ namespace athena::io::Compression { atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen); atInt32 compressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen); +#if AT_LZOKAY // lzo compression atInt32 decompressLZO(const atUint8* source, atInt32 sourceSize, atUint8* dst, atInt32& dstSize); +#endif // Yaz0 encoding atUint32 yaz0Decode(const atUint8* src, atUint8* dst, atUint32 uncompressedSize); diff --git a/include/athena/Utility.hpp b/include/athena/Utility.hpp index f311679..85bcf88 100644 --- a/include/athena/Utility.hpp +++ b/include/athena/Utility.hpp @@ -56,12 +56,14 @@ inline atInt64 swap64(atInt64 val) { } inline atUint64 swapU64(atUint64 val) { return (atUint64)swap64(val); } inline float swapFloat(float val) { - atInt32 ival = swap32(*((atInt32*)(&val))); - return *((float*)(&ival)); + union { float f; atInt32 i; } uval1 = {val}; + union { atInt32 i; float f; } uval2 = {swap32(uval1.i)}; + return uval2.f; } inline double swapDouble(double val) { - atInt64 ival = swap64(*((atInt64*)(&val))); - return *((double*)(&ival)); + union { double f; atInt64 i; } uval1 = {val}; + union { atInt64 i; double f; } uval2 = {swap64(uval1.i)}; + return uval2.f; } inline atInt16 LittleInt16(atInt16& val) { if (athena::utility::isSystemBigEndian()) diff --git a/include/athena/simd/simd_sse.hpp b/include/athena/simd/simd_sse.hpp index c5bc25f..7a17125 100644 --- a/include/athena/simd/simd_sse.hpp +++ b/include/athena/simd/simd_sse.hpp @@ -221,12 +221,15 @@ public: sse_data[__index % 2] = __val; __storage_[__index / 2] = _mm_load_pd(sse_data.data()); } - constexpr __simd_storage(double a, double b, double c, double d) : __storage_{__m128d{a, b}, __m128d{c, d}} {} + static constexpr storage_type __make_array(__m128d a, __m128d b) { return {a, b}; } + constexpr __simd_storage(double a, double b, double c, double d) + : __storage_(__make_array(__m128d{a, b}, __m128d{c, d})) {} void __set4(double a, double b, double c, double d) noexcept { __storage_[0] = _mm_set_pd(b, a); __storage_[1] = _mm_set_pd(d, c); } - constexpr __simd_storage(double rv) : __storage_{__m128d{rv, rv}, __m128d{rv, rv}} {} + constexpr __simd_storage(double rv) + : __storage_(__make_array(__m128d{rv, rv}, __m128d{rv, rv})) {} void __broadcast(double __val) noexcept { for (int i = 0; i < 2; ++i) __storage_[i] = _mm_set1_pd(__val); diff --git a/include/optional.hpp b/include/optional.hpp deleted file mode 100644 index 14eb2ef..0000000 --- a/include/optional.hpp +++ /dev/null @@ -1,981 +0,0 @@ -// Copyright (C) 2011 - 2012 Andrzej Krzemienski. -// -// Use, modification, and distribution is subject to the Boost Software -// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// The idea and interface is based on Boost.Optional library -// authored by Fernando Luis Cacciola Carballal - -#ifndef ___OPTIONAL_HPP___ -#define ___OPTIONAL_HPP___ - -#include -#include -#include -#include -#include -#include -#include "athena/Global.hpp" - -#define TR2_OPTIONAL_REQUIRES(...) typename enable_if<__VA_ARGS__::value, bool>::type = false - -#if defined __GNUC__ // NOTE: GNUC is also defined for Clang -#if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8) -#define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ -#elif (__GNUC__ > 4) -#define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ -#endif -# -#if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7) -#define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___ -#elif (__GNUC__ > 4) -#define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___ -#endif -# -#if (__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && (__GNUC_PATCHLEVEL__ >= 1) -#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ -#elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9) -#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ -#elif (__GNUC__ > 4) -#define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ -#endif -#endif -# -#if defined __clang_major__ -#if (__clang_major__ == 3 && __clang_minor__ >= 5) -#define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ -#elif (__clang_major__ > 3) -#define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ -#endif -#if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ -#define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ -#elif (__clang_major__ == 3 && __clang_minor__ == 4 && __clang_patchlevel__ >= 2) -#define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ -#endif -#endif -# -#if defined _MSC_VER -#if (_MSC_VER >= 1900) -#define TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ -#endif -#endif - -#if defined __clang__ -#if (__clang_major__ > 2) || (__clang_major__ == 2) && (__clang_minor__ >= 9) -#define OPTIONAL_HAS_THIS_RVALUE_REFS 1 -#else -#define OPTIONAL_HAS_THIS_RVALUE_REFS 0 -#endif -#elif defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ -#define OPTIONAL_HAS_THIS_RVALUE_REFS 1 -#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ -#define OPTIONAL_HAS_THIS_RVALUE_REFS 1 -#else -#define OPTIONAL_HAS_THIS_RVALUE_REFS 0 -#endif - -#if defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___ -#define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 1 -#define OPTIONAL_CONSTEXPR_INIT_LIST constexpr -#else -#define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 0 -#define OPTIONAL_CONSTEXPR_INIT_LIST -#endif - -#if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ && (defined __cplusplus) && (__cplusplus != 201103L) -#define OPTIONAL_HAS_MOVE_ACCESSORS 1 -#else -#define OPTIONAL_HAS_MOVE_ACCESSORS 0 -#endif - -#// In C++11 constexpr implies const, so we need to make non-const members also non-constexpr -#if (defined __cplusplus) && (__cplusplus == 201103L) -#define OPTIONAL_MUTABLE_CONSTEXPR -#else -#define OPTIONAL_MUTABLE_CONSTEXPR constexpr -#endif - -namespace std { - -namespace experimental { - -// BEGIN workaround for missing is_trivially_destructible -#if defined TR2_OPTIONAL_GCC_4_8_AND_HIGHER___ -// leave it: it is already there -#elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ -// leave it: it is already there -#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ -// leave it: it is already there -#elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS -// leave it: the user doesn't want it -#else -template -using is_trivially_destructible = std::has_trivial_destructor; -#endif -// END workaround for missing is_trivially_destructible - -#if (defined TR2_OPTIONAL_GCC_4_7_AND_HIGHER___) -// leave it; our metafunctions are already defined. -#elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_ -// leave it; our metafunctions are already defined. -#elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___ -// leave it: it is already there -#elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS -// leave it: the user doesn't want it -#else - -// workaround for missing traits in GCC and CLANG -template -struct is_nothrow_move_constructible { - constexpr static bool value = std::is_nothrow_constructible::value; -}; - -template -struct is_assignable { - template - constexpr static bool has_assign(...) { - return false; - } - - template () = std::declval(), true))> - // the comma operator is necessary for the cases where operator= returns void - constexpr static bool has_assign(bool) { - return true; - } - - constexpr static bool value = has_assign(true); -}; - -template -struct is_nothrow_move_assignable { - template - struct has_nothrow_move_assign { - constexpr static bool value = false; - }; - - template - struct has_nothrow_move_assign { - constexpr static bool value = noexcept(std::declval() = std::declval()); - }; - - constexpr static bool value = has_nothrow_move_assign::value>::value; -}; -// end workaround - -#endif - -// 20.5.4, optional for object types -template -class optional; - -// 20.5.5, optional for lvalue reference types -template -class optional; - -// workaround: std utility functions aren't constexpr yet -template -inline constexpr T&& constexpr_forward(typename std::remove_reference::type& t) noexcept { - return static_cast(t); -} - -template -inline constexpr T&& constexpr_forward(typename std::remove_reference::type&& t) noexcept { - static_assert(!std::is_lvalue_reference::value, "!!"); - return static_cast(t); -} - -template -inline constexpr typename std::remove_reference::type&& constexpr_move(T&& t) noexcept { - return static_cast::type&&>(t); -} - -#if defined NDEBUG -#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR) -#else -#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR))) -#endif - -namespace detail_ { - -// static_addressof: a constexpr version of addressof -template -struct has_overloaded_addressof { - template - constexpr static bool has_overload(...) { - return false; - } - - template ().operator&())> - constexpr static bool has_overload(bool) { - return true; - } - - constexpr static bool value = has_overload(true); -}; - -template )> -constexpr T* static_addressof(T& ref) { - return &ref; -} - -template )> -T* static_addressof(T& ref) { - return std::addressof(ref); -} - -// the call to convert(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A -template -U convert(U v) { - return v; -} - -} // namespace detail_ - -constexpr struct trivial_init_t { -} trivial_init{}; - -// 20.5.6, In-place construction -constexpr struct in_place_t { -} in_place{}; - -// 20.5.7, Disengaged state indicator -struct nullopt_t { - struct init {}; - constexpr explicit nullopt_t(init) {} -}; -constexpr nullopt_t nullopt{nullopt_t::init()}; - -// 20.5.8, class bad_optional_access -class bad_optional_access : public logic_error { -public: - explicit bad_optional_access(const string& what_arg) : logic_error{what_arg} {} - explicit bad_optional_access(const char* what_arg) : logic_error{what_arg} {} -}; - -template -union storage_t { - unsigned char dummy_; - T value_; - - constexpr storage_t(trivial_init_t) noexcept : dummy_(){}; - - template - constexpr storage_t(Args&&... args) : value_(constexpr_forward(args)...) {} - - ~storage_t() {} -}; - -template -union constexpr_storage_t { - unsigned char dummy_; - T value_; - - constexpr constexpr_storage_t(trivial_init_t) noexcept : dummy_(){}; - - template - constexpr constexpr_storage_t(Args&&... args) : value_(constexpr_forward(args)...) {} - - ~constexpr_storage_t() = default; -}; - -template -struct optional_base { - bool init_; - storage_t storage_; - - constexpr optional_base() noexcept : init_(false), storage_(trivial_init){}; - - explicit constexpr optional_base(const T& v) : init_(true), storage_(v) {} - - explicit constexpr optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {} - - template - explicit optional_base(in_place_t, Args&&... args) : init_(true), storage_(constexpr_forward(args)...) {} - - template >)> - explicit optional_base(in_place_t, std::initializer_list il, Args&&... args) - : init_(true), storage_(il, std::forward(args)...) {} - - ~optional_base() { - if (init_) - storage_.value_.T::~T(); - } -}; - -template -struct constexpr_optional_base { - bool init_; - constexpr_storage_t storage_; - - constexpr constexpr_optional_base() noexcept : init_(false), storage_(trivial_init){}; - - explicit constexpr constexpr_optional_base(const T& v) : init_(true), storage_(v) {} - - explicit constexpr constexpr_optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {} - - template - explicit constexpr constexpr_optional_base(in_place_t, Args&&... args) - : init_(true), storage_(constexpr_forward(args)...) {} - - template >)> - OPTIONAL_CONSTEXPR_INIT_LIST explicit constexpr_optional_base(in_place_t, std::initializer_list il, Args&&... args) - : init_(true), storage_(il, std::forward(args)...) {} - - ~constexpr_optional_base() = default; -}; - -template -using OptionalBase = - typename std::conditional::value, constexpr_optional_base, optional_base>::type; - -template -class optional : private OptionalBase { - static_assert(!std::is_same::type, nullopt_t>::value, "bad T"); - static_assert(!std::is_same::type, in_place_t>::value, "bad T"); - - constexpr bool initialized() const noexcept { return OptionalBase::init_; } - T* dataptr() { return std::addressof(OptionalBase::storage_.value_); } - constexpr const T* dataptr() const { return detail_::static_addressof(OptionalBase::storage_.value_); } - -#if OPTIONAL_HAS_THIS_RVALUE_REFS == 1 - constexpr const T& contained_val() const& { return OptionalBase::storage_.value_; } -#if OPTIONAL_HAS_MOVE_ACCESSORS == 1 - OPTIONAL_MUTABLE_CONSTEXPR T&& contained_val() && { return std::move(OptionalBase::storage_.value_); } - OPTIONAL_MUTABLE_CONSTEXPR T& contained_val() & { return OptionalBase::storage_.value_; } -#else - T& contained_val() & { return OptionalBase::storage_.value_; } - T&& contained_val() && { return std::move(OptionalBase::storage_.value_); } -#endif -#else - constexpr const T& contained_val() const { return OptionalBase::storage_.value_; } - T& contained_val() { return OptionalBase::storage_.value_; } -#endif - - void clear() noexcept { - if (initialized()) - dataptr()->T::~T(); - OptionalBase::init_ = false; - } - - template - void initialize(Args&&... args) noexcept(noexcept(T(std::forward(args)...))) { - assert(!OptionalBase::init_); - ::new (static_cast(dataptr())) T(std::forward(args)...); - OptionalBase::init_ = true; - } - - template - void initialize(std::initializer_list il, Args&&... args) noexcept(noexcept(T(il, std::forward(args)...))) { - assert(!OptionalBase::init_); - ::new (static_cast(dataptr())) T(il, std::forward(args)...); - OptionalBase::init_ = true; - } - -public: - typedef T value_type; - - // 20.5.5.1, constructors - constexpr optional() noexcept : OptionalBase(){}; - constexpr optional(nullopt_t) noexcept : OptionalBase(){}; - - optional(const optional& rhs) : OptionalBase() { - if (rhs.initialized()) { - ::new (static_cast(dataptr())) T(*rhs); - OptionalBase::init_ = true; - } - } - - optional(optional&& rhs) noexcept(is_nothrow_move_constructible::value) : OptionalBase() { - if (rhs.initialized()) { - ::new (static_cast(dataptr())) T(std::move(*rhs)); - OptionalBase::init_ = true; - } - } - - constexpr optional(const T& v) : OptionalBase(v) {} - - constexpr optional(T&& v) : OptionalBase(constexpr_move(v)) {} - - template - explicit constexpr optional(in_place_t, Args&&... args) - : OptionalBase(in_place_t{}, constexpr_forward(args)...) {} - - template >)> - OPTIONAL_CONSTEXPR_INIT_LIST explicit optional(in_place_t, std::initializer_list il, Args&&... args) - : OptionalBase(in_place_t{}, il, constexpr_forward(args)...) {} - - // 20.5.4.2, Destructor - ~optional() = default; - - // 20.5.4.3, assignment - optional& operator=(nullopt_t) noexcept { - clear(); - return *this; - } - - optional& operator=(const optional& rhs) { - if (initialized() == true && rhs.initialized() == false) - clear(); - else if (initialized() == false && rhs.initialized() == true) - initialize(*rhs); - else if (initialized() == true && rhs.initialized() == true) - contained_val() = *rhs; - return *this; - } - - optional& - operator=(optional&& rhs) noexcept(is_nothrow_move_assignable::value&& is_nothrow_move_constructible::value) { - if (initialized() == true && rhs.initialized() == false) - clear(); - else if (initialized() == false && rhs.initialized() == true) - initialize(std::move(*rhs)); - else if (initialized() == true && rhs.initialized() == true) - contained_val() = std::move(*rhs); - return *this; - } - - template - auto operator=(U&& v) -> typename enable_if::type, T>::value, optional&>::type { - if (initialized()) { - contained_val() = std::forward(v); - } else { - initialize(std::forward(v)); - } - return *this; - } - - template - void emplace(Args&&... args) { - clear(); - initialize(std::forward(args)...); - } - - template - void emplace(initializer_list il, Args&&... args) { - clear(); - initialize(il, std::forward(args)...); - } - - // 20.5.4.4, Swap - void swap(optional& rhs) noexcept(is_nothrow_move_constructible::value&& noexcept(swap(declval(), - declval()))) { - if (initialized() == true && rhs.initialized() == false) { - rhs.initialize(std::move(**this)); - clear(); - } else if (initialized() == false && rhs.initialized() == true) { - initialize(std::move(*rhs)); - rhs.clear(); - } else if (initialized() == true && rhs.initialized() == true) { - using std::swap; - swap(**this, *rhs); - } - } - - // 20.5.4.5, Observers - - explicit constexpr operator bool() const noexcept { return initialized(); } - - constexpr T const* operator->() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr()); } - -#if OPTIONAL_HAS_MOVE_ACCESSORS == 1 - - OPTIONAL_MUTABLE_CONSTEXPR T* operator->() { - assert(initialized()); - return dataptr(); - } - - constexpr T const& operator*() const& { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); } - - OPTIONAL_MUTABLE_CONSTEXPR T& operator*() & { - assert(initialized()); - return contained_val(); - } - - OPTIONAL_MUTABLE_CONSTEXPR T&& operator*() && { - assert(initialized()); - return constexpr_move(contained_val()); - } - - constexpr T const& value() const& { - if (!initialized()) - atFatal("bad optional access"); - return contained_val(); - } - - OPTIONAL_MUTABLE_CONSTEXPR T& value() & { - if (!initialized()) - atFatal("bad optional access"); - return contained_val(); - } - - OPTIONAL_MUTABLE_CONSTEXPR T&& value() && { - if (!initialized()) - atFatal("bad optional access"); - return std::move(contained_val()); - } - -#else - - T* operator->() { - assert(initialized()); - return dataptr(); - } - - constexpr T const& operator*() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); } - - T& operator*() { - assert(initialized()); - return contained_val(); - } - - constexpr T const& value() const { - if (!initialized()) - atFatal("bad optional access"); - return contained_val(); - } - - T& value() { - if (!initialized()) - atFatal("bad optional access"); - return contained_val(); - } - -#endif - -#if OPTIONAL_HAS_THIS_RVALUE_REFS == 1 - - template - constexpr T value_or(V&& v) const& { - return *this ? **this : detail_::convert(constexpr_forward(v)); - } - -#if OPTIONAL_HAS_MOVE_ACCESSORS == 1 - - template - OPTIONAL_MUTABLE_CONSTEXPR T value_or(V&& v) && { - return *this ? constexpr_move(const_cast&>(*this).contained_val()) - : detail_::convert(constexpr_forward(v)); - } - -#else - - template - T value_or(V&& v) && { - return *this ? constexpr_move(const_cast&>(*this).contained_val()) - : detail_::convert(constexpr_forward(v)); - } - -#endif - -#else - - template - constexpr T value_or(V&& v) const { - return *this ? **this : detail_::convert(constexpr_forward(v)); - } - -#endif -}; - -template -class optional { - static_assert(!std::is_same::value, "bad T"); - static_assert(!std::is_same::value, "bad T"); - T* ref; - -public: - // 20.5.5.1, construction/destruction - constexpr optional() noexcept : ref(nullptr) {} - - constexpr optional(nullopt_t) noexcept : ref(nullptr) {} - - constexpr optional(T& v) noexcept : ref(detail_::static_addressof(v)) {} - - optional(T&&) = delete; - - constexpr optional(const optional& rhs) noexcept : ref(rhs.ref) {} - - explicit constexpr optional(in_place_t, T& v) noexcept : ref(detail_::static_addressof(v)) {} - - explicit optional(in_place_t, T&&) = delete; - - ~optional() = default; - - // 20.5.5.2, mutation - optional& operator=(nullopt_t) noexcept { - ref = nullptr; - return *this; - } - - // optional& operator=(const optional& rhs) noexcept { - // ref = rhs.ref; - // return *this; - // } - - // optional& operator=(optional&& rhs) noexcept { - // ref = rhs.ref; - // return *this; - // } - - template - auto operator=(U&& rhs) noexcept -> - typename enable_if::type, optional>::value, optional&>::type { - ref = rhs.ref; - return *this; - } - - template - auto operator=(U&& rhs) noexcept -> - typename enable_if::type, optional>::value, optional&>::type = delete; - - void emplace(T& v) noexcept { ref = detail_::static_addressof(v); } - - void emplace(T&&) = delete; - - void swap(optional& rhs) noexcept { std::swap(ref, rhs.ref); } - - // 20.5.5.3, observers - constexpr T* operator->() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref); } - - constexpr T& operator*() const { return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref); } - - constexpr T& value() const { - if (!ref) - atFatal("bad optional access"); - return *ref; - } - - explicit constexpr operator bool() const noexcept { return ref != nullptr; } - - template - constexpr typename decay::type value_or(V&& v) const { - return *this ? **this : detail_::convert::type>(constexpr_forward(v)); - } -}; - -template -class optional { - static_assert(sizeof(T) == 0, "optional rvalue references disallowed"); -}; - -// 20.5.8, Relational operators -template -constexpr bool operator==(const optional& x, const optional& y) { - return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y; -} - -template -constexpr bool operator!=(const optional& x, const optional& y) { - return !(x == y); -} - -template -constexpr bool operator<(const optional& x, const optional& y) { - return (!y) ? false : (!x) ? true : *x < *y; -} - -template -constexpr bool operator>(const optional& x, const optional& y) { - return (y < x); -} - -template -constexpr bool operator<=(const optional& x, const optional& y) { - return !(y < x); -} - -template -constexpr bool operator>=(const optional& x, const optional& y) { - return !(x < y); -} - -// 20.5.9, Comparison with nullopt -template -constexpr bool operator==(const optional& x, nullopt_t) noexcept { - return (!x); -} - -template -constexpr bool operator==(nullopt_t, const optional& x) noexcept { - return (!x); -} - -template -constexpr bool operator!=(const optional& x, nullopt_t) noexcept { - return bool(x); -} - -template -constexpr bool operator!=(nullopt_t, const optional& x) noexcept { - return bool(x); -} - -template -constexpr bool operator<(const optional&, nullopt_t) noexcept { - return false; -} - -template -constexpr bool operator<(nullopt_t, const optional& x) noexcept { - return bool(x); -} - -template -constexpr bool operator<=(const optional& x, nullopt_t) noexcept { - return (!x); -} - -template -constexpr bool operator<=(nullopt_t, const optional&) noexcept { - return true; -} - -template -constexpr bool operator>(const optional& x, nullopt_t) noexcept { - return bool(x); -} - -template -constexpr bool operator>(nullopt_t, const optional&) noexcept { - return false; -} - -template -constexpr bool operator>=(const optional&, nullopt_t) noexcept { - return true; -} - -template -constexpr bool operator>=(nullopt_t, const optional& x) noexcept { - return (!x); -} - -// 20.5.10, Comparison with T -template -constexpr bool operator==(const optional& x, const T& v) { - return bool(x) ? *x == v : false; -} - -template -constexpr bool operator==(const T& v, const optional& x) { - return bool(x) ? v == *x : false; -} - -template -constexpr bool operator!=(const optional& x, const T& v) { - return bool(x) ? *x != v : true; -} - -template -constexpr bool operator!=(const T& v, const optional& x) { - return bool(x) ? v != *x : true; -} - -template -constexpr bool operator<(const optional& x, const T& v) { - return bool(x) ? *x < v : true; -} - -template -constexpr bool operator>(const T& v, const optional& x) { - return bool(x) ? v > *x : true; -} - -template -constexpr bool operator>(const optional& x, const T& v) { - return bool(x) ? *x > v : false; -} - -template -constexpr bool operator<(const T& v, const optional& x) { - return bool(x) ? v < *x : false; -} - -template -constexpr bool operator>=(const optional& x, const T& v) { - return bool(x) ? *x >= v : false; -} - -template -constexpr bool operator<=(const T& v, const optional& x) { - return bool(x) ? v <= *x : false; -} - -template -constexpr bool operator<=(const optional& x, const T& v) { - return bool(x) ? *x <= v : true; -} - -template -constexpr bool operator>=(const T& v, const optional& x) { - return bool(x) ? v >= *x : true; -} - -// Comparison of optional with T -template -constexpr bool operator==(const optional& x, const T& v) { - return bool(x) ? *x == v : false; -} - -template -constexpr bool operator==(const T& v, const optional& x) { - return bool(x) ? v == *x : false; -} - -template -constexpr bool operator!=(const optional& x, const T& v) { - return bool(x) ? *x != v : true; -} - -template -constexpr bool operator!=(const T& v, const optional& x) { - return bool(x) ? v != *x : true; -} - -template -constexpr bool operator<(const optional& x, const T& v) { - return bool(x) ? *x < v : true; -} - -template -constexpr bool operator>(const T& v, const optional& x) { - return bool(x) ? v > *x : true; -} - -template -constexpr bool operator>(const optional& x, const T& v) { - return bool(x) ? *x > v : false; -} - -template -constexpr bool operator<(const T& v, const optional& x) { - return bool(x) ? v < *x : false; -} - -template -constexpr bool operator>=(const optional& x, const T& v) { - return bool(x) ? *x >= v : false; -} - -template -constexpr bool operator<=(const T& v, const optional& x) { - return bool(x) ? v <= *x : false; -} - -template -constexpr bool operator<=(const optional& x, const T& v) { - return bool(x) ? *x <= v : true; -} - -template -constexpr bool operator>=(const T& v, const optional& x) { - return bool(x) ? v >= *x : true; -} - -// Comparison of optional with T -template -constexpr bool operator==(const optional& x, const T& v) { - return bool(x) ? *x == v : false; -} - -template -constexpr bool operator==(const T& v, const optional& x) { - return bool(x) ? v == *x : false; -} - -template -constexpr bool operator!=(const optional& x, const T& v) { - return bool(x) ? *x != v : true; -} - -template -constexpr bool operator!=(const T& v, const optional& x) { - return bool(x) ? v != *x : true; -} - -template -constexpr bool operator<(const optional& x, const T& v) { - return bool(x) ? *x < v : true; -} - -template -constexpr bool operator>(const T& v, const optional& x) { - return bool(x) ? v > *x : true; -} - -template -constexpr bool operator>(const optional& x, const T& v) { - return bool(x) ? *x > v : false; -} - -template -constexpr bool operator<(const T& v, const optional& x) { - return bool(x) ? v < *x : false; -} - -template -constexpr bool operator>=(const optional& x, const T& v) { - return bool(x) ? *x >= v : false; -} - -template -constexpr bool operator<=(const T& v, const optional& x) { - return bool(x) ? v <= *x : false; -} - -template -constexpr bool operator<=(const optional& x, const T& v) { - return bool(x) ? *x <= v : true; -} - -template -constexpr bool operator>=(const T& v, const optional& x) { - return bool(x) ? v >= *x : true; -} - -// 20.5.12, Specialized algorithms -template -void swap(optional& x, optional& y) noexcept(noexcept(x.swap(y))) { - x.swap(y); -} - -template -constexpr optional::type> make_optional(T&& v) { - return optional::type>(constexpr_forward(v)); -} - -template -constexpr optional make_optional(reference_wrapper v) { - return optional(v.get()); -} - -} // namespace experimental -} // namespace std - -namespace std { -template -struct hash> { - typedef typename hash::result_type result_type; - typedef std::experimental::optional argument_type; - - constexpr result_type operator()(argument_type const& arg) const { - return arg ? std::hash{}(*arg) : result_type{}; - } -}; - -template -struct hash> { - typedef typename hash::result_type result_type; - typedef std::experimental::optional argument_type; - - constexpr result_type operator()(argument_type const& arg) const { - return arg ? std::hash{}(*arg) : result_type{}; - } -}; -} // namespace std - -#undef TR2_OPTIONAL_REQUIRES -#undef TR2_OPTIONAL_ASSERTED_EXPRESSION - -#endif //___OPTIONAL_HPP___ diff --git a/src/athena/Compression.cpp b/src/athena/Compression.cpp index 8c3a3ec..4f72837 100644 --- a/src/athena/Compression.cpp +++ b/src/athena/Compression.cpp @@ -1,5 +1,7 @@ #include "athena/Compression.hpp" +#if AT_LZOKAY #include +#endif #include #include #include "LZ77/LZType10.hpp" @@ -77,6 +79,7 @@ atInt32 compressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 return ret; } +#if AT_LZOKAY atInt32 decompressLZO(const atUint8* source, const atInt32 sourceSize, atUint8* dst, atInt32& dstSize) { size_t size = dstSize; auto result = lzokay::decompress(source, sourceSize, dst, size); @@ -84,6 +87,7 @@ atInt32 decompressLZO(const atUint8* source, const atInt32 sourceSize, atUint8* return (atInt32)result; } +#endif // src points to the yaz0 source data (to the "real" source data, not at the header!) // dst points to a buffer uncompressedSize bytes large (you get uncompressedSize from diff --git a/src/athena/FileReader.cpp b/src/athena/FileReader.cpp index 6ea301d..9922154 100644 --- a/src/athena/FileReader.cpp +++ b/src/athena/FileReader.cpp @@ -61,7 +61,6 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { // check block position if (m_blockSize > 0) { - atUint64 oldOff = m_offset; switch (origin) { case SeekOrigin::Begin: m_offset = pos; @@ -74,7 +73,6 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { break; } if (m_offset > length()) { - oldOff = m_offset; if (m_globalErr) atError("Unable to seek in file"); setError(); @@ -82,10 +80,10 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { } size_t block = m_offset / m_blockSize; - if (block != m_curBlock) { + if (atInt32(block) != m_curBlock) { fseeko64(m_fileHandle, block * m_blockSize, SEEK_SET); fread(m_cacheData.get(), 1, m_blockSize, m_fileHandle); - m_curBlock = (atInt32)block; + m_curBlock = atInt32(block); } } else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { if (m_globalErr) @@ -141,14 +139,14 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { atUint8* dst = (atUint8*)buf; while (rem) { - if (block != m_curBlock) { + if (atInt32(block) != m_curBlock) { fseeko64(m_fileHandle, block * m_blockSize, SEEK_SET); fread(m_cacheData.get(), 1, m_blockSize, m_fileHandle); - m_curBlock = (atInt32)block; + m_curBlock = atInt32(block); } cacheSize = rem; - if (cacheSize + cacheOffset > m_blockSize) + if (atInt32(cacheSize + cacheOffset) > m_blockSize) cacheSize = m_blockSize - cacheOffset; memmove(dst, m_cacheData.get() + cacheOffset, cacheSize); @@ -165,8 +163,9 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { void FileReader::setCacheSize(const atInt32 blockSize) { m_blockSize = blockSize; - if (m_blockSize > length()) - m_blockSize = (atInt32)length(); + atInt32 len = atInt32(length()); + if (m_blockSize > len) + m_blockSize = len; m_curBlock = -1; if (m_blockSize > 0)