LatchRestore.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // LatchRestore.h /////////////////////////////////////////////////////////////////////////////////
  24. // Author: John K. McDonald, Jr.
  25. // 09/19/2002
  26. // DO NOT DISTRIBUTE
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. #pragma once
  29. #ifndef __LATCHRESTORE_H__
  30. #define __LATCHRESTORE_H__
  31. /*
  32. The purpose of the LatchRestore class is to allow you to override member variables for the scope
  33. of a function. Here's the code that this saves:
  34. void Foo::func(Team *overrideTeam)
  35. {
  36. Team *saveTeam = m_saveTeam;
  37. m_saveTeam = overrideTeam;
  38. // ... stuff ...
  39. if (fu)
  40. {
  41. // early return
  42. m_saveTeam = saveTeam;
  43. return true;
  44. }
  45. if (bar)
  46. {
  47. // early return
  48. m_saveTeam = saveTeam;
  49. return true;
  50. }
  51. if (munkees)
  52. {
  53. // early return
  54. m_saveTeam = saveTeam;
  55. return true;
  56. }
  57. m_saveTeam = saveTeam;
  58. return false;
  59. }
  60. Instead, the code would simply be:
  61. void Foo::func(Team *overrideTeam)
  62. {
  63. LatchRestore<Team *> latch(m_saveTeam, overrideTeam);
  64. // ... stuff ...
  65. if (fu)
  66. return true;
  67. if (bar)
  68. return true;
  69. if (munkees)
  70. return true;
  71. return false;
  72. }
  73. */
  74. template <typename T>
  75. class LatchRestore
  76. {
  77. protected:
  78. T valueToRestore;
  79. T& whereToRestore;
  80. public:
  81. LatchRestore(T& dest, const T& src) : whereToRestore(dest)
  82. {
  83. valueToRestore = dest;
  84. dest = src;
  85. }
  86. virtual ~LatchRestore()
  87. {
  88. whereToRestore = valueToRestore;
  89. }
  90. };
  91. #endif /* __LATCHRESTORE_H__ */