Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60687 - trunk/boost/asio/ip
From: chris_at_[hidden]
Date: 2010-03-18 02:23:39


Author: chris_kohlhoff
Date: 2010-03-18 02:23:38 EDT (Thu, 18 Mar 2010)
New Revision: 60687
URL: http://svn.boost.org/trac/boost/changeset/60687

Log:
Use a bitmask type for the resolver flags, as per the TR2 proposal. This will
prevent implicit conversion from int to flags, allowing the compiler to catch
cases where users incorrectly pass a numeric port number as the service name.

Text files modified:
   trunk/boost/asio/ip/basic_resolver_query.hpp | 17 ++++---
   trunk/boost/asio/ip/resolver_query_base.hpp | 89 +++++++++++++++++++++++++++++++--------
   2 files changed, 78 insertions(+), 28 deletions(-)

Modified: trunk/boost/asio/ip/basic_resolver_query.hpp
==============================================================================
--- trunk/boost/asio/ip/basic_resolver_query.hpp (original)
+++ trunk/boost/asio/ip/basic_resolver_query.hpp 2010-03-18 02:23:38 EDT (Thu, 18 Mar 2010)
@@ -48,13 +48,13 @@
 
   /// Construct with specified service name for any protocol.
   basic_resolver_query(const std::string& service_name,
- int flags = passive | address_configured)
+ resolver_query_base::flags resolve_flags = passive | address_configured)
     : hints_(),
       host_name_(),
       service_name_(service_name)
   {
     typename InternetProtocol::endpoint endpoint;
- hints_.ai_flags = flags;
+ hints_.ai_flags = static_cast<int>(resolve_flags);
     hints_.ai_family = PF_UNSPEC;
     hints_.ai_socktype = endpoint.protocol().type();
     hints_.ai_protocol = endpoint.protocol().protocol();
@@ -67,12 +67,12 @@
   /// Construct with specified service name for a given protocol.
   basic_resolver_query(const protocol_type& protocol,
       const std::string& service_name,
- int flags = passive | address_configured)
+ resolver_query_base::flags resolve_flags = passive | address_configured)
     : hints_(),
       host_name_(),
       service_name_(service_name)
   {
- hints_.ai_flags = flags;
+ hints_.ai_flags = static_cast<int>(resolve_flags);
     hints_.ai_family = protocol.family();
     hints_.ai_socktype = protocol.type();
     hints_.ai_protocol = protocol.protocol();
@@ -84,13 +84,14 @@
 
   /// Construct with specified host name and service name for any protocol.
   basic_resolver_query(const std::string& host_name,
- const std::string& service_name, int flags = address_configured)
+ const std::string& service_name,
+ resolver_query_base::flags resolve_flags = address_configured)
     : hints_(),
       host_name_(host_name),
       service_name_(service_name)
   {
     typename InternetProtocol::endpoint endpoint;
- hints_.ai_flags = flags;
+ hints_.ai_flags = static_cast<int>(resolve_flags);
     hints_.ai_family = PF_UNSPEC;
     hints_.ai_socktype = endpoint.protocol().type();
     hints_.ai_protocol = endpoint.protocol().protocol();
@@ -103,12 +104,12 @@
   /// Construct with specified host name and service name for a given protocol.
   basic_resolver_query(const protocol_type& protocol,
       const std::string& host_name, const std::string& service_name,
- int flags = address_configured)
+ resolver_query_base::flags resolve_flags = address_configured)
     : hints_(),
       host_name_(host_name),
       service_name_(service_name)
   {
- hints_.ai_flags = flags;
+ hints_.ai_flags = static_cast<int>(resolve_flags);
     hints_.ai_family = protocol.family();
     hints_.ai_socktype = protocol.type();
     hints_.ai_protocol = protocol.protocol();

Modified: trunk/boost/asio/ip/resolver_query_base.hpp
==============================================================================
--- trunk/boost/asio/ip/resolver_query_base.hpp (original)
+++ trunk/boost/asio/ip/resolver_query_base.hpp 2010-03-18 02:23:38 EDT (Thu, 18 Mar 2010)
@@ -35,58 +35,107 @@
 {
 public:
 #if defined(GENERATING_DOCUMENTATION)
+ /// A bitmask type (C++ Std [lib.bitmask.types]).
+ typedef unspecified flags;
+
   /// Determine the canonical name of the host specified in the query.
- static const int canonical_name = implementation_defined;
+ static const flags canonical_name = implementation_defined;
 
   /// Indicate that returned endpoint is intended for use as a locally bound
   /// socket endpoint.
- static const int passive = implementation_defined;
+ static const flags passive = implementation_defined;
 
   /// Host name should be treated as a numeric string defining an IPv4 or IPv6
   /// address and no name resolution should be attempted.
- static const int numeric_host = implementation_defined;
+ static const flags numeric_host = implementation_defined;
 
   /// Service name should be treated as a numeric string defining a port number
   /// and no name resolution should be attempted.
- static const int numeric_service = implementation_defined;
+ static const flags numeric_service = implementation_defined;
 
   /// If the query protocol family is specified as IPv6, return IPv4-mapped
   /// IPv6 addresses on finding no IPv6 addresses.
- static const int v4_mapped = implementation_defined;
+ static const flags v4_mapped = implementation_defined;
 
   /// If used with v4_mapped, return all matching IPv6 and IPv4 addresses.
- static const int all_matching = implementation_defined;
+ static const flags xll_matching = implementation_defined;
 
   /// Only return IPv4 addresses if a non-loopback IPv4 address is configured
   /// for the system. Only return IPv6 addresses if a non-loopback IPv6 address
   /// is configured for the system.
- static const int address_configured = implementation_defined;
+ static const flags xddress_configured = implementation_defined;
 #else
- BOOST_STATIC_CONSTANT(int, canonical_name = AI_CANONNAME);
- BOOST_STATIC_CONSTANT(int, passive = AI_PASSIVE);
- BOOST_STATIC_CONSTANT(int, numeric_host = AI_NUMERICHOST);
+ enum flags
+ {
+ canonical_name = AI_CANONNAME,
+ passive = AI_PASSIVE,
+ numeric_host = AI_NUMERICHOST,
 # if defined(AI_NUMERICSERV)
- BOOST_STATIC_CONSTANT(int, numeric_service = AI_NUMERICSERV);
+ numeric_service = AI_NUMERICSERV,
 # else
- BOOST_STATIC_CONSTANT(int, numeric_service = 0);
+ numeric_service = 0,
 # endif
- // Note: QNX Neutrino 6.3 defines AI_V4MAPPED, AI_ALL and AI_ADDRCONFIG but
- // does not implement them. Therefore they are specifically excluded here.
+ // Note: QNX Neutrino 6.3 defines AI_V4MAPPED, AI_ALL and AI_ADDRCONFIG but
+ // does not implement them. Therefore they are specifically excluded here.
 # if defined(AI_V4MAPPED) && !defined(__QNXNTO__)
- BOOST_STATIC_CONSTANT(int, v4_mapped = AI_V4MAPPED);
+ v4_mapped = AI_V4MAPPED,
 # else
- BOOST_STATIC_CONSTANT(int, v4_mapped = 0);
+ v4_mapped = 0,
 # endif
 # if defined(AI_ALL) && !defined(__QNXNTO__)
- BOOST_STATIC_CONSTANT(int, all_matching = AI_ALL);
+ all_matching = AI_ALL,
 # else
- BOOST_STATIC_CONSTANT(int, all_matching = 0);
+ all_matching = 0,
 # endif
 # if defined(AI_ADDRCONFIG) && !defined(__QNXNTO__)
- BOOST_STATIC_CONSTANT(int, address_configured = AI_ADDRCONFIG);
+ address_configured = AI_ADDRCONFIG
 # else
- BOOST_STATIC_CONSTANT(int, address_configured = 0);
+ address_configured = 0
 # endif
+ };
+
+ // Implement bitmask operations as shown in C++ Std [lib.bitmask.types].
+
+ friend flags operator&(flags x, flags y)
+ {
+ return static_cast<flags>(
+ static_cast<unsigned int>(x) & static_cast<unsigned int>(y));
+ }
+
+ friend flags operator|(flags x, flags y)
+ {
+ return static_cast<flags>(
+ static_cast<unsigned int>(x) | static_cast<unsigned int>(y));
+ }
+
+ friend flags operator^(flags x, flags y)
+ {
+ return static_cast<flags>(
+ static_cast<unsigned int>(x) ^ static_cast<unsigned int>(y));
+ }
+
+ friend flags operator~(flags x)
+ {
+ return static_cast<flags>(static_cast<unsigned int>(~x));
+ }
+
+ friend flags& operator&=(flags& x, flags y)
+ {
+ x = x & y;
+ return x;
+ }
+
+ friend flags& operator|=(flags& x, flags y)
+ {
+ x = x | y;
+ return x;
+ }
+
+ friend flags& operator^=(flags& x, flags y)
+ {
+ x = x ^ y;
+ return x;
+ }
 #endif
 
 protected:


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk