00001 // port_address.hpp, created on 24.06.2009 by Sebastian Mies 00002 00003 #ifndef PORT_ADDRESS_HPP_ 00004 #define PORT_ADDRESS_HPP_ 00005 00006 #include<string> 00007 #include<boost/tr1/functional.hpp> 00008 00009 #include "detail/address_convenience.hpp" 00010 00011 namespace ariba { 00012 namespace addressing { 00013 00014 struct port_address_info { 00015 static const uint16_t type_id; 00016 static const std::string type_name; 00017 }; 00018 00024 template<class AddressInfo = port_address_info> 00025 class port_address_tpl: public detail::address_convenience< 00026 port_address_tpl<AddressInfo> > { 00027 private: 00028 uint16_t port; 00029 00030 public: 00031 typedef port_address_tpl<AddressInfo> self; 00032 00033 port_address_tpl() { 00034 port = 0; 00035 } 00036 00037 port_address_tpl( const port_address_tpl& copy ) : port(copy.port) { 00038 00039 } 00040 00041 port_address_tpl(const std::string& text) { 00042 assign( text ); 00043 } 00044 00045 port_address_tpl(const char* text) { 00046 assign( std::string(text) ); 00047 } 00048 00049 port_address_tpl(uint16_t port) : port(port) { 00050 00051 } 00052 00053 port_address_tpl(const uint8_t* bytes, size_t size) { 00054 assign( bytes, size ); 00055 } 00056 00057 //--- compare operations -------------------------------------------------- 00058 00060 int compare_to(const self& rhs) const { 00061 return port - rhs.port; 00062 } 00063 00064 //--- bytes representation ------------------------------------------------ 00065 00067 bool is_bytes_size_static() const { 00068 return true; 00069 } 00070 00072 size_t to_bytes_size() const { 00073 return 2; 00074 } 00075 00077 void to_bytes(uint8_t* bytes) const { 00078 bytes[0] = port >> 8; 00079 bytes[1] = port & 0xFF; 00080 } 00081 00083 bool assign(const uint8_t* bytes, size_t size) { 00084 port = ((bytes[0] << 8) + bytes[1]); 00085 return false; 00086 } 00087 00088 //--- text representation ------------------------------------------------- 00089 00091 std::string to_string() const { 00092 char str[8]; 00093 sprintf(str, "%d", port); 00094 return std::string(str); 00095 } 00096 00098 bool assign(const std::string& text) { 00099 unsigned int port_; 00100 sscanf(text.c_str(), "%d", &port_); 00101 if (port_ >= 0 && port_ <= 65535) { 00102 port = (uint16_t) port_; 00103 return false; 00104 } 00105 return true; 00106 } 00107 00108 //--- assignment ---------------------------------------------------------- 00109 00111 bool assign(const self& rhs) { 00112 port = rhs.port; 00113 return false; 00114 } 00115 00116 //--- address info -------------------------------------------------------- 00117 00119 const std::string& type_name() const { 00120 return AddressInfo::type_name; 00121 } 00122 00124 const uint16_t type_id() const { 00125 return AddressInfo::type_id; 00126 } 00127 00128 //--- conversions --------------------------------------------------------- 00129 00130 uint16_t asio() const { 00131 return port; 00132 } 00133 00134 void asio( uint16_t port ) { 00135 this->port = port; 00136 } 00137 00138 uint16_t value() const { 00139 return port; 00140 } 00141 00142 void value( uint16_t v ) { 00143 port = v; 00144 } 00145 }; 00146 00147 typedef port_address_tpl<> port_address; 00148 00149 }} // namespace ariba::addressing 00150 00151 namespace boost { 00152 00153 template<class T> 00154 struct hash<ariba::addressing::port_address_tpl<T> >: public std::unary_function<ariba::addressing::port_address_tpl<T>, std::size_t> { 00155 std::size_t operator()(const ariba::addressing::port_address_tpl<T>& ep) const { 00156 return hash_value(ep.value()); 00157 } 00158 }; 00159 00160 } 00161 00162 #endif /* PORT_ADDRESS_HPP_ */