summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/autorestorer.h8
-rw-r--r--core/fxcrt/autorestorer_unittest.cpp23
2 files changed, 29 insertions, 2 deletions
diff --git a/core/fxcrt/autorestorer.h b/core/fxcrt/autorestorer.h
index 745a56fff2..cafa075137 100644
--- a/core/fxcrt/autorestorer.h
+++ b/core/fxcrt/autorestorer.h
@@ -12,10 +12,14 @@ class AutoRestorer {
public:
explicit AutoRestorer(T* location)
: m_Location(location), m_OldValue(*location) {}
- ~AutoRestorer() { *m_Location = m_OldValue; }
+ ~AutoRestorer() {
+ if (m_Location)
+ *m_Location = m_OldValue;
+ }
+ void AbandonRestoration() { m_Location = nullptr; }
private:
- T* const m_Location;
+ T* m_Location;
const T m_OldValue;
};
diff --git a/core/fxcrt/autorestorer_unittest.cpp b/core/fxcrt/autorestorer_unittest.cpp
new file mode 100644
index 0000000000..ac1d613e7c
--- /dev/null
+++ b/core/fxcrt/autorestorer_unittest.cpp
@@ -0,0 +1,23 @@
+// Copyright 2018 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fxcrt/autorestorer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(fxcrt, AutoRestorer) {
+ int x = 5;
+ {
+ AutoRestorer<int> restorer(&x);
+ x = 6;
+ EXPECT_EQ(6, x);
+ }
+ EXPECT_EQ(5, x);
+ {
+ AutoRestorer<int> restorer(&x);
+ x = 6;
+ EXPECT_EQ(6, x);
+ restorer.AbandonRestoration();
+ }
+ EXPECT_EQ(6, x);
+}