1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-18 06:50:27 +00:00

Get hyper_function.h compiling on GCC 4.6.

We must avoid the following features:
- the `using` syntax for typedefs
- alias templates (so, rename `hyper_function` to `function`)
- the `override` keyword
- defaulted virtual destructors
This commit is contained in:
Arthur O'Dwyer 2019-02-10 11:49:55 -05:00
parent 5efd1f5662
commit ac37e5c000
3 changed files with 20 additions and 40 deletions

View File

@ -99,7 +99,6 @@ ifeq (${TOOLCHAIN},gcc46)
CXXFLAGS_EARLY += -std=c++0x CXXFLAGS_EARLY += -std=c++0x
CXXFLAGS_EARLY += -W -Wall -Wextra -Werror CXXFLAGS_EARLY += -W -Wall -Wextra -Werror
CXXFLAGS_EARLY += -Wno-missing-field-initializers -Wno-unused-parameter CXXFLAGS_EARLY += -Wno-missing-field-initializers -Wno-unused-parameter
CXXFLAGS_EARLY += -DUSE_STDFUNCTION=1
endif endif
ifeq (${TOOLCHAIN},mingw) ifeq (${TOOLCHAIN},mingw)

View File

@ -1,67 +1,54 @@
#pragma once #pragma once
#if USE_STDFUNCTION
#include <functional>
namespace hr {
using std::function;
} // namespace hr
#else
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
namespace hr { namespace hr {
template<class Sig> template<class Sig>
class hyper_function; class function;
template<class Sig>
using function = hyper_function<Sig>;
template<class R, class... Args> template<class R, class... Args>
struct hyper_function_state_base { struct function_state_base {
virtual R call(Args...) const = 0; virtual R call(Args...) const = 0;
virtual hyper_function_state_base *clone() const = 0; virtual function_state_base *clone() const = 0;
virtual ~hyper_function_state_base() = default; virtual ~function_state_base() {}
}; };
template<class T, class R, class... Args> template<class T, class R, class... Args>
struct hyper_function_state : hyper_function_state_base<R, Args...> { struct function_state : function_state_base<R, Args...> {
using Self = hyper_function_state<T, R, Args...>;
T t_; T t_;
explicit hyper_function_state(T t) : t_(std::move(t)) {} explicit function_state(T t) : t_(std::move(t)) {}
R call(Args... args) const override { R call(Args... args) const /*override*/ {
return const_cast<T&>(t_)(static_cast<Args&&>(args)...); return const_cast<T&>(t_)(static_cast<Args&&>(args)...);
} }
hyper_function_state_base<R, Args...> *clone() const override { function_state_base<R, Args...> *clone() const /*override*/ {
return new Self(*this); return new function_state(*this);
} }
}; };
template<class R, class... Args> template<class R, class... Args>
class hyper_function<R(Args...)> class function<R(Args...)>
{ {
hyper_function_state_base<R, Args...> *ptr_ = nullptr; function_state_base<R, Args...> *ptr_;
public: public:
hyper_function() = default; function() : ptr_(nullptr) {}
template<class Callable, class = decltype(R(std::declval<typename std::decay<Callable>::type>()(std::declval<Args>()...)))> template<class Callable, class = decltype(R(std::declval<typename std::decay<Callable>::type>()(std::declval<Args>()...)))>
hyper_function(Callable&& t) : function(Callable&& t) :
ptr_(new hyper_function_state<typename std::decay<Callable>::type, R, Args...>(static_cast<Callable&&>(t))) ptr_(new function_state<typename std::decay<Callable>::type, R, Args...>(static_cast<Callable&&>(t)))
{} {}
~hyper_function() { ~function() {
delete ptr_; delete ptr_;
} }
hyper_function(hyper_function& rhs) : ptr_(rhs.ptr_ ? rhs.ptr_->clone() : nullptr) {} function(function& rhs) : ptr_(rhs.ptr_ ? rhs.ptr_->clone() : nullptr) {}
hyper_function(const hyper_function& rhs) : ptr_(rhs.ptr_ ? rhs.ptr_->clone() : nullptr) {} function(const function& rhs) : ptr_(rhs.ptr_ ? rhs.ptr_->clone() : nullptr) {}
hyper_function(hyper_function&& rhs) noexcept : ptr_(rhs.ptr_) { rhs.ptr_ = nullptr; } function(function&& rhs) noexcept : ptr_(rhs.ptr_) { rhs.ptr_ = nullptr; }
hyper_function(const hyper_function&& rhs) = delete; function(const function&& rhs) = delete;
void operator=(hyper_function rhs) noexcept { void operator=(function rhs) noexcept {
std::swap(ptr_, rhs.ptr_); std::swap(ptr_, rhs.ptr_);
} }
@ -75,5 +62,3 @@ public:
}; };
} // namespace hr } // namespace hr
#endif

View File

@ -72,10 +72,6 @@
#define NOLICENSE ISSTEAM #define NOLICENSE ISSTEAM
#endif #endif
#ifndef USE_STDFUNCTION
#define USE_STDFUNCTION ISSTEAM
#endif
#ifndef CAP_SHADER #ifndef CAP_SHADER
#define CAP_SHADER CAP_GL #define CAP_SHADER CAP_GL
#endif #endif