当前位置: 首页>>代码示例>>C++>>正文


C++ Registry::getElementsCopy方法代码示例

本文整理汇总了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);
}
开发者ID:evildr,项目名称:Util,代码行数:100,代码来源:RegistryTest.cpp


注:本文中的Registry::getElementsCopy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。