annotate win32-mingw/include/kj/refcount.h @ 64:eccd51b72864

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