/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef XP_WIN # include <cstdint> #endif #ifdefined(XP_DARWIN) && !defined(RUST_BINDGEN) # include <mach/mach.h> #endif
namespace mozilla {
/** * MakeUniqueFallible works exactly like MakeUnique, except that the memory * allocation performed is done fallibly, i.e. it can return nullptr.
*/ template <typename T, typename... Args> typename detail::UniqueSelector<T>::SingleObject MakeUniqueFallible(
Args&&... aArgs) { return UniquePtr<T>(new (fallible) T(std::forward<Args>(aArgs)...));
}
/** * MakeUniqueForOverwrite and MakeUniqueFallibleForOverwrite are like MakeUnique * and MakeUniqueFallible except they use default-initialization. This is * useful, for example, when you have a POD type array that will be overwritten * directly after construction and so zero-initialization is a waste.
*/ template <typename T, typename... Args> typename detail::UniqueSelector<T>::SingleObject MakeUniqueForOverwrite() { return UniquePtr<T>(new T);
}
#if !defined(RUST_BINDGEN) # ifdefined(XP_WIN) // Can't include <windows.h> to get the actual definition of HANDLE // because of namespace pollution. typedefvoid* FileHandleType; # elif defined(XP_UNIX) typedefint FileHandleType; # else # error "Unsupported OS?" # endif
booloperator!=(std::nullptr_t) const { # ifdef XP_WIN // Windows uses both nullptr and INVALID_HANDLE_VALUE (-1 cast to // HANDLE) in different situations, but nullptr is more reliably // null while -1 is also valid input to some calls that take // handles. So class considers both to be null (since neither // should be closed) but default-constructs as nullptr. if (mHandle == (void*)-1) { returnfalse;
} # endif return mHandle != kInvalidHandle;
}
# ifdef XP_WIN // NSPR uses an integer type for PROsfd, so this conversion is // provided for working with it without needing reinterpret casts // everywhere. operator std::intptr_t() const { returnreinterpret_cast<std::intptr_t>(mHandle);
} # endif
// When there's only one user-defined conversion operator, the // compiler will use that to derive equality, but that doesn't work // when the conversion is ambiguoug (the XP_WIN case above). booloperator==(const FileHandleHelper& aOther) const { return mHandle == aOther.mHandle;
}
private:
FileHandleType mHandle;
# ifdef XP_WIN // See above for why this is nullptr. (Also, INVALID_HANDLE_VALUE // can't be expressed as a constexpr.) static constexpr FileHandleType kInvalidHandle = nullptr; # else static constexpr FileHandleType kInvalidHandle = -1; # endif
};
struct FileHandleDeleter { using pointer = FileHandleHelper; using receiver = FileHandleType;
MFBT_API voidoperator()(FileHandleHelper aHelper);
}; #endif
struct MachPortSetDeleter { using pointer = MachPortHelper; using receiver = mach_port_t;
MFBT_API voidoperator()(MachPortHelper aHelper) {
DebugOnly<kern_return_t> kr = mach_port_mod_refs(
mach_task_self(), aHelper, MACH_PORT_RIGHT_PORT_SET, -1);
MOZ_ASSERT(kr == KERN_SUCCESS, "failed to release mach port set");
}
}; #endif
} // namespace detail
template <typename T> using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
#if !defined(RUST_BINDGEN) // A RAII class for the OS construct used for open files and similar // objects: a file descriptor on Unix or a handle on Windows. using UniqueFileHandle =
UniquePtr<detail::FileHandleType, detail::FileHandleDeleter>;
# ifndef __wasm__ // WASI does not have `dup`
MFBT_API UniqueFileHandle DuplicateFileHandle(detail::FileHandleType aFile); inline UniqueFileHandle DuplicateFileHandle(const UniqueFileHandle& aFile) { return DuplicateFileHandle(aFile.get());
} # endif #endif
#ifdefined(XP_DARWIN) && !defined(RUST_BINDGEN) // A RAII class for a Mach port that names a send right. using UniqueMachSendRight =
UniquePtr<mach_port_t, detail::MachSendRightDeleter>; // A RAII class for a Mach port that names a receive right. using UniqueMachReceiveRight =
UniquePtr<mach_port_t, detail::MachReceiveRightDeleter>; // A RAII class for a Mach port set. using UniqueMachPortSet = UniquePtr<mach_port_t, detail::MachPortSetDeleter>;
// Increases the user reference count for MACH_PORT_RIGHT_SEND by 1 and returns // a new UniqueMachSendRight to manage the additional right. inline UniqueMachSendRight RetainMachSendRight(mach_port_t aPort) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), aPort, MACH_PORT_RIGHT_SEND, 1); if (kr == KERN_SUCCESS) { return UniqueMachSendRight(aPort);
} return nullptr;
} #endif
// operator void** is conditionally enabled if `Receiver` is a pointer. template <typename U = Receiver,
std::enable_if_t<
std::is_pointer_v<U> && std::is_same_v<U, Receiver>, int> = 0> operatorvoid**() { returnreinterpret_cast<void**>(&mReceiver);
}
private:
Ptr& mPtr;
Receiver mReceiver;
};
} // namespace detail
// Helper for passing a UniquePtr to an old-style function that uses raw // pointers for out params. Example usage: // // void AllocateFoo(Foo** out) { *out = new Foo(); } // UniquePtr<Foo> foo; // AllocateFoo(getter_Transfers(foo)); template <typename T, typename D> auto getter_Transfers(UniquePtr<T, D>& up) { return detail::UniquePtrGetterTransfers<T, D>(up);
}
} // namespace mozilla
#endif// mozilla_UniquePtrExtensions_h
¤ Dauer der Verarbeitung: 0.30 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.