wavelength 1.0.0
Loading...
Searching...
No Matches
fxpool.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
14template <class T, bool bArg>
15class FxPool {
16public:
24 explicit FxPool(int nCount) : mCount(nCount), mItems(std::make_unique<T[]>(nCount)) {
25 mFree = mItems.get();
26 for (int i = 0; i < nCount; ++i) {
27 mFree[i].SetNextFree(i == nCount - 1 ? nullptr : &mFree[i + 1]);
28 Prepare(&mFree[i]);
29 }
30 }
31
41 void Prepare([[maybe_unused]] T *pItem) {
42 }
43
50 virtual ~FxPool() = default;
51
60 if (mCount == 0) {
61 return nullptr;
62 }
63 T *const pItems = mItems.get();
64 T *pOldest = pItems;
65 for (T *pItem = pItems + 1; pItem != pItems + mCount; ++pItem) {
66 if (pItem->GetStartTime() < pOldest->GetStartTime()) {
67 pOldest = pItem;
68 }
69 }
70 return pOldest;
71 }
72
80 T *Pop() {
81 T *pItem = mFree;
82 if (pItem == nullptr) {
83 return nullptr;
84 }
85 mFree = pItem->GetNextFree();
86 pItem->SetNextFree(nullptr);
87 return pItem;
88 }
89
98 template <class... Args>
99 T *Start(Args... args) {
100 T *pItem = Pop();
101 if (pItem != nullptr) {
102 pItem->Start(args...);
103 }
104 return pItem;
105 }
106
114 void Release(T *pItem) {
115 pItem->Stop();
116 pItem->SetNextFree(mFree);
117 mFree = pItem;
118 }
119
127 void Free(T *pItem) {
128 Release(pItem);
129 }
130
131protected:
132 int mCount;
133 T *mFree;
134 std::unique_ptr<T[]> mItems;
135};
virtual ~FxPool()=default
Destroy the effects.
void Prepare(T *pItem)
Prepare a new effect of the pool.
Definition fxpool.h:41
int mCount
Definition fxpool.h:132
void Free(T *pItem)
Return an effect to the pool.
Definition fxpool.h:127
T * mFree
Definition fxpool.h:133
std::unique_ptr< T[]> mItems
Definition fxpool.h:134
T * Pop()
Take an effect off the free list.
Definition fxpool.h:80
T * Start(Args... args)
Take an effect off the free list and start it.
Definition fxpool.h:99
void Release(T *pItem)
Stop an effect and return it to the free list.
Definition fxpool.h:114
T * FindOldest()
Report the effect that started first.
Definition fxpool.h:59
FxPool(int nCount)
Construct a pool of effects, every one of them free.
Definition fxpool.h:24