[gstreamermm] Gst::Value: replace some classes with template class



commit ad853d65186041e5982fb5a3ecb58941b85c67f4
Author: Marcin Kolny <marcin kolny gmail com>
Date:   Sat Aug 27 12:44:12 2016 +0200

    Gst::Value: replace some classes with template class

 .gitignore              |    1 +
 gstreamer/src/value.ccg |  140 +++++----------------------------
 gstreamer/src/value.hg  |  199 ++++++++++++++---------------------------------
 tests/Makefile.am       |    2 +
 tests/test-structure.cc |    4 +-
 tests/test-value.cc     |   46 +++++++++++
 6 files changed, 130 insertions(+), 262 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 34a627c..2d1c891 100644
--- a/.gitignore
+++ b/.gitignore
@@ -550,6 +550,7 @@ tests/test-sample
 tests/test-structure
 tests/test-taglist
 tests/test-urihandler
+tests/test-value
 
 tests/test-plugin-alsasink
 tests/test-plugin-alsasrc
diff --git a/gstreamer/src/value.ccg b/gstreamer/src/value.ccg
index 6ded900..b3fda07 100644
--- a/gstreamer/src/value.ccg
+++ b/gstreamer/src/value.ccg
@@ -1,6 +1,6 @@
 /* gstreamermm - a C++ wrapper for gstreamer
  *
- * Copyright 2008 The gstreamermm Development Team
+ * Copyright 2008-2016 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
@@ -105,72 +105,18 @@ Fraction& Fraction::operator=(const Fraction& src)
   return *this;
 }
 
-
-IntRange::IntRange()
-: min(0), max(0)
-{}
-
-IntRange::IntRange(int min, int max)
-: min(min), max(max)
-{}
-
-IntRange::IntRange(const IntRange& src)
-: min(src.min), max(src.max)
-{}
-
-IntRange::IntRange(const Glib::ValueBase& value)
-: min(0), max(0)
+template <>
+void Range<int>::init_range(const GValue& value)
 {
-//  if(GST_VALUE_HOLDS_INT_RANGE(value.gobj()))
-  {
-    min = gst_value_get_int_range_min(value.gobj());
-    max = gst_value_get_int_range_max(value.gobj());
-  }
-}
-
-IntRange::IntRange(const GValue& value)
-: min(0), max(0)
-{
-//  if(GST_VALUE_HOLDS_INT_RANGE(&value))
+  if(GST_VALUE_HOLDS_INT_RANGE(&value))
   {
     min = gst_value_get_int_range_min(&value);
     max = gst_value_get_int_range_max(&value);
   }
 }
 
-IntRange& IntRange::operator=(const IntRange& src)
-{
-  min = src.min;
-  max = src.max;
-
-  return *this;
-}
-
-
-DoubleRange::DoubleRange()
-: min(0), max(0)
-{}
-
-DoubleRange::DoubleRange(double min, double max)
-: min(min), max(max)
-{}
-
-DoubleRange::DoubleRange(const DoubleRange& r)
-: min(r.min), max(r.max)
-{}
-
-DoubleRange::DoubleRange(const Glib::ValueBase& value)
-: min(0), max(0)
-{
-  if(GST_VALUE_HOLDS_DOUBLE_RANGE(value.gobj()))
-  {
-    min = gst_value_get_double_range_min(value.gobj());
-    max = gst_value_get_double_range_max(value.gobj());
-  }
-}
-
-DoubleRange::DoubleRange(const GValue& value)
-: min(0), max(0)
+template <>
+void Range<double>::init_range(const GValue& value)
 {
   if(GST_VALUE_HOLDS_DOUBLE_RANGE(&value))
   {
@@ -179,54 +125,10 @@ DoubleRange::DoubleRange(const GValue& value)
   }
 }
 
-DoubleRange& DoubleRange::operator=(const DoubleRange& src)
-{
-  min = src.min;
-  max = src.max;
-
-  return *this;
-}
-
-FractionRange::FractionRange()
-: min(), max()
-{}
-
-FractionRange::FractionRange(const Fraction& min, const Fraction& max)
-: min(min), max(max)
-{}
-
-FractionRange::FractionRange(const FractionRange& src)
-: min(src.min), max(src.max)
-{}
-
-FractionRange& FractionRange::operator=(const FractionRange& src)
-{
-  min = src.min;
-  max = src.max;
-
-  return *this;
-}
-
-FractionRange::FractionRange(const Glib::ValueBase& value)
-: min(), max()
-{
- // if(GST_VALUE_HOLDS_FRACTION_RANGE(value.gobj()))
-  {
-    const GValue* min = gst_value_get_fraction_range_min(value.gobj());
-    const GValue* max = gst_value_get_fraction_range_max(value.gobj());
-
-    this->min.num = gst_value_get_fraction_numerator(min);
-    this->min.denom = gst_value_get_fraction_denominator(min);
-
-    this->max.num = gst_value_get_fraction_numerator(max);
-    this->max.denom = gst_value_get_fraction_denominator(max);
-  }
-}
-
-FractionRange::FractionRange(const GValue& value)
-: min(), max()
+template <>
+void Range<Gst::Fraction>::init_range(const GValue& value)
 {
-//  if(GST_VALUE_HOLDS_FRACTION_RANGE(&value))
+  if(GST_VALUE_HOLDS_FRACTION_RANGE(&value))
   {
     const GValue* min = gst_value_get_fraction_range_min(&value);
     const GValue* max = gst_value_get_fraction_range_max(&value);
@@ -245,35 +147,35 @@ namespace Glib
 {
 
 // static
-GType Value<Gst::IntRange>::value_type()
+GType Value<Gst::Range<int>>::value_type()
 {
   return GST_TYPE_INT_RANGE;
 }
 
-void Value<Gst::IntRange>::set(const Gst::IntRange& data)
+void Value<Gst::Range<int>>::set(const Gst::Range<int>& data)
 {
   gst_value_set_int_range(&gobject_, data.min, data.max);
 }
 
-Gst::IntRange Value<Gst::IntRange>::get() const
+Gst::Range<int> Value<Gst::Range<int>>::get() const
 {
-  return Gst::IntRange(gobject_);
+  return Gst::Range<int>(gobject_);
 }
 
 // static
-GType Value<Gst::DoubleRange>::value_type()
+GType Value<Gst::Range<double>>::value_type()
 {
   return GST_TYPE_DOUBLE_RANGE;
 }
 
-void Value<Gst::DoubleRange>::set(const Gst::DoubleRange& data)
+void Value<Gst::Range<double>>::set(const Gst::Range<double>& data)
 {
   gst_value_set_double_range(&gobject_, data.min, data.max);
 }
 
-Gst::DoubleRange Value<Gst::DoubleRange>::get() const
+Gst::Range<double> Value<Gst::Range<double>>::get() const
 {
-  return Gst::DoubleRange(gobject_);
+  return Gst::Range<double>(gobject_);
 }
 
 // static
@@ -293,20 +195,20 @@ Gst::Fraction Value<Gst::Fraction>::get() const
 }
 
 // static
-GType Value<Gst::FractionRange>::value_type()
+GType Value<Gst::Range<Gst::Fraction>>::value_type()
 {
   return GST_TYPE_FRACTION_RANGE;
 }
 
-void Value<Gst::FractionRange>::set(const Gst::FractionRange& data)
+void Value<Gst::Range<Gst::Fraction>>::set(const Gst::Range<Gst::Fraction>& data)
 {
   gst_value_set_fraction_range_full(&gobject_, data.min.num, data.min.denom,
     data.max.num, data.max.denom);
 }
 
-Gst::FractionRange Value<Gst::FractionRange>::get() const
+Gst::Range<Gst::Fraction> Value<Gst::Range<Gst::Fraction>>::get() const
 {
-  return Gst::FractionRange(gobject_);
+  return Gst::Range<Gst::Fraction>(gobject_);
 }
 
 } // namespace Glib
diff --git a/gstreamer/src/value.hg b/gstreamer/src/value.hg
index 0d4db08..5e18cd3 100644
--- a/gstreamer/src/value.hg
+++ b/gstreamer/src/value.hg
@@ -1,6 +1,6 @@
 /* gstreamermm - a C++ wrapper for gstreamer
  *
- * Copyright 2008 The gstreamermm Development Team
+ * Copyright 2008-2016 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
@@ -53,6 +53,9 @@ namespace Gst
  * char first_char = fourcc.first;
  * ...
  * @endcode
+ *
+ * Last reviewed on 2016-08-27 (1.8.0)
+ *
  * @ingroup GstHelperClasses
  */
 class Fourcc
@@ -116,6 +119,9 @@ private:
  * int numerator = fract.num;
  * ...
  * @endcode
+ *
+ * Last reviewed on 2016-08-27 (1.8.0) 
+ *
  * @ingroup GstHelperClasses
  */
 class Fraction
@@ -149,10 +155,8 @@ public:
   int denom;
 };
 
-//TODO: Use a template for IntRange, DoubleRange, FractionRange? murrayc
-
-/** A class that represents an integer range (min - max).
- * Gst::IntRange is used to store an integer range in Gst::Structures of
+/** A class that represents a range (min - max).
+ * Gst::Range is used to store a range in Gst::Structures of
  * Gst::Caps as a value representing a property (see GStreamer Application
  * Development Manual section 8.2.2 and GstValue docs).  When the value is set,
  * it is transformed to a GStreamer GType so retrieving the value is a bit
@@ -160,157 +164,70 @@ public:
  * field like so:
  *
  * @code
- * Glib::Value<Gst::IntRange> value;
- * value.init(Glib::Value<Gst::IntRange>::value_type());
- * value.set(Gst::IntRange(8000, 50000));
+ * Glib::Value<Gst::Range<int>> value;
+ * value.init(Glib::Value<Gst::Range<int>>::value_type());
+ * value.set(Gst::Range<int>(8000, 50000));
  *
  * Gst::Structure structure("my-structure");
  * structure.set_field("rate", value);
  * ...
  * Glib::ValueBase gst_value;
  * structure.get_field("rate", gst_value);
- * Gst::IntRange range(gst_value);
+ * Gst::Range<int> range(gst_value);
  * int max = range.max;
  * ...
  * @endcode
- * @ingroup GstHelperClasses
- */
-class IntRange
-{
-public:
-  /** Constructs a zero Gst::IntRange (0 - 0).
-   */
-  IntRange();
-
-  /** Constructs an Gst::IntRange (min - max).
-   */
-  IntRange(int min, int max);
-
-  /** Constructs an Gst::IntRange from another.
-   */
-  IntRange(const IntRange& src);
-
-  /** Constructs an Gst::IntRange from a GST_TYPE_INT_RANGE.
-   */
-  explicit IntRange(const Glib::ValueBase& gst_int_range_value);
-
-  /** Constructs an Gst::IntRange from a GST_TYPE_INT_RANGE.
-   */
-  explicit IntRange(const GValue& gst_int_range_value);
-
-  IntRange& operator=(const IntRange& src);
-
-public:
-  int min;
-  int max;
-};
-
-/** A class that represents a double range (min - max).
- * Gst::DoubleRange is used to use to store a double precision floating point
- * range in Gst::Structures of Gst::Caps as a value representing a property
- * (see GStreamer Application Development Manual section 8.2.2 and GstValue
- * docs).  When the value is set, it is transformed to a GStreamer GType so
- * retrieving the value is a bit different.  The class can be used in setting
- * and getting a Gst::Structure field like so:
  *
- * @code
- * Glib::Value<Gst::DoubleRange> value;
- * value.init(Glib::Value<Gst::DoubleRange>::value_type());
- * value.set(Gst::DoubleRange(44.1, 48.0));
+ * Gst::Range should be used with following types: int, double, Gst::Fraction.
  *
- * Gst::Structure structure("my-structure");
- * structure.set_field("rate", value);
- * ...
- * Glib::ValueBase gst_value;
- * structure.get_field("rate", gst_value);
- * Gst::DoubleRange range(gst_value);
- * double min = range.min;
- * ...
- * @endcode
- * @ingroup GstHelperClasses
- */
-class DoubleRange
-{
-public:
-  /** Constructs a zero Gst::DoubleRange (0.0 - 0.0).
-   */
-  DoubleRange();
-
-  /** Constructs a Gst::DoubleRange (min - max).
-   */
-  DoubleRange(double min, double max);
-
-  /** Constructs a Gst::DoubleRange from another.
-   */
-  DoubleRange(const DoubleRange& src);
-
-  /** Constructs a Gst::DoubleRange from a GST_TYPE_DOUBLE_RANGE.
-   */
-  explicit DoubleRange(const Glib::ValueBase& gst_double_range_value);
-
-  /** Constructs a Gst::DoubleRange from a GST_TYPE_DOUBLE_RANGE.
-   */
-  explicit DoubleRange(const GValue& gst_double_range_value);
-
-  DoubleRange& operator=(const DoubleRange& src);
-
-public:
-  double min;
-  double max;
-};
-
-/** A class that represents a fractional range.
- * Gst::FractionRange is used to store a fractional range in Gst::Structures of
- * Gst::Caps as a value representing a property (see GStreamer Application
- * Development Manual section 8.2.2 and GstValue docs).  When the value is set,
- * it is transformed to a GStreamer GType so retrieving the value is a bit
- * different.  The class can be used in setting and getting a Gst::Structure
- * field like so:
- *
- * @code
- * Glib::Value<Gst::FractionRange> value;
- * value.init(Glib::Value<Gst::Fraction>::value_type());
- * value.set(Gst::FractionRange(Gst::Fraction(1,2), Gst::Fraction(3,4)));
+ * Last reviewed on 2016-08-27 (1.8.0)
  *
- * Gst::Structure structure("my-structure");
- * structure.set_field("range", value);
- * ...
- * Glib::ValueBase gst_value;
- * structure.get_field("rate", gst_value);
- * Gst::FractionRange range(gst_value);
- * int min_numerator = range.min.num;
- * ...
- * @endcode
  * @ingroup GstHelperClasses
  */
-class FractionRange
+template<typename T>
+class Range
 {
 public:
-  /** Constructs a zero Gst::FractionRange (0/1 - 0/1).
+  /** Constructs a zero Gst::Range.
    */
-  FractionRange();
+  Range() : min(T()), max(T()) {}
 
-  /** Constructs a Gst::FractionRange (min - max).
+  /** Constructs a Gst::Range (min - max).
    */
-  FractionRange(const Fraction& min, const Fraction& max);
+  Range(T min, T max) : min(min), max(max) {}
 
-  /** Constructs a Gst::FractionRange from another.
+  /** Constructs an Gst::Range from another range.
    */
-  FractionRange(const FractionRange& src);
+  Range(const Range& src) : min(src.min), max(src.max) {}
 
-  /** Constructs a Gst::FractionRange from a GST_TYPE_FRACTION_RANGE.
+  /** Constructs an Gst::Range from a Glib::ValueBase.
    */
-  explicit FractionRange(const Glib::ValueBase& gst_fraction_range_value);
+  explicit Range(const Glib::Value<Range<T>>& range_value)
+  {
+    min = range_value.get().min;
+    max = range_value.get().max;
+  }
 
-  /** Constructs a Gst::FractionRange from a GST_TYPE_FRACTION_RANGE.
+  /** Constructs an Gst::Range from a GValue.
    */
-  explicit FractionRange(const GValue& gst_fraction_range_value);
-
-  FractionRange& operator=(const FractionRange& src);
+  explicit Range(const GValue& range_value)
+  {
+    init_range(range_value);
+  }
+
+  Range& operator=(const Range& src)
+  {
+    min = src.min;
+    max = src.max;
+    return *this;
+  }
 
 public:
-  Fraction min;
-  Fraction max;
+  T min;
+  T max;
+  
+private:
+  void init_range(const GValue& range_value);
 };
 
 } //namespace Gst
@@ -333,27 +250,27 @@ public:
 };
 
 template<>
-class Value<Gst::IntRange> : public ValueBase_Boxed
+class Value<Gst::Range<int>> : public ValueBase_Boxed
 {
 public:
-  typedef Gst::IntRange CppType;
+  typedef Gst::Range<int> CppType;
 
   static GType value_type() G_GNUC_CONST;
 
-  void set(const Gst::IntRange& data);
-  Gst::IntRange get() const;
+  void set(const Gst::Range<int>& data);
+  Gst::Range<int> get() const;
 };
 
 template<>
-class Value<Gst::DoubleRange> : public ValueBase_Boxed
+class Value<Gst::Range<double>> : public ValueBase_Boxed
 {
 public:
-  typedef Gst::DoubleRange CppType;
+  typedef Gst::Range<double> CppType;
 
   static GType value_type() G_GNUC_CONST;
 
-  void set(const Gst::DoubleRange& data);
-  Gst::DoubleRange get() const;
+  void set(const Gst::Range<double>& data);
+  Gst::Range<double> get() const;
 };
 
 template<>
@@ -369,15 +286,15 @@ public:
 };
 
 template<>
-class Value<Gst::FractionRange> : public ValueBase_Boxed
+class Value<Gst::Range<Gst::Fraction>> : public ValueBase_Boxed
 {
 public:
-  typedef Gst::FractionRange CppType;
+  typedef Gst::Range<Gst::Fraction> CppType;
 
   static GType value_type() G_GNUC_CONST;
 
-  void set(const Gst::FractionRange& data);
-  Gst::FractionRange get() const;
+  void set(const Gst::Range<Gst::Fraction>& data);
+  Gst::Range<Gst::Fraction> get() const;
 };
 
 } // namespace Glib
diff --git a/tests/Makefile.am b/tests/Makefile.am
index caba3b9..fca1aa1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -43,6 +43,7 @@ check_PROGRAMS =                                \
         test-structure                          \
         test-taglist                            \
         test-urihandler                         \
+        test-value                             \
                                                 \
         test-plugin-appsink                     \
         test-plugin-appsrc                      \
@@ -87,6 +88,7 @@ test_sample_SOURCES                             = $(TEST_GTEST_SOURCES) test-sam
 test_structure_SOURCES                          = $(TEST_GTEST_SOURCES) test-structure.cc
 test_taglist_SOURCES                            = $(TEST_GTEST_SOURCES) test-taglist.cc
 test_urihandler_SOURCES                         = $(TEST_GTEST_SOURCES) test-urihandler.cc
+test_value_SOURCES                              = $(TEST_GTEST_SOURCES) test-value.cc
 
 test_plugin_appsink_SOURCES                     = $(TEST_GTEST_SOURCES) plugins/test-plugin-appsink.cc
 test_plugin_appsrc_SOURCES                      = $(TEST_GTEST_SOURCES) plugins/test-plugin-appsrc.cc
diff --git a/tests/test-structure.cc b/tests/test-structure.cc
index 31f5cdb..82fefc0 100644
--- a/tests/test-structure.cc
+++ b/tests/test-structure.cc
@@ -25,7 +25,7 @@ void CheckEq(const Fraction& expected, const Fraction& output)
 }
 
 template<>
-void CheckEq(const FractionRange& expected, const FractionRange& output)
+void CheckEq(const Range<Fraction>& expected, const Range<Fraction>& output)
 {
   CheckEq(expected.min, output.min);
   CheckEq(expected.max, output.max);
@@ -90,7 +90,7 @@ TEST_F(StructureTest, GetSetFractionVariable)
 
 TEST_F(StructureTest, GetSetFractionRangeVariable)
 {
-  CheckGetSetField(FractionRange(Fraction(5,6), Gst::Fraction(13,14)), "fraction_range");
+  CheckGetSetField(Gst::Range<Gst::Fraction>(Fraction(5,6), Fraction(13,14)), "fraction_range");
 }
 
 TEST_F(StructureTest, GetSetDateVariable)
diff --git a/tests/test-value.cc b/tests/test-value.cc
new file mode 100644
index 0000000..696322f
--- /dev/null
+++ b/tests/test-value.cc
@@ -0,0 +1,46 @@
+/*
+ * test-value.cc
+ *
+ *  Created on: Aug 27, 2016
+ *      Author: m.kolny
+ */
+#include <gtest/gtest.h>
+#include <gstreamermm.h>
+
+TEST(ValueTest, TestDefaultRangeValue)
+{
+  Gst::Range<Gst::Fraction> fraction_range;
+  Gst::Fraction default_fraction;
+
+  ASSERT_EQ(default_fraction.denom, fraction_range.max.denom);
+  ASSERT_EQ(default_fraction.num, fraction_range.min.num);
+}
+
+TEST(ValueTest, TestUserDefinedRangeValue)
+{
+  Gst::Range<int> int_range(10, 20);
+
+  ASSERT_EQ(10, int_range.min);
+  ASSERT_EQ(20, int_range.max);
+}
+
+TEST(ValueTest, TestIntRangeToGValue)
+{
+  Glib::Value<Gst::Range<int>> value;
+  value.init(Glib::Value<Gst::Range<int>>::value_type());
+  value.set(Gst::Range<int>(8000, 50000));
+
+  Gst::Structure structure("my-structure");
+  Glib::Value<int> v;
+  v.init(G_TYPE_INT);
+  v.set(12);
+  structure.set_field("rate", value);
+
+  Glib::Value<Gst::Range<int>> gst_value;
+  structure.get_field("rate", gst_value);
+  Gst::Range<int> range(gst_value);
+  int max = range.max;
+
+  ASSERT_EQ(max, 50000);
+}
+


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