#pragma once /* * Copyright (c) 2015 Dropbox, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include namespace dropbox { namespace oxygen { // Marker type and value for use by nn below. struct i_promise_i_checked_for_null_t {}; static constexpr i_promise_i_checked_for_null_t i_promise_i_checked_for_null{}; // Helper to get the type pointed to by a raw or smart pointer. This can be // explicitly // specialized if need be to provide compatibility with user-defined smart // pointers. namespace nn_detail { template struct element_type { using type = typename T::element_type; }; template struct element_type { using type = Pointee; }; } template class nn; // Trait to check whether a given type is a non-nullable pointer template struct is_nn : public std::false_type {}; template struct is_nn> : public std::true_type {}; /* nn * * Wrapper around a pointer that is guaranteed to not be null. This works with * raw pointers * as well as any smart pointer: nn, nn>, * nn>, * etc. An nn can be used just like a PtrType. * * An nn can be constructed from another nn, if the underlying * type would * allow such construction. For example, nn> can be copied * and moved, but * nn> can only be moved; an nn> can be * explicitly * (but not implicitly) created from an nn; implicit upcasts are * allowed; and so on. * * Similarly, non-nullable pointers can be compared with regular or other * non-nullable * pointers, using the same rules as the underlying pointer types. * * This module also provides helpers for creating an nn from operations * that would * always return a non-null pointer: nn_make_unique, nn_make_shared, * nn_shared_from_this, and * nn_addr (a replacement for operator&). * * We abbreviate nn as nn_unique_ptr - it's a little more readable. * Likewise, * nn can be written as nn_shared_ptr. * * Finally, we define macros NN_CHECK_ASSERT and NN_CHECK_THROW, to convert a * nullable pointer * to a non-nullable pointer. At Dropbox, these use customized error-handling * infrastructure * and are in a separate file. We've included sample implementations here. */ template class nn { public: static_assert(!is_nn::value, "nn> is disallowed"); using element_type = typename nn_detail::element_type::type; // Pass through calls to operator* and operator-> transparently element_type &operator*() const { return *ptr; } element_type *operator->() const { return &*ptr; } // Expose the underlying PtrType operator const PtrType &() const & { return ptr; } operator PtrType &&() && { return std::move(ptr); } // Trying to use the assignment operator to assign a nn to a PtrType // using the // above conversion functions hits an ambiguous resolution bug in clang: // http://llvm.org/bugs/show_bug.cgi?id=18359 // While that exists, we can use these as simple ways of accessing the // underlying type // (instead of workarounds calling the operators explicitly or adding a // constructor call). const PtrType &as_nullable() const & { return ptr; } PtrType &&as_nullable() && { return std::move(ptr); } // Can't convert to bool (that would be silly). The explicit delete results in // "value of type 'nn<...>' is not contextually convertible to 'bool'", rather // than // "no viable conversion", which is a bit more clear. operator bool() const = delete; // Explicitly deleted constructors. These help produce clearer error messages, // as trying // to use them will result in clang printing the whole line, including the // comment. nn(std::nullptr_t) = delete; // nullptr is not allowed here nn &operator=(std::nullptr_t) = delete; // nullptr is not allowed here nn(PtrType) = delete; // must use NN_CHECK_ASSERT or NN_CHECK_THROW nn &operator=(PtrType) = delete; // must use NN_CHECK_ASSERT or NN_CHECK_THROW //PROJ_DLL ~nn(); // Semi-private constructor for use by NN_CHECK_ macros. explicit nn(i_promise_i_checked_for_null_t, const PtrType &arg) noexcept : ptr(arg) { } explicit nn(i_promise_i_checked_for_null_t, PtrType &&arg) noexcept : ptr(std::move(arg)) { } // Type-converting move and copy constructor. We have four separate cases // here, for // implicit and explicit move and copy. template ::value && !std::is_convertible::value, int>::type = 0> explicit nn(const nn &other) : ptr(other.operator const OtherType &()) {} template ::value && !std::is_convertible::value && !std::is_pointer::value, int>::type = 0> explicit nn(nn &&other) : ptr(std::move(other).operator OtherType &&()) {} template ::value, int>::type = 0> nn(const nn &other) : ptr(other.operator const OtherType &()) {} template < typename OtherType, typename std::enable_if::value && !std::is_pointer::value, int>::type = 0> nn(nn &&other) : ptr(std::move(other).operator OtherType &&()) {} // A type-converting move and copy assignment operator aren't necessary; // writing // "base_ptr = derived_ptr;" will run the type-converting constructor followed // by the // implicit move assignment operator. // Two-argument constructor, designed for use with the shared_ptr aliasing // constructor. // This will not be instantiated if PtrType doesn't have a suitable // constructor. template < typename OtherType, typename std::enable_if< std::is_constructible::value, int>::type = 0> nn(const nn &ownership_ptr, nn target_ptr) : ptr(ownership_ptr.operator const OtherType &(), target_ptr) {} // Comparisons. Other comparisons are implemented in terms of these. template friend bool operator==(const nn &, const R &); template friend bool operator==(const L &, const nn &); template friend bool operator==(const nn &, const nn &); template friend bool operator<(const nn &, const R &); template friend bool operator<(const L &, const nn &); template friend bool operator<(const nn &, const nn &); // ostream operator template friend std::ostream &operator<<(std::ostream &, const nn &); template element_type *get() const { return ptr.get(); } private: // Backing pointer PtrType ptr; }; // Base comparisons - these are friends of nn, so they can access .ptr // directly. template bool operator==(const nn &l, const R &r) { return l.ptr == r; } template bool operator==(const L &l, const nn &r) { return l == r.ptr; } template bool operator==(const nn &l, const nn &r) { return l.ptr == r.ptr; } template bool operator<(const nn &l, const R &r) { return l.ptr < r; } template bool operator<(const L &l, const nn &r) { return l < r.ptr; } template bool operator<(const nn &l, const nn &r) { return l.ptr < r.ptr; } template std::ostream &operator<<(std::ostream &os, const nn &p) { return os << p.ptr; } #define NN_DERIVED_OPERATORS(op, base) \ template \ bool operator op(const nn &l, const R &r) { \ return base; \ } \ template \ bool operator op(const L &l, const nn &r) { \ return base; \ } \ template \ bool operator op(const nn &l, const nn &r) { \ return base; \ } NN_DERIVED_OPERATORS(>, r < l) NN_DERIVED_OPERATORS(<=, !(l > r)) NN_DERIVED_OPERATORS(>=, !(l < r)) NN_DERIVED_OPERATORS(!=, !(l == r)) #undef NN_DERIVED_OPERATORS // Convenience typedefs template using nn_unique_ptr = nn>; template using nn_shared_ptr = nn>; template nn_unique_ptr nn_make_unique(Args &&... args) { return nn_unique_ptr( i_promise_i_checked_for_null, std::unique_ptr(new T(std::forward(args)...))); } template nn_shared_ptr nn_make_shared(Args &&... args) { return nn_shared_ptr(i_promise_i_checked_for_null, std::make_shared(std::forward(args)...)); } template class nn_enable_shared_from_this : public std::enable_shared_from_this { public: using std::enable_shared_from_this::enable_shared_from_this; nn_shared_ptr nn_shared_from_this() { return nn_shared_ptr(i_promise_i_checked_for_null, this->shared_from_this()); } nn_shared_ptr nn_shared_from_this() const { return nn_shared_ptr(i_promise_i_checked_for_null, this->shared_from_this()); } }; template nn nn_addr(T &object) { return nn(i_promise_i_checked_for_null, &object); } template nn nn_addr(const T &object) { return nn(i_promise_i_checked_for_null, &object); } /* Non-nullable equivalents of shared_ptr's specialized casting functions. * These convert through a shared_ptr since nn> lacks the * ref-count-sharing cast * constructor, but thanks to moves there shouldn't be any significant extra * cost. */ template nn_shared_ptr nn_static_pointer_cast(const nn_shared_ptr &org_ptr) { auto raw_ptr = static_cast::element_type *>(org_ptr.get()); std::shared_ptr nullable_ptr(org_ptr.as_nullable(), raw_ptr); return nn_shared_ptr(i_promise_i_checked_for_null, std::move(nullable_ptr)); } template std::shared_ptr nn_dynamic_pointer_cast(const nn_shared_ptr &org_ptr) { auto raw_ptr = dynamic_cast::element_type *>(org_ptr.get()); if (!raw_ptr) { return nullptr; } else { return std::shared_ptr(org_ptr.as_nullable(), raw_ptr); } } template nn_shared_ptr nn_const_pointer_cast(const nn_shared_ptr &org_ptr) { auto raw_ptr = const_cast::element_type *>(org_ptr.get()); std::shared_ptr nullable_ptr(org_ptr.as_nullable(), raw_ptr); return nn_shared_ptr(i_promise_i_checked_for_null, std::move(nullable_ptr)); } } } /* end namespace dropbox::oxygen */ namespace std { template struct hash<::dropbox::oxygen::nn> { using argument_type = ::dropbox::oxygen::nn; using result_type = size_t; result_type operator()(const argument_type &obj) const { return std::hash{}(obj.as_nullable()); } }; } /* These have to be macros because our internal versions invoke other macros * that use * __FILE__ and __LINE__, which we want to correctly point to the call site. * We're looking * forward to std::source_location :) * * The lambdas ensure that we only evaluate _e once. */ #include // NN_CHECK_ASSERT takes a pointer of type PT (e.g. raw pointer, std::shared_ptr // or std::unique_ptr) // and returns a non-nullable pointer of type nn. // Triggers an assertion if expression evaluates to null. #define NN_CHECK_ASSERT(_e) \ (([&](typename std::remove_reference::type p) { \ /* note: assert() alone is not sufficient here, because it might be \ * compiled out. */ \ assert(p &&#_e " must not be null"); \ if (!p) \ std::abort(); \ return dropbox::oxygen::nn< \ typename std::remove_reference::type>( \ dropbox::oxygen::i_promise_i_checked_for_null, std::move(p)); \ })(_e)) // NN_CHECK_THROW takes a pointer of type PT (e.g. raw pointer, std::shared_ptr // or std::unique_ptr) // and returns a non-nullable pointer of type nn. // Throws if expression evaluates to null. #define NN_CHECK_THROW(_e) \ (([&](typename std::remove_reference::type p) { \ if (!p) \ throw std::runtime_error(#_e " must not be null"); \ return dropbox::oxygen::nn< \ typename std::remove_reference::type>( \ dropbox::oxygen::i_promise_i_checked_for_null, std::move(p)); \ })(_e))