| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 // TemporaryChange<> is useful for setting a variable to a new value only within
a | 33 // TemporaryChange<> is useful for setting a variable to a new value only within
a |
| 34 // particular scope. An TemporaryChange<> object changes a variable to its origi
nal | 34 // particular scope. An TemporaryChange<> object changes a variable to its origi
nal |
| 35 // value upon destruction, making it an alternative to writing "var = false;" | 35 // value upon destruction, making it an alternative to writing "var = false;" |
| 36 // or "var = oldVal;" at all of a block's exit points. | 36 // or "var = oldVal;" at all of a block's exit points. |
| 37 // | 37 // |
| 38 // This should be obvious, but note that an TemporaryChange<> instance should ha
ve a | 38 // This should be obvious, but note that an TemporaryChange<> instance should ha
ve a |
| 39 // shorter lifetime than its scopedVariable, to prevent invalid memory writes | 39 // shorter lifetime than its scopedVariable, to prevent invalid memory writes |
| 40 // when the TemporaryChange<> object is destroyed. | 40 // when the TemporaryChange<> object is destroyed. |
| 41 | 41 |
| 42 template<typename T> | 42 template <typename T> |
| 43 class TemporaryChange { | 43 class TemporaryChange { |
| 44 WTF_MAKE_NONCOPYABLE(TemporaryChange); | 44 WTF_MAKE_NONCOPYABLE(TemporaryChange); |
| 45 public: | |
| 46 TemporaryChange(T& scopedVariable, T newValue) | |
| 47 : m_scopedVariable(scopedVariable) | |
| 48 , m_originalValue(scopedVariable) | |
| 49 { | |
| 50 m_scopedVariable = newValue; | |
| 51 } | |
| 52 | 45 |
| 53 ~TemporaryChange() | 46 public: |
| 54 { | 47 TemporaryChange(T& scopedVariable, T newValue) |
| 55 m_scopedVariable = m_originalValue; | 48 : m_scopedVariable(scopedVariable), m_originalValue(scopedVariable) { |
| 56 } | 49 m_scopedVariable = newValue; |
| 50 } |
| 57 | 51 |
| 52 ~TemporaryChange() { m_scopedVariable = m_originalValue; } |
| 58 | 53 |
| 59 private: | 54 private: |
| 60 T& m_scopedVariable; | 55 T& m_scopedVariable; |
| 61 T m_originalValue; | 56 T m_originalValue; |
| 62 }; | 57 }; |
| 63 | |
| 64 } | 58 } |
| 65 | 59 |
| 66 using WTF::TemporaryChange; | 60 using WTF::TemporaryChange; |
| 67 | 61 |
| 68 #endif | 62 #endif |
| OLD | NEW |