Commit 653a7cdc4d979cbf25aa7af6346c74508168940f

Authored by Perry Werneck
1 parent e886a427
Exists in master and in 1 other branch develop

Implementing object attributes.

client/src/core/attribute.cc
... ... @@ -47,188 +47,53 @@
47 47  
48 48 namespace TN3270 {
49 49  
50   - Attribute::~Attribute() {
51   - }
52   -
53   - std::string Attribute::getString() const {
54   - throw std::system_error(ENOTSUP, std::system_category());
55   - }
56   -
57   - int32_t Attribute::getInt32() const {
58   - throw std::system_error(ENOTSUP, std::system_category());
59   - }
60   -
61   - uint32_t Attribute::getUint32() const {
62   - throw std::system_error(ENOTSUP, std::system_category());
63   - }
64   -
65   - bool Attribute::getBool() const {
66   - throw std::system_error(ENOTSUP, std::system_category());
67   - }
68   -
69   -
70   - /*
71   - class StringProperty : public Property, std::string {
72   - public:
73   - StringProperty(const char *str) : Property(Property::String), std::string(str) {
74   - }
  50 + Attribute::Attribute(H3270 *hSession, Type type, void * worker) {
75 51  
76   - StringProperty(const std::string &str) : Property(Property::String), std::string(str) {
77   - }
  52 + this->hSession = hSession;
  53 + this->type = type;
  54 + this->worker = worker;
78 55  
79   - std::string toString() const override {
80   - return std::string(this->c_str());
81   - }
  56 + get.asString = [](const Attribute &attr, const void *worker) {
82 57  
83   - int32_t toInt32() const override {
84   - return (int32_t) atoi(this->c_str());
85   - }
86   -
87   - uint32_t toUint32() const override {
88   - return (uint32_t) atoi(this->c_str());
89   - }
90   -
91   - bool toBool() const override {
92   - return atoi(this->c_str()) != 0;
93   - }
94   -
95   - };
96   -
97   - Property::Property(Property::Type type) {
98   - this->type = type;
99   -
100   - }
101   -
102   - Property::~Property() {
103   - }
  58 + throw std::system_error(ENOTSUP, std::system_category());
  59 + return "";
  60 + };
104 61  
105   - std::string Property::toString() const {
106   - throw runtime_error("The value can't be converted to string");
107   - }
  62 + get.asInt32 = [](const Attribute &attr, const void *worker) {
  63 + throw std::system_error(ENOTSUP, std::system_category());
  64 + return (int32_t) 0;
  65 + };
108 66  
109   - int32_t Property::toInt32() const {
110   - throw runtime_error("The value can't be converted to a signed integer");
111   - }
  67 + get.asUint32 = [](const Attribute &attr, const void *worker) {
  68 + return (uint32_t) attr.get.asInt32(attr, worker);
  69 + };
112 70  
113   - uint32_t Property::toUint32() const {
114   - throw runtime_error("The value can't be converted to an unsigned integer");
115   - }
  71 + get.asBoolean = [](const Attribute &attr, const void *worker) {
  72 + return (bool) attr.get.asInt32(attr, worker) != 0;
  73 + };
116 74  
117   - bool Property::toBool() const {
118   - throw runtime_error("The value can't be converted to boolean");
119 75 }
120 76  
121   -
122   - Property * Property::create(const char *str) {
123   - return new StringProperty(str);
  77 + Attribute::~Attribute() {
124 78 }
125 79  
126   - Property * Property::create(const std::string &str) {
127   - return new StringProperty(str);
  80 + /*
  81 + std::string Attribute::getString() const {
  82 + printf("*************** %s\n",__FUNCTION__);
  83 + throw std::system_error(ENOTSUP, std::system_category());
128 84 }
129 85  
130   - Property * Property::create(const int value) {
131   -
132   - class Value : public Property {
133   - private:
134   - int32_t value;
135   -
136   - public:
137   - Value(int value) : Property(Property::Int32) {
138   - this->value = (int32_t) value;
139   - }
140   -
141   - std::string toString() const override {
142   - return std::to_string(value);
143   - }
144   -
145   - int32_t toInt32() const override {
146   - return (int32_t) value;
147   - }
148   -
149   - uint32_t toUint32() const override {
150   - return (uint32_t) value;
151   - }
152   -
153   - bool toBool() const override {
154   - return value != 0;
155   - }
156   -
157   - };
158   -
159   - return new Value(value);
160   -
  86 + int32_t Attribute::getInt32() const {
  87 + throw std::system_error(ENOTSUP, std::system_category());
161 88 }
162 89  
163   - Property * Property::create(const unsigned int value) {
164   -
165   - class Value : public Property {
166   - private:
167   - uint32_t value;
168   -
169   - public:
170   - Value(unsigned int value) : Property(Property::Uint32) {
171   - this->value = (uint32_t) value;
172   - }
173   -
174   - std::string toString() const override {
175   - return std::to_string(value);
176   - }
177   -
178   - int32_t toInt32() const override {
179   - return (int32_t) value;
180   - }
181   -
182   - uint32_t toUint32() const override {
183   - return (uint32_t) value;
184   - }
185   -
186   - bool toBool() const override {
187   - return value != 0;
188   - }
189   -
190   - };
191   -
192   - return new Value(value);
193   -
  90 + uint32_t Attribute::getUint32() const {
  91 + throw std::system_error(ENOTSUP, std::system_category());
194 92 }
195 93  
196   - Property * Property::create(const bool value) {
197   -
198   - class Value : public Property {
199   - private:
200   - bool value;
201   -
202   - public:
203   - Value(bool value) : Property(Property::Boolean) {
204   - this->value = value;
205   - }
206   -
207   - std::string toString() const override {
208   - return std::to_string(value);
209   - }
210   -
211   - int32_t toInt32() const override {
212   - return (int32_t) value;
213   - }
214   -
215   - uint32_t toUint32() const override {
216   - return (uint32_t) value;
217   - }
218   -
219   - bool toBool() const override {
220   - return value;
221   - }
222   -
223   - };
224   -
225   - return new Value(value);
226   -
  94 + bool Attribute::getBool() const {
  95 + throw std::system_error(ENOTSUP, std::system_category());
227 96 }
228 97 */
229 98  
230   -
231 99 }
232   -
233   -
234   -
... ...
client/src/core/session.cc
... ... @@ -409,84 +409,24 @@
409 409 throw std::system_error(ENOTSUP, std::system_category());
410 410 }
411 411  
412   - Attribute * Session::getAttribute(const char *name) const {
  412 + Attribute Session::getAttribute(const char *name) const {
413 413 throw std::system_error(ENOTSUP, std::system_category());
414 414 }
415 415  
416 416 void Session::getAttribute(const char *name, int &value) const {
417   -
418   - Attribute *attr = getAttribute(name);
419   -
420   - try {
421   -
422   - value = attr->getInt32();
423   -
424   - } catch(...) {
425   -
426   - delete attr;
427   - throw;
428   -
429   - }
430   -
431   - delete attr;
432   -
  417 + value = getAttribute(name).getInt32();
433 418 }
434 419  
435 420 void Session::getAttribute(const char *name, unsigned int &value) const {
436   -
437   - Attribute *attr = getAttribute(name);
438   -
439   - try {
440   -
441   - value = attr->getUint32();
442   -
443   - } catch(...) {
444   -
445   - delete attr;
446   - throw;
447   -
448   - }
449   -
450   - delete attr;
451   -
  421 + value = getAttribute(name).getUint32();
452 422 }
453 423  
454 424 void Session::getAttribute(const char *name, std::string &value) const {
455   -
456   - Attribute *attr = getAttribute(name);
457   -
458   - try {
459   -
460   - value.assign(attr->getString().c_str());
461   -
462   - } catch(...) {
463   -
464   - delete attr;
465   - throw;
466   -
467   - }
468   -
469   - delete attr;
470   -
  425 + value.assign(value = getAttribute(name).getString().c_str());
471 426 }
472 427  
473 428 void Session::getAttribute(const char *name, bool &value) const {
474   -
475   - Attribute *attr = getAttribute(name);
476   -
477   - try {
478   -
479   - value = attr->getBool();
480   -
481   - } catch(...) {
482   -
483   - delete attr;
484   - throw;
485   -
486   - }
487   -
488   - delete attr;
489   -
  429 + value = getAttribute(name).getBoolean();
490 430 }
491 431  
492 432  
... ...
client/src/host/properties.cc
... ... @@ -40,7 +40,7 @@
40 40  
41 41 /*---[ Implement ]----------------------------------------------------------------------------------*/
42 42  
43   -TN3270::Attribute * TN3270::Host::getAttribute(const char *name) const {
  43 +TN3270::Attribute TN3270::Host::getAttribute(const char *name) const {
44 44  
45 45 if(!this->session)
46 46 throw std::system_error(ENODATA, std::system_category());
... ...
client/src/include/lib3270/ipc.h
... ... @@ -259,22 +259,47 @@
259 259 Uint64 = 't'
260 260 };
261 261  
262   - private:
263   - Type type;
  262 + Type type; ///< @brief Data type.
  263 + H3270 * hSession; ///< @brief TN3270 Session which "owns" this attribute.
  264 + void * worker; ///< @brief Internal worker.
264 265  
265 266 protected:
266   - Attribute(Type type) {
267   - this->type = type;
268   - }
  267 +
  268 + struct {
  269 + std::function <std::string (const Attribute & attr, const void *worker)> asString;
  270 + std::function <int32_t (const Attribute & attr, const void *worker)> asInt32;
  271 + std::function <uint32_t (const Attribute & attr, const void *worker)> asUint32;
  272 + std::function <bool (const Attribute & attr, const void *worker)> asBoolean;
  273 + } get;
  274 +
  275 + Attribute(H3270 *hSession, Type type, void * worker);
269 276  
270 277 public:
  278 + ~Attribute();
271 279  
272   - virtual std::string getString() const;
273   - virtual int32_t getInt32() const;
274   - virtual uint32_t getUint32() const;
275   - virtual bool getBool() const;
  280 + inline const H3270 * getTN3270Session() const {
  281 + return this->hSession;
  282 + }
  283 +
  284 + inline std::string getString() const {
  285 + return get.asString(*this,worker);
  286 + }
276 287  
277   - virtual ~Attribute();
  288 + inline int32_t getInt32() const {
  289 + return get.asInt32(*this,worker);
  290 + }
  291 +
  292 + inline uint32_t getUint32() const {
  293 + return get.asUint32(*this,worker);
  294 + }
  295 +
  296 + inline bool getBoolean() const {
  297 + return get.asBoolean(*this,worker);
  298 + }
  299 +
  300 + inline std::string toString() const {
  301 + return get.asString(*this, worker);
  302 + }
278 303  
279 304 inline bool operator==(Type type) const noexcept {
280 305 return this->type == type;
... ... @@ -401,7 +426,7 @@
401 426 LIB3270_KEYBOARD_LOCK_STATE input(const std::string &str, const char control_char = '@');
402 427  
403 428 // Attributes
404   - virtual Attribute * getAttribute(const char *name) const;
  429 + virtual Attribute getAttribute(const char *name) const;
405 430  
406 431 virtual void getAttribute(const char *name, int &value) const;
407 432 virtual void getAttribute(const char *name, unsigned int &value) const;
... ... @@ -635,9 +660,9 @@
635 660  
636 661  
637 662 // Get properties
638   - Attribute * getAttribute(const char *name) const;
  663 + Attribute getAttribute(const char *name) const;
639 664  
640   - inline Attribute * operator[](const char *name) const {
  665 + inline Attribute operator[](const char *name) const {
641 666 return getAttribute(name);
642 667 }
643 668  
... ... @@ -829,6 +854,10 @@
829 854 return stream;
830 855 }
831 856  
  857 + inline std::ostream & operator<<(std::ostream &stream, const TN3270::Attribute& attribute) {
  858 + stream << attribute.toString();
  859 + return stream;
  860 + }
832 861  
833 862 #endif
834 863  
... ...
client/src/session/local/attribute.cc
... ... @@ -46,143 +46,141 @@
46 46  
47 47 namespace TN3270 {
48 48  
49   - Attribute * Local::Session::getAttribute(const char *name) const {
  49 + Attribute Local::Session::getAttribute(const char *name) const {
50 50  
51 51 // Signed int attribute
52 52 class IntAttribute : public Attribute {
53   - private:
54   - H3270 * hSession;
55   - const LIB3270_INT_PROPERTY * descriptor;
56   -
57 53 public:
58   - IntAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *descriptor) : Attribute(Attribute::Int32) {
59   - this->hSession = hSession;
60   - this->descriptor = descriptor;
61   - }
  54 + IntAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : Attribute(hSession, Attribute::Int32, (void *) worker) {
62 55  
63   - std::string getString() const override {
64   - return std::to_string(getInt32());
65   - }
  56 + get.asString = [](const Attribute & attr, const void *worker) {
  57 + return std::to_string(attr.getInt32());
  58 + };
66 59  
67   - int32_t getInt32() const override {
  60 + get.asInt32 = [](const Attribute & attr, const void *worker) {
68 61  
69   - errno = 0;
70   - int value = descriptor->get(hSession);
  62 + errno = 0;
  63 + int value = ((const LIB3270_INT_PROPERTY *) worker)->get(attr.getTN3270Session());
71 64  
72   - if(errno != 0) {
73   - throw std::system_error(errno, std::system_category());
74   - }
  65 + if(errno != 0) {
  66 + throw std::system_error(errno, std::system_category());
  67 + }
75 68  
76   - return ((int32_t) value) != 0;
  69 + return (int32_t) value;
77 70  
78   - }
  71 + };
79 72  
80   - uint32_t getUint32() const override {
81   - return (uint32_t) descriptor->get(this->hSession);
82   - }
  73 + get.asUint32 = [](const Attribute & attr, const void *worker) {
  74 + return (uint32_t) attr.getInt32();
  75 + };
  76 +
  77 + get.asBoolean = [](const Attribute & attr, const void *worker) {
  78 + return (attr.getInt32() != 0);
  79 + };
83 80  
84   - bool getBool() const override {
85   - return getInt32() != 0;
86 81 }
87 82  
88 83 };
89 84  
90 85 // Unsigned int attribute
91 86 class UnsignedIntAttribute : public Attribute {
92   - private:
93   - H3270 * hSession;
94   - const LIB3270_UINT_PROPERTY * descriptor;
95   -
96 87 public:
97   - UnsignedIntAttribute(H3270 *hSession, const LIB3270_UINT_PROPERTY *descriptor) : Attribute(Attribute::Uint32) {
98   - this->hSession = hSession;
99   - this->descriptor = descriptor;
100   - }
  88 + UnsignedIntAttribute(H3270 *hSession, const LIB3270_UINT_PROPERTY *worker) : Attribute(hSession, Attribute::Uint32, (void *) worker) {
101 89  
102   - std::string getString() const override {
103   - return std::to_string(getInt32());
104   - }
  90 + get.asString = [](const Attribute & attr, const void *worker) {
  91 + return std::to_string(attr.getUint32());
  92 + };
105 93  
106   - int32_t getInt32() const override {
  94 + get.asInt32 = [](const Attribute & attr, const void *worker) {
  95 + return (int32_t) attr.getUint32();
  96 + };
107 97  
108   - return (int32_t) getUint32();
109   - }
  98 + get.asUint32 = [](const Attribute & attr, const void *worker) {
110 99  
111   - uint32_t getUint32() const override {
  100 + errno = 0;
  101 + unsigned int value = ((const LIB3270_UINT_PROPERTY *) worker)->get(attr.getTN3270Session());
112 102  
113   - errno = 0;
114   - unsigned int value = descriptor->get(hSession);
  103 + if(errno != 0) {
  104 + throw std::system_error(errno, std::system_category());
  105 + }
115 106  
116   - if(errno != 0) {
117   - throw std::system_error(errno, std::system_category());
118   - }
  107 + return (uint32_t) value;
119 108  
120   - return ((uint32_t) value) != 0;
121   - }
  109 + };
  110 +
  111 + get.asBoolean = [](const Attribute & attr, const void *worker) {
  112 + return (attr.getUint32() != 0);
  113 + };
122 114  
123   - bool getBool() const override {
124   - return getUint32() != 0;
125 115 }
126 116  
127 117 };
128 118  
129 119 // String attribute
130 120 class StringAttribute : public Attribute {
131   - private:
132   - H3270 * hSession;
133   - const LIB3270_STRING_PROPERTY * descriptor;
134   -
135 121 public:
136   - StringAttribute(H3270 *hSession, const LIB3270_STRING_PROPERTY *descriptor) : Attribute(Attribute::String) {
137   - this->hSession = hSession;
138   - this->descriptor = descriptor;
139   - }
  122 + StringAttribute(H3270 *hSession, const LIB3270_STRING_PROPERTY *worker) : Attribute(hSession, Attribute::String, (void *) worker) {
140 123  
141   - std::string getString() const override {
  124 + get.asString = [](const Attribute & attr, const void *worker) {
142 125  
143   - const char * str = descriptor->get(hSession);
  126 + const char * str = ((const LIB3270_STRING_PROPERTY *) worker)->get(attr.getTN3270Session());
144 127  
145   - if(str) {
146   - return string(str);
147   - }
  128 + if(str) {
  129 + return string(str);
  130 + }
148 131  
149   - throw std::system_error(errno, std::system_category());
  132 + throw std::system_error(errno, std::system_category());
150 133  
151   - }
  134 + };
152 135  
153   - int32_t getInt32() const override {
154   - return (int32_t) atoi(getString().c_str());
155   - }
  136 + get.asInt32 = [](const Attribute & attr, const void *worker) {
  137 +
  138 + const char * str = ((const LIB3270_STRING_PROPERTY *) worker)->get(attr.getTN3270Session());
  139 +
  140 + if(str) {
  141 + return (int32_t) atoi(str);
  142 + }
  143 +
  144 + throw std::system_error(errno, std::system_category());
  145 + };
156 146  
157   - uint32_t getUint32() const override {
158   - return (uint32_t) atol(getString().c_str());
159   - }
160 147  
161   - bool getBool() const override {
162   - return getUint32() != 0;
163 148 }
164 149  
165 150 };
166 151  
167 152 // Boolean attribute
168 153 class BooleanAttribute : public Attribute {
169   - H3270 * hSession;
170   - const LIB3270_INT_PROPERTY * descriptor;
171   -
172 154 public:
173   - BooleanAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *descriptor) : Attribute(Attribute::Boolean) {
174   - this->hSession = hSession;
175   - this->descriptor = descriptor;
  155 + BooleanAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : Attribute(hSession, Attribute::Boolean, (void *) worker) {
  156 +
  157 + get.asString = [](const Attribute & attr, const void *worker) {
  158 + return attr.getBoolean() ? "true" : "false";
  159 + };
  160 +
  161 + get.asInt32 = [](const Attribute & attr, const void *worker) {
  162 +
  163 + errno = 0;
  164 + int value = ((const LIB3270_INT_PROPERTY *) worker)->get(attr.getTN3270Session());
  165 +
  166 + if(errno != 0) {
  167 + throw std::system_error(errno, std::system_category());
  168 + }
  169 +
  170 + return (int32_t) value;
  171 +
  172 + };
  173 +
176 174 }
177 175  
  176 + /*
178 177 std::string getString() const override {
179   - return std::to_string(getInt32());
180 178 }
181 179  
182 180 int32_t getInt32() const override {
183 181  
184 182 errno = 0;
185   - int value = descriptor->get(hSession);
  183 + int value = ((const LIB3270_INT_PROPERTY *) worker)->get(hSession);
186 184  
187 185 if(errno != 0) {
188 186 throw std::system_error(errno, std::system_category());
... ... @@ -199,35 +197,7 @@
199 197 bool getBool() const override {
200 198 return getInt32() != 0;
201 199 }
202   -
203   - };
204   -
205   - // Toogle attribute
206   - class ToggleAttribute : public Attribute {
207   - H3270 * hSession;
208   - LIB3270_TOGGLE toggle;
209   -
210   - public:
211   - ToggleAttribute(H3270 *hSession, const LIB3270_TOGGLE toggle) : Attribute(Attribute::Boolean) {
212   - this->hSession = hSession;
213   - this->toggle = toggle;
214   - }
215   -
216   - std::string getString() const override {
217   - return (lib3270_get_toggle(hSession, toggle) ? "1" : "0");
218   - }
219   -
220   - int32_t getInt32() const override {
221   - return (int32_t) lib3270_get_toggle(hSession, toggle);
222   - }
223   -
224   - uint32_t getUint32() const override {
225   - return (uint32_t) lib3270_get_toggle(hSession, toggle);
226   - }
227   -
228   - bool getBool() const override {
229   - return (lib3270_get_toggle(hSession, toggle) != 0);
230   - }
  200 + */
231 201  
232 202 };
233 203  
... ... @@ -239,7 +209,7 @@
239 209 for(size_t ix = 0; intprop[ix].name; ix++) {
240 210  
241 211 if(!strcasecmp(name,intprop[ix].name)) {
242   - return new IntAttribute(hSession,&intprop[ix]);
  212 + return IntAttribute(hSession,&intprop[ix]);
243 213 }
244 214  
245 215 }
... ... @@ -251,7 +221,7 @@
251 221 for(size_t ix = 0; intprop[ix].name; ix++) {
252 222  
253 223 if(!strcasecmp(name,intprop[ix].name)) {
254   - return new UnsignedIntAttribute(hSession,&intprop[ix]);
  224 + return UnsignedIntAttribute(hSession,&intprop[ix]);
255 225 }
256 226  
257 227 }
... ... @@ -265,7 +235,7 @@
265 235 for(size_t ix = 0; strprop[ix].name; ix++) {
266 236  
267 237 if(!strcasecmp(name,strprop[ix].name)) {
268   - return new StringAttribute(hSession,&strprop[ix]);
  238 + return StringAttribute(hSession,&strprop[ix]);
269 239 }
270 240  
271 241 }
... ... @@ -278,7 +248,7 @@
278 248 if(toggle != (LIB3270_TOGGLE) -1) {
279 249  
280 250 // Is a Tn3270 toggle, get it!
281   - return new ToggleAttribute(hSession,toggle);
  251 + throw std::system_error(ENOTSUP, std::system_category());
282 252  
283 253 }
284 254  
... ... @@ -288,7 +258,7 @@
288 258 if(!strcasecmp(name,intprop[ix].name)) {
289 259  
290 260 if(!strcasecmp(name,intprop[ix].name)) {
291   - return new BooleanAttribute(hSession,&intprop[ix]);
  261 + return BooleanAttribute(hSession,&intprop[ix]);
292 262 }
293 263  
294 264 }
... ...
client/src/session/local/private.h
... ... @@ -113,7 +113,7 @@
113 113 SSLState getSSLState() const override;
114 114  
115 115 // Attributes
116   - Attribute * getAttribute(const char *name) const override;
  116 + Attribute getAttribute(const char *name) const override;
117 117  
118 118 std::string getVersion() const override;
119 119 std::string getRevision() const override;
... ...
client/src/testprogram/testprogram.cc
... ... @@ -92,19 +92,13 @@
92 92  
93 93 try {
94 94  
95   - auto version = host["version"];
96   - cout << "Property[version]: " << version->getString() << endl;
97   - delete version;
98   -
99   - auto connected = host["connected"];
100   - cout << "Property[connected]: " << connected->getString() << endl;
101   - delete connected;
102   -
103 95 cout
104   - << "Version: " << host.getVersion()
105   - << "\tRevision: " << host.getRevision()
  96 + << "Version: " << host["version"]
  97 + << "\tRevision: " << host["Revision"]
  98 + << "\tConnected: " << host["Connected"]
106 99 << std::endl;
107 100  
  101 + /*
108 102 host.connect(nullptr);
109 103  
110 104 cout
... ... @@ -136,6 +130,8 @@
136 130  
137 131 host.disconnect();
138 132  
  133 + */
  134 +
139 135 } catch(const std::exception &e) {
140 136  
141 137 cerr << std::endl << e.what() << std::endl << std::endl;
... ... @@ -172,6 +168,7 @@
172 168 }
173 169  
174 170 cout << "Session: " << session << endl;
  171 +
175 172 testHost(session);
176 173  
177 174 return 0;
... ...