}
}
+#if GDB_SELF_TEST
+static void
+test_assign_set_return_if_changed ()
+{
+ bool changed;
+ int a;
+
+ for (bool initial : { false, true })
+ {
+ changed = initial;
+ a = 1;
+ assign_set_if_changed (a, 1, changed);
+ SELF_CHECK (a == 1);
+ SELF_CHECK (changed == initial);
+ }
+
+ for (bool initial : { false, true })
+ {
+ changed = initial;
+ a = 1;
+ assign_set_if_changed (a, 2, changed);
+ SELF_CHECK (a == 2);
+ SELF_CHECK (changed == true);
+ }
+
+ a = 1;
+ changed = assign_return_if_changed (a, 1);
+ SELF_CHECK (a == 1);
+ SELF_CHECK (changed == false);
+
+ a = 1;
+ assign_set_if_changed (a, 2, changed);
+ SELF_CHECK (a == 2);
+ SELF_CHECK (changed == true);
+}
+#endif
+
void _initialize_utils ();
void
_initialize_utils ()
selftests::register_test ("strncmp_iw_with_mode",
strncmp_iw_with_mode_tests);
selftests::register_test ("pager", test_pager);
+ selftests::register_test ("assign_set_return_if_changed",
+ test_assign_set_return_if_changed);
#endif
}
extern int readline_hidden_cols;
+/* Assign VAL to LVAL, and set CHANGED to true if the assignment changed
+ LVAL. */
+
+template<typename T>
+void
+assign_set_if_changed (T &lval, const T &val, bool &changed)
+{
+ if (lval == val)
+ return;
+
+ lval = val;
+ changed = true;
+}
+
+/* Assign VAL to LVAL, and return true if the assignment changed LVAL. */
+
+template<typename T>
+bool
+assign_return_if_changed (T &lval, const T &val)
+{
+ if (lval == val)
+ return false;
+
+ lval = val;
+ return true;
+}
+
#endif /* UTILS_H */