57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include "core/bts_core/utils/bts_no_copy.h"
|
|
#include "Shm/FixedSecurity.h"
|
|
#include "../bts_conn_exports.h"
|
|
#include <memory>
|
|
#include <functional>
|
|
#include <atomic>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
|
|
class BTS_EXPORT SecurityConstructor {
|
|
// An object "pool" optimized for the case where only one instance is ever used at a time
|
|
template <class T>
|
|
class SingleObjectPool<T> {
|
|
public:
|
|
using ptr_type = std::unique_ptr<T, std::function<void(T*)>>;
|
|
ptr_type acquire() {
|
|
T* pt = _inst ? _inst.release() : new T();
|
|
return ptr_type(pt, [this](T* ptr) {
|
|
return_inst(std::unique_ptr<T>(ptr));
|
|
});
|
|
}
|
|
private:
|
|
void return_inst(std::unique_ptr<T> t) {
|
|
if (!_inst) {
|
|
reset(t);
|
|
_inst = std::move(t);
|
|
}
|
|
}
|
|
unique_ptr<T> _inst;
|
|
};
|
|
|
|
public:
|
|
SecurityConstructor() : _future(), _option(), _spread() {}
|
|
|
|
SingleObjectPool<FixedFutureSecurity>& future() { return _future; }
|
|
SingleObjectPool<FixedOptionSecurity>& option() { return _option; }
|
|
SingleObjectPool<FixedSpreadSecurityL>& spread() { return _spread; }
|
|
|
|
private:
|
|
template <typename T>
|
|
void reset(T& t) {
|
|
new (&t)T();
|
|
}
|
|
|
|
SingleObjectPool<FixedFutureSecurity> _future;
|
|
SingleObjectPool<FixedOptionSecurity> _option;
|
|
SingleObjectPool<FixedSpreadSecurityL> _spread;
|
|
};
|
|
|
|
|
|
}}//namespace
|
|
|