1 /* A class that sets a boolean when an object of the class gets destroyed. 2 */ 3 struct S { 4 S(bool *freed) : freed(freed) {} 5 ~S(); 6 7 bool *freed; 8 }; 9 10 /* S destructor. 11 * 12 * Set the boolean, a pointer to which was passed to the constructor. 13 */ 14 S::~S() 15 { 16 *freed = true; 17 } 18 19 /* Construct an isl::id with an S object attached that sets *freed 20 * when it gets destroyed. 21 */ 22 static isl::id construct_id(isl::ctx ctx, bool *freed) 23 { 24 auto s = std::make_shared<S>(freed); 25 isl::id id(ctx, "S", s); 26 return id; 27 } 28 29 /* Test id::try_user. 30 * 31 * In particular, check that the object attached to an identifier 32 * can be retrieved again, that trying to retrieve an object of the wrong type 33 * or trying to retrieve an object when no object was attached fails. 34 * Furthermore, check that the object attached to an identifier 35 * gets properly freed. 36 */ 37 static void test_try_user(isl::ctx ctx) 38 { 39 isl::id id(ctx, "test", 5); 40 isl::id id2(ctx, "test2"); 41 42 auto maybe_int = id.try_user<int>(); 43 auto maybe_s = id.try_user<std::shared_ptr<S>>(); 44 auto maybe_int2 = id2.try_user<int>(); 45 46 if (!maybe_int) 47 die("integer cannot be retrieved from isl::id"); 48 if (maybe_int.value() != 5) 49 die("wrong integer retrieved from isl::id"); 50 if (maybe_s) 51 die("structure unexpectedly retrieved from isl::id"); 52 if (maybe_int2) 53 die("integer unexpectedly retrieved from isl::id"); 54 55 bool freed = false; 56 { 57 isl::id id = construct_id(ctx, &freed); 58 if (freed) 59 die("data structure freed prematurely"); 60 auto maybe_s = id.try_user<std::shared_ptr<S>>(); 61 if (!maybe_s) 62 die("structure cannot be retrieved from isl::id"); 63 if (maybe_s.value()->freed != &freed) 64 die("invalid structure retrieved from isl::id"); 65 } 66 if (!freed) 67 die("data structure not freed"); 68 } 69