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 Test : sosc::Pool<User> {
protected:
void ProcessClient(User* client) override;
bool ProcessClient(User* client) override;
};
int main(int argc, char **argv) {

View file

@ -42,6 +42,9 @@ public:
protected:
virtual bool ProcessClient(T* client) = 0;
private:
bool IsStackFull(int stackCount) const;
bool CanAddStack() const;
class Stack {
public:
Stack(Pool<T>* pool);
@ -94,10 +97,50 @@ void Pool<T>::Start() {
}
template<class T>
bool Pool<T>::AddClient(T* client) {
// TODO
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>
bool Pool<T>::AddClient(T* client) {
if(!this->is_running)
return;
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>