#pragma once #include #include #include template::value>> class SpriteContainer { public: explicit SpriteContainer(std::initializer_list&& types, std::unique_ptr&& factory, std::size_t reserve_size = 20) : _sprite_factory(std::move(factory)), _poll_reserve_size(reserve_size) { for (const Type& type : types) reallocatePoll(type); } inline std::shared_ptr getSprite(Type type) { SpritePoll& poll = _sprite_dispatcher.at(type); if (poll.empty()) reallocatePoll(type); std::shared_ptr sprite = poll.top(); poll.pop(); return sprite; } inline void resetSprite(const std::shared_ptr &sprite, Type action) noexcept { _sprite_dispatcher[action].push(sprite); } ~SpriteContainer(){} private: inline void reallocatePoll(Type sprite_type) { SpritePoll &poll = _sprite_dispatcher[sprite_type]; for (std::size_t i = 0; i < _poll_reserve_size; ++i) { poll.push(_sprite_factory->create(sprite_type)); } } using SpritePoll = std::stack>; std::map _sprite_dispatcher; std::unique_ptr _sprite_factory; std::size_t _poll_reserve_size; };