[5284] | 1 | #ifndef ADDRESS_V_H_
|
---|
| 2 | #define ADDRESS_V_H_
|
---|
| 3 |
|
---|
[6786] | 4 | #include <stdint.h>
|
---|
[5284] | 5 | #include <string>
|
---|
| 6 | #include <iostream>
|
---|
| 7 |
|
---|
| 8 | #include "vfacade.hpp"
|
---|
| 9 |
|
---|
| 10 | #include "to_bytes_v.hpp"
|
---|
| 11 | #include "to_string_v.hpp"
|
---|
| 12 | #include "comparable_v.hpp"
|
---|
| 13 |
|
---|
| 14 | #include "../detail/address_convenience.hpp"
|
---|
| 15 |
|
---|
[12060] | 16 | #include <boost/shared_ptr.hpp>
|
---|
| 17 |
|
---|
[5284] | 18 | namespace ariba {
|
---|
| 19 | namespace addressing {
|
---|
| 20 |
|
---|
| 21 | using std::string;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * A virtual interface to a protocol address.
|
---|
| 25 | *
|
---|
| 26 | * @author Sebastian Mies <mies@tm.uka.de>
|
---|
| 27 | */
|
---|
| 28 | class address_v: public detail::address_convenience<address_v> {
|
---|
| 29 | public:
|
---|
[12060] | 30 | typedef boost::shared_ptr<address_v> shared_ptr;
|
---|
| 31 |
|
---|
[10789] | 32 | virtual ~address_v() {}
|
---|
| 33 |
|
---|
[5284] | 34 | //--- to_string_v ---------------------------------------------------------
|
---|
| 35 |
|
---|
| 36 | /// convert address to a string that can be used to reconstruct the address
|
---|
| 37 | virtual string to_string() const = 0;
|
---|
| 38 |
|
---|
| 39 | /// Assigns an address using a human-readable
|
---|
| 40 | virtual bool assign(const std::string& text) = 0;
|
---|
| 41 |
|
---|
| 42 | //--- to_bytes_v ----------------------------------------------------------
|
---|
| 43 |
|
---|
| 44 | /// returns true, if this address has a fixed size in bytes
|
---|
| 45 | virtual bool is_bytes_size_static() const = 0;
|
---|
| 46 |
|
---|
| 47 | /// returns the number of bytes used for serialization of this address
|
---|
| 48 | virtual size_t to_bytes_size() const = 0;
|
---|
| 49 |
|
---|
| 50 | /// converts this address to a binary representation
|
---|
| 51 | virtual void to_bytes(uint8_t* bytes) const = 0;
|
---|
| 52 |
|
---|
| 53 | /// Assigns an address using a bunch of bytes
|
---|
| 54 | virtual bool assign(const uint8_t* bytes, size_t size) = 0;
|
---|
| 55 |
|
---|
| 56 | //--- comparable_v --------------------------------------------------------
|
---|
| 57 |
|
---|
| 58 | /// implements comparison operators
|
---|
| 59 | virtual int compare_to(const address_v& rhs) const = 0;
|
---|
| 60 |
|
---|
| 61 | //--- assignment ----------------------------------------------------------
|
---|
| 62 |
|
---|
| 63 | /// Assigns an address
|
---|
| 64 | virtual bool assign(const address_v& rhs) = 0;
|
---|
| 65 |
|
---|
| 66 | //--- address info --------------------------------------------------------
|
---|
| 67 |
|
---|
| 68 | /// returns the name of the address
|
---|
| 69 | virtual const string& type_name() const = 0;
|
---|
| 70 |
|
---|
| 71 | /// sets the type id, if possible
|
---|
| 72 | virtual uint16_t type_id() const = 0;
|
---|
| 73 |
|
---|
| 74 | //--- cloneing and conversion ---------------------------------------------
|
---|
| 75 |
|
---|
| 76 | /// Obtain the underlaying data type or null if it does not match the type
|
---|
| 77 | virtual void* data(const std::type_info& type) = 0;
|
---|
| 78 |
|
---|
| 79 | /// Clones this address
|
---|
| 80 | virtual address_v* clone() const = 0;
|
---|
| 81 |
|
---|
| 82 | //--- convenience ---------------------------------------------------------
|
---|
| 83 |
|
---|
| 84 | template<class T>
|
---|
| 85 | bool instanceof() const {
|
---|
| 86 | void* value = const_cast<address_v*>(this)->data(typeid(T));
|
---|
| 87 | return value!=NULL;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /// cast operator to detailed type
|
---|
| 91 | template<class T>
|
---|
| 92 | inline operator T&() {
|
---|
| 93 | void* value = data(typeid(T));
|
---|
| 94 | assert (value!=NULL);
|
---|
| 95 | return *((T*) value);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /// cast operator to detailed type
|
---|
| 99 | template<class T>
|
---|
| 100 | inline operator T () const {
|
---|
| 101 | return T( (const T&)*this );
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /// cast operator to detailed type
|
---|
| 105 | template<class T>
|
---|
| 106 | inline operator const T& () const {
|
---|
| 107 | void* value = const_cast<address_v*>(this)->data(typeid(T));
|
---|
| 108 | assert (value!=NULL);
|
---|
| 109 | return *((const T*) value);
|
---|
| 110 | }
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | /// stream operator
|
---|
| 114 | std::ostream& operator<<(std::ostream& s, const address_v* addr);
|
---|
| 115 |
|
---|
| 116 | /// define the virtual facade for the address
|
---|
| 117 | typedef vfacade<address_v> address_vf;
|
---|
| 118 |
|
---|
| 119 | }} // namespace ariba::addressing
|
---|
| 120 |
|
---|
| 121 | /// the virtual adaptor to certain class of objects
|
---|
| 122 | template<class NonVirtual, class AdaptorType>
|
---|
| 123 | class vobject_hull<NonVirtual, ariba::addressing::address_v, AdaptorType> : public ariba::addressing::address_v {
|
---|
| 124 |
|
---|
| 125 | private:
|
---|
| 126 | typedef ariba::addressing::address_v self;
|
---|
| 127 | typename AdaptorType::template adaptor_type<NonVirtual> obj;
|
---|
| 128 |
|
---|
| 129 | static inline NonVirtual& conv(ariba::addressing::address_v* obj) {
|
---|
| 130 | return *((NonVirtual*) obj->data(typeid(NonVirtual)));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static inline const NonVirtual& conv(const ariba::addressing::address_v* obj) {
|
---|
| 134 | const NonVirtual* v =
|
---|
| 135 | (const NonVirtual*) const_cast<ariba::addressing::address_v*> (obj)->data(
|
---|
| 136 | typeid(NonVirtual));
|
---|
| 137 | return *v;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public:
|
---|
| 141 | template<typename T>
|
---|
| 142 | explicit vobject_hull(T& obj) :
|
---|
| 143 | obj(obj) {
|
---|
| 144 | }
|
---|
| 145 | explicit vobject_hull() :
|
---|
| 146 | obj() {
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | //--- assignment ----------------------------------------------------------
|
---|
| 150 |
|
---|
| 151 | /// Assigns an address
|
---|
| 152 | virtual bool assign(const self& rhs) {
|
---|
| 153 | return obj->assign(conv(&rhs));
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //--- address info --------------------------------------------------------
|
---|
| 157 |
|
---|
| 158 | /// returns the name of the address
|
---|
| 159 | virtual const string& type_name() const {
|
---|
| 160 | return obj->type_name();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /// returns the id of the address
|
---|
| 164 | virtual uint16_t type_id() const {
|
---|
| 165 | return obj->type_id();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /// returns a capsule of the object
|
---|
| 169 | virtual address_v* clone() const {
|
---|
| 170 | return vcapsule<address_v> (*obj);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /// Obtain the underlaying data type or null if it does not match the type
|
---|
| 174 | virtual void* data(const std::type_info& type) {
|
---|
| 175 | if (typeid(NonVirtual)!=type) return NULL;
|
---|
| 176 | return &(*obj);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | // to_string_v
|
---|
| 180 |
|
---|
| 181 | /// convert address to a string that can be used to reconstruct the address
|
---|
| 182 | virtual string to_string() const {
|
---|
| 183 | return obj->to_string();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /// Assigns an address using a human-readable
|
---|
| 187 | virtual bool assign(const std::string& text) {
|
---|
| 188 | return obj->assign(text);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | // comparable_v
|
---|
| 192 |
|
---|
| 193 | /// implements comparison operators
|
---|
| 194 | virtual int compare_to(const self& rhs) const {
|
---|
| 195 | const address_v* compare = dynamic_cast<const address_v*> (&rhs);
|
---|
| 196 | return obj->compare_to(conv(compare));
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | // to_bytes_v
|
---|
| 200 |
|
---|
| 201 | /// returns true, if this address has a fixed size in bytes
|
---|
| 202 | virtual bool is_bytes_size_static() const {
|
---|
| 203 | return obj->is_bytes_size_static();
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /// returns the number of bytes used for serialization of this address
|
---|
| 207 | virtual size_t to_bytes_size() const {
|
---|
| 208 | return obj->to_bytes_size();
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /// converts this address to a binary representation
|
---|
| 212 | virtual void to_bytes(uint8_t* bytes) const {
|
---|
| 213 | obj->to_bytes(bytes);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /// Assigns an address using a bunch of bytes
|
---|
| 217 | virtual bool assign(const uint8_t* bytes, size_t size) {
|
---|
| 218 | return obj->assign(bytes, size);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | };
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | #endif /* ADDRESS_V_H_ */
|
---|