annotate win64-msvc/include/kj/refcount.h @ 62:0994c39f1e94

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