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