本文整理汇总了C++中ObjectPool类的典型用法代码示例。如果您正苦于以下问题:C++ ObjectPool类的具体用法?C++ ObjectPool怎么用?C++ ObjectPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ObjectPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **args)
{
ObjectPool<int> pool;
int &a = pool.acquireObject();
pool.releaseObject(a);
return 0;
}
示例2: test1
void test1()
{
ObjectPool<SmallObject> op;
SmallObject* s = op.get();
op.put(s);
s = op.get();
s = op.get();
s = op.get();
s = op.get();
printf("++++++++++++++++\n");
}
示例3: TEST_F
TEST_F(ObjectPoolTest, MovableObjectPool) {
ObjectPool<int> to;
// Verify a move works correctly when an object has been checked out:
{
ObjectPool<int> from;
auto original = from.Wait();
to = std::move(from);
}
// Now verify that the pooled object returned home to the right place:
ASSERT_EQ(1UL, to.GetCached()) << "Object pool move operation did not correctly relay checked out types";
}
示例4: auxCacheForSrcPhrase
void LexicalReorderingTableTree::auxCacheForSrcPhrase(const Phrase& f)
{
if(m_FactorsE.empty()) {
//f is all of key...
Candidates cands;
m_Table->GetCandidates(MakeTableKey(f,Phrase(ARRAY_SIZE_INCR)),&cands);
m_Cache[MakeCacheKey(f,Phrase(ARRAY_SIZE_INCR))] = cands;
} else {
ObjectPool<PPimp> pool;
PPimp* pPos = m_Table->GetRoot();
//1) goto subtree for f
for(size_t i = 0; i < f.GetSize() && 0 != pPos && pPos->isValid(); ++i) {
/* old code
pPos = m_Table.Extend(pPos, auxClearString(f.GetWord(i).ToString(m_FactorsF)), SourceVocId);
*/
pPos = m_Table->Extend(pPos, f.GetWord(i).GetString(m_FactorsF, false), SourceVocId);
}
if(0 != pPos && pPos->isValid()) {
pPos = m_Table->Extend(pPos, PrefixTreeMap::MagicWord);
}
if(0 == pPos || !pPos->isValid()) {
return;
}
//2) explore whole subtree depth first & cache
std::string cache_key = auxClearString(f.GetStringRep(m_FactorsF)) + "|||";
std::vector<State> stack;
stack.push_back(State(pool.get(PPimp(pPos->ptr()->getPtr(pPos->idx),0,0)),""));
Candidates cands;
while(!stack.empty()) {
if(stack.back().pos->isValid()) {
LabelId w = stack.back().pos->ptr()->getKey(stack.back().pos->idx);
std::string next_path = stack.back().path + " " + m_Table->ConvertWord(w,TargetVocId);
//cache this
m_Table->GetCandidates(*stack.back().pos,&cands);
if(!cands.empty()) {
m_Cache[cache_key + auxClearString(next_path)] = cands;
}
cands.clear();
PPimp* next_pos = pool.get(PPimp(stack.back().pos->ptr()->getPtr(stack.back().pos->idx),0,0));
++stack.back().pos->idx;
stack.push_back(State(next_pos,next_path));
} else {
stack.pop_back();
}
}
}
}
示例5: _g_objPool
namespace KBEngine
{
static ObjectPool<MemoryStream> _g_objPool("MemoryStream");
//-------------------------------------------------------------------------------------
ObjectPool<MemoryStream>& MemoryStream::ObjPool()
{
return _g_objPool;
}
//-------------------------------------------------------------------------------------
void MemoryStream::destroyObjPool()
{
DEBUG_MSG(boost::format("MemoryStream::destroyObjPool(): size %1%.\n") %
_g_objPool.size());
_g_objPool.destroy();
}
//-------------------------------------------------------------------------------------
MemoryStream::SmartPoolObjectPtr MemoryStream::createSmartPoolObj()
{
return SmartPoolObjectPtr(new SmartPoolObject<MemoryStream>(ObjPool().createObject(), _g_objPool));
}
//-------------------------------------------------------------------------------------
}
示例6: _g_objPool
namespace KBEngine
{
static ObjectPool<MemoryStream> _g_objPool("MemoryStream");
//-------------------------------------------------------------------------------------
ObjectPool<MemoryStream>& MemoryStream::ObjPool()
{
return _g_objPool;
}
//-------------------------------------------------------------------------------------
void MemoryStream::destroyObjPool()
{
DEBUG_MSG(fmt::format("MemoryStream::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
//-------------------------------------------------------------------------------------
MemoryStream::SmartPoolObjectPtr MemoryStream::createSmartPoolObj()
{
return SmartPoolObjectPtr(new SmartPoolObject<MemoryStream>(ObjPool().createObject(), _g_objPool));
}
//-------------------------------------------------------------------------------------
size_t MemoryStream::getPoolObjectBytes()
{
size_t bytes = sizeof(rpos_) + sizeof(wpos_) + data_.size();
return bytes;
}
//-------------------------------------------------------------------------------------
}
示例7: main
int main() {
ObjectPool<int32_t, 128, false> pool;
int32_t* a = pool.fetch();
int32_t* b = pool.fetch();
int32_t* c = pool.fetch();
int32_t* d = pool.fetch();
pool.release(b);
int32_t* e = pool.fetch();
fprintf(stdout, "fetch() %p\n", a);
fprintf(stdout, "fetch() %p\n", b);
fprintf(stdout, "fetch() %p\n", c);
fprintf(stdout, "fetch() %p\n", d);
fprintf(stdout, "fetch() %p\n", e);
return 0;
}
示例8: testExclusivity
void ObjectPoolTest::testExclusivity()
{
ObjectPool<Serial> myPool;
const size_t numberOfObjectsToRetrieve = 10;
std::vector<ObjectPool<Serial>::Object> retrievedSerials;
std::set<size_t> seenSerialNumbers;
for (size_t i = 0; i < numberOfObjectsToRetrieve; i++) {
auto nextSerial = myPool.acquireObject();
// Add the retrieved Serial to the vector to keep it 'alive',
// and add the serial number to the set.
retrievedSerials.push_back(nextSerial);
seenSerialNumbers.insert(nextSerial->getSerialNumber());
}
// Assert that all retrieved serial numbers are different.
Assert::AreEqual(numberOfObjectsToRetrieve, seenSerialNumbers.size());
}
示例9: destroyObjPool
//-------------------------------------------------------------------------------------
void MemoryStream::destroyObjPool()
{
DEBUG_MSG(boost::format("MemoryStream::destroyObjPool(): size %1%.\n") %
_g_objPool.size());
_g_objPool.destroy();
}
示例10: destroyObjPool
//-------------------------------------------------------------------------------------
void Bundle::destroyObjPool()
{
DEBUG_MSG(fmt::format("Bundle::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
示例11: runtime_error
void
add (uint32_t id, char side, uint32_t price, uint32_t size)
{
if (orders.end () != orders.find (id, OrderIdHasher (), OrderIdEqual ()))
{
throw std::runtime_error ("duplicate add");
}
AggregateOrders::iterator found = aggregateOrders.find (std::make_pair (side, price), AggregateOrderLess ());
if (found == aggregateOrders.end ())
{
AggregateOrder *a = aggregateOrderPool.construct (boost::in_place (side, price));
found = aggregateOrders.insert (*a).first;
}
assert(aggregateOrders.end () != found);
Order *order = orderPool.construct (boost::in_place (id, boost::ref (*found)));
assert(order);
bool orderInserted __attribute__((unused)) = orders.insert (*order).second;
assert(orderInserted);
order->size = size;
order->aggregateOrder.size += size;
assert(order->size);
assert(order->aggregateOrder.size);
}
示例12: destroyObjPool
//-------------------------------------------------------------------------------------
void UDPPacketReceiver::destroyObjPool()
{
DEBUG_MSG(boost::format("UDPPacketReceiver::destroyObjPool(): size %1%.\n") %
_g_objPool.size());
_g_objPool.destroy();
}
示例13: destroyObjPool
//-------------------------------------------------------------------------------------
void UDPPacketReceiver::destroyObjPool()
{
DEBUG_MSG(fmt::format("UDPPacketReceiver::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
示例14: destroyObjPool
//-------------------------------------------------------------------------------------
void TCPPacket::destroyObjPool()
{
DEBUG_MSG(fmt::format("TCPPacket::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
示例15: destroyObjPool
//-------------------------------------------------------------------------------------
void MemoryStream::destroyObjPool()
{
DEBUG_MSG(fmt::format("MemoryStream::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}