[vte] parser: glue: Add some convenience function to collect parameters



commit 35b484de9c4f559b445e48535bb39b79170f7c5a
Author: Christian Persch <chpe src gnome org>
Date:   Tue Mar 27 19:40:12 2018 +0200

    parser: glue: Add some convenience function to collect parameters

 src/parser-arg.hh  |   14 ++++++++++
 src/parser-glue.hh |   73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/parser-test.cc |   68 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 153 insertions(+), 2 deletions(-)
---
diff --git a/src/parser-arg.hh b/src/parser-arg.hh
index ead82f0..88a31b2 100644
--- a/src/parser-arg.hh
+++ b/src/parser-arg.hh
@@ -39,6 +39,7 @@ typedef int vte_seq_arg_t;
 
 #define VTE_SEQ_ARG_FLAG_VALUE    (1 << 16)
 #define VTE_SEQ_ARG_FLAG_NONFINAL (1 << 17)
+#define VTE_SEQ_ARG_FLAG_MASK     (VTE_SEQ_ARG_FLAG_VALUE | VTE_SEQ_ARG_FLAG_NONFINAL)
 #define VTE_SEQ_ARG_VALUE_MASK    (0xffff)
 
 /*
@@ -161,3 +162,16 @@ static constexpr inline int vte_seq_arg_value(vte_seq_arg_t arg,
 {
         return (arg & VTE_SEQ_ARG_FLAG_VALUE) ? (arg & VTE_SEQ_ARG_VALUE_MASK) : default_value;
 }
+
+/*
+ * vte_seq_arg_value_final:
+ * @arg:
+ * @default_value: (defaults to -1)
+ *
+ * Returns: the value of @arg, or @default_value if @arg has default value or is not final
+ */
+static constexpr inline int vte_seq_arg_value_final(vte_seq_arg_t arg,
+                                                    int default_value = -1)
+{
+        return ((arg & VTE_SEQ_ARG_FLAG_MASK) == VTE_SEQ_ARG_FLAG_VALUE) ? (arg & VTE_SEQ_ARG_VALUE_MASK) : 
default_value;
+}
diff --git a/src/parser-glue.hh b/src/parser-glue.hh
index 5a48887..98ef424 100644
--- a/src/parser-glue.hh
+++ b/src/parser-glue.hh
@@ -200,6 +200,79 @@ public:
                 return size();
         }
 
+        /* collect:
+         *
+         * Collects some final parameters.
+         *
+         * Returns: %true if the sequence parameter list begins with
+         *  a run of final parameters that were collected.
+         */
+        inline constexpr bool collect(unsigned int start_idx,
+                                      std::initializer_list<int*> params,
+                                      int default_v = -1) const noexcept
+        {
+                unsigned int idx = start_idx;
+                for (auto i : params) {
+                        *i = param(idx, default_v);
+                        idx = next(idx);
+                }
+
+                return (idx - start_idx) == params.size();
+        }
+
+        /* collect1:
+         * @idx:
+         * @default_v:
+         *
+         * Collects one final parameter.
+         *
+         * Returns: the parameter value, or @default_v if the parameter has
+         *   default value or is not a final parameter
+         */
+        inline constexpr int collect1(unsigned int idx,
+                                      int default_v = -1) const noexcept
+        {
+                return __builtin_expect(idx < size(), 1) ? vte_seq_arg_value_final(m_seq->args[idx], 
default_v) : default_v;
+        }
+
+        /* collect1:
+         * @idx:
+         * @default_v:
+         * @min_v:
+         * @max_v
+         *
+         * Collects one final parameter.
+         *
+         * Returns: the parameter value clamped to the @min_v .. @max_v range,
+         *   or @default_v if the parameter has default value or is not a final parameter
+         */
+        inline constexpr int collect1(unsigned int idx,
+                                      int default_v,
+                                      int min_v,
+                                      int max_v) const noexcept
+        {
+                int v = __builtin_expect(idx < size(), 1) ? vte_seq_arg_value_final(m_seq->args[idx], 
default_v) : default_v;
+                return std::min(std::max(v, min_v), max_v);
+        }
+
+        /* collect_subparams:
+         *
+         * Collects some subparameters.
+         *
+         * Returns: %true if the sequence parameter list contains enough
+         *   subparams at @start_idx
+         */
+        inline constexpr bool collect_subparams(unsigned int start_idx,
+                                                std::initializer_list<int*> params,
+                                                int default_v = -1) const noexcept
+        {
+                unsigned int idx = start_idx;
+                for (auto i : params) {
+                        *i = param(idx++, default_v);
+                }
+
+                return idx <= next(start_idx);
+        }
 
         //FIMXE remove this one
         inline constexpr int operator[](int position) const
diff --git a/src/parser-test.cc b/src/parser-test.cc
index a207675..190f069 100644
--- a/src/parser-test.cc
+++ b/src/parser-test.cc
@@ -892,9 +892,9 @@ test_seq_glue(void)
 {
         vte::parser::Sequence seq{};
 
-        test_seq_glue(":0:1000;2;:", 6, 3, seq);
+        test_seq_glue(":0:1000;2;3;4;:;", 9, 6, seq);
         g_assert_cmpuint(seq.cbegin(), ==, 0);
-        g_assert_cmpuint(seq.cend(), ==, 6);
+        g_assert_cmpuint(seq.cend(), ==, 9);
 
         auto it = seq.cbegin();
         g_assert_cmpuint(it, ==, 0);
@@ -903,13 +903,22 @@ test_seq_glue(void)
         it = seq.next(it);
         g_assert_cmpuint(it, ==, 4);
         it = seq.next(it);
+        g_assert_cmpuint(it, ==, 5);
+        it = seq.next(it);
         g_assert_cmpuint(it, ==, 6);
+        it = seq.next(it);
+        g_assert_cmpuint(it, ==, 8);
+        it = seq.next(it);
+        g_assert_cmpuint(it, ==, 9);
 
         it = seq.cbegin();
         g_assert_cmpint(seq.param(it++), ==, -1);
         g_assert_cmpint(seq.param(it++), ==, 0);
         g_assert_cmpint(seq.param(it++), ==, 1000);
         g_assert_cmpint(seq.param(it++), ==, 2);
+        g_assert_cmpint(seq.param(it++), ==, 3);
+        g_assert_cmpint(seq.param(it++), ==, 4);
+        g_assert_cmpint(seq.param(it++), ==, -1);
         g_assert_cmpint(seq.param(it++), ==, -1);
         g_assert_cmpint(seq.param(it++), ==, -1);
         g_assert_cmpint(it, ==, seq.cend());
@@ -922,6 +931,61 @@ test_seq_glue(void)
         g_assert_cmpint(seq.param(it, -2, 20, 100), ==, 100);
         g_assert_cmpint(seq.param(it, -2, 200, 2000), ==, 1000);
         g_assert_cmpint(seq.param(it, -2, 2000, 4000), ==, 2000);
+
+        int a, b, c,d ;
+        it = seq.cbegin();
+        g_assert_false(seq.collect(it, {&a, &b, &c}));
+        g_assert_true(seq.collect_subparams(it, {&a}));
+        g_assert_true(seq.collect_subparams(it, {&a, &b}));
+        g_assert_true(seq.collect_subparams(it, {&a, &b, &c}));
+        g_assert_cmpint(a, ==, -1);
+        g_assert_cmpint(b, ==, 0);
+        g_assert_cmpint(c, ==, 1000);
+        g_assert_false(seq.collect_subparams(it, {&a, &b, &c, &d}));
+
+        it = seq.next(it);
+        g_assert_true(seq.collect(it, {&a}));
+        g_assert_true(seq.collect(it, {&a, &b}));
+        g_assert_true(seq.collect(it, {&a, &b, &c}));
+        g_assert_cmpint(a, ==, 2);
+        g_assert_cmpint(b, ==, 3);
+        g_assert_cmpint(c, ==, 4);
+        g_assert_false(seq.collect(it, {&a, &b, &c, &d}));
+
+        it = seq.next(it);
+        it = seq.next(it);
+        it = seq.next(it);
+        g_assert_false(seq.collect(it, {&a}));
+        g_assert_true(seq.collect_subparams(it, {&a}));
+        g_assert_true(seq.collect_subparams(it, {&a, &b}));
+        g_assert_cmpint(a, ==, -1);
+        g_assert_cmpint(b, ==, -1);
+        g_assert_false(seq.collect_subparams(it, {&a, &b, &c}));
+        it = seq.next(it);
+        g_assert_true(seq.collect(it, {&a}));
+        g_assert_cmpint(a, ==, -1);
+        g_assert_true(seq.collect(it, {&a, &b})); /* past-the-end params are final and default */
+        g_assert_cmpint(a, ==, -1);
+        g_assert_cmpint(b, ==, -1);
+
+        it = seq.cbegin();
+        g_assert_cmpint(seq.collect1(it, -2), ==, -2);
+        it = seq.next(it);
+        g_assert_cmpint(seq.collect1(it), ==, 2);
+        g_assert_cmpint(seq.collect1(it), ==, 2);
+        it = seq.next(it);
+        g_assert_cmpint(seq.collect1(it), ==, 3);
+        it = seq.next(it);
+        g_assert_cmpint(seq.collect1(it), ==, 4);
+        it = seq.next(it);
+        g_assert_cmpint(seq.collect1(it, -3), ==, -3);
+        it = seq.next(it);
+        g_assert_cmpint(seq.collect1(it), ==, -1);
+        g_assert_cmpint(seq.collect1(it, 42), ==, 42);
+        g_assert_cmpint(seq.collect1(it, -1, 0, 100), ==, 0);
+        g_assert_cmpint(seq.collect1(it, 42, 0, 100), ==, 42);
+        g_assert_cmpint(seq.collect1(it, 42, 0, 10), ==, 10);
+        g_assert_cmpint(seq.collect1(it, 42, 100, 200), ==, 100);
 }
 
 int


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]