本文整理汇总了C++中Registry::getElementsCopy方法的典型用法代码示例。如果您正苦于以下问题:C++ Registry::getElementsCopy方法的具体用法?C++ Registry::getElementsCopy怎么用?C++ Registry::getElementsCopy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Registry
的用法示例。
在下文中一共展示了Registry::getElementsCopy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testChangesDuringExecution
void RegistryTest::testChangesDuringExecution() {
typedef Util::Registry<std::list<std::function<void ()>>> Registry;
Registry funcRegistry;
typedef Registry::handle_t RegistryHandle;
CPPUNIT_ASSERT(funcRegistry.getElements().empty());
// Register a function that registers a function when called.
std::unique_ptr<RegistryHandle> optionalHandleB;
auto handleA = funcRegistry.registerElement(
[&funcRegistry, &optionalHandleB] {
optionalHandleB.reset(new RegistryHandle(funcRegistry.registerElement([] {})));
}
);
// Register a second function that registers a function when called.
std::unique_ptr<RegistryHandle> optionalHandleD;
auto handleC = funcRegistry.registerElement(
[&funcRegistry, &optionalHandleD] {
optionalHandleD.reset(new RegistryHandle(funcRegistry.registerElement([] {})));
}
);
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), funcRegistry.getElements().size());
CPPUNIT_ASSERT(!optionalHandleB);
CPPUNIT_ASSERT(!optionalHandleD);
for(const auto & func : funcRegistry.getElementsCopy()) {
func();
}
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(4), funcRegistry.getElements().size());
CPPUNIT_ASSERT(optionalHandleB);
CPPUNIT_ASSERT(optionalHandleD);
funcRegistry.unregisterElement(std::move(handleC));
funcRegistry.unregisterElement(std::move(handleA));
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), funcRegistry.getElements().size());
CPPUNIT_ASSERT(optionalHandleB);
CPPUNIT_ASSERT(optionalHandleD);
// Register a function that removes a function when called.
auto handleE = funcRegistry.registerElement(
[&funcRegistry, &optionalHandleB] {
funcRegistry.unregisterElement(std::move(*optionalHandleB.get()));
optionalHandleB.reset();
}
);
// Register a second function that removes a function when called.
auto handleF = funcRegistry.registerElement(
[&funcRegistry, &optionalHandleD] {
funcRegistry.unregisterElement(std::move(*optionalHandleD.get()));
optionalHandleD.reset();
}
);
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(4), funcRegistry.getElements().size());
CPPUNIT_ASSERT(optionalHandleB);
CPPUNIT_ASSERT(optionalHandleD);
for(const auto & func : funcRegistry.getElementsCopy()) {
func();
}
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), funcRegistry.getElements().size());
CPPUNIT_ASSERT(!optionalHandleB);
CPPUNIT_ASSERT(!optionalHandleD);
funcRegistry.unregisterElement(std::move(handleF));
funcRegistry.unregisterElement(std::move(handleE));
CPPUNIT_ASSERT(funcRegistry.getElements().empty());
// Register a function that removes itself from the registry when called.
optionalHandleB.reset(new RegistryHandle(funcRegistry.registerElement(
[&funcRegistry, &optionalHandleB] {
funcRegistry.unregisterElement(std::move(*optionalHandleB.get()));
optionalHandleB.reset();
}
)));
// Register a second function that removes itself from the registry when called.
optionalHandleD.reset(new RegistryHandle(funcRegistry.registerElement(
[&funcRegistry, &optionalHandleD] {
funcRegistry.unregisterElement(std::move(*optionalHandleD.get()));
optionalHandleD.reset();
}
)));
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), funcRegistry.getElements().size());
CPPUNIT_ASSERT(optionalHandleB);
CPPUNIT_ASSERT(optionalHandleD);
for(const auto & func : funcRegistry.getElementsCopy()) {
func();
}
CPPUNIT_ASSERT(funcRegistry.getElements().empty());
CPPUNIT_ASSERT(!optionalHandleB);
CPPUNIT_ASSERT(!optionalHandleD);
}