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