blob: 296fbada79bd62058d90a92b237d8ef30c09fc60 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// Copyright (C) 2002 Brad King (brad.king@kitware.com)
Jeff Thompsonf7d49942013-08-01 16:47:40 -07002// Douglas Gregor (gregod@cs.rpi.edu)
3//
4// Copyright (C) 2002, 2008 Peter Dimov
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10// For more information, see http://www.boost.org
11
Jeff Thompson3d613fd2013-10-15 15:39:04 -070012#ifndef NDNBOOST_UTILITY_ADDRESSOF_HPP
13# define NDNBOOST_UTILITY_ADDRESSOF_HPP
Jeff Thompsonf7d49942013-08-01 16:47:40 -070014
Jeff Thompson2277ce52013-08-01 17:34:11 -070015# include <ndnboost/config.hpp>
16# include <ndnboost/detail/workaround.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070017
18namespace ndnboost
19{
20
21namespace detail
22{
23
24template<class T> struct addr_impl_ref
25{
26 T & v_;
27
28 inline addr_impl_ref( T & v ): v_( v ) {}
29 inline operator T& () const { return v_; }
30
31private:
32 addr_impl_ref & operator=(const addr_impl_ref &);
33};
34
35template<class T> struct addressof_impl
36{
37 static inline T * f( T & v, long )
38 {
39 return reinterpret_cast<T*>(
40 &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
41 }
42
43 static inline T * f( T * v, int )
44 {
45 return v;
46 }
47};
48
49} // namespace detail
50
51template<class T> T * addressof( T & v )
52{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070053#if (defined( __BORLANDC__ ) && NDNBOOST_WORKAROUND( __BORLANDC__, NDNBOOST_TESTED_AT( 0x610 ) ) ) || defined( __SUNPRO_CC )
Jeff Thompsonf7d49942013-08-01 16:47:40 -070054
55 return ndnboost::detail::addressof_impl<T>::f( v, 0 );
56
57#else
58
59 return ndnboost::detail::addressof_impl<T>::f( ndnboost::detail::addr_impl_ref<T>( v ), 0 );
60
61#endif
62}
63
Jeff Thompson3d613fd2013-10-15 15:39:04 -070064#if defined( __SUNPRO_CC ) && NDNBOOST_WORKAROUND( __SUNPRO_CC, NDNBOOST_TESTED_AT( 0x590 ) )
Jeff Thompsonf7d49942013-08-01 16:47:40 -070065
66namespace detail
67{
68
69template<class T> struct addressof_addp
70{
71 typedef T * type;
72};
73
74} // namespace detail
75
76template< class T, std::size_t N >
77typename detail::addressof_addp< T[N] >::type addressof( T (&t)[N] )
78{
79 return &t;
80}
81
82#endif
83
84// Borland doesn't like casting an array reference to a char reference
85// but these overloads work around the problem.
Jeff Thompson3d613fd2013-10-15 15:39:04 -070086#if defined( __BORLANDC__ ) && NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x564))
Jeff Thompsonf7d49942013-08-01 16:47:40 -070087template<typename T,std::size_t N>
88T (*addressof(T (&t)[N]))[N]
89{
90 return reinterpret_cast<T(*)[N]>(&t);
91}
92
93template<typename T,std::size_t N>
94const T (*addressof(const T (&t)[N]))[N]
95{
96 return reinterpret_cast<const T(*)[N]>(&t);
97}
98#endif
99
100} // namespace ndnboost
101
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700102#endif // NDNBOOST_UTILITY_ADDRESSOF_HPP