annotate win64-msvc/include/kj/refcount.h @ 148:b4bfdf10c4b3

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