annotate win64-msvc/include/kj/refcount.h @ 83:ae30d91d2ffe

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