source: source/ariba/utility/addressing/facades/address_v.hpp@ 9992

Last change on this file since 9992 was 6786, checked in by mies, 14 years ago

Changed Data to Message conversion constructor in Message to explicit
Fixed some general bugs in Data: operator<<
Fixed bug in DHTMessage: allow unspecified key/values
Added local DHT message delivery
Adapted sources to work with gcc 4.4.1

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