annotate win32-mingw/include/kj/refcount.h @ 50:37d53a7e8262

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