Changeset 4:48b5811d4b46 in xplra
- Timestamp:
- 01/03/13 18:25:06 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/xplra
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/xplra/GetDataRefTask.cc
r3 r4 35 35 //------------------------------------------------------------------------------ 36 36 37 using xplra::GetByteArrayDataRefTask;38 39 using xplcommon::Util;40 41 using std::string;42 43 //------------------------------------------------------------------------------44 45 GetByteArrayDataRefTask::GetByteArrayDataRefTask(const std::string& name,46 int maxBytes, int offset) :47 DataRefTask(name),48 maxBytes(maxBytes),49 offset(offset),50 data((maxBytes>0) ? new unsigned char[maxBytes] : 0),51 length(-1)52 {53 }54 55 //------------------------------------------------------------------------------56 57 GetByteArrayDataRefTask::GetByteArrayDataRefTask(XPLMDataRef dataRef,58 int maxBytes, int offset) :59 DataRefTask(dataRef),60 maxBytes(maxBytes),61 offset(offset),62 data((maxBytes>0) ? new unsigned char[maxBytes] : 0),63 length(-1)64 {65 }66 67 //------------------------------------------------------------------------------68 69 GetByteArrayDataRefTask::~GetByteArrayDataRefTask()70 {71 delete [] data;72 }73 74 //------------------------------------------------------------------------------75 76 void GetByteArrayDataRefTask::process()77 {78 if (maxBytes<0) {79 maxBytes = XPLMGetDatab(getDataRef(), 0, 0, 0);80 81 if (maxBytes>0) {82 maxBytes -= offset;83 }84 85 if (maxBytes>0) {86 data = new unsigned char[maxBytes];87 } else {88 maxBytes = 0;89 }90 }91 92 Util::debug("GetByteArrayDataRefTask::process: dataRef=%p, maxBytes=%d, data=%p, offset\n",93 getDataRef(), maxBytes, data, offset);94 if (maxBytes>0) {95 length = XPLMGetDatab(getDataRef(), data, offset, maxBytes);96 } else {97 length = -1;98 }99 }100 37 101 38 //------------------------------------------------------------------------------ -
src/xplra/GetDataRefTask.h
r3 r4 40 40 41 41 /** 42 * Base class for tasks that query the value of some dataref. 43 */ 44 class GetDataRefTask : public DataRefTask 45 { 46 public: 47 /** 48 * Construct the task for the dataref with the given name. 49 */ 50 GetDataRefTask(const std::string& name); 51 52 /** 53 * Construct the task for the given dataref. 54 */ 55 GetDataRefTask(XPLMDataRef dataRef); 56 }; 57 58 //------------------------------------------------------------------------------ 59 60 /** 61 * Base class for dataref querying tasks that query some scalar value. 62 * 63 * ConcreteClass is the actual child class. It should have a function 64 * called queryData, which wraps the corresponding function in XPLM. 65 */ 66 template <typename T, class ConcreteClass> 67 class GetScalarDataRefTask : public GetDataRefTask 68 { 69 private: 70 /** 71 * The value retrieved. 72 */ 73 T value; 74 75 public: 76 /** 77 * Construct the task for the dataref with the given name. 78 */ 79 GetScalarDataRefTask(const std::string& name); 80 81 /** 82 * Construct the task for the given dataref. 83 */ 84 GetScalarDataRefTask(XPLMDataRef dataRef); 85 86 /** 87 * Get the value 88 */ 89 T getValue() const; 90 91 protected: 92 /** 93 * Perform the actual operation. 94 */ 95 virtual void process(); 96 }; 97 98 //------------------------------------------------------------------------------ 99 //------------------------------------------------------------------------------ 100 101 /** 102 * A dataref task which retrieves the value of an integer. 103 */ 104 class GetIntDataRefTask : 105 public GetScalarDataRefTask<int, GetIntDataRefTask> 106 { 107 public: 108 /** 109 * Query the value from XPLM 110 */ 111 static int queryData(XPLMDataRef dataRef); 112 113 /** 114 * Construct the task for the dataref with the given name. 115 */ 116 GetIntDataRefTask(const std::string& name); 117 118 /** 119 * Construct the task for the given dataref. 120 */ 121 GetIntDataRefTask(XPLMDataRef dataRef); 122 }; 123 124 //------------------------------------------------------------------------------ 125 //------------------------------------------------------------------------------ 126 127 /** 128 * A dataref task which retrieves the value of a single-precision 129 * floating point data. 130 */ 131 class GetFloatDataRefTask : 132 public GetScalarDataRefTask<float, GetFloatDataRefTask> 133 { 134 public: 135 /** 136 * Query the value from XPLM 137 */ 138 static float queryData(XPLMDataRef dataRef); 139 140 /** 141 * Construct the task for the dataref with the given name. 142 */ 143 GetFloatDataRefTask(const std::string& name); 144 145 /** 146 * Construct the task for the given dataref. 147 */ 148 GetFloatDataRefTask(XPLMDataRef dataRef); 149 }; 150 151 //------------------------------------------------------------------------------ 152 //------------------------------------------------------------------------------ 153 154 /** 155 * A dataref task which retrieves the value of a double-precision 156 * floating point data. 157 */ 158 class GetDoubleDataRefTask : 159 public GetScalarDataRefTask<double, GetDoubleDataRefTask> 160 { 161 public: 162 /** 163 * Query the value from XPLM 164 */ 165 static double queryData(XPLMDataRef dataRef); 166 167 /** 168 * Construct the task for the dataref with the given name. 169 */ 170 GetDoubleDataRefTask(const std::string& name); 171 172 /** 173 * Construct the task for the given dataref. 174 */ 175 GetDoubleDataRefTask(XPLMDataRef dataRef); 176 }; 177 178 //------------------------------------------------------------------------------ 179 //------------------------------------------------------------------------------ 180 181 /** 182 * Base class for dataref querying tasks that query some array. 183 * 184 * ConcreteClass is the actual child class. It should have a function 185 * called queryData, which wraps the corresponding function in XPLM. 186 */ 187 template <typename T, class ConcreteClass> 188 class GetArrayDataRefTask : public GetDataRefTask 189 { 190 private: 191 /** 192 * The maximal number of data items returned. 193 */ 194 int maxCount; 195 196 /** 197 * The offset within the dataref's value to start the query from. 198 */ 199 int offset; 200 201 /** 202 * The data. 203 */ 204 T* data; 205 206 /** 207 * The actual length of the data retrieved. 208 */ 209 int length; 210 211 protected: 212 /** 213 * Construct the task for the dataref with the given name. 214 * 215 * @param maxCount the maximal number of items to retrieve. If <0, 216 * the function will perform an extra query to retrieve the size of 217 * of the dataref's data, which it will store in the task, so 218 * later on it can be reused. 219 */ 220 GetArrayDataRefTask(const std::string& name, 221 int maxCount = -1, int offset = 0); 222 223 /** 224 * Construct the task for the given dataref 225 * 226 * @param maxCount the maximal number of items to retrieve. If <0, 227 * the function will perform an extra query to retrieve the size 228 * of the dataref's data, which it will store in the task, so 229 * later on it can be reused. 230 */ 231 GetArrayDataRefTask(XPLMDataRef dataRef, 232 int maxCount = -1, int offset = 0); 233 234 public: 235 /** 236 * Destroy the object. 237 */ 238 virtual ~GetArrayDataRefTask(); 239 240 /** 241 * Get the data. 242 */ 243 const T* getData() const; 244 245 /** 246 * Get the length of the data. 247 */ 248 int getLength() const; 249 250 protected: 251 /** 252 * Process the dataref by querying its value. 253 */ 254 virtual void process(); 255 }; 256 257 //------------------------------------------------------------------------------ 258 //------------------------------------------------------------------------------ 259 260 /** 261 * A dataref task which retrieves the value of a float array. 262 */ 263 class GetFloatArrayDataRefTask : 264 public GetArrayDataRefTask<float, GetFloatArrayDataRefTask> 265 { 266 public: 267 /** 268 * Get the data via XPLM 269 */ 270 static int queryData(XPLMDataRef dataRef, float* dest, 271 int offset, int count); 272 273 public: 274 /** 275 * Construct the task for the dataref with the given name. 276 * 277 * @param maxCount the maximal number of data items to 278 * retrieve. If <0, the function will perform an extra query to 279 * retrieve the size of the dataref's data, which it will store in 280 * the task, so later on it can be reused. 281 */ 282 GetFloatArrayDataRefTask(const std::string& name, 283 int maxCount = -1, int offset = 0); 284 285 /** 286 * Construct the task for the given dataref 287 * 288 * @param maxBytes the maximal number of data items to 289 * retrieve. If <0, the function will perform an extra query to 290 * retrieve the size of the dataref's data, which it will store in 291 * the task, so later on it can be reused. 292 */ 293 GetFloatArrayDataRefTask(XPLMDataRef dataRef, 294 int maxCount = -1, int offset = 0); 295 }; 296 297 //------------------------------------------------------------------------------ 298 //------------------------------------------------------------------------------ 299 300 /** 301 * A dataref task which retrieves the value of an integer array. 302 */ 303 class GetIntArrayDataRefTask : 304 public GetArrayDataRefTask<int, GetIntArrayDataRefTask> 305 { 306 public: 307 /** 308 * Get the data via XPLM 309 */ 310 static int queryData(XPLMDataRef dataRef, int* dest, 311 int offset, int count); 312 313 public: 314 /** 315 * Construct the task for the dataref with the given name. 316 * 317 * @param maxCount the maximal number of data items to 318 * retrieve. If <0, the function will perform an extra query to 319 * retrieve the size of the dataref's data, which it will store in 320 * the task, so later on it can be reused. 321 */ 322 GetIntArrayDataRefTask(const std::string& name, 323 int maxCount = -1, int offset = 0); 324 325 /** 326 * Construct the task for the given dataref 327 * 328 * @param maxBytes the maximal number of data items to 329 * retrieve. If <0, the function will perform an extra query to 330 * retrieve the size of the dataref's data, which it will store in 331 * the task, so later on it can be reused. 332 */ 333 GetIntArrayDataRefTask(XPLMDataRef dataRef, 334 int maxCount = -1, int offset = 0); 335 }; 336 337 //------------------------------------------------------------------------------ 338 //------------------------------------------------------------------------------ 339 340 /** 42 341 * A dataref task which retrieves the value of a byte array. 43 342 */ 44 class GetByteArrayDataRefTask : public DataRefTask 45 { 46 private: 47 /** 48 * The maximal number of bytes accepted. 49 */ 50 int maxBytes; 51 52 /** 53 * The offset within the dataref's value to start querying from. 54 */ 55 int offset; 56 57 /** 58 * The array to store the data in. 59 */ 60 unsigned char* data; 61 62 /** 63 * The actual length of the data retrieved. 64 */ 65 int length; 343 class GetByteArrayDataRefTask : 344 public GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask> 345 { 346 public: 347 /** 348 * Get the data via XPLM 349 */ 350 static int queryData(XPLMDataRef dataRef, void* dest, 351 int offset, int count); 66 352 67 353 public: … … 87 373 GetByteArrayDataRefTask(XPLMDataRef dataRef, 88 374 int maxBytes = -1, int offset = 0); 89 90 91 /** 92 * Destroy the task object. 93 */ 94 virtual ~GetByteArrayDataRefTask(); 95 96 /** 97 * Get the data. 98 */ 99 const unsigned char* getData() const; 100 101 /** 102 * Get the length of the data. 103 */ 104 int getLength() const; 105 106 protected: 107 /** 108 * Process the dataref by querying its value. 109 */ 110 virtual void process(); 111 }; 375 }; 376 377 //------------------------------------------------------------------------------ 378 // Template definitions 379 //------------------------------------------------------------------------------ 380 381 template <typename T, class ConcreteClass> 382 inline GetScalarDataRefTask<T, ConcreteClass>:: 383 GetScalarDataRefTask(const std::string& name) : 384 GetDataRefTask(name), 385 value(0) 386 { 387 } 388 389 //------------------------------------------------------------------------------ 390 391 template <typename T, class ConcreteClass> 392 inline GetScalarDataRefTask<T, ConcreteClass>:: 393 GetScalarDataRefTask(XPLMDataRef dataRef) : 394 GetDataRefTask(dataRef), 395 value(0) 396 { 397 } 398 399 //------------------------------------------------------------------------------ 400 401 template <typename T, class ConcreteClass> 402 inline T GetScalarDataRefTask<T, ConcreteClass>::getValue() const 403 { 404 return value; 405 } 406 407 //------------------------------------------------------------------------------ 408 409 template <typename T, class ConcreteClass> 410 void GetScalarDataRefTask<T, ConcreteClass>::process() 411 { 412 value = ConcreteClass::queryData(getDataRef()); 413 } 414 415 //------------------------------------------------------------------------------ 416 //------------------------------------------------------------------------------ 417 418 template <typename T, class ConcreteClass> 419 inline GetArrayDataRefTask<T, ConcreteClass>:: 420 GetArrayDataRefTask(const std::string& name, int maxCount, int offset) : 421 GetDataRefTask(name), 422 maxCount(maxCount), 423 offset(offset), 424 data( (maxCount>0) ? new T[maxCount] : 0 ), 425 length(-1) 426 { 427 } 428 429 //------------------------------------------------------------------------------ 430 431 template <typename T, class ConcreteClass> 432 inline GetArrayDataRefTask<T, ConcreteClass>:: 433 GetArrayDataRefTask(XPLMDataRef dataRef, int maxCount, int offset) : 434 GetDataRefTask(dataRef), 435 maxCount(maxCount), 436 offset(offset), 437 data( (maxCount>0) ? new T[maxCount] : 0), 438 length(-1) 439 { 440 } 441 442 //------------------------------------------------------------------------------ 443 444 template <typename T, class ConcreteClass> 445 inline GetArrayDataRefTask<T, ConcreteClass>::~GetArrayDataRefTask() 446 { 447 delete [] data; 448 } 449 450 //------------------------------------------------------------------------------ 451 452 template <typename T, class ConcreteClass> 453 inline const T* GetArrayDataRefTask<T, ConcreteClass>::getData() const 454 { 455 return data; 456 } 457 458 //------------------------------------------------------------------------------ 459 460 template <typename T, class ConcreteClass> 461 inline int GetArrayDataRefTask<T, ConcreteClass>::getLength() const 462 { 463 return length; 464 } 465 466 //------------------------------------------------------------------------------ 467 468 template <typename T, class ConcreteClass> 469 void GetArrayDataRefTask<T, ConcreteClass>::process() 470 { 471 if (maxCount<0) { 472 maxCount = ConcreteClass::queryData(getDataRef(), 0, 0, 0); 473 474 if (maxCount>0) { 475 maxCount -= offset; 476 } 477 478 if (maxCount>0) { 479 data = new T[maxCount]; 480 } else { 481 maxCount = 0; 482 } 483 } 484 485 // Util::debug("GetByteArrayDataRefTask::process: dataRef=%p, maxBytes=%d, data=%p, offset\n", 486 // getDataRef(), maxBytes, data, offset); 487 if (maxCount>0) { 488 length = ConcreteClass::queryData(getDataRef(), data, offset, maxCount); 489 } else { 490 length = maxCount; 491 } 492 } 112 493 113 494 //------------------------------------------------------------------------------ … … 115 496 //------------------------------------------------------------------------------ 116 497 117 inline const unsigned char* GetByteArrayDataRefTask::getData() const 118 { 119 return data; 120 } 121 122 //------------------------------------------------------------------------------ 123 124 inline int GetByteArrayDataRefTask::getLength() const 125 { 126 return length; 498 inline GetDataRefTask::GetDataRefTask(const std::string& name) : 499 DataRefTask(name) 500 { 501 } 502 503 //------------------------------------------------------------------------------ 504 505 inline GetDataRefTask::GetDataRefTask(XPLMDataRef dataRef) : 506 DataRefTask(dataRef) 507 { 508 } 509 510 //------------------------------------------------------------------------------ 511 //------------------------------------------------------------------------------ 512 513 inline int GetIntDataRefTask::queryData(XPLMDataRef dataRef) 514 { 515 return XPLMGetDatai(dataRef); 516 } 517 518 //------------------------------------------------------------------------------ 519 520 inline GetIntDataRefTask::GetIntDataRefTask(const std::string& name) : 521 GetScalarDataRefTask<int, GetIntDataRefTask>(name) 522 { 523 } 524 525 //------------------------------------------------------------------------------ 526 527 inline GetIntDataRefTask::GetIntDataRefTask(XPLMDataRef dataRef) : 528 GetScalarDataRefTask<int, GetIntDataRefTask>(dataRef) 529 { 530 } 531 532 //------------------------------------------------------------------------------ 533 //------------------------------------------------------------------------------ 534 535 inline float GetFloatDataRefTask::queryData(XPLMDataRef dataRef) 536 { 537 return XPLMGetDataf(dataRef); 538 } 539 540 //------------------------------------------------------------------------------ 541 542 inline GetFloatDataRefTask::GetFloatDataRefTask(const std::string& name) : 543 GetScalarDataRefTask<float, GetFloatDataRefTask>(name) 544 { 545 } 546 547 //------------------------------------------------------------------------------ 548 549 inline GetFloatDataRefTask::GetFloatDataRefTask(XPLMDataRef dataRef) : 550 GetScalarDataRefTask<float, GetFloatDataRefTask>(dataRef) 551 { 552 } 553 554 //------------------------------------------------------------------------------ 555 //------------------------------------------------------------------------------ 556 557 inline double GetDoubleDataRefTask::queryData(XPLMDataRef dataRef) 558 { 559 return XPLMGetDatad(dataRef); 560 } 561 562 //------------------------------------------------------------------------------ 563 564 inline GetDoubleDataRefTask::GetDoubleDataRefTask(const std::string& name) : 565 GetScalarDataRefTask<double, GetDoubleDataRefTask>(name) 566 { 567 } 568 569 //------------------------------------------------------------------------------ 570 571 inline GetDoubleDataRefTask::GetDoubleDataRefTask(XPLMDataRef dataRef) : 572 GetScalarDataRefTask<double, GetDoubleDataRefTask>(dataRef) 573 { 574 } 575 576 //------------------------------------------------------------------------------ 577 //------------------------------------------------------------------------------ 578 579 inline int GetFloatArrayDataRefTask::queryData(XPLMDataRef dataRef, float* dest, 580 int offset, int count) 581 { 582 return XPLMGetDatavf(dataRef, dest, offset, count); 583 } 584 585 //------------------------------------------------------------------------------ 586 587 inline 588 GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(const std::string& name, 589 int maxCount, int offset) : 590 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(name, maxCount, offset) 591 { 592 } 593 594 //------------------------------------------------------------------------------ 595 596 inline 597 GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(XPLMDataRef dataRef, 598 int maxCount, int offset) : 599 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(dataRef, 600 maxCount, offset) 601 { 602 } 603 604 //------------------------------------------------------------------------------ 605 //------------------------------------------------------------------------------ 606 607 inline int GetIntArrayDataRefTask::queryData(XPLMDataRef dataRef, int* dest, 608 int offset, int count) 609 { 610 return XPLMGetDatavi(dataRef, dest, offset, count); 611 } 612 613 //------------------------------------------------------------------------------ 614 615 inline 616 GetIntArrayDataRefTask::GetIntArrayDataRefTask(const std::string& name, 617 int maxCount, int offset) : 618 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(name, maxCount, offset) 619 { 620 } 621 622 //------------------------------------------------------------------------------ 623 624 inline 625 GetIntArrayDataRefTask::GetIntArrayDataRefTask(XPLMDataRef dataRef, 626 int maxCount, int offset) : 627 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(dataRef, 628 maxCount, offset) 629 { 630 } 631 632 //------------------------------------------------------------------------------ 633 //------------------------------------------------------------------------------ 634 635 inline int GetByteArrayDataRefTask::queryData(XPLMDataRef dataRef, void* dest, 636 int offset, int count) 637 { 638 return XPLMGetDatab(dataRef, dest, offset, count); 639 } 640 641 //------------------------------------------------------------------------------ 642 643 inline 644 GetByteArrayDataRefTask::GetByteArrayDataRefTask(const std::string& name, 645 int maxBytes, int offset) : 646 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(name, maxBytes, 647 offset) 648 { 649 } 650 651 //------------------------------------------------------------------------------ 652 653 inline 654 GetByteArrayDataRefTask::GetByteArrayDataRefTask(XPLMDataRef dataRef, 655 int maxBytes, int offset) : 656 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(dataRef, 657 maxBytes, 658 offset) 659 { 127 660 } 128 661 -
src/xplra/Makefile.am
r3 r4 29 29 ServerThread.cc \ 30 30 TaskRequest.cc \ 31 DataRefTask.cc \ 32 GetDataRefTask.cc 31 DataRefTask.cc 33 32 34 33 libxplra_la_LIBADD=$(LIBXPLCOMMON_LIBS)
Note:
See TracChangeset
for help on using the changeset viewer.