本文整理汇总了C++中AutoRequired类的典型用法代码示例。如果您正苦于以下问题:C++ AutoRequired类的具体用法?C++ AutoRequired怎么用?C++ AutoRequired使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AutoRequired类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST_F
TEST_F(AutoPacketFactoryTest, AutoPacketStatistics) {
// Create a context, fill it up, kick it off:
AutoCurrentContext ctxt;
AutoRequired<DelaysAutoPacketsOneMS> dapoms;
AutoRequired<AutoPacketFactory> factory;
ctxt->Initiate();
int numPackets = 20;
// Send 20 packets which should all be delayed 1ms
for (int i = 0; i < numPackets; ++i) {
auto packet = factory->NewPacket();
packet->Decorate(i);
}
// Shutdown our context, and rundown our factory
ctxt->SignalShutdown();
factory->Wait();
// Ensure that the statistics are not too wrong
// We delayed each packet by one ms, and our statistics are given in nanoseconds
double packetDelay = (double) std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::milliseconds(1)).count();
ASSERT_EQ(numPackets, factory->GetTotalPacketCount()) << "The factory did not get enough packets";
ASSERT_LE(packetDelay, factory->GetMeanPacketLifetime()) << "The mean packet lifetime was less than the delay on each packet";
}
示例2: TEST_F
TEST_F(DecoratorTest, VerifyDecoratorAwareness) {
auto filter = [](const Decoration<0>& zero, const Decoration<1>& one) {};
// Create a packet while the factory has no subscribers:
AutoRequired<AutoPacketFactory> factory;
auto packet1 = factory->NewPacket();
// Verify subscription-free status:
ASSERT_FALSE(packet1->HasSubscribers<Decoration<0>>()) << "Subscription exists where one should not have existed";
// Create another packet where a subscriber exists:
*factory += filter;
auto packet2 = factory->NewPacket();
// Verify the first packet still does not have subscriptions:
ASSERT_THROW(packet1->GetSatisfaction<decltype(filter)>(), autowiring_error) << "Subscription was incorrectly, retroactively added to a packet";
ASSERT_FALSE(packet1->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";
ASSERT_EQ(0UL, packet1->GetDecorationTypeCount()) << "Subscription was incorrectly, retroactively added to a packet";
ASSERT_FALSE(packet1->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";
// Verify the second one does:
ASSERT_NO_THROW(packet2->GetSatisfaction<decltype(filter)>()) << "Packet lacked an expected subscription";
ASSERT_EQ(2UL, packet2->GetDecorationTypeCount()) << "Incorrect count of expected decorations";
ASSERT_TRUE(packet2->HasSubscribers<Decoration<0>>()) << "Packet lacked an expected subscription";
}
示例3: main
int main(){
// The 2 main thread classes in Autowiring are the BasicThread and CoreThread.
// Classes that inherit from these types will have thread capabilities
// Both start when their enclosing context is 'initiated'. Threads injected
// after the context is initiated will start immediatly
AutoRequired<MyBasicThread> myBasic;
AutoCurrentContext ctxt;
ctxt->Initiate(); // myBasic->Run() starts now in its own thread
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::cout << "injecting a CoreThread" << std::endl;
// Types inheriting from CoreThread implement a dispatch queue in their 'run()'
// function. Lambdas can be appended with operator+=
AutoRequired<MyCoreThread> myCore;
myCore->AddToQueue(42);
myCore->AddToQueue(1337);
*myCore += []{
std::cout << "This gets run after '1337'" << std::endl;
};
// This should be run before 'myCore' is finished
std::cout << "This thread is faster\n";
// This will wait for all outstanding threads to finish before terminating the context
ctxt->SignalShutdown(true);
}
示例4: TEST_F
TEST_F(ContextCreatorTest, ClearAndTeardown) {
// Create a context and verify it gets evicted from the context creator:
std::weak_ptr<CoreContext> ctxtWeak;
{
AutoCreateContext mainContext;
CurrentContextPusher pusher(mainContext);
AutoRequired<Creator> creator;
std::shared_ptr<CoreContext> ctxt;
// Make a sub-context
ctxt = creator->CreateContext(0).first;
// Obtain a weak pointer, in order to ensure proper teardown:
ctxtWeak = ctxt;
//Call clear on the creator.
creator->Clear(true);
//Make another one!
ctxt = creator->CreateContext(1).first;
//Let the creator go out of scope
}
// Context must be destroyed as a precondition of the subsequent assertion
ASSERT_TRUE(ctxtWeak.expired()) << "Expected the context to be destroyed";
// Verify that our creator is now empty:
// ASSERT_EQ(0UL, creator->GetSize()) << "Context creator is non-empty after all created contexts were destroyed";
}
示例5: IssuesPacketWaitsThenQuits
IssuesPacketWaitsThenQuits(void) {
// Note: Don't do this in practice. This only works because we only inject this type
// into a context that's already running; normally, creating a packet from our ctor can
// cause an exception if we are being injected before Initiate is called.
AutoRequired<AutoPacketFactory> factory;
m_packet = factory->NewPacket();
}
示例6: TEST_F
TEST_F(ContextMapTest, VerifyWithThreadsPathological) {
ContextMap<size_t> mp;
// Context collection and exit race threads:
vector<std::shared_ptr<CoreContext>> contexts;
// Exit race controller:
AutoRequired<ExitRaceSignal> signal;
// Create a number of dependent contexts:
for(size_t i = 0; i < 100; i++) {
AutoCreateContext context;
contexts.push_back(context);
// Store a shared pointer
mp.Add(i, context);
// Start the context
context->Initiate();
}
// Set the signal:
signal->Signal();
// Verify that the map empties once our zero-count is hit:
for(size_t i = 0; i < contexts.size(); i++) {
contexts[i]->SignalShutdown(true);
}
// Clear the context collection:
contexts.clear();
EXPECT_EQ(0UL, mp.size()) << "Context map did not empty as expected";
}
示例7: TEST_F
TEST_F(DecoratorTest, VerifySimplePacketDecoration) {
AutoRequired<AutoPacketFactory> factory;
// Create the packet we will be persisting:
auto packet = factory->NewPacket();
// Add a few decorations on this packet:
auto& knownDec0 = packet->Decorate(Decoration<0>());
auto& knownDec1 = packet->Decorate(Decoration<1>());
auto& knownDec2 = packet->Decorate(Decoration<2>());
// Verify we can get these packets back--might throw exceptions here!
auto& dec0 = packet->Get<Decoration<0>>();
auto& dec1 = packet->Get<Decoration<1>>();
auto& dec2 = packet->Get<Decoration<2>>();
// Verify identities:
EXPECT_EQ(&knownDec0, &dec0) << "Decoration 0 returned at an incorrect location";
EXPECT_EQ(&knownDec1, &dec1) << "Decoration 1 returned at an incorrect location";
EXPECT_EQ(&knownDec2, &dec2) << "Decoration 2 returned at an incorrect location";
// Verify content correctness:
EXPECT_EQ(0, dec0.i) << "Decoration 0 incorrectly persisted";
EXPECT_EQ(1, dec1.i) << "Decoration 1 incorrectly persisted";
EXPECT_EQ(2, dec2.i) << "Decoration 2 incorrectly persisted";
}
示例8: main
int main(){
AutoCurrentContext ctxt;
// A context must be initiated before events can be received. Similar to CoreThread
ctxt->Initiate();
// This creates an proxy object that can fire MyEvent::* events
AutoFired<MyEvent> eventFirer;
// Inject receiver types into current context
AutoRequired<FooEvent> foo;
AutoRequired<BarEvent> bar;
std::cout << "Foo should be 0: " << foo->getSecret() << std::endl;
std::cout << "Bar Should be 0: " << bar->getSecret() << std::endl;
// Fire event, this should set m_secret on both foo and bar
eventFirer(&MyEvent::myFunction)(42);
std::cout << "Foo should be 42: " << foo->getSecret() << std::endl;
std::cout << "Bar should be 42: " << bar->getSecret() << std::endl;
// You can also manually fire events on a context with `Invoke`
// Since the function pointer is to `BarEvent`, `FooEvent` won't receive the event
ctxt->Invoke(&BarEvent::myFunction)(77);
std::cout << "Foo should be 42: " << foo->getSecret() << std::endl;
std::cout << "Bar should be 77: " << bar->getSecret() << std::endl;
}
示例9: TEST_F
TEST_F(ObjectPoolTest, VerifyAsynchronousUsage) {
AutoCreateContext ctxt;
CurrentContextPusher pshr(ctxt);
AutoRequired<SimpleThreadedT<PooledObject>> obj;
AutoFired<SharedPtrReceiver<PooledObject>> spr;
ObjectPool<PooledObject> pool(3);
{
// Obtain the pool limit in objects:
std::shared_ptr<PooledObject> obj1, obj2, obj3;
pool(obj1);
pool(obj2);
pool(obj3);
ASSERT_TRUE(nullptr != obj1.get()) << "Failed to obtain an entry from a new object pool";
// Block--verify that we _do not_ get any of those objects back while they are
// still outstanding.
{
auto obj4 = pool.WaitFor(std::chrono::milliseconds(1));
EXPECT_TRUE(obj4 == nullptr) << "Pool issued another element even though it should have hit its outstanding limit";
}
// Now we kick off threads:
AutoCurrentContext()->Initiate();
// Fire off a few events:
spr(&SharedPtrReceiver<PooledObject>::OnEvent)(obj1);
spr(&SharedPtrReceiver<PooledObject>::OnEvent)(obj2);
spr(&SharedPtrReceiver<PooledObject>::OnEvent)(obj3);
}
// This should return more or less right away as objects become available:
{
auto obj4 = pool.WaitFor(std::chrono::milliseconds(10));
EXPECT_TRUE(obj4 != nullptr) << "Object pool failed to be notified that it received a new element";
}
// Cause the thread to quit:
*obj += [&obj] { obj->Stop(); };
obj->Wait();
}
示例10: TEST_F
TEST_F(CoreContextTest, UnlinkOnTeardown) {
std::weak_ptr<ClassThatPoints1> weakA;
std::shared_ptr<ClassThatPoints2> strongB;
AutoRequired<SimpleObject> so;
// Set up a subcontext with some cycles and external links:
{
// Main context that gets reset second
AutoCreateContext ctxt;
// Sibling context that we're also going to reset
AutoCreateContext otherContext;
otherContext->Inject<std::vector<int>>();
AutoRequired<ClassThatPoints1> a(ctxt);
AutoRequired<ClassThatPoints2> b(ctxt, otherContext);
weakA = a;
strongB = b;
ASSERT_TRUE(a->so.IsAutowired()) << "Root object pointer not correctly obtained";
ASSERT_TRUE(b->so.IsAutowired()) << "Root object pointer not correctly obtained";
ctxt->onTeardown +=
[weakA, strongB] (const CoreContext&) {
// Verify that nothing got screwed up at this point:
auto a = weakA.lock();
ASSERT_FALSE(weakA.expired()) << "Weak pointer expired prematurely";
ASSERT_EQ(strongB, a->b) << "Unlink occurred prematurely";
ASSERT_EQ(a, strongB->a) << "Unlink occured prematurely";
};
// Set the flag at the last possible and to ensure things still get torn down
ctxt->SetUnlinkOnTeardown(true);
}
ASSERT_TRUE(weakA.expired()) << "A reference was leaked even though unlinking was turned on";
ASSERT_TRUE(strongB->v.IsAutowired()) << "An Autowired field pointing to a foreign context was incorrectly unlinked";
ASSERT_EQ(so.get(), strongB->so.get()) << "An Autowired field was unlinked on teardown even though it pointed outside of a context";
}