Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- struct ObjectFactory2 {
- int count = 0;
- ObjectFactory2(int count) : count(count) {
- }
- ObjectFactory2() = default;
- ObjectPtr operator()(std::string id) {
- id += "_"s + to_string(count);
- ++count;
- return std::make_shared<Object>(std::move(id));
- }
- };
- void CacheTest() {
- using namespace std;
- weak_ptr<Object> bob_wp;
- {
- Cache<string, Object, ObjectFactory2> cache;
- auto alice1 = cache.GetValue("Alice"s);
- auto bob = cache.GetValue("Bob"s);
- auto alice2 = cache.GetValue("Alice"s);
- assert(alice1== alice2);
- weak_ptr alice_wp{ alice1 };
- bob_wp = bob;
- assert(!alice_wp.expired());
- assert(!bob_wp.expired());
- alice1.reset();
- assert(!alice_wp.expired());
- alice2.reset();
- assert(alice_wp.expired());
- }
- assert(bob_wp.expired());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement