Index: trunk/willow/src/include/util.h |
— | — | @@ -89,6 +89,9 @@ |
90 | 90 | }; |
91 | 91 | |
92 | 92 | #ifdef WILLOW_DEBUG |
| 93 | +/* |
| 94 | + * Helper classes for sockaddr_caster, see below. |
| 95 | + */ |
93 | 96 | typedef mpl::map< |
94 | 97 | mpl::pair<sockaddr_in, mpl::int_<AF_INET> >, |
95 | 98 | mpl::pair<sockaddr_in6, mpl::int_<AF_INET6> >, |
— | — | @@ -130,12 +133,33 @@ |
131 | 134 | |
132 | 135 | #endif |
133 | 136 | |
| 137 | +/** |
| 138 | + * Cast from one sockaddr type to another. If the conversion can be proved |
| 139 | + * invalid at compile time (e.g. the from type is not a sockaddr), a compile |
| 140 | + * time error will be generated. Otherwise an assertion failure will be |
| 141 | + * triggered at runtime if the sa_family of the type being cast does not match |
| 142 | + * the destination type. |
| 143 | + * |
| 144 | + * Usage: |
| 145 | + * |
| 146 | + * \code |
| 147 | + * void f(sockaddr *s) { |
| 148 | + * sockaddr_in *sin = sockaddr_cast<sockaddr_in *>(s); |
| 149 | + * ... |
| 150 | + * } |
| 151 | + * \endcode |
| 152 | + * |
| 153 | + * \param To sockaddr type to cast to |
| 154 | + * \param From sockaddr type to cast from |
| 155 | + * \param f sockaddr struct to cast |
| 156 | + * \returns the result of the cast |
| 157 | + */ |
134 | 158 | template<typename To, typename From> |
135 | | -To sockaddr_cast(From from) { |
| 159 | +To sockaddr_cast(From f) { |
136 | 160 | #ifdef WILLOW_DEBUG |
137 | | - return sockaddr_caster<To, From>::cast(from); |
| 161 | + return sockaddr_caster<To, From>::cast(f); |
138 | 162 | #else |
139 | | - return reinterpret_cast<To>(from); |
| 163 | + return reinterpret_cast<To>(f); |
140 | 164 | #endif |
141 | 165 | } |
142 | 166 | |