Chris@50: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@50: // Licensed under the MIT License: Chris@50: // Chris@50: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@50: // of this software and associated documentation files (the "Software"), to deal Chris@50: // in the Software without restriction, including without limitation the rights Chris@50: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@50: // copies of the Software, and to permit persons to whom the Software is Chris@50: // furnished to do so, subject to the following conditions: Chris@50: // Chris@50: // The above copyright notice and this permission notice shall be included in Chris@50: // all copies or substantial portions of the Software. Chris@50: // Chris@50: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@50: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@50: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@50: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@50: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@50: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@50: // THE SOFTWARE. Chris@50: Chris@50: #include "memory.h" Chris@50: Chris@50: #ifndef KJ_REFCOUNT_H_ Chris@50: #define KJ_REFCOUNT_H_ Chris@50: Chris@50: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@50: #pragma GCC system_header Chris@50: #endif Chris@50: Chris@50: namespace kj { Chris@50: Chris@50: class Refcounted: private Disposer { Chris@50: // Subclass this to create a class that contains a reference count. Then, use Chris@50: // `kj::refcounted()` to allocate a new refcounted pointer. Chris@50: // Chris@50: // Do NOT use this lightly. Refcounting is a crutch. Good designs should strive to make object Chris@50: // ownership clear, so that refcounting is not necessary. All that said, reference counting can Chris@50: // sometimes simplify code that would otherwise become convoluted with explicit ownership, even Chris@50: // when ownership relationships are clear at an abstract level. Chris@50: // Chris@50: // NOT THREADSAFE: This refcounting implementation assumes that an object's references are Chris@50: // manipulated only in one thread, because atomic (thread-safe) refcounting is surprisingly slow. Chris@50: // Chris@50: // In general, abstract classes should _not_ subclass this. The concrete class at the bottom Chris@50: // of the hierarchy should be the one to decide how it implements refcounting. Interfaces should Chris@50: // expose only an `addRef()` method that returns `Own`. There are two reasons for Chris@50: // this rule: Chris@50: // 1. Interfaces would need to virtually inherit Refcounted, otherwise two refcounted interfaces Chris@50: // could not be inherited by the same subclass. Virtual inheritance is awkward and Chris@50: // inefficient. Chris@50: // 2. An implementation may decide that it would rather return a copy than a refcount, or use Chris@50: // some other strategy. Chris@50: // Chris@50: // TODO(cleanup): Rethink above. Virtual inheritance is not necessarily that bad. OTOH, a Chris@50: // virtual function call for every refcount is sad in its own way. A Ref type to replace Chris@50: // Own could also be nice. Chris@50: Chris@50: public: Chris@50: virtual ~Refcounted() noexcept(false); Chris@50: Chris@50: inline bool isShared() const { return refcount > 1; } Chris@50: // Check if there are multiple references to this object. This is sometimes useful for deciding Chris@50: // whether it's safe to modify the object vs. make a copy. Chris@50: Chris@50: private: Chris@50: mutable uint refcount = 0; Chris@50: // "mutable" because disposeImpl() is const. Bleh. Chris@50: Chris@50: void disposeImpl(void* pointer) const override; Chris@50: template Chris@50: static Own addRefInternal(T* object); Chris@50: Chris@50: template Chris@50: friend Own addRef(T& object); Chris@50: template Chris@50: friend Own refcounted(Params&&... params); Chris@50: }; Chris@50: Chris@50: template Chris@50: inline Own refcounted(Params&&... params) { Chris@50: // Allocate a new refcounted instance of T, passing `params` to its constructor. Returns an Chris@50: // initial reference to the object. More references can be created with `kj::addRef()`. Chris@50: Chris@50: return Refcounted::addRefInternal(new T(kj::fwd(params)...)); Chris@50: } Chris@50: Chris@50: template Chris@50: Own addRef(T& object) { Chris@50: // Return a new reference to `object`, which must subclass Refcounted and have been allocated Chris@50: // using `kj::refcounted<>()`. It is suggested that subclasses implement a non-static addRef() Chris@50: // method which wraps this and returns the appropriate type. Chris@50: Chris@50: KJ_IREQUIRE(object.Refcounted::refcount > 0, "Object not allocated with kj::refcounted()."); Chris@50: return Refcounted::addRefInternal(&object); Chris@50: } Chris@50: Chris@50: template Chris@50: Own Refcounted::addRefInternal(T* object) { Chris@50: Refcounted* refcounted = object; Chris@50: ++refcounted->refcount; Chris@50: return Own(object, *refcounted); Chris@50: } Chris@50: Chris@50: } // namespace kj Chris@50: Chris@50: #endif // KJ_REFCOUNT_H_