src/cpp-common: make `bt2c::JsonVal` visitable
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 21 May 2024 16:52:08 +0000 (12:52 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
This patch adds the bt2c::JsonVal::accept() method which accepts a JSON
value visitor (`bt2c::JsonValVisitor`), dispatching to the virtual
bt2c::JsonVal::_accept() method.

This is part of an effort to support CTF2‑SPEC‑2.0 [1].

[1]: https://diamon.org/ctf/CTF2-SPEC-2.0.html

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I75462799f9433ee868b618ecba4ba63b5822b96c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7485
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12685

src/cpp-common/bt2c/json-val.cpp
src/cpp-common/bt2c/json-val.hpp

index 4daac672e7d14d662163f82aa67e56acfca5fbcd..f5065a5ab3b521c00b5d6cd4573a13854817c992 100644 (file)
@@ -65,10 +65,20 @@ const JsonObjVal& JsonVal::asObj() const noexcept
     return static_cast<const JsonObjVal&>(*this);
 }
 
+void JsonVal::accept(JsonValVisitor& visitor) const
+{
+    this->_accept(visitor);
+}
+
 JsonNullVal::JsonNullVal(TextLoc loc) noexcept : JsonVal {Type::Null, std::move(loc)}
 {
 }
 
+void JsonNullVal::_accept(JsonValVisitor& visitor) const
+{
+    visitor.visit(*this);
+}
+
 #ifdef BT_DEBUG_MODE
 
 namespace {
@@ -104,6 +114,11 @@ JsonArrayVal::JsonArrayVal(Container&& vals, TextLoc loc) :
 #endif
 }
 
+void JsonArrayVal::_accept(JsonValVisitor& visitor) const
+{
+    visitor.visit(*this);
+}
+
 JsonObjVal::JsonObjVal(Container&& vals, TextLoc loc) :
     JsonCompoundVal {std::move(vals), std::move(loc)}
 {
@@ -114,6 +129,11 @@ JsonObjVal::JsonObjVal(Container&& vals, TextLoc loc) :
 #endif
 }
 
+void JsonObjVal::_accept(JsonValVisitor& visitor) const
+{
+    visitor.visit(*this);
+}
+
 JsonNullVal::UP createJsonVal(TextLoc loc)
 {
     return bt2s::make_unique<const JsonNullVal>(std::move(loc));
index 7f7f669f96c76137079db73fa086af997ef2dbac..6c1d265fc503fe1a92c6115d8d0a3d9759b18b45 100644 (file)
@@ -68,6 +68,50 @@ using JsonStrVal = JsonScalarVal<std::string, JsonValType::Str>;
 class JsonArrayVal;
 class JsonObjVal;
 
+/*
+ * Visitor of JSON value.
+ */
+class JsonValVisitor
+{
+protected:
+    explicit JsonValVisitor() = default;
+
+public:
+    virtual ~JsonValVisitor() = default;
+
+    virtual void visit(const JsonNullVal&)
+    {
+    }
+
+    virtual void visit(const JsonBoolVal&)
+    {
+    }
+
+    virtual void visit(const JsonSIntVal&)
+    {
+    }
+
+    virtual void visit(const JsonUIntVal&)
+    {
+    }
+
+    virtual void visit(const JsonRealVal&)
+    {
+    }
+
+    virtual void visit(const JsonStrVal&)
+    {
+    }
+
+    virtual void visit(const JsonArrayVal&)
+    {
+    }
+
+    virtual void visit(const JsonObjVal&)
+    {
+    }
+};
+
 /*
  * Abstract base class for any JSON value.
  */
@@ -93,6 +137,8 @@ public:
     JsonVal& operator=(const JsonVal&) = delete;
     JsonVal& operator=(JsonVal&&) = delete;
 
+    virtual ~JsonVal() = default;
+
     /*
      * Type of this JSON value.
      */
@@ -213,7 +259,14 @@ public:
      */
     const JsonObjVal& asObj() const noexcept;
 
+    /*
+     * Accepts the visitor `visitor` to visit this JSON value.
+     */
+    void accept(JsonValVisitor& visitor) const;
+
 private:
+    virtual void _accept(JsonValVisitor& visitor) const = 0;
+
     /* JSON value type */
     Type _mType;
 
@@ -234,6 +287,9 @@ public:
      * Builds a JSON null value located at `loc`.
      */
     explicit JsonNullVal(TextLoc loc) noexcept;
+
+private:
+    void _accept(JsonValVisitor& visitor) const override;
 };
 
 /*
@@ -275,6 +331,12 @@ public:
         return _mVal;
     }
 
+private:
+    void _accept(JsonValVisitor& visitor) const override
+    {
+        visitor.visit(*this);
+    }
+
 private:
     /* Raw value */
     ValT _mVal;
@@ -363,6 +425,9 @@ public:
         BT_ASSERT_DBG(index < this->_mVals.size());
         return *_mVals[index];
     }
+
+private:
+    void _accept(JsonValVisitor& visitor) const override;
 };
 
 /*
@@ -573,6 +638,9 @@ public:
     {
         return _mVals.find(key) != _mVals.end();
     }
+
+private:
+    void _accept(JsonValVisitor& visitor) const override;
 };
 
 /*
This page took 0.02707 seconds and 4 git commands to generate.