// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. #pragma once #define HAVE_IRCD_UTIL_LIFE_GUARD_H namespace ircd { inline namespace util { // Convenience functions for types shared_from_this template std::shared_ptr shared_from(const T &t); template std::shared_ptr shared_from(T &t); template std::weak_ptr weak_from(const T &t); template std::weak_ptr weak_from(T &t); template struct life_guard; }} /// life_guard is a convenience which takes advantage of std::enable_shared_from_this. /// The life_guard glorifies the constructor of an std::shared_ptr by accepting /// std::weak_ptr and T& itself all with proper semantics. Once construction is /// successful, the user holds it for the duration of the scope ensuring T& survives /// context interleaving without being destructed. /// /// Use the life_guard to keep an object alive within a function running in a context. /// /// Example: /// /// void foo(client &c) /// { /// const life_guard lg(c); /// /// c.call(); // This call was always safe with or w/o life_guard. /// ctx::wait(); // The context has now yielded and another context might destroy client &c /// c.call(); // The context continues and this would have made a call on a dead c. /// } /// template struct ircd::util::life_guard :std::shared_ptr { // This constructor is used when the templated type inherits from std::enable_shared_from_this<> template life_guard(T &t, typename std::enable_if(), void>::type * = 0) :std::shared_ptr(shared_from(t)) {} // This constructor uses our convention for forward declaring objects that internally // inherit from std::enable_shared_from_this<>. Our convention is to provide: // // std::shared_ptr shared_from(T &c); // template life_guard(T &t, typename std::enable_if(), void>::type * = 0) :std::shared_ptr(shared_from(t)) {} // This constructor is used with a weak_ptr of the type. This throws an exception // to abort the scope when the object already died before being able to guard at all. life_guard(const std::weak_ptr &wp) :std::shared_ptr(wp.lock()) { if(wp.expired()) throw std::bad_weak_ptr(); } }; template std::weak_ptr ircd::util::weak_from(T &t) { return shared_from(t); }; template std::weak_ptr ircd::util::weak_from(const T &t) { return shared_from(t); }; template std::shared_ptr ircd::util::shared_from(T &t) { return dynamic_pointer_cast(t.shared_from_this()); }; template std::shared_ptr ircd::util::shared_from(const T &t) { return dynamic_pointer_cast(t.shared_from_this()); };