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