38 lines
842 B
C++
38 lines
842 B
C++
|
#ifndef PKMNLIB_CONTEXTPOOL_HPP
|
||
|
#define PKMNLIB_CONTEXTPOOL_HPP
|
||
|
|
||
|
#include <angelscript.h>
|
||
|
#include <vector>
|
||
|
|
||
|
class ContextPool {
|
||
|
std::vector<asIScriptContext*> _pool;
|
||
|
asIScriptEngine* _engine;
|
||
|
|
||
|
public:
|
||
|
ContextPool(asIScriptEngine* engine) : _engine(engine) {}
|
||
|
|
||
|
~ContextPool(){
|
||
|
for (auto ctx: _pool){
|
||
|
ctx->Release();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
asIScriptContext* RequestContext() {
|
||
|
// Get a context from the pool, or create a new
|
||
|
asIScriptContext* ctx = nullptr;
|
||
|
if (!_pool.empty()) {
|
||
|
ctx = *_pool.rbegin();
|
||
|
_pool.pop_back();
|
||
|
} else
|
||
|
ctx = _engine->CreateContext();
|
||
|
return ctx;
|
||
|
}
|
||
|
|
||
|
void ReturnContextToPool(asIScriptContext* ctx) {
|
||
|
_pool.push_back(ctx);
|
||
|
ctx->Unprepare();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
#endif // PKMNLIB_CONTEXTPOOL_HPP
|