[gstreamermm] Gst::Structure: class review



commit 3b50f5ad9d8d4325e5d8763436b7b3c7c7601ae9
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Wed Sep 16 22:05:08 2015 +0000

    Gst::Structure: class review
    
        * gstreamer/src/structure.{ccg|hg}: update comments, allow to
          initialize structure with fields.
        * tests/test-structure.cc: add test which checks constructor with
          variable arguments.

 gstreamer/src/structure.ccg |    5 ---
 gstreamer/src/structure.hg  |   58 ++++++++++++++++++++++++++++++++++---------
 tests/test-structure.cc     |   20 ++++++++++++--
 3 files changed, 63 insertions(+), 20 deletions(-)
---
diff --git a/gstreamer/src/structure.ccg b/gstreamer/src/structure.ccg
index 4912e87..e7f6c52 100644
--- a/gstreamer/src/structure.ccg
+++ b/gstreamer/src/structure.ccg
@@ -79,11 +79,6 @@ Structure_Map_gstreamermm_callback(GQuark field_id, GValue *value, void* data)
 namespace Gst
 {
 
-Structure::Structure(const Glib::ustring& name)
-{
-  gobject_ = gst_structure_new_empty(name.c_str());
-}
-
 Structure::operator const void*() const
 {
   return gobject_ ? GINT_TO_POINTER(1) : nullptr;
diff --git a/gstreamer/src/structure.hg b/gstreamer/src/structure.hg
index f6dab69..47ffa52 100644
--- a/gstreamer/src/structure.hg
+++ b/gstreamer/src/structure.hg
@@ -1,6 +1,6 @@
 /* gstreamermm - a C++ wrapper for gstreamer
  *
- * Copyright 2008 The gstreamermm Development Team
+ * Copyright 2008-2015 The gstreamermm Development Team
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -28,20 +28,37 @@ _DEFS(gstreamermm,gst)
 namespace Gst
 {
 
-/** A generic class containing fields of names and values.
+/**
+ * Generic class containing fields of names and values.
  * A Gst::Structure is a collection of key/value pairs. The keys are expressed
  * as GQuarks and the values can be of any GType.
  *
- * In addition to the key/value pairs, a Gst::Structure also has a name. The
- * name starts with a letter and can be folled by letters, numbers and any of
- * "/-_.:".
+ * In addition to the key/value pairs, a Gst::Structure also has a name. The name
+ * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
  *
  * Gst::Structure is used by various GStreamer subsystems to store information
  * in a flexible and extensible way. A Gst::Structure does not have a refcount
- * because it usually is part of a higher level object such as Gst::Caps. It
- * provides a means to enforce mutability using the refcount of the parent.
+ * because it usually is part of a higher level object such as Gst::Caps,
+ * Gst::Message, Gst::Event, Gst::Query.
  *
- * Last reviewed on 2007-10-16 (0.10.15)
+ * A Gst::Structure can be created with constructor, which take a name
+ * and an optional set of key/value pairs along with the types of the values.
+ *
+ * Field values can be changed with set_field() or set_fields().
+ *
+ * Field values can be retrieved with gst_get_field().
+ *
+ * Fields can be removed with remove_field().
+ *
+ * Strings in structures must be ASCII or UTF-8 encoded.
+ *
+ * Be aware that the current Gst::Caps / Gst::Structure serialization into string
+ * has limited support for nested Gst::Caps / Gst::Structure fields. It can only
+ * support one level of nesting. Using more levels will lead to unexpected
+ * behavior when using serialization features, such as Gst::Caps::to_string() or
+ * gst_value_serialize() and their counterparts.
+ *
+ * Last reviewed on 2015-09-16 (1.5.2)
  */
 class Structure
 {
@@ -49,10 +66,23 @@ class Structure
   _IGNORE(gst_structure_copy, gst_structure_free, gst_structure_set_parent_refcount)
 
 public:
-  /** Creates a Structure with the given @a name.
-   * You should then use set_field() to set field values.
-   */
-  explicit Structure(const Glib::ustring& name);
+  /** Creates a Structure with the given @name. Parses the list of variable
+   * arguments and sets fields to the values listed. Variable arguments
+   * should be passed as field name and value
+   */
+  template<class ...DataTypes>
+  explicit Structure(const Glib::ustring &name, DataTypes... data)
+  {
+    gobject_ = gst_structure_new_empty(name.c_str());
+    set_fields(data...);
+  }
+
+  template<class DataType, class ...DataTypes>
+  void set_fields(const Glib::ustring & name, const DataType& data, DataTypes ...further_data)
+  {
+    this->set_field(name, data);
+    this->set_fields(further_data...);
+  }
 
   /** Creates a Gst::Structure from a string representation.
    *
@@ -558,6 +588,10 @@ public:
 
   _WRAP_METHOD(void fixate(), gst_structure_fixate)
 
+private:
+  // This method is used for varadic template recursion
+  void set_fields() {}
+
 
 };
 
diff --git a/tests/test-structure.cc b/tests/test-structure.cc
index 4cacd80..0594d60 100644
--- a/tests/test-structure.cc
+++ b/tests/test-structure.cc
@@ -49,15 +49,21 @@ protected:
   }
 
   template<typename T>
-  void CheckGetSetField(const T& expected, const Glib::ustring& field_name)
+  void CheckGetField(const T& expected, const Glib::ustring& field_name)
   {
-    structure.set_field(field_name, expected);
-
     T output;
     structure.get_field(field_name, output);
 
     CheckEq(expected, output);
   }
+
+  template<typename T>
+  void CheckGetSetField(const T& expected, const Glib::ustring& field_name)
+  {
+    structure.set_field(field_name, expected);
+
+    CheckGetField(expected, field_name);
+  }
 };
 
 TEST_F(StructureTest, GetSetStringVariable)
@@ -95,3 +101,11 @@ TEST_F(StructureTest, GetSetEnumVariable)
 
   EXPECT_EQ(input_state, (State)output_state);
 }
+
+TEST_F(StructureTest, CreateStructureFromFieldsList)
+{
+  structure = Structure("first", "field1", "sample string", "field2", 12);
+
+  CheckGetField(Glib::ustring("sample string"), "field1");
+  CheckGetField(12, "field2");
+}


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