cool and cute pool

This commit is contained in:
malloc 2018-04-16 13:24:12 -05:00
parent 3ce74b2312
commit 8976efe29b
2 changed files with 46 additions and 3 deletions

View file

@ -18,7 +18,7 @@
class User; class User;
class Test : sosc::Pool<User> { class Test : sosc::Pool<User> {
protected: protected:
void ProcessClient(User* client) override; bool ProcessClient(User* client) override;
}; };
int main(int argc, char **argv) { int main(int argc, char **argv) {

View file

@ -42,6 +42,9 @@ public:
protected: protected:
virtual bool ProcessClient(T* client) = 0; virtual bool ProcessClient(T* client) = 0;
private: private:
bool IsStackFull(int stackCount) const;
bool CanAddStack() const;
class Stack { class Stack {
public: public:
Stack(Pool<T>* pool); Stack(Pool<T>* pool);
@ -93,11 +96,51 @@ void Pool<T>::Start() {
this->is_running = true; this->is_running = true;
} }
template<class T>
bool Pool<T>::IsStackFull(int stackCount) const {
poolinfo_t *info = &this->info;
return info->max_size != -1
&& stackCount <
info->initial_size
+ (info->size_growth
* (this->stacks.size() - info->initial_count))
&& stackCount < info->max_size;
}
template<class T>
bool Pool<T>::CanAddStack() const {
return this->info.max_count == -1
|| this->stacks.size() < this->info.max_count;
}
template<class T> template<class T>
bool Pool<T>::AddClient(T* client) { bool Pool<T>::AddClient(T* client) {
// TODO if(!this->is_running)
return;
return false; if(this->info.max_total != -1)
if(this->ClientCount() >= this->info.max_total)
return false;
int lowestCount = -1;
Stack* lowestStack = nullptr;
for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) {
int thisCount;
if((thisCount = i->ClientCount()) > lowestCount) {
lowestCount = thisCount;
lowestStack = &(*i);
}
}
if(lowestStack != nullptr && !this->IsStackFull(lowestCount))
lowestStack->AddClient(client);
else if(this->CanAddStack()) {
this->stacks.push_back(Stack(this));
this->stacks.back().AddClient(client);
} else
return false;
return true;
} }
template<class T> template<class T>