示例#1
0
	/**
	 * Create an object of the Type specification of Factory.  If the object has a boost::weak_ptr
	 * to itself, it is the task of this method to set this after the object is created.
	 *
	 * @return A pointer to the freshly created object.
	 */
	virtual void create(Pointer& outPointer) throw (Error) {
		try {
			Pointer temp(new _Tp()); //Do not remove
			outPointer = temp;
			WeakPointer weakPtr(outPointer);
			outPointer->setWeakThis(weakPtr);
		} catch(std::exception& e) //May receive std::bad_alloc
		{
			//TBD::ERROR
		}
	}
示例#2
0
    void LruCacheTest::ClearTestHelper(int keyCount)
    {
        size_t cacheLimit = keyCount;
        size_t bucketCount = 128;

        auto cacheSPtr = make_shared<TestCache>(cacheLimit, bucketCount);

        for (auto ix=0; ix<keyCount; ++ix)
        {
            auto key = wformatString("key{0}", ix);
            SyncTryPut(*cacheSPtr, key, true);
            SyncTryGet(*cacheSPtr, key, true);
        }

        vector<weak_ptr<TestCacheEntry>> weakPtrs;

        for (auto ix=0; ix<keyCount; ++ix)
        {
            auto key = wformatString("key{0}", ix);

            TestCacheEntrySPtr entry;
            cacheSPtr->TryGet(key, entry);

            weak_ptr<TestCacheEntry> weakPtr(entry);

            entry.reset();

            VERIFY_IS_TRUE_FMT(weakPtr.use_count() == 1, "{0}: wptr use_count {1} != 1", key, weakPtr.use_count());
            VERIFY_IS_TRUE_FMT(weakPtr.lock().get() != nullptr, "{0}: wptr lock() failed", key);
            VERIFY_IS_TRUE_FMT(!weakPtr.expired(), "{0}: wptr expired {1}", key, weakPtr.expired());
            weakPtr.reset();

            cacheSPtr->TryGet(key, entry);
            weakPtrs.push_back(weak_ptr<TestCacheEntry>(entry));
        }

        cacheSPtr.reset();

        for (auto ix=0; ix<keyCount; ++ix)
        {
            auto key = wformatString("key{0}", ix);

            auto & weakPtr = weakPtrs[ix];

            VERIFY_IS_TRUE_FMT(weakPtr.use_count() == 0, "{0}: wptr use_count {1} != 0", key, weakPtr.use_count());
            VERIFY_IS_TRUE_FMT(weakPtr.lock().get() == nullptr, "{0}: wptr lock() succeeded", key);
            VERIFY_IS_TRUE_FMT(weakPtr.expired(), "{0}: wptr !expired", key, weakPtr.expired());
        }
    }
void benchmark(size_t n) {
  MainPtr<int> mainPtr(folly::make_unique<int>(42));

  std::vector<std::thread> ts;

  for (size_t t = 0; t < threadCount; ++t) {
    ts.emplace_back([&]() {
        WeakPtr<int> weakPtr(mainPtr);

        for (size_t i = 0; i < n; ++i) {
          weakPtr.lock();
        }
      });
  }

  for (auto& t: ts) {
    t.join();
  }
}
示例#4
0
void SelectionSet::addNode(const scene::INodePtr& node)
{
    scene::INodeWeakPtr weakPtr(node);
    _nodes.insert(weakPtr);
}