From 73a0cf44e8d4a852b58b3126ddd3fa3b8a465af5 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Wed, 16 Oct 2013 00:17:05 +0100 Subject: First pass at replacing boost with c++11 features --- CMakeLists.txt | 8 ++++---- entityx/3rdparty/simplesignal.h | 17 ++++++++--------- entityx/Entity.h | 5 +++-- entityx/Event.h | 16 +++++++--------- entityx/Manager.h | 4 ++-- entityx/System.h | 10 +++++----- entityx/help/NonCopyable.h | 23 +++++++++++++++++++++++ entityx/help/Timer.cc | 37 +++++++++++++++++++++++++++++++++++++ entityx/help/Timer.h | 27 +++++++++++++++++++++++++++ entityx/python/PythonSystem.cc | 7 ++++--- entityx/tags/TagsComponent.h | 4 ++-- 11 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 entityx/help/NonCopyable.h create mode 100644 entityx/help/Timer.cc create mode 100644 entityx/help/Timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ba530fa..6cae773 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,10 +13,10 @@ set(ENTITYX_RUN_BENCHMARKS false CACHE BOOL "Run benchmarks (in conjunction with set(ENTITYX_MAX_COMPONENTS 64 CACHE STRING "Set the maximum number of components.") set(ENTITYX_USE_CPP11_STDLIB false CACHE BOOL "For Clang, specify whether to use libc++ (-stdlib=libc++).") # Check for which shared_ptr implementation to use. -set(ENTITYX_USE_STD_SHARED_PTR false CACHE BOOL "Use std::shared_ptr rather than boost::shared_ptr?") -set(ENTITYX_BUILD_SHARED true CACHE BOOL "Build shared libraries?") -set(ENTITYX_BUILD_PYTHON true CACHE BOOL "Build entityx::python?") -set(ENTITYX_BOOST_SP_DISABLE_THREADS false CACHE BOOL "Disable multi-threading support in boost::shared_ptr") +set(ENTITYX_USE_STD_SHARED_PTR true CACHE BOOL "Use std::shared_ptr rather than boost::shared_ptr?") +set(ENTITYX_BUILD_SHARED false CACHE BOOL "Build shared libraries?") +set(ENTITYX_BUILD_PYTHON false CACHE BOOL "Build entityx::python?") +set(ENTITYX_BOOST_SP_DISABLE_THREADS true CACHE BOOL "Disable multi-threading support in boost::shared_ptr") include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake) include(CheckCXXSourceCompiles) diff --git a/entityx/3rdparty/simplesignal.h b/entityx/3rdparty/simplesignal.h index eaca148..0af2644 100644 --- a/entityx/3rdparty/simplesignal.h +++ b/entityx/3rdparty/simplesignal.h @@ -6,7 +6,6 @@ #include #include #include -#include namespace Simple { @@ -46,7 +45,7 @@ struct CollectorDefault { template struct CollectorInvocation { inline bool - invoke (Collector &collector, const boost::function &cbf, Args... args) + invoke (Collector &collector, const std::function &cbf, Args... args) { return collector (cbf (args...)); } @@ -56,7 +55,7 @@ struct CollectorInvocation { template struct CollectorInvocation { inline bool - invoke (Collector &collector, const boost::function &cbf, Args... args) + invoke (Collector &collector, const std::function &cbf, Args... args) { cbf (args...); return collector(); } @@ -66,7 +65,7 @@ struct CollectorInvocation { template class ProtoSignal : private CollectorInvocation { protected: - typedef boost::function CbFunction; + typedef std::function CbFunction; typedef typename CbFunction::result_type Result; typedef typename Collector::CollectorResult CollectorResult; private: @@ -237,7 +236,7 @@ public: * The overhead of an unused signal is intentionally kept very low, around the size of a single pointer. * Note that the Signal template types is non-copyable. */ -template ::result_type> > +template ::result_type> > struct Signal /*final*/ : Lib::ProtoSignal { @@ -247,15 +246,15 @@ struct Signal /*final*/ : Signal (const CbFunction &method = CbFunction()) : ProtoSignal (method) {} }; -/// This function creates a boost::function by binding @a object to the member function pointer @a method. -template boost::function +/// This function creates a std::function by binding @a object to the member function pointer @a method. +template std::function slot (Instance &object, R (Class::*method) (Args...)) { return [&object, method] (Args... args) { return (object .* method) (args...); }; } -/// This function creates a boost::function by binding @a object to the member function pointer @a method. -template boost::function +/// This function creates a std::function by binding @a object to the member function pointer @a method. +template std::function slot (Class *object, R (Class::*method) (Args...)) { return [object, method] (Args... args) { return (object ->* method) (args...); }; diff --git a/entityx/Entity.h b/entityx/Entity.h index 17e3be0..6b37ed2 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -25,6 +25,7 @@ #include "entityx/config.h" #include "entityx/Event.h" +#include "entityx/help/NonCopyable.h" namespace entityx { @@ -239,7 +240,7 @@ struct ComponentRemovedEvent : public Event> { /** * Manages Entity::Id creation and component assignment. */ -class EntityManager : boost::noncopyable, public enable_shared_from_this { +class EntityManager : entityx::help::NonCopyable, public enable_shared_from_this { public: typedef std::bitset ComponentMask; @@ -252,7 +253,7 @@ class EntityManager : boost::noncopyable, public enable_shared_from_this &, const Entity::Id &)> Predicate; + typedef std::function &, const Entity::Id &)> Predicate; /// A predicate that matches valid entities with the given component mask. class ComponentMaskPredicate { diff --git a/entityx/Event.h b/entityx/Event.h index c375aa1..e60e7d7 100644 --- a/entityx/Event.h +++ b/entityx/Event.h @@ -11,14 +11,12 @@ #pragma once #include -#include -#include -#include -#include #include #include +#include #include "entityx/config.h" #include "entityx/3rdparty/simplesignal.h" +#include "entityx/help/NonCopyable.h" namespace entityx { @@ -104,7 +102,7 @@ class Receiver : public BaseReceiver { * * Subscriptions are automatically removed when receivers are destroyed.. */ -class EventManager : boost::noncopyable { +class EventManager : entityx::help::NonCopyable { public: EventManager(); virtual ~EventManager(); @@ -132,7 +130,7 @@ class EventManager : boost::noncopyable { void subscribe(Receiver &receiver) { //NOLINT void (Receiver::*receive)(const E &) = &Receiver::receive; auto sig = signal_for(E::family()); - auto wrapper = EventCallbackWrapper(boost::bind(receive, &receiver, _1)); + auto wrapper = EventCallbackWrapper(std::bind(receive, &receiver, std::placeholders::_1)); auto connection = sig->connect(wrapper); static_cast(receiver).connections_.push_back( std::make_pair(EventSignalWeakPtr(sig), connection)); @@ -189,12 +187,12 @@ class EventManager : boost::noncopyable { // Functor used as an event signal callback that casts to E. template struct EventCallbackWrapper { - EventCallbackWrapper(boost::function callback) : callback(callback) {} + EventCallbackWrapper(std::function callback) : callback(callback) {} void operator()(const BaseEvent* event) { callback(*(static_cast(event))); } - boost::function callback; + std::function callback; }; - boost::unordered_map handlers_; + std::unordered_map handlers_; }; } // namespace entityx diff --git a/entityx/Manager.h b/entityx/Manager.h index 5151df5..3a7e99a 100644 --- a/entityx/Manager.h +++ b/entityx/Manager.h @@ -10,10 +10,10 @@ #pragma once -#include #include "entityx/Entity.h" #include "entityx/Event.h" #include "entityx/System.h" +#include "entityx/help/Timer.h" namespace entityx { @@ -58,7 +58,7 @@ class Manager { ptr system_manager; private: - boost::timer timer_; + help::Timer timer_; bool running_ = false; }; diff --git a/entityx/System.h b/entityx/System.h index e4dcfec..132b8b9 100644 --- a/entityx/System.h +++ b/entityx/System.h @@ -11,13 +11,13 @@ #pragma once -#include -#include +#include #include #include #include "entityx/config.h" #include "entityx/Entity.h" #include "entityx/Event.h" +#include "entityx/help/NonCopyable.h" namespace entityx { @@ -25,7 +25,7 @@ namespace entityx { /** * Base System class. Generally should not be directly used, instead see System. */ -class BaseSystem : boost::noncopyable { +class BaseSystem : entityx::help::NonCopyable { public: typedef uint64_t Family; @@ -72,7 +72,7 @@ class System : public BaseSystem { }; -class SystemManager : boost::noncopyable, public enable_shared_from_this { +class SystemManager : entityx::help::NonCopyable, public enable_shared_from_this { public: SystemManager(ptr entity_manager, ptr event_manager) : @@ -150,7 +150,7 @@ class SystemManager : boost::noncopyable, public enable_shared_from_this entity_manager_; ptr event_manager_; - boost::unordered_map> systems_; + std::unordered_map> systems_; }; } // namespace entityx diff --git a/entityx/help/NonCopyable.h b/entityx/help/NonCopyable.h new file mode 100644 index 0000000..5f193ee --- /dev/null +++ b/entityx/help/NonCopyable.h @@ -0,0 +1,23 @@ +// Inspired heavily by boost::noncopyable + +#pragma once + +namespace entityx { +namespace help { + +class NonCopyable +{ + protected: + + NonCopyable() = default; + ~NonCopyable() = default; + + + NonCopyable( const NonCopyable& ) = delete; + NonCopyable& operator=( const NonCopyable& ) = delete; + +}; + + +} // namespace help +} // namespace entityx \ No newline at end of file diff --git a/entityx/help/Timer.cc b/entityx/help/Timer.cc new file mode 100644 index 0000000..de25df0 --- /dev/null +++ b/entityx/help/Timer.cc @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2013 Antony Woods + * All rights reserved. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. + * + * Author: Antony Woods + */ + +#include "entityx/help/Timer.h" + +namespace entityx { +namespace help { + +Timer::Timer() +{ + +} + +Timer::~Timer() +{ + +} + +void Timer::restart() +{ + +} + +double Timer::elapsed() +{ + return 1.0; +} + +} // namespace help +} // namespace entityx \ No newline at end of file diff --git a/entityx/help/Timer.h b/entityx/help/Timer.h new file mode 100644 index 0000000..eaf51ab --- /dev/null +++ b/entityx/help/Timer.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Antony Woods + * All rights reserved. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. + * + * Author: Antony Woods + */ + +#pragma once + +namespace entityx { +namespace help { + +class Timer +{ +public: + Timer(); + ~Timer(); + + void restart(); + double elapsed(); +}; + +} // namespace help +} // namespace entityx \ No newline at end of file diff --git a/entityx/python/PythonSystem.cc b/entityx/python/PythonSystem.cc index bb59e7d..032b283 100644 --- a/entityx/python/PythonSystem.cc +++ b/entityx/python/PythonSystem.cc @@ -15,6 +15,7 @@ #include #include #include "entityx/python/PythonSystem.h" +#include "entityx/help/NonCopyableEvent.h" namespace py = boost::python; @@ -103,7 +104,7 @@ BOOST_PYTHON_MODULE(_entityx) { py::class_("Logger", py::no_init) .def("write", &PythonEntityXLogger::write); - py::class_, boost::noncopyable>("BaseEvent", py::no_init); + py::class_, entityx::help::NonCopyable>("BaseEvent", py::no_init); py::class_("Entity", py::init()) .def_readonly("_entity", &PythonEntity::_entity) @@ -121,12 +122,12 @@ BOOST_PYTHON_MODULE(_entityx) { .def("get_component", &get_component) .staticmethod("get_component"); - py::class_, boost::noncopyable>("EntityManager", py::no_init) + py::class_, entityx::help::NonCopyable>("EntityManager", py::no_init) .def("create", &EntityManager::create); void (EventManager::*emit)(const BaseEvent &) = &EventManager::emit; - py::class_, boost::noncopyable>("EventManager", py::no_init) + py::class_, entityx::help::NonCopyable>("EventManager", py::no_init) .def("emit", emit); py::implicitly_convertible(); diff --git a/entityx/tags/TagsComponent.h b/entityx/tags/TagsComponent.h index b208c63..6fcb937 100644 --- a/entityx/tags/TagsComponent.h +++ b/entityx/tags/TagsComponent.h @@ -10,8 +10,8 @@ #pragma once -#include #include +#include #include "entityx/Entity.h" namespace entityx { @@ -50,7 +50,7 @@ class TagsComponent : public Component { return EntityManager::View(view, TagsPredicate(tag)); } - boost::unordered_set tags; + std::unordered_set tags; private: template -- cgit v1.2.3