1 /*
2 Copyright (c) 2017-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Class-based object ownership system
31  *
32  * Description:
33  * Object ownership system similar to Delphi's. All classes deriving from Owner
34  * can store references to objects implementing Owned interface (and other Owner
35  * objects as well). When an owner is deleted, its owned objects are also deleted.
36  *
37  * This module is not compatible with GC-collected objects. It can be used only with
38  * dlib.core.memory. Using it with objects allocated any other way will cause application to crash.
39  *
40  * Copyright: Timur Gafarov 2017-2021.
41  * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0).
42  * Authors: Timur Gafarov
43  */
44 module dlib.core.ownership;
45 
46 import dlib.core.memory;
47 import dlib.container.array;
48 
49 /**
50  * Interface for objects that can be owned, but not own other objects
51  */
52 interface Owned
53 {
54 }
55 
56 /**
57  * Basic owner object class.
58  * When you delete it, all owned object are automatically deleted
59  */
60 class Owner: Owned
61 {
62     protected Array!Owned ownedObjects;
63 
64     /**
65      * Constructor. owner can be null, in this case object won't have an owner.
66      * Such objects are called root owners and should be deleted manually.
67      */
68     this(Owner owner)
69     {
70         if (owner)
71             owner.addOwnedObject(this);
72     }
73 
74     /// Add owned object. Usually you don't have to do it explicitly, just pass the owner to constructor
75     void addOwnedObject(Owned obj)
76     {
77         ownedObjects.append(obj);
78     }
79 
80     /// Delete owned object without deleting object itself
81     void clearOwnedObjects()
82     {
83         foreach(i, obj; ownedObjects)
84             Delete(obj);
85         ownedObjects.free();
86     }
87 
88     /// Delete particular owned object, if it is there
89     void deleteOwnedObject(Owned obj)
90     {
91         if (ownedObjects.removeFirst(obj))
92         {
93             Delete(obj);
94         }
95     }
96 
97     /// Destructor
98     ~this()
99     {
100         clearOwnedObjects();
101     }
102 }
103 
104 ///
105 unittest
106 {
107     class Test: Owner
108     {
109         string name;
110         static bool[string] available;
111 
112         this(string name, Owner o = null)
113         {
114             super(o);
115             this.name = name;
116             available[name] = true;
117         }
118 
119         ~this()
120         {
121             available[name] = false;
122         }
123     }
124 
125     Test obj1 = New!Test("obj1", null);
126     Test obj2 = New!Test("obj2", obj1);
127     Test obj3 = New!Test("obj3", obj2);
128     obj2.deleteOwnedObject(obj3);
129     assert(!Test.available["obj3"]);
130     Delete(obj1);
131     assert(!Test.available["obj2"]);
132     assert(!Test.available["obj1"]);
133 }