Mercurial > hg > sv-dependency-builds
comparison osx/include/kj/refcount.h @ 134:41e769c91eca
Add Capnp and KJ builds for OSX
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 25 Oct 2016 14:48:23 +0100 |
parents | |
children | 0994c39f1e94 |
comparison
equal
deleted
inserted
replaced
133:1ac99bfc383d | 134:41e769c91eca |
---|---|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | |
2 // Licensed under the MIT License: | |
3 // | |
4 // Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 // of this software and associated documentation files (the "Software"), to deal | |
6 // in the Software without restriction, including without limitation the rights | |
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 // copies of the Software, and to permit persons to whom the Software is | |
9 // furnished to do so, subject to the following conditions: | |
10 // | |
11 // The above copyright notice and this permission notice shall be included in | |
12 // all copies or substantial portions of the Software. | |
13 // | |
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 // THE SOFTWARE. | |
21 | |
22 #include "memory.h" | |
23 | |
24 #ifndef KJ_REFCOUNT_H_ | |
25 #define KJ_REFCOUNT_H_ | |
26 | |
27 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS | |
28 #pragma GCC system_header | |
29 #endif | |
30 | |
31 namespace kj { | |
32 | |
33 class Refcounted: private Disposer { | |
34 // Subclass this to create a class that contains a reference count. Then, use | |
35 // `kj::refcounted<T>()` to allocate a new refcounted pointer. | |
36 // | |
37 // Do NOT use this lightly. Refcounting is a crutch. Good designs should strive to make object | |
38 // ownership clear, so that refcounting is not necessary. All that said, reference counting can | |
39 // sometimes simplify code that would otherwise become convoluted with explicit ownership, even | |
40 // when ownership relationships are clear at an abstract level. | |
41 // | |
42 // NOT THREADSAFE: This refcounting implementation assumes that an object's references are | |
43 // manipulated only in one thread, because atomic (thread-safe) refcounting is surprisingly slow. | |
44 // | |
45 // In general, abstract classes should _not_ subclass this. The concrete class at the bottom | |
46 // of the hierarchy should be the one to decide how it implements refcounting. Interfaces should | |
47 // expose only an `addRef()` method that returns `Own<InterfaceType>`. There are two reasons for | |
48 // this rule: | |
49 // 1. Interfaces would need to virtually inherit Refcounted, otherwise two refcounted interfaces | |
50 // could not be inherited by the same subclass. Virtual inheritance is awkward and | |
51 // inefficient. | |
52 // 2. An implementation may decide that it would rather return a copy than a refcount, or use | |
53 // some other strategy. | |
54 // | |
55 // TODO(cleanup): Rethink above. Virtual inheritance is not necessarily that bad. OTOH, a | |
56 // virtual function call for every refcount is sad in its own way. A Ref<T> type to replace | |
57 // Own<T> could also be nice. | |
58 | |
59 public: | |
60 virtual ~Refcounted() noexcept(false); | |
61 | |
62 inline bool isShared() const { return refcount > 1; } | |
63 // Check if there are multiple references to this object. This is sometimes useful for deciding | |
64 // whether it's safe to modify the object vs. make a copy. | |
65 | |
66 private: | |
67 mutable uint refcount = 0; | |
68 // "mutable" because disposeImpl() is const. Bleh. | |
69 | |
70 void disposeImpl(void* pointer) const override; | |
71 template <typename T> | |
72 static Own<T> addRefInternal(T* object); | |
73 | |
74 template <typename T> | |
75 friend Own<T> addRef(T& object); | |
76 template <typename T, typename... Params> | |
77 friend Own<T> refcounted(Params&&... params); | |
78 }; | |
79 | |
80 template <typename T, typename... Params> | |
81 inline Own<T> refcounted(Params&&... params) { | |
82 // Allocate a new refcounted instance of T, passing `params` to its constructor. Returns an | |
83 // initial reference to the object. More references can be created with `kj::addRef()`. | |
84 | |
85 return Refcounted::addRefInternal(new T(kj::fwd<Params>(params)...)); | |
86 } | |
87 | |
88 template <typename T> | |
89 Own<T> addRef(T& object) { | |
90 // Return a new reference to `object`, which must subclass Refcounted and have been allocated | |
91 // using `kj::refcounted<>()`. It is suggested that subclasses implement a non-static addRef() | |
92 // method which wraps this and returns the appropriate type. | |
93 | |
94 KJ_IREQUIRE(object.Refcounted::refcount > 0, "Object not allocated with kj::refcounted()."); | |
95 return Refcounted::addRefInternal(&object); | |
96 } | |
97 | |
98 template <typename T> | |
99 Own<T> Refcounted::addRefInternal(T* object) { | |
100 Refcounted* refcounted = object; | |
101 ++refcounted->refcount; | |
102 return Own<T>(object, *refcounted); | |
103 } | |
104 | |
105 } // namespace kj | |
106 | |
107 #endif // KJ_REFCOUNT_H_ |