mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
add pointer_cast
This commit is contained in:
parent
897d1e25b6
commit
fbb95d9068
36 changed files with 553 additions and 72 deletions
|
@ -12,9 +12,14 @@
|
|||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <ostream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/workaround/std/cstddef.hpp>
|
||||
#include <sprout/functional/hash.hpp>
|
||||
#include <sprout/functional/less.hpp>
|
||||
#include <sprout/type_traits/common_decay.hpp>
|
||||
#include <sprout/memory/pointer_cast.hpp>
|
||||
#include <sprout/memory/get_pointer.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
|
@ -100,10 +105,7 @@ namespace sprout {
|
|||
SPROUT_EXPLICIT_CONVERSION SPROUT_CONSTEXPR operator bool() const SPROUT_NOEXCEPT {
|
||||
return get();
|
||||
}
|
||||
SPROUT_CONSTEXPR operator pointer() SPROUT_NOEXCEPT {
|
||||
return get();
|
||||
}
|
||||
SPROUT_CONSTEXPR operator const_pointer() const SPROUT_NOEXCEPT {
|
||||
SPROUT_EXPLICIT_CONVERSION SPROUT_CONSTEXPR operator pointer() const SPROUT_NOEXCEPT {
|
||||
return get();
|
||||
}
|
||||
SPROUT_CXX14_CONSTEXPR pointer
|
||||
|
@ -186,25 +188,27 @@ namespace sprout {
|
|||
operator!=(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) SPROUT_NOEXCEPT {
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::exempt_ptr<T> const& x, std::nullptr_t y) SPROUT_NOEXCEPT {
|
||||
return x.get() == y;
|
||||
operator==(sprout::exempt_ptr<T> const& x, std::nullptr_t) SPROUT_NOEXCEPT {
|
||||
return !x;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::exempt_ptr<T> const& x, std::nullptr_t y) SPROUT_NOEXCEPT {
|
||||
return !(x == y);
|
||||
operator!=(sprout::exempt_ptr<T> const& x, std::nullptr_t) SPROUT_NOEXCEPT {
|
||||
return static_cast<bool>(x);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(std::nullptr_t, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
|
||||
return !y;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(std::nullptr_t x, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
|
||||
return x == y.get();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(std::nullptr_t x, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
|
||||
return !(x == y);
|
||||
operator!=(std::nullptr_t, sprout::exempt_ptr<T> const& y) SPROUT_NOEXCEPT {
|
||||
return static_cast<bool>(y);
|
||||
}
|
||||
//
|
||||
// operator<
|
||||
|
@ -215,7 +219,8 @@ namespace sprout {
|
|||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
|
||||
return x.get() < y.get();
|
||||
typedef typename sprout::common_decay<T*, U*>::type type;
|
||||
return sprout::less<type>()(x.get(), y.get());
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
|
@ -232,6 +237,48 @@ namespace sprout {
|
|||
operator>=(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<U> const& y) {
|
||||
return !(x < y);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<(sprout::exempt_ptr<T> const& x, std::nullptr_t y) {
|
||||
return sprout::less<T*>()(x.get(), y);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>(sprout::exempt_ptr<T> const& x, std::nullptr_t y) {
|
||||
return y < x;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<=(sprout::exempt_ptr<T> const& x, std::nullptr_t y) {
|
||||
return !(y < x);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>=(sprout::exempt_ptr<T> const& x, std::nullptr_t y) {
|
||||
return !(x < y);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<(std::nullptr_t x, sprout::exempt_ptr<T> const& y) {
|
||||
return sprout::less<T*>()(x, y.get());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>(std::nullptr_t x, sprout::exempt_ptr<T> const& y) {
|
||||
return y < x;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<=(std::nullptr_t x, sprout::exempt_ptr<T> const& y) {
|
||||
return !(y < x);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>=(std::nullptr_t x, sprout::exempt_ptr<T> const& y) {
|
||||
return !(x < y);
|
||||
}
|
||||
//
|
||||
// operator+
|
||||
// operator-
|
||||
|
@ -246,6 +293,14 @@ namespace sprout {
|
|||
operator-(sprout::exempt_ptr<T> const& x, sprout::exempt_ptr<T> const& y) {
|
||||
return x.get() - y.get();
|
||||
}
|
||||
//
|
||||
// operator<<
|
||||
//
|
||||
template<typename T, typename Elem, typename Traits>
|
||||
inline SPROUT_CONSTEXPR std::basic_ostream<Elem, Traits>&
|
||||
operator<<(std::basic_ostream<Elem, Traits>& lhs, sprout::exempt_ptr<T> const& rhs) {
|
||||
return lhs << rhs.get();
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
namespace sprout {
|
||||
|
@ -276,4 +331,46 @@ namespace std {
|
|||
#endif
|
||||
} // namespace std
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// static_pointer_cast
|
||||
// const_pointer_cast
|
||||
// dynamic_pointer_cast
|
||||
// reinterpret_pointer_cast
|
||||
//
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
|
||||
static_pointer_cast(sprout::exempt_ptr<U> const& p) {
|
||||
typedef typename sprout::exempt_ptr<T>::element_type element_type;
|
||||
return sprout::exempt_ptr<T>(sprout::static_pointer_cast<element_type>(p.get()));
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
|
||||
const_pointer_cast(sprout::exempt_ptr<U> const& p) {
|
||||
typedef typename sprout::exempt_ptr<T>::element_type element_type;
|
||||
return sprout::exempt_ptr<T>(sprout::const_pointer_cast<element_type>(p.get()));
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
|
||||
dynamic_pointer_cast(sprout::exempt_ptr<U> const& p) {
|
||||
typedef typename sprout::exempt_ptr<T>::element_type element_type;
|
||||
return sprout::exempt_ptr<T>(sprout::dynamic_pointer_cast<element_type>(p.get()));
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
|
||||
reinterpret_pointer_cast(sprout::exempt_ptr<U> const& p) {
|
||||
typedef typename sprout::exempt_ptr<T>::element_type element_type;
|
||||
return sprout::exempt_ptr<T>(sprout::reinterpret_pointer_cast<element_type>(p.get()));
|
||||
}
|
||||
|
||||
//
|
||||
// get_pointer
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_NON_CONSTEXPR T*
|
||||
get_pointer(sprout::exempt_ptr<T> const& p) SPROUT_NOEXCEPT {
|
||||
return p.get();
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MEMORY_EXEMPT_PTR_HPP
|
||||
|
|
46
sprout/memory/get_pointer.hpp
Normal file
46
sprout/memory/get_pointer.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_MEMORY_GET_POINTER_HPP
|
||||
#define SPROUT_MEMORY_GET_POINTER_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// get_pointer
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T*
|
||||
get_pointer(T* p) SPROUT_NOEXCEPT {
|
||||
return p;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_NON_CONSTEXPR T*
|
||||
get_pointer(std::auto_ptr<T> const& p) SPROUT_NOEXCEPT {
|
||||
return p.get();
|
||||
}
|
||||
|
||||
#if !defined(SPROUT_NO_CXX11_SMART_PTR)
|
||||
template<typename T>
|
||||
inline SPROUT_NON_CONSTEXPR T*
|
||||
get_pointer(std::unique_ptr<T> const& p) SPROUT_NOEXCEPT {
|
||||
return p.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline SPROUT_NON_CONSTEXPR T*
|
||||
get_pointer(std::shared_ptr<T> const& p) SPROUT_NOEXCEPT {
|
||||
return p.get();
|
||||
}
|
||||
#endif
|
||||
} // namespace sprout
|
||||
|
||||
|
||||
#endif // #ifndef SPROUT_MEMORY_GET_POINTER_HPP
|
33
sprout/memory/observer_ptr.hpp
Normal file
33
sprout/memory/observer_ptr.hpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_MEMORY_OBSERVER_PTR_HPP
|
||||
#define SPROUT_MEMORY_OBSERVER_PTR_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/memory/exempt_ptr.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// make_observer
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::exempt_ptr<T>
|
||||
make_observer(T* p) SPROUT_NOEXCEPT {
|
||||
return sprout::exempt_ptr<T>(p);
|
||||
}
|
||||
|
||||
#if SPROUT_USE_TEMPLATE_ALIASES
|
||||
//
|
||||
// observer_ptr
|
||||
//
|
||||
template<typename T>
|
||||
using observer_ptr = sprout::exempt_ptr<T>;
|
||||
#endif // #if SPROUT_USE_TEMPLATE_ALIASES
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MEMORY_OBSERVER_PTR_HPP
|
69
sprout/memory/pointer_cast.hpp
Normal file
69
sprout/memory/pointer_cast.hpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_MEMORY_POINTER_CAST_HPP
|
||||
#define SPROUT_MEMORY_POINTER_CAST_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/dyn_cast.hpp>
|
||||
#include <sprout/utility/reinter_cast.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// static_pointer_cast
|
||||
// const_pointer_cast
|
||||
// dynamic_pointer_cast
|
||||
// reinterpret_pointer_cast
|
||||
//
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR T*
|
||||
static_pointer_cast(U* p) {
|
||||
return static_cast<T*>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR T*
|
||||
const_pointer_cast(U* p) {
|
||||
return const_cast<T*>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR T*
|
||||
dynamic_pointer_cast(U* p) {
|
||||
return sprout::dyn_cast<T*>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR T*
|
||||
reinterpret_pointer_cast(U* p) {
|
||||
return sprout::reinter_cast<T*>(p);
|
||||
}
|
||||
|
||||
#if !defined(SPROUT_NO_CXX11_SMART_PTR)
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_NON_CONSTEXPR std::shared_ptr<T>
|
||||
static_pointer_cast(std::shared_ptr<U> const& p) {
|
||||
return std::static_pointer_cast<T>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_NON_CONSTEXPR std::shared_ptr<T>
|
||||
const_pointer_cast(std::shared_ptr<U> const& p) {
|
||||
return std::const_pointer_cast<T>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_NON_CONSTEXPR std::shared_ptr<T>
|
||||
dynamic_pointer_cast(std::shared_ptr<U> const& p) {
|
||||
return std::dynamic_pointer_cast<T>(p);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
inline SPROUT_NON_CONSTEXPR std::shared_ptr<T>
|
||||
reinterpret_pointer_cast(std::shared_ptr<U> const& p) {
|
||||
typedef typename std::shared_ptr<T>::element_type element_type;
|
||||
return std::shared_ptr<T>(p, reinterpret_cast<element_type*>(p.get()));
|
||||
}
|
||||
#endif
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MEMORY_POINTER_CAST_HPP
|
Loading…
Add table
Add a link
Reference in a new issue