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


C++ AutoRequired::get方法代码示例

本文整理汇总了C++中AutoRequired::get方法的典型用法代码示例。如果您正苦于以下问题:C++ AutoRequired::get方法的具体用法?C++ AutoRequired::get怎么用?C++ AutoRequired::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AutoRequired的用法示例。


在下文中一共展示了AutoRequired::get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: pshr

TEST_F(ContextCreatorTest, ValidateMultipleEviction) {
  AutoCurrentContext()->Initiate();

  // Number of dependent contexts to be created
  const size_t count = 100;

  // Teardown lock, counter, and condition:
  std::mutex lock;
  std::condition_variable cond;
  int counter = count;

  // Obtain creator pointer:
  AutoRequired<Creator> creator;

  // Set up a signal manager at global context scope:
  AutoRequired<GlobalSignal> signal;
  {
    // Array of objects to test destruction on, and corresponding collection of contexts:
    std::shared_ptr<WaitMember> members[count];

    // Create a few contexts:
    for(int i = count; i--;) {
      AutoCreateContext ctxt;
      CurrentContextPusher pshr(ctxt);

      // Trivial validation that the newly created context is an empty context:
      ASSERT_EQ(static_cast<size_t>(0), ctxt->GetMemberCount()) << "A created context was not empty";

      // Add in an object to test asynchronous destruction:
      AutoRequired<WaitMember> obj;
      members[i] = obj;
      ASSERT_EQ(signal.get(), obj->m_signal.get()) << "Dependent context wiring did not correctly match to the enclosing scope";

      // Add a notifier to signal a continue condition when we have everything we need:
      ctxt->AddTeardownListener([&lock, &cond, &counter] {
        (std::lock_guard<std::mutex>)lock,
        counter--,
        cond.notify_all();
      });

      // Kick off the context:
      ctxt->Initiate();
    }

    // Signal all members and then release everything:
    signal->Signal();
  }

  // Wait for all contexts to be destroyed
  std::unique_lock<std::mutex> lk(lock);
  bool wait_status = cond.wait_for(lk, std::chrono::seconds(1), [&counter] {return counter == 0;});
  ASSERT_TRUE(wait_status) << "All teardown listeners didn't trigger, counter still at " << counter;

  // Validate that everything expires:
  ASSERT_EQ(static_cast<size_t>(0), creator->GetSize()) << "Not all contexts were evicted as expected";
}
开发者ID:c-zheng,项目名称:autowiring,代码行数:56,代码来源:ContextCreatorTest.cpp

示例2: a

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";
}
开发者ID:c-zheng,项目名称:autowiring,代码行数:39,代码来源:CoreContextTest.cpp


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