本文整理汇总了C++中ObjectPool::acquireObject方法的典型用法代码示例。如果您正苦于以下问题:C++ ObjectPool::acquireObject方法的具体用法?C++ ObjectPool::acquireObject怎么用?C++ ObjectPool::acquireObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectPool
的用法示例。
在下文中一共展示了ObjectPool::acquireObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **args)
{
ObjectPool<int> pool;
int &a = pool.acquireObject();
pool.releaseObject(a);
return 0;
}
示例2: testRelease
void ObjectPoolTest::testRelease()
{
ObjectPool<Serial> myPool;
const size_t numberOfObjectsToRetrieve = 10;
std::vector<ObjectPool<Serial>::Object> retrievedSerials;
for (size_t i = 0; i < numberOfObjectsToRetrieve; i++) {
// Add the retrieved Serial to the vector to keep it 'alive'.
retrievedSerials.push_back(myPool.acquireObject());
}
size_t lastSerialNumber = retrievedSerials.back()->getSerialNumber();
// Release all objects back to the pool.
retrievedSerials.clear();
// The above loop has created ten Serial objects, with serial
// numbers 0-9, and released all ten Serial objects back to the pool.
// The next loop first again retrieves ten Serial objects. The serial
// numbers of these should all be <= lastSerialNumber.
// The Serial object acquired after that should have a new serial number.
bool wronglyNewObjectCreated = false;
for (size_t i = 0; i < numberOfObjectsToRetrieve; i++) {
auto nextSerial = myPool.acquireObject();
if (nextSerial->getSerialNumber() > lastSerialNumber) {
wronglyNewObjectCreated = true;
break;
}
retrievedSerials.push_back(nextSerial);
}
// Acquire one more Serial object, which should have a serial
// number > lastSerialNumber.
auto anotherSerial = myPool.acquireObject();
bool newObjectCreated = (anotherSerial->getSerialNumber() > lastSerialNumber);
Assert::IsFalse(wronglyNewObjectCreated);
Assert::IsTrue(newObjectCreated);
}
示例3: 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());
}
示例4: testAcquire
void ObjectPoolTest::testAcquire()
{
ObjectPool<Serial> myPool;
shared_ptr<Serial> serial = myPool.acquireObject();
CPPUNIT_ASSERT(serial->getSerialNumber() >= 0);
}
示例5: testAcquire
void ObjectPoolTest::testAcquire()
{
ObjectPool<Serial> myPool;
auto serial = myPool.acquireObject();
Assert::IsTrue(serial->getSerialNumber() >= 0);
}