Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
down_cast.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/down_cast.hh
11 /// @brief Fast polymorphic down-casting functions
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 ///
14 /// @note A fast polymorphic down-cast when the cast is known to be valid
15 /// @note The cast validity is assert-checked in debug builds
16 
17 
18 #ifndef INCLUDED_utility_down_cast_hh
19 #define INCLUDED_utility_down_cast_hh
20 
21 
22 // C++ headers
23 #include <utility/assert.hh>
24 
25 
26 namespace utility {
27 
28 
29 /// @brief Meta-programming classes to provide the pointer type for down_cast
30 template< typename T > struct RawType { typedef T * Pointer; };
31 template< typename T > struct RawType< T & > { typedef T * Pointer; };
32 template< typename T > struct RawType< T * > { typedef T * Pointer; };
33 
34 
35 /// @brief Fast assert-checked polymorphic down-cast: reference argument
36 ///
37 /// @note Usage: down_cast< Type & > where Type can be const-qualified
38 /// @note For down-casting when you know the cast is valid
39 /// @note Can't use for hierarchies with virtual base classes
40 /// @note Assert intentionally won't compile if a virtual base class is present
41 template< class Target, class Source >
42 inline
43 Target
44 down_cast( Source & s )
45 {
46  debug_assert( dynamic_cast< typename RawType< Target >::Pointer >( &s ) == &s );
47  return static_cast< Target >( s );
48 }
49 
50 
51 /// @brief Fast assert-checked polymorphic down-cast: pointer argument
52 ///
53 /// @note Usage: down_cast< Type * > where Type can be const-qualified
54 /// @note For down-casting when you know the cast is valid
55 /// @note Can't use for hierarchies with virtual base classes
56 /// @note Assert intentionally won't compile if a virtual base class is present
57 template< class Target, class Source >
58 inline
59 Target
60 down_cast( Source * p )
61 {
62  debug_assert( dynamic_cast< typename RawType< Target >::Pointer >( p ) == p );
63  return static_cast< Target >( p );
64 }
65 
66 
67 } // namespace utility
68 
69 
70 #endif // INCLUDED_utility_down_cast_HH
#define debug_assert(condition)
Definition: backtrace.hh:140
FileVectorOptionKey const s
Definition: SQLPDB.py:309
Target down_cast(Source &s)
Fast assert-checked polymorphic down-cast: reference argument.
Definition: down_cast.hh:44
Meta-programming classes to provide the pointer type for down_cast.
Definition: down_cast.hh:30