FTIMER.H (41850B)
1 // 2 // Copyright 2020 Electronic Arts Inc. 3 // 4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 5 // software: you can redistribute it and/or modify it under the terms of 6 // the GNU General Public License as published by the Free Software Foundation, 7 // either version 3 of the License, or (at your option) any later version. 8 9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 16 /* $Header: /CounterStrike/FTIMER.H 1 3/03/97 10:24a Joe_bostic $ */ 17 /*********************************************************************************************** 18 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *** 19 *********************************************************************************************** 20 * * 21 * Project Name : Command & Conquer * 22 * * 23 * File Name : FTIMER.H * 24 * * 25 * Programmer : Joe L. Bostic * 26 * * 27 * Start Date : 03/16/95 * 28 * * 29 * Last Update : July 6, 1996 [JLB] * 30 * * 31 * * 32 *---------------------------------------------------------------------------------------------* 33 * Functions: * 34 * BasicTimerClass<T>::BasicTimerClass -- Constructor for basic timer class. * 35 * BasicTimerClass<T>::operator () -- Function operator for timer object. * 36 * BasicTimerClass<T>::operator long -- Conversion to long operator. * 37 * BasicTimerClass<T>::~BasicTimerClass -- Destructor for basic timer object. * 38 * TTimerClass<T>::Is_Active -- Checks to see if the timer is counting. * 39 * TTimerClass<T>::Start -- Starts (resumes) a stopped timer. * 40 * TTimerClass<T>::Stop -- Stops the current timer from incrementing. * 41 * TTimerClass<T>::TTimerClass -- Constructor for timer class object. * 42 * TTimerClass<T>::operator () -- Function operator for timer object. * 43 * TTimerClass<T>::operator long -- Conversion operator for timer object. * 44 * CDTimerClass<T>::CDTimerClass -- Constructor for count down timer. * 45 * CDTimerClass<T>::Is_Active -- Checks to see if the timer object is active. * 46 * CDTimerClass<T>::Start -- Starts (resumes) the count down timer. * 47 * CDTimerClass<T>::Stop -- Stops (pauses) the count down timer. * 48 * CDTimerClass<T>::operator () -- Function operator for the count down timer. * 49 * CDTimerClass<T>::operator long -- Conversion to long operator function. * 50 * CDTimerClass<T>::~CDTimerClass -- Destructor for the count down timer object. * 51 * TTimerClass<T>::Value -- Returns with the current value of the timer. * 52 * CDTimerClass<T>::Value -- Fetches the current value of the countdown timer. * 53 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 54 55 #ifndef FTIMER_H 56 #define FTIMER_H 57 58 59 /* 60 ** The "bool" integral type was defined by the C++ committee in 61 ** November of '94. Until the compiler supports this, use the following 62 ** definition. 63 */ 64 #ifndef __BORLANDC__ 65 #ifndef TRUE_FALSE_DEFINED 66 #define TRUE_FALSE_DEFINED 67 enum {false=0,true=1}; 68 typedef int bool; 69 #endif 70 #endif 71 72 73 /********************************************************************** 74 ** This class is solely used as a parameter to a constructor that does 75 ** absolutely no initialization to the object being constructed. By using 76 ** this method, it is possible to load and save data directly from a 77 ** class that has virtual functions. The construction process automatically 78 ** takes care of initializing the virtual function table pointer and the 79 ** rest of the constructor doesn't initialize any data members. After loading 80 ** into a class object, simply perform an in-place new operation. 81 */ 82 #ifndef NOINITCLASS 83 #define NOINITCLASS 84 struct NoInitClass { 85 public: 86 void operator () (void) const {}; 87 }; 88 #endif 89 90 91 /* 92 ** This is a timer class that watches a constant rate timer (specified by the parameter 93 ** type class) and provides basic timer functionality. It is possible to set the start value 94 ** WITHOUT damaging or otherwise affecting any other timer that may be built upon the same 95 ** specified timer class object. Treat an object of this type as if it were a "magic" integral 96 ** long that automatically advances at the speed of the timer class object controlling it. 97 */ 98 // Let lint know that non-virtual destructor is OK for this class. 99 //lint -esym(1509,BasicTimerClass) 100 template<class T> 101 class BasicTimerClass { 102 public: 103 // Constructor allows assignment as if class was integral 'long' type. 104 BasicTimerClass(unsigned long set=0); 105 BasicTimerClass(NoInitClass const & ); 106 107 ~BasicTimerClass(void); 108 109 // Fetch current value of timer. 110 unsigned long Value(void) const; 111 112 // Conversion operator to allow consistent treatment with integral types. 113 operator unsigned long(void) const; 114 115 // Function operator to allow timer object definition to be cascaded. 116 unsigned long operator () (void) const; 117 118 protected: 119 T Timer; // Timer regulator (ticks at constant rate). 120 unsigned long Started; // Time started. 121 }; 122 123 124 template<class T> 125 inline BasicTimerClass<T>::BasicTimerClass(NoInitClass const & ) 126 { 127 } 128 129 130 /*********************************************************************************************** 131 * BasicTimerClass<T>::BasicTimerClass -- Constructor for basic timer class. * 132 * * 133 * This is the constructor for the basic timer class object. It sets the timer counting * 134 * up from zero at the rate of the controlling timer class object. * 135 * * 136 * INPUT: set -- Alternate initial start value for the counter. If not specified, then * 137 * the timer is assumed to start at zero and count upwards. * 138 * * 139 * OUTPUT: none * 140 * * 141 * WARNINGS: none * 142 * * 143 * HISTORY: * 144 * 02/05/1996 JLB : Created. * 145 *=============================================================================================*/ 146 //lint -esym(1403,BasicTimerClass<class FrameTimerClass>::Timer) 147 //lint -esym(1403,BasicTimerClass<class SystemTimerClass>::Timer) 148 template<class T> 149 inline BasicTimerClass<T>::BasicTimerClass(unsigned long set) : 150 Started(Timer()-set) 151 { 152 } 153 154 155 /*********************************************************************************************** 156 * BasicTimerClass<T>::~BasicTimerClass -- Destructor for basic timer object. * 157 * * 158 * The destructor for the basic timer object doesn't have to do anything. * 159 * * 160 * INPUT: none * 161 * * 162 * OUTPUT: none * 163 * * 164 * WARNINGS: none * 165 * * 166 * HISTORY: * 167 * 02/05/1996 JLB : Created. * 168 *=============================================================================================*/ 169 template<class T> 170 inline BasicTimerClass<T>::~BasicTimerClass(void) 171 { 172 } 173 174 175 template<class T> 176 inline unsigned long BasicTimerClass<T>::Value(void) const 177 { 178 return(Timer()-Started); 179 } 180 181 182 /*********************************************************************************************** 183 * BasicTimerClass<T>::operator long -- Conversion to long operator. * 184 * * 185 * This conversion operator allows the basic timer object to function in much the same * 186 * manner as the integral "long" type. One can assign a long with a timer object and the * 187 * actual value of the timer is extracted from the object and used. * 188 * * 189 * INPUT: none * 190 * * 191 * OUTPUT: Returns with the timer value expressed as a long. * 192 * * 193 * WARNINGS: none * 194 * * 195 * HISTORY: * 196 * 02/05/1996 JLB : Created. * 197 *=============================================================================================*/ 198 template<class T> 199 inline BasicTimerClass<T>::operator unsigned long(void) const 200 { 201 return(Timer()-Started); 202 } 203 204 205 /*********************************************************************************************** 206 * BasicTimerClass<T>::operator () -- Function operator for timer object. * 207 * * 208 * This function operator allows the timer to also serve as the parameter type class for * 209 * additional timer objects. This allows one to instantiate a controlling timer class that * 210 * can control (e.g., turn on or off) all timers that are based upon it. * 211 * * 212 * INPUT: none * 213 * * 214 * OUTPUT: Returns the current timer value expressed as a long. * 215 * * 216 * WARNINGS: none * 217 * * 218 * HISTORY: * 219 * 02/05/1996 JLB : Created. * 220 *=============================================================================================*/ 221 template<class T> 222 inline unsigned long BasicTimerClass<T>::operator () (void) const 223 { 224 return(Timer()-Started); 225 } 226 227 228 /* 229 ** This timer class functions similarly to the basic timer class. In addition to the 230 ** normal timer operation, this class has the ability to be stopped and started at 231 ** will. If you have no need to start or stop the timer, then use the basic timer 232 ** class instead. 233 */ 234 template<class T> 235 class TTimerClass : public BasicTimerClass<T> { 236 public: 237 // Constructor allows assignment as if class was integral 'long' type. 238 TTimerClass(unsigned long set=0); 239 TTimerClass(NoInitClass const & x); 240 241 ~TTimerClass(void) {}; 242 243 // Fetches current value of timer. 244 unsigned long Value(void) const; 245 246 // Conversion operator to allow consistent treatment with integral types. 247 operator unsigned long(void) const; 248 249 // Function operator to allow timer object definition to be cascaded. 250 unsigned long operator () (void) const; 251 252 // Stops (pauses) the timer. 253 void Stop(void); 254 255 // Starts (resumes) the timer. 256 void Start(void); 257 258 // Queries whether the timer is currently active. 259 bool Is_Active(void) const; 260 261 private: 262 unsigned long Accumulated; // Total accumulated ticks. 263 }; 264 265 266 template<class T> 267 inline TTimerClass<T>::TTimerClass(NoInitClass const & x) : 268 BasicTimerClass<T>(x) 269 { 270 } 271 272 273 /*********************************************************************************************** 274 * TTimerClass<T>::TTimerClass -- Constructor for timer class object. * 275 * * 276 * This is the constructor for the advanced timer class object. This object class can start * 277 * or stop the timer under user control. * 278 * * 279 * INPUT: set -- The initial value to set the timer to. If no value is specified, then * 280 * the timer is assumed to start from zero. * 281 * * 282 * OUTPUT: none * 283 * * 284 * WARNINGS: none * 285 * * 286 * HISTORY: * 287 * 02/05/1996 JLB : Created. * 288 *=============================================================================================*/ 289 template<class T> 290 inline TTimerClass<T>::TTimerClass(unsigned long set) : 291 BasicTimerClass<T>(set), 292 Accumulated(0) 293 { 294 } 295 296 297 /*********************************************************************************************** 298 * TTimerClass<T>::Value -- Returns with the current value of the timer. * 299 * * 300 * This routine will return with the current value of the timer. It takes into account * 301 * whether the timer has stopped or not so as to always return the correct value regardless * 302 * of that condition. * 303 * * 304 * INPUT: none * 305 * * 306 * OUTPUT: Returns with the current value of the timer. * 307 * * 308 * WARNINGS: none * 309 * * 310 * HISTORY: * 311 * 07/06/1996 JLB : Created. * 312 *=============================================================================================*/ 313 template<class T> 314 inline unsigned long TTimerClass<T>::Value(void) const 315 { 316 unsigned long value = Accumulated; 317 if (Started != 0xFFFFFFFFU) { 318 value += BasicTimerClass<T>::Value(); 319 } 320 return(value); 321 } 322 323 324 /*********************************************************************************************** 325 * TTimerClass<T>::operator long -- Conversion operator for timer object. * 326 * * 327 * This conversion operator allows this timer object to function as an "rvalue" of a "long" * 328 * type. This is consistent with the integral "long" value. It is possible to assign a * 329 * timer object to a long and have the long initialized with the current value of the * 330 * timer. * 331 * * 332 * INPUT: none * 333 * * 334 * OUTPUT: Returns with the current time value expressed as a long. * 335 * * 336 * WARNINGS: none * 337 * * 338 * HISTORY: * 339 * 02/05/1996 JLB : Created. * 340 *=============================================================================================*/ 341 template<class T> 342 inline TTimerClass<T>::operator unsigned long(void) const 343 { 344 unsigned long value = Accumulated; 345 if (Started != 0xFFFFFFFFU) { 346 value += BasicTimerClass<T>::Value(); 347 } 348 return(value); 349 } 350 351 352 /*********************************************************************************************** 353 * TTimerClass<T>::operator () -- Function operator for timer object. * 354 * * 355 * This function operator for the timer class allows this timer class to be used as the * 356 * template parameter for other timer class objects. With this ability, one can control * 357 * several timers (e.g., start or stop them) by using a single controlling timer class * 358 * that other timers are instantiated from. * 359 * * 360 * INPUT: none * 361 * * 362 * OUTPUT: Returns with the current time expressed as a long. * 363 * * 364 * WARNINGS: none * 365 * * 366 * HISTORY: * 367 * 02/05/1996 JLB : Created. * 368 *=============================================================================================*/ 369 template<class T> 370 inline unsigned long TTimerClass<T>::operator () (void) const 371 { 372 unsigned long value = Accumulated; 373 if (Started != 0xFFFFFFFFU) { 374 value += BasicTimerClass<T>::Value(); 375 } 376 return(value); 377 } 378 379 380 /*********************************************************************************************** 381 * TTimerClass<T>::Stop -- Stops the current timer from incrementing. * 382 * * 383 * This routine will stop (pause) the timer from further increments. To cause the timer * 384 * to begin anew, call the Start() function. * 385 * * 386 * * 387 * INPUT: * 388 * * 389 * OUTPUT: * 390 * * 391 * WARNINGS: * 392 * * 393 * HISTORY: * 394 * 02/05/1996 JLB : Created. * 395 *=============================================================================================*/ 396 template<class T> 397 void TTimerClass<T>::Stop(void) 398 { 399 if (Started != 0xFFFFFFFFU) { 400 Accumulated += BasicTimerClass<T>::operator unsigned long(); 401 Started = 0xFFFFFFFFU; 402 } 403 } 404 405 406 /*********************************************************************************************** 407 * TTimerClass<T>::Start -- Starts (resumes) a stopped timer. * 408 * * 409 * This routine will resume a timer that was previously stopped with the Stop() function. * 410 * * 411 * INPUT: none * 412 * * 413 * OUTPUT: none * 414 * * 415 * WARNINGS: none * 416 * * 417 * HISTORY: * 418 * 02/06/1996 JLB : Created. * 419 *=============================================================================================*/ 420 template<class T> 421 void TTimerClass<T>::Start(void) 422 { 423 if (Started == 0xFFFFFFFFU) { 424 Started = Timer(); 425 } 426 } 427 428 429 /*********************************************************************************************** 430 * TTimerClass<T>::Is_Active -- Checks to see if the timer is counting. * 431 * * 432 * Since this timer can be paused, this routine is used to examine the timer to see if it * 433 * is currently paused or active. If the timer is active, then the return value will be * 434 * true. * 435 * * 436 * INPUT: none * 437 * * 438 * OUTPUT: bool; Is this timer currently active? * 439 * * 440 * WARNINGS: none * 441 * * 442 * HISTORY: * 443 * 02/06/1996 JLB : Created. * 444 *=============================================================================================*/ 445 template<class T> 446 inline bool TTimerClass<T>::Is_Active(void) const 447 { 448 return(Started != 0xFFFFFFFFU); 449 } 450 451 452 /* 453 ** This timer counts down from the specified (or constructed) value down towards zero. 454 ** The countdown rate is controlled by the timer object specified. This timer object can 455 ** be started or stopped. It can also be tested to see if it has expired or not. An expired 456 ** count down timer is one that has value of zero. You can treat this class object as if it 457 ** were an integral "magic" long that automatically counts down toward zero. 458 */ 459 template<class T> 460 class CDTimerClass : public BasicTimerClass<T> { 461 public: 462 // Constructor allows assignment as if class was integral 'long' type. 463 CDTimerClass(unsigned long set=0); 464 CDTimerClass(NoInitClass const & x); 465 466 ~CDTimerClass(void); 467 468 // Fetches current value of count down timer. 469 unsigned long Value(void) const; 470 471 // Conversion operator to allow consistent treatment with integral types. 472 operator unsigned long(void) const; 473 474 // Function operator to allow timer object definition to be cascaded. 475 unsigned long operator () (void) const; 476 477 // Stops (pauses) the timer. 478 void Stop(void); 479 480 // Starts (resumes) the timer. 481 void Start(void); 482 483 // Queries whether the timer is currently active. 484 bool Is_Active(void) const; 485 486 bool Was_Started(void) const { return WasStarted; } 487 488 private: 489 unsigned long DelayTime; // Ticks remaining before countdown timer expires. 490 bool WasStarted; 491 }; 492 493 494 template<class T> 495 inline CDTimerClass<T>::CDTimerClass(NoInitClass const & x) : 496 BasicTimerClass<T>(x), WasStarted(false) 497 { 498 } 499 500 501 /*********************************************************************************************** 502 * CDTimerClass<T>::CDTimerClass -- Constructor for count down timer. * 503 * * 504 * This is the constructor for the count down timer object. The optional starting value * 505 * can be used to initiate the timer. Because of this constructor it is possible to assign * 506 * a long to a count down timer object in order to begin the countdown process. * 507 * * 508 * INPUT: set -- The initial starting value for the countdown timer. * 509 * * 510 * OUTPUT: none * 511 * * 512 * WARNINGS: none * 513 * * 514 * HISTORY: * 515 * 02/06/1996 JLB : Created. * 516 *=============================================================================================*/ 517 template<class T> 518 inline CDTimerClass<T>::CDTimerClass(unsigned long set) : 519 BasicTimerClass<T>(0), 520 DelayTime(set), 521 WasStarted(false) 522 { 523 } 524 525 526 /*********************************************************************************************** 527 * CDTimerClass<T>::~CDTimerClass -- Destructor for the count down timer object. * 528 * * 529 * The destructor for the count down timer object does nothing. * 530 * * 531 * INPUT: none * 532 * * 533 * OUTPUT: none * 534 * * 535 * WARNINGS: none * 536 * * 537 * HISTORY: * 538 * 02/06/1996 JLB : Created. * 539 *=============================================================================================*/ 540 template<class T> 541 inline CDTimerClass<T>::~CDTimerClass(void) 542 { 543 } 544 545 546 /*********************************************************************************************** 547 * CDTimerClass<T>::Value -- Fetches the current value of the countdown timer. * 548 * * 549 * Use this routine to fetch the current value of the timer. It takes into consideration * 550 * whether the timer has been stopped or not. It returns the correct value regardless of * 551 * this condition. * 552 * * 553 * INPUT: none * 554 * * 555 * OUTPUT: Returns with the correct value of this count down timer. * 556 * * 557 * WARNINGS: none * 558 * * 559 * HISTORY: * 560 * 07/06/1996 JLB : Created. * 561 *=============================================================================================*/ 562 template<class T> 563 inline unsigned long CDTimerClass<T>::Value(void) const 564 { 565 unsigned long remain = DelayTime; 566 if (Started != 0xFFFFFFFFU) { 567 unsigned long value = BasicTimerClass<T>::Value(); 568 if (value < remain) { 569 return(remain - value); 570 } else { 571 return(0); 572 } 573 } 574 return(remain); 575 } 576 577 578 /*********************************************************************************************** 579 * CDTimerClass<T>::operator long -- Conversion to long operator function. * 580 * * 581 * This conversion operator allows the count down timer object to be used as if it were * 582 * a "magic" long that automatically counted downward at the controller class tick rate. * 583 * The count down object can be used in any place that an rvalue long could be used. * 584 * * 585 * INPUT: none * 586 * * 587 * OUTPUT: Returns with the current count down time expressed in the form of a long value. * 588 * * 589 * WARNINGS: none * 590 * * 591 * HISTORY: * 592 * 02/06/1996 JLB : Created. * 593 *=============================================================================================*/ 594 template<class T> 595 inline CDTimerClass<T>::operator unsigned long(void) const 596 { 597 unsigned long remain = DelayTime; 598 if (Started != 0xFFFFFFFFU) { 599 unsigned long value = BasicTimerClass<T>::Value(); 600 if (value < remain) { 601 return(remain - value); 602 } else { 603 return(0); 604 } 605 } 606 return(remain); 607 } 608 609 610 /*********************************************************************************************** 611 * CDTimerClass<T>::operator () -- Function operator for the count down timer. * 612 * * 613 * This is the function operator for the count down timer object. By supporting this * 614 * function operator, this class (or one derived from this class) could be used as the * 615 * controlling timer to the timer templates. * 616 * * 617 * INPUT: none * 618 * * 619 * OUTPUT: Returns with the current count down time expressed in the form of a long. * 620 * * 621 * WARNINGS: none * 622 * * 623 * HISTORY: * 624 * 02/06/1996 JLB : Created. * 625 *=============================================================================================*/ 626 template<class T> 627 inline unsigned long CDTimerClass<T>::operator () (void) const 628 { 629 unsigned long remain = DelayTime; 630 if (Started != 0xFFFFFFFFU) { 631 unsigned long value = BasicTimerClass<T>::Value(); 632 if (value < remain) { 633 return(remain - value); 634 } else { 635 return(0); 636 } 637 } 638 return(remain); 639 } 640 641 642 /*********************************************************************************************** 643 * CDTimerClass<T>::Stop -- Stops (pauses) the count down timer. * 644 * * 645 * This routine is used to stop (pause) the count down timer object. A timer object paused * 646 * in this fashion will be resumed by a call to Start() or by assigning a new count down * 647 * value to the timer. * 648 * * 649 * INPUT: none * 650 * * 651 * OUTPUT: none * 652 * * 653 * WARNINGS: none * 654 * * 655 * HISTORY: * 656 * 02/06/1996 JLB : Created. * 657 *=============================================================================================*/ 658 template<class T> 659 void CDTimerClass<T>::Stop(void) 660 { 661 if (Started != 0xFFFFFFFFU) { 662 DelayTime = *this; 663 Started = 0xFFFFFFFFU; 664 } 665 } 666 667 668 /*********************************************************************************************** 669 * CDTimerClass<T>::Start -- Starts (resumes) the count down timer. * 670 * * 671 * This routine is used to start (resume) the count down timer that was previously stopped * 672 * with the Stop() function. The timer will also resume when a new timer value is assigned. * 673 * * 674 * INPUT: none * 675 * * 676 * OUTPUT: none * 677 * * 678 * WARNINGS: none * 679 * * 680 * HISTORY: * 681 * 02/06/1996 JLB : Created. * 682 *=============================================================================================*/ 683 template<class T> 684 void CDTimerClass<T>::Start(void) 685 { 686 WasStarted = true; 687 688 if (Started == 0xFFFFFFFFU) { 689 Started = Timer(); 690 } 691 } 692 693 694 /*********************************************************************************************** 695 * CDTimerClass<T>::Is_Active -- Checks to see if the timer object is active. * 696 * * 697 * Because the timer object counting can be stopped, this routine is used to determine * 698 * if the timer is currently paused or active. * 699 * * 700 * INPUT: none * 701 * * 702 * OUTPUT: bool; Is the timer currently active? * 703 * * 704 * WARNINGS: Note that if the timer has counted down to zero, then it may be active, but * 705 * the value will, naturally, not change. * 706 * * 707 * HISTORY: * 708 * 02/06/1996 JLB : Created. * 709 *=============================================================================================*/ 710 template<class T> 711 inline bool CDTimerClass<T>::Is_Active(void) const 712 { 713 return(Started != 0xFFFFFFFFU); 714 } 715 716 #endif