Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
ObjectPool.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <mutex>
5#include <shared_mutex>
6#include <unordered_map>
7
8namespace sgui
9{
14template <typename Object, typename ObjectId = uint32_t>
16{
17public:
21 ObjectPool () = default;
31 Object& add (Object&& object, const ObjectId& id);
32 Object& add (const Object& object, const ObjectId& id);
38 template <typename... ObjectArgs>
39 Object& emplace (const ObjectId& id, ObjectArgs&&... constructorArgs);
44 void remove (const ObjectId& object);
49 void reserve (const size_t amount);
53 void clear ();
58 bool has (const ObjectId& object) const;
63 bool empty () const;
69 Object& get (const ObjectId& object);
70 const Object& get (const ObjectId& object) const;
76 void changeId (const ObjectId& oldId, const ObjectId& newId);
80 std::vector<ObjectId> ids () const;
85 size_t size () const;
89 auto begin () { return std::begin (mObjects); }
90 auto end () { return std::end (mObjects); }
91 auto begin () const { return std::cbegin (mObjects); }
92 auto end () const { return std::cend (mObjects); }
93private:
94 void trackSlot (const ObjectId& slot);
95private:
96 using MutexType = std::shared_timed_mutex;
97 using ReadLock = std::shared_lock <MutexType>;
98 using WriteLock = std::unique_lock <MutexType>;
99 mutable MutexType mMutex;
100 std::unordered_map <ObjectId, size_t> mIdToSlot;
101 std::vector <ObjectId> mSlotToId;
102 std::vector <Object> mObjects;
103};
104
105} // namespace sgui
106
ObjectPool & operator=(const ObjectPool< Object, ObjectId > &rhs)
auto end() const
Definition ObjectPool.h:92
auto begin()
Definition ObjectPool.h:89
void changeId(const ObjectId &oldId, const ObjectId &newId)
Change id of a stored object.
const Object & get(const ObjectId &object) const
Object & add(const Object &object, const ObjectId &id)
size_t size() const
Get count of pooled objects.
void remove(const ObjectId &object)
Remove a specific object from pool.
std::vector< ObjectId > ids() const
Get list of all identifiers used to store object.
Object & emplace(const ObjectId &id, ObjectArgs &&... constructorArgs)
Build a new object in pool.
void clear()
Clear data in object pool.
ObjectPool(ObjectPool< Object, ObjectId > &&rhs)
ObjectPool(const ObjectPool< Object, ObjectId > &rhs)
ObjectPool & operator=(ObjectPool< Object, ObjectId > &&rhs)
ObjectPool()=default
auto end()
Definition ObjectPool.h:90
bool empty() const
Test if pool is empty.
Object & get(const ObjectId &object)
Get oject in pool.
auto begin() const
Definition ObjectPool.h:91
Object & add(Object &&object, const ObjectId &id)
Add object in pool.
bool has(const ObjectId &object) const
Test if an object exists in pool.
void reserve(const size_t amount)
Reserve some amount of memory.
Definition Interpolation.h:16