本文整理汇总了C++中Autowired类的典型用法代码示例。如果您正苦于以下问题:C++ Autowired类的具体用法?C++ Autowired怎么用?C++ Autowired使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Autowired类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST_F
TEST_F(AutoInjectableTest, VerifySimpleThreadWait) {
// Immediate kickoff:
AutoCurrentContext()->Initiate();
// Make an injectable, run it, and stuff it right into a future:
AutoFuture future;
MakeInjectable<CoreThread>()(&future);
// Make a thread and then start it going:
Autowired<CoreThread> thread;
ASSERT_TRUE(thread.IsAutowired()) << "Thread was not injected by an injector as expected";
AutoRequired<std::mutex> barr;
{
std::lock_guard<std::mutex> lk(*barr);
// Add a lambda that we intentionally block:
*thread += [] {
Autowired<CoreThread> thread;
Autowired<std::mutex> barr;
ASSERT_TRUE(thread && barr) << "Failed to find a required type in the current context";
std::lock_guard<std::mutex> lk(*barr);
thread->Stop();
};
// Instant wait:
ASSERT_FALSE(future.WaitFor(std::chrono::nanoseconds(1))) << "Premature wait return on an injector-provided future";
}
// Now that the thread is unblocked, verify that it quits:
ASSERT_TRUE(future.WaitFor(std::chrono::seconds(5))) << "Wait failed to return on an injector-provided future";
}
示例2: AutoGlobalContext
void AutowiringEnclosure::OnTestEnd(const testing::TestInfo& info) {
// Verify we can grab the test case back out and that the pointer is correct:
Autowired<TestInfoProxy> ti;
Autowired<AutowiringEnclosureExceptionFilter> ecef;
auto ctxt = ecef ? ecef->GetContext() : nullptr;
// Unconditionally reset the global context as the current context
AutoGlobalContext()->SetCurrent();
// Global initialization tests are special, we don't bother checking closure principle on them:
if(!strcmp("GlobalInitTest", info.test_case_name()))
return;
// Need to make sure we got back our exception filter before continuing:
ASSERT_TRUE(ecef.IsAutowired()) << "Failed to find the enclosed context exception filter; unit test may have incorrectly reset the enclosing context before returning";
// Now try to tear down the test context enclosure:
ctxt->SignalShutdown();
// Do not allow teardown to take more than 250 milliseconds or so
if(!ctxt->Wait(std::chrono::milliseconds(250))) {
// Critical error--took too long to tear down
assert(false);
}
static const char s_autothrow[] = "AUTOTHROW_";
if(!strncmp(s_autothrow, info.name(), ARRAYCOUNT(s_autothrow) - 1))
// Throw expected, end here
return;
// If an exception occurred somewhere, report it:
ASSERT_FALSE(ecef->m_excepted)
<< "An unhandled exception occurred in this context" << std::endl
<< "[" << (ecef->m_ti ? ecef->m_ti->name() : "unknown") << "] " << ecef->m_what;
}
示例3: TEST_F
TEST_F(CoreContextTest, CoreContextAdd) {
auto myClass = std::make_shared<CoreContextAddTestClass>();
AutoCurrentContext ctxt;
ctxt->Add(myClass);
Autowired<CoreContextAddTestClass> mc;
ASSERT_TRUE(mc.IsAutowired()) << "Manually registered interface was not detected as expected";
}
示例4: TEST_F
TEST_F(PostConstructTest, DelayedNotifyWhenAutowired) {
// Autorequire, and add a registration:
bool called = false;
Autowired<SimpleObject> so;
so.NotifyWhenAutowired([&called] { called = true; });
// Inject a type after:
AutoRequired<SimpleObject>();
ASSERT_TRUE(called) << "An autowiring notification was not invoked on an already-satisfied field as expected";
}
示例5: TEST_F
TEST_F(ContextMemberTest, PathologicalResetCase) {
Autowired<TypeThatIsNotInjected>* pv;
volatile std::atomic<size_t> nBarr{ 0 };
volatile bool proceed = true;
volatile bool go = false;
auto resetsV = [&] {
while(proceed) {
++nBarr;
while (proceed && !go)
std::this_thread::yield();
if (!proceed)
break;
pv->reset();
--nBarr;
while (proceed && go)
std::this_thread::yield();
}
};
std::thread a{ resetsV };
std::thread b{ resetsV };
for (size_t i = 1000; i--;) {
Autowired<TypeThatIsNotInjected> v;
pv = &v;
for (size_t j = 10; j--;)
v.NotifyWhenAutowired([] {});
// Bump the threads to spin around:
while (nBarr != 2)
std::this_thread::yield();
go = true;
while (nBarr)
std::this_thread::yield();
go = false;
}
proceed = false;
a.join();
b.join();
}
示例6: main
int main() {
// Initiate the current context
AutoCurrentContext()->Initiate();
// Inject our AutoFilter enabled context members into the context
AutoRequired<StringFilter>();
AutoRequired<StringIntFilter>();
// Each context automatically includes an AutoPacketFactory type. This
// can be used to create new packets in the AutoFilter network
Autowired<AutoPacketFactory> factory;
// When decorating a packet, all AutoFilters that have that type as an argument
// will be called. It is only called when all argument types have been decorated
auto packet = factory->NewPacket();
packet->Decorate(std::string("Hello World"));
std::cout << "StringIntFilter not called yet" << std::endl;
// StringIntFilter will now be called since all AutoFilter arguments have ben decorated
packet->Decorate(42);
}
示例7: main
int main () {
// Initialization of context, injection of context members, obtaining access to the factory.
AutoCurrentContext()->Initiate();
AutoRequired<Hippo1> hippo1;
AutoRequired<Hippo2> hippo2;
AutoRequired<Hippo3> hippo3;
AutoRequired<Hippo4> hippo4;
AutoRequired<Hippo5> hippo5;
AutoRequired<Hippo6> hippo6;
Autowired<AutoPacketFactory> factory;
// Declare a packet to use in the following code blocks.
std::shared_ptr<AutoPacket> packet;
/// At this point we will execute the filter network a number of times, each with a different type related to `std::string`,
/// in order to observe which filters are executed. The first six executions demonstrate the types that are equivalent to
/// `std::string` as input parameters (in which we expect only `Hippo#::AutoFilter` to be called).
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `std::string`:\n";
std::string s("std::string");
packet->Decorate(s);
std::cout << '\n';
}
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `const std::string`:\n";
const std::string s("const std::string");
packet->Decorate(s);
std::cout << '\n';
}
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `std::shared_ptr<std::string>`:\n";
std::shared_ptr<std::string> s = std::make_shared<std::string>("std::shared_ptr<std::string>");
packet->Decorate(s);
std::cout << '\n';
}
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `const std::shared_ptr<std::string>`:\n";
const std::shared_ptr<std::string> s = std::make_shared<std::string>("const std::shared_ptr<std::string>");
packet->Decorate(s);
std::cout << '\n';
}
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `std::shared_ptr<const std::string>`:\n";
std::shared_ptr<const std::string> s = std::make_shared<const std::string>("std::shared_ptr<const std::string>");
packet->Decorate(s);
std::cout << '\n';
}
{
packet = factory->NewPacket();
std::cout << "Decorating packet with instance of `const std::shared_ptr<const std::string>`:\n";
const std::shared_ptr<const std::string> s = std::make_shared<const std::string>("const std::shared_ptr<const std::string>");
packet->Decorate(s);
std::cout << '\n';
}
/// Verify that the accumulated values in the `m_received_input_decoration_types` for each context member are what we expect.
{
std::unordered_set<std::string> expected_hippo_inputs({
"std::string",
"const std::string",
"std::shared_ptr<std::string>",
"const std::shared_ptr<std::string>",
"std::shared_ptr<const std::string>",
"const std::shared_ptr<const std::string>"
});
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo1 did not receive the expected inputs.");
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo2 did not receive the expected inputs.");
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo3 did not receive the expected inputs.");
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo4 did not receive the expected inputs.");
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo5 did not receive the expected inputs.");
if (hippo1->m_received_input_decoration_types != expected_hippo_inputs)
throw std::runtime_error("Hippo6 did not receive the expected inputs.");
std::cout << "All Hippo# context members received the expected inputs.\n";
}
std::cout << "All verifications passed.\n";
return 0; // Return with no error.
}
示例8: Run
void Run(void) override {
// Wait for our event to be signalled, then leave
m_signal->Delay();
}