summaryrefslogtreecommitdiff
path: root/src/third_party/mozjs-60/extract/js/src/ds/Nestable.h
diff options
context:
space:
mode:
authorGabriel Russell <gabriel.russell@mongodb.com>2019-01-30 16:35:10 +0000
committerGabriel Russell <gabriel.russell@mongodb.com>2019-01-30 19:06:17 +0000
commitd80755651cbd268c7db03ebba914051428fedeb4 (patch)
treeedeef955820a96d615876729dfa6e03ff021ea16 /src/third_party/mozjs-60/extract/js/src/ds/Nestable.h
parent6dc4072453b4ba17fed636f049c10cf1316357bb (diff)
downloadmongo-d80755651cbd268c7db03ebba914051428fedeb4.tar.gz
SERVER-29286 import and use mozjs-60
Diffstat (limited to 'src/third_party/mozjs-60/extract/js/src/ds/Nestable.h')
-rw-r--r--src/third_party/mozjs-60/extract/js/src/ds/Nestable.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/third_party/mozjs-60/extract/js/src/ds/Nestable.h b/src/third_party/mozjs-60/extract/js/src/ds/Nestable.h
new file mode 100644
index 00000000000..c02264abcea
--- /dev/null
+++ b/src/third_party/mozjs-60/extract/js/src/ds/Nestable.h
@@ -0,0 +1,63 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef ds_Nestable_h
+#define ds_Nestable_h
+
+namespace js {
+
+// A base class for nestable structures.
+template <typename Concrete>
+class MOZ_STACK_CLASS Nestable
+{
+ Concrete** stack_;
+ Concrete* enclosing_;
+
+ protected:
+ explicit Nestable(Concrete** stack)
+ : stack_(stack),
+ enclosing_(*stack)
+ {
+ *stack_ = static_cast<Concrete*>(this);
+ }
+
+ // These method are protected. Some derived classes, such as ParseContext,
+ // do not expose the ability to walk the stack.
+ Concrete* enclosing() const {
+ return enclosing_;
+ }
+
+ template <typename Predicate /* (Concrete*) -> bool */>
+ static Concrete* findNearest(Concrete* it, Predicate predicate) {
+ while (it && !predicate(it))
+ it = it->enclosing();
+ return it;
+ }
+
+ template <typename T>
+ static T* findNearest(Concrete* it) {
+ while (it && !it->template is<T>())
+ it = it->enclosing();
+ return it ? &it->template as<T>() : nullptr;
+ }
+
+ template <typename T, typename Predicate /* (T*) -> bool */>
+ static T* findNearest(Concrete* it, Predicate predicate) {
+ while (it && (!it->template is<T>() || !predicate(&it->template as<T>())))
+ it = it->enclosing();
+ return it ? &it->template as<T>() : nullptr;
+ }
+
+ public:
+ ~Nestable() {
+ MOZ_ASSERT(*stack_ == static_cast<Concrete*>(this));
+ *stack_ = enclosing_;
+ }
+};
+
+} // namespace js
+
+#endif /* ds_Nestable_h */