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