annotate win64-msvc/include/kj/refcount.h @ 137:b38f6c5a0453

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