Changeset 27:a228b1e1e16a in xplra for src/client
- Timestamp:
- 02/08/13 18:21:29 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/client/c/hu/varadiistvan/xplra
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/client/c/hu/varadiistvan/xplra/xplra.cc
r25 r27 183 183 184 184 /** 185 * Information about a connection slot in the vector of connections.185 * Template for handling slots. 186 186 */ 187 template <class Value> 187 188 struct Slot 188 189 { … … 191 192 * The slots. 192 193 */ 193 static std::vector< Slot> slots;194 static std::vector< Slot<Value> > slots; 194 195 195 196 /** … … 200 201 public: 201 202 /** 202 * Add a new connection. 203 */ 204 static int addConnection(Connection* connection) throw(); 205 206 /** 207 * Get the connection with the given ID. 208 */ 209 static Connection* getConnection(int connectionID) throw(); 210 211 /** 212 * Clear the connection with the given ID. 213 */ 214 static void clearConnection(int connectionID) throw(); 215 216 // Indicate if the slot contains a valid connection or not 203 * Add a new value. 204 * 205 * @return the ID of the new value 206 */ 207 static int addValue(Value value) throw(); 208 209 /** 210 * Get the value with the given ID. 211 */ 212 static Value getValue(int valueID) throw(); 213 214 /** 215 * Clear the value with the given ID. 216 */ 217 static void clearValue(int valueID) throw(); 218 219 private: 220 /** 221 * Indicate if the slot contains a valid value or not. 222 */ 217 223 bool valid; 218 224 219 225 union { 220 // The connection, if it is a valid connection221 Connection* connection;226 // The value, if the slot contains a value 227 Value value; 222 228 223 229 // The index of the next free slot … … 226 232 227 233 /** 228 * Construct a slot with the given connection 229 */ 230 Slot(Connection* connection) throw(); 231 232 /** 233 * Set the connection and return the former next free index. It 234 * should be called for a slot with no connection. 235 */ 236 int setConnection(Connection* connection) throw(); 237 238 /** 239 * Get the connection if this slot contains a connection. 240 */ 241 Connection* getConnection() const throw(); 242 243 /** 244 * Clear the connection and set the given next free index. 234 * Construct a slot with the given value 235 */ 236 Slot(Value value) throw(); 237 238 /** 239 * Set the value and return the former next free index. It 240 * should be called for a slot with no value. 241 */ 242 int setValue(Value value) throw(); 243 244 /** 245 * Get the value if this slot contains a value. Otherwise 0 is 246 * returned (converted to value). 247 */ 248 Value getValue() const throw(); 249 250 /** 251 * Clear the value and set the given next free index. 245 252 */ 246 253 void clear(int nextFreeIndex) throw(); … … 249 256 //------------------------------------------------------------------------------ 250 257 251 vector<Slot> Slot::slots; 252 253 //------------------------------------------------------------------------------ 254 255 int Slot::firstFree = -1; 256 257 //------------------------------------------------------------------------------ 258 259 inline Slot::Slot(Connection* connection) throw() : 258 template <class Value> vector< Slot<Value> > Slot<Value>::slots; 259 260 //------------------------------------------------------------------------------ 261 262 template <class Value> int Slot<Value>::firstFree = -1; 263 264 //------------------------------------------------------------------------------ 265 266 template <class Value> 267 inline Slot<Value>::Slot(Value value) throw() : 260 268 valid(true) 261 269 { 262 this->connection = connection; 263 } 264 265 //------------------------------------------------------------------------------ 266 267 inline int Slot::setConnection(Connection* connection) throw() 270 this->value = value; 271 } 272 273 //------------------------------------------------------------------------------ 274 275 template <class Value> 276 inline int Slot<Value>::setValue(Value value) throw() 268 277 { 269 278 assert(!valid); 270 279 int nextFreeIndex = this->nextFreeIndex; 271 this-> connection = connection;280 this->value = value; 272 281 valid = true; 273 282 return nextFreeIndex; … … 276 285 //------------------------------------------------------------------------------ 277 286 278 inline Connection* Slot::getConnection() const throw() 279 { 280 return valid ? connection : 0; 281 } 282 283 //------------------------------------------------------------------------------ 284 285 inline void Slot::clear(int nextFreeIndex) throw() 287 template <class Value> 288 inline Value Slot<Value>::getValue() const throw() 289 { 290 return valid ? value : static_cast<Value>(0); 291 } 292 293 //------------------------------------------------------------------------------ 294 295 template <class Value> 296 inline void Slot<Value>::clear(int nextFreeIndex) throw() 286 297 { 287 298 assert(valid); … … 292 303 //------------------------------------------------------------------------------ 293 304 294 int Slot::addConnection(Connection* connection) throw() 305 template <class Value> 306 int Slot<Value>::addValue(Value value) throw() 295 307 { 296 308 int id = firstFree; 297 309 if (id<0) { 298 310 id = slots.size(); 299 slots.push_back(Slot (connection));311 slots.push_back(Slot<Value>(value)); 300 312 } else { 301 313 Slot& slot = slots[id]; 302 firstFree = slot.set Connection(connection);314 firstFree = slot.setValue(value); 303 315 } 304 316 … … 308 320 //------------------------------------------------------------------------------ 309 321 310 Connection* Slot::getConnection(int connectionID) throw() 311 { 312 size_t index = static_cast<size_t>(connectionID); 313 return (index<slots.size()) ? slots[index].getConnection() : 0; 314 } 315 316 //------------------------------------------------------------------------------ 317 318 void Slot::clearConnection(int connectionID) throw() 319 { 320 size_t index = static_cast<size_t>(connectionID); 322 template <class Value> 323 Value Slot<Value>::getValue(int valueID) throw() 324 { 325 size_t index = static_cast<size_t>(valueID); 326 return (index<slots.size()) ? 327 slots[index].getValue() : static_cast<Value>(0); 328 } 329 330 //------------------------------------------------------------------------------ 331 332 template <class Value> 333 void Slot<Value>::clearValue(int valueID) throw() 334 { 335 size_t index = static_cast<size_t>(valueID); 321 336 if (index<slots.size()) { 322 337 slots[index].clear(firstFree); … … 326 341 327 342 //------------------------------------------------------------------------------ 343 //------------------------------------------------------------------------------ 344 345 /** 346 * Slot for connections. 347 */ 348 typedef Slot<Connection*> ConnectionSlot; 349 350 //------------------------------------------------------------------------------ 328 351 329 352 } /* anonymous namespace */ … … 333 356 extern "C" int xplra_get_last_error(int connectionID, unsigned long* subCode) 334 357 { 335 Connection* connection = Slot::getConnection(connectionID);358 Connection* connection = ConnectionSlot::getValue(connectionID); 336 359 return (connection==0) ? -1 : connection->getLastError(subCode); 337 360 } … … 341 364 extern "C" const char* xplra_get_last_error_string(int connectionID) 342 365 { 343 Connection* connection = Slot::getConnection(connectionID);366 Connection* connection = ConnectionSlot::getValue(connectionID); 344 367 return (connection==0) ? 0 : connection->getLastErrorString(); 345 368 } … … 349 372 extern "C" void xplra_clear_last_error(int connectionID) 350 373 { 351 Connection* connection = Slot::getConnection(connectionID);374 Connection* connection = ConnectionSlot::getValue(connectionID); 352 375 if (connection!=0) connection->clearLastError(); 353 376 } … … 360 383 auto_ptr<Connection> connection(new Connection()); 361 384 connection->connect(); 362 return Slot::addConnection(connection.release());385 return ConnectionSlot::addValue(connection.release()); 363 386 } catch(...) { 364 387 return -1; … … 370 393 extern "C" int xplra_get_int(int* value, int connectionID, const char* name) 371 394 { 372 Connection* connection = Slot::getConnection(connectionID);395 Connection* connection = ConnectionSlot::getValue(connectionID); 373 396 if (connection==0) return -1; 374 397 try { … … 385 408 extern "C" int xplra_get_float(float* value, int connectionID, const char* name) 386 409 { 387 Connection* connection = Slot::getConnection(connectionID);410 Connection* connection = ConnectionSlot::getValue(connectionID); 388 411 if (connection==0) return -1; 389 412 try { … … 401 424 int connectionID, const char* name) 402 425 { 403 Connection* connection = Slot::getConnection(connectionID);426 Connection* connection = ConnectionSlot::getValue(connectionID); 404 427 if (connection==0) return -1; 405 428 try { … … 417 440 int connectionID, const char* name) 418 441 { 419 Connection* connection = Slot::getConnection(connectionID);442 Connection* connection = ConnectionSlot::getValue(connectionID); 420 443 if (connection==0) return -1; 421 444 try { … … 432 455 int connectionID, const char* name) 433 456 { 434 Connection* connection = Slot::getConnection(connectionID);457 Connection* connection = ConnectionSlot::getValue(connectionID); 435 458 if (connection==0) return 0; 436 459 try { … … 447 470 int connectionID, const char* name) 448 471 { 449 Connection* connection = Slot::getConnection(connectionID);472 Connection* connection = ConnectionSlot::getValue(connectionID); 450 473 if (connection==0) return -1; 451 474 try { … … 462 485 int connectionID, const char* name) 463 486 { 464 Connection* connection = Slot::getConnection(connectionID);487 Connection* connection = ConnectionSlot::getValue(connectionID); 465 488 if (connection==0) return 0; 466 489 try { … … 477 500 int connectionID, const char* name) 478 501 { 479 Connection* connection = Slot::getConnection(connectionID);502 Connection* connection = ConnectionSlot::getValue(connectionID); 480 503 if (connection==0) return -1; 481 504 try { … … 493 516 int connectionID, const char* name) 494 517 { 495 Connection* connection = Slot::getConnection(connectionID);518 Connection* connection = ConnectionSlot::getValue(connectionID); 496 519 if (connection==0) return 0; 497 520 try { … … 507 530 extern "C" int xplra_set_int(int connectionID, const char* name, int value) 508 531 { 509 Connection* connection = Slot::getConnection(connectionID);532 Connection* connection = ConnectionSlot::getValue(connectionID); 510 533 if (connection==0) return 0; 511 534 try { … … 522 545 extern "C" int xplra_set_float(int connectionID, const char* name, float value) 523 546 { 524 Connection* connection = Slot::getConnection(connectionID);547 Connection* connection = ConnectionSlot::getValue(connectionID); 525 548 if (connection==0) return 0; 526 549 try { … … 538 561 double value) 539 562 { 540 Connection* connection = Slot::getConnection(connectionID);563 Connection* connection = ConnectionSlot::getValue(connectionID); 541 564 if (connection==0) return 0; 542 565 try { … … 555 578 size_t length, size_t offset) 556 579 { 557 Connection* connection = Slot::getConnection(connectionID);580 Connection* connection = ConnectionSlot::getValue(connectionID); 558 581 if (connection==0) return 0; 559 582 try { … … 572 595 size_t length, size_t offset) 573 596 { 574 Connection* connection = Slot::getConnection(connectionID);597 Connection* connection = ConnectionSlot::getValue(connectionID); 575 598 if (connection==0) return 0; 576 599 try { … … 589 612 size_t length, size_t offset) 590 613 { 591 Connection* connection = Slot::getConnection(connectionID);614 Connection* connection = ConnectionSlot::getValue(connectionID); 592 615 if (connection==0) return 0; 593 616 try { … … 607 630 size_t length, size_t offset) 608 631 { 609 Connection* connection = Slot::getConnection(connectionID);632 Connection* connection = ConnectionSlot::getValue(connectionID); 610 633 if (connection==0) return 0; 611 634 try { … … 622 645 extern "C" int xplra_disconnect(int connectionID) 623 646 { 624 Connection* connection = Slot::getConnection(connectionID);647 Connection* connection = ConnectionSlot::getValue(connectionID); 625 648 if (connection==0) return -1; 626 649 627 Slot::clearConnection(connectionID);650 ConnectionSlot::clearValue(connectionID); 628 651 connection->disconnect(); 629 652 delete connection; -
src/client/c/hu/varadiistvan/xplra/xplra.h
r25 r27 126 126 127 127 /*----------------------------------------------------------------------------*/ 128 /* Single dataref support */ 129 /*----------------------------------------------------------------------------*/ 128 130 129 131 /** … … 315 317 316 318 /*----------------------------------------------------------------------------*/ 319 /* Multi-dataref support */ 320 /*----------------------------------------------------------------------------*/ 321 322 323 324 /*----------------------------------------------------------------------------*/ 317 325 318 326 /**
Note:
See TracChangeset
for help on using the changeset viewer.