comparison base/test/TestById.h @ 1752:6d09d68165a4 by-id

Further review of ById: make IDs only available when adding a model to the ById store, not by querying the item directly. This means any id encountered in the wild must have been added to the store at some point (even if later released), which simplifies reasoning about lifecycles
author Chris Cannam
date Fri, 05 Jul 2019 15:28:07 +0100
parents 52705a328b34
children 043e05c956fb
comparison
equal deleted inserted replaced
1751:77543124651b 1752:6d09d68165a4
21 21
22 using namespace std; 22 using namespace std;
23 23
24 struct WithoutId {}; 24 struct WithoutId {};
25 25
26 struct A : public WithTypedId<A> {}; 26 // We'll need to change access levels for getId() and getUntypedId()
27 // to test the raw calls
28
29 struct A : public WithTypedId<A> { public: using WithTypedId<A>::getId; };
27 struct B1 : public A {}; 30 struct B1 : public A {};
28 struct B2 : public A {}; 31 struct B2 : public A {};
29 32
30 struct M {}; 33 struct M {};
31 34
32 typedef TypedById<A, A::Id> AById; 35 typedef TypedById<A, A::Id> AById;
33 36
34 struct X : virtual public WithId {}; 37 struct X : virtual public WithId { public: using WithId::getUntypedId; };
35 struct Y : public X, public B2, public M {}; 38 struct Y : public X, public B2, public M {};
36 39
37 class TestById : public QObject 40 class TestById : public QObject
38 { 41 {
39 Q_OBJECT 42 Q_OBJECT
75 QVERIFY(!p); 78 QVERIFY(!p);
76 } 79 }
77 80
78 void anySimple() { 81 void anySimple() {
79 auto a = std::make_shared<A>(); 82 auto a = std::make_shared<A>();
80 AnyById::add(a->getId().untyped, a); 83 int id = AnyById::add(a);
81 84 QCOMPARE(id, a->getId().untyped);
82 auto aa = AnyById::getAs<A>(a->getId().untyped); 85
86 auto aa = AnyById::getAs<A>(id);
83 QVERIFY(!!aa); 87 QVERIFY(!!aa);
84 QCOMPARE(aa->getId(), a->getId()); 88 QCOMPARE(aa->getId(), a->getId());
85 QCOMPARE(aa.get(), a.get()); // same object, not just same id! 89 QCOMPARE(aa.get(), a.get()); // same object, not just same id!
86 AnyById::release(a->getId().untyped); 90 AnyById::release(id);
87 } 91 }
88 92
89 void typedEmpty() { 93 void typedEmpty() {
90 auto p = AById::get({}); 94 auto p = AById::get({});
91 QVERIFY(!p); 95 QVERIFY(!p);
100 QCOMPARE(aa->getId(), a->getId()); 104 QCOMPARE(aa->getId(), a->getId());
101 QCOMPARE(aa.get(), a.get()); // same object, not just same id! 105 QCOMPARE(aa.get(), a.get()); // same object, not just same id!
102 AById::release(a); 106 AById::release(a);
103 } 107 }
104 108
105 void typedRelease() { 109 void typedReleaseById() {
106 auto a = std::make_shared<A>(); 110 auto a = std::make_shared<A>();
107 AById::add(a); 111 auto aid = AById::add(a);
108 112
109 auto aa = AById::get(a->getId()); 113 auto aa = AById::get(aid);
110 QVERIFY(!!aa); 114 QVERIFY(!!aa);
111 AById::release(a); 115 AById::release(aid);
112 116
113 aa = AById::get(a->getId()); 117 aa = AById::get(aid);
118 QVERIFY(!aa);
119 }
120
121 void typedReleaseByItem() {
122 auto a = std::make_shared<A>();
123 auto aid = AById::add(a);
124
125 auto aa = AById::get(aid);
126 QVERIFY(!!aa);
127 AById::release(a);
128
129 aa = AById::get(aid);
114 QVERIFY(!aa); 130 QVERIFY(!aa);
115 } 131 }
116 132
117 void typedDowncast() { 133 void typedDowncast() {
118 auto a = std::make_shared<A>(); 134 auto a = std::make_shared<A>();