commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] r5048 - gnuradio/branches/developers/eb/ibu/pmt/src/li


From: eb
Subject: [Commit-gnuradio] r5048 - gnuradio/branches/developers/eb/ibu/pmt/src/lib
Date: Wed, 18 Apr 2007 21:54:02 -0600 (MDT)

Author: eb
Date: 2007-04-18 21:54:02 -0600 (Wed, 18 Apr 2007)
New Revision: 5048

Modified:
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.h
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.cc
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.h
Log:
Added code to pmt to wrap boost::any's, which in turn can wrap anything ;)


Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc      2007-04-18 
22:00:47 UTC (rev 5047)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc      2007-04-19 
03:54:02 UTC (rev 5048)
@@ -135,6 +135,12 @@
   return dynamic_cast<pmt_dict*>(x.get());
 }
 
+static pmt_any *
+_any(pmt_t x)
+{
+  return dynamic_cast<pmt_any*>(x.get());
+}
+
 ////////////////////////////////////////////////////////////////////////////
 //                           Globals
 ////////////////////////////////////////////////////////////////////////////
@@ -200,7 +206,7 @@
   unsigned int h = 0;
   unsigned int g = 0;
 
-  for (std::string::const_iterator p = s.begin(); p != s.end(); p++){
+  for (std::string::const_iterator p = s.begin(); p != s.end(); ++p){
     h = (h << 4) + (*p & 0xff);
     g = h & 0xf0000000;
     if (g){
@@ -630,6 +636,40 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////
+//                                 Any
+////////////////////////////////////////////////////////////////////////////
+
+pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
+
+bool
+pmt_is_any(pmt_t obj)
+{
+  return obj->is_any();
+}
+
+pmt_t
+pmt_make_any(const boost::any &any)
+{
+  return pmt_t(new pmt_any(any));
+}
+
+boost::any
+pmt_any_ref(pmt_t obj)
+{
+  if (!obj->is_any())
+    throw pmt_wrong_type("pmt_any_ref", obj);
+  return _any(obj)->ref();
+}
+
+void
+pmt_any_set(pmt_t obj, const boost::any &any)
+{
+  if (!obj->is_any())
+    throw pmt_wrong_type("pmt_any_ref", obj);
+  _any(obj)->set(any);
+}
+
+////////////////////////////////////////////////////////////////////////////
 //                          General Functions
 ////////////////////////////////////////////////////////////////////////////
 

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.h       2007-04-18 
22:00:47 UTC (rev 5047)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.h       2007-04-19 
03:54:02 UTC (rev 5048)
@@ -24,6 +24,7 @@
 #define INCLUDED_PMT_H
 
 #include <boost/shared_ptr.hpp>
+#include <boost/any.hpp>
 #include <complex>
 #include <string>
 #include <stdint.h>
@@ -421,6 +422,28 @@
 
 /*
  * ------------------------------------------------------------------------
+ *   Any (wraps boost::any -- can be used to wrap pretty much anything)
+ *
+ * Cannot be serialized or used across process boundaries.
+ * See http://www.boost.org/doc/html/any.html
+ * ------------------------------------------------------------------------
+ */
+
+//! Return true if \p obj is an any
+bool pmt_is_any(pmt_t obj);
+
+//! make an any
+pmt_t pmt_make_any(const boost::any &any);
+
+//! Return underlying boost::any
+boost::any pmt_any_ref(pmt_t obj);
+
+//! Store \p any in \p obj
+void pmt_any_set(pmt_t obj, const boost::any &any);
+
+
+/*
+ * ------------------------------------------------------------------------
  *                       General functions
  * ------------------------------------------------------------------------
  */

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h   2007-04-18 
22:00:47 UTC (rev 5047)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h   2007-04-19 
03:54:02 UTC (rev 5048)
@@ -49,6 +49,7 @@
   virtual bool is_pair()    const { return false; }
   virtual bool is_vector()  const { return false; }
   virtual bool is_dict()    const { return false; }
+  virtual bool is_any()     const { return false; }
 
   virtual bool is_uniform_vector() const { return false; }
   virtual bool is_u8vector()  const { return false; }
@@ -195,6 +196,20 @@
   pmt_t values() const;
 };
 
+class pmt_any : public pmt_base
+{
+  boost::any   d_any;
+
+public:
+  pmt_any(const boost::any &any);
+  //~pmt_any();
+
+  bool is_any() const { return true; }
+  const boost::any &ref() const { return d_any; }
+  void  set(const boost::any &any) { d_any = any; }
+};
+
+
 class pmt_uniform_vector : public pmt_base
 {
 public:

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.cc
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.cc     
2007-04-18 22:00:47 UTC (rev 5047)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.cc     
2007-04-19 03:54:02 UTC (rev 5048)
@@ -295,7 +295,58 @@
   CPPUNIT_ASSERT_EQUAL(std::string("k0"), pmt_write_string(k0));
 }
 
+// ------------------------------------------------------------------------
+
+// class foo is used in test_any below.
+// It can't be declared in the scope of test_any because of template
+// namespace problems.
+
+class foo {
+public:
+  double       d_double;
+  int          d_int;
+  foo(double d=0, int i=0) : d_double(d), d_int(i) {}
+};
+
+bool operator==(const foo &a, const foo &b)
+{
+  return a.d_double == b.d_double && a.d_int == b.d_int;
+}
+
+std::ostream& operator<<(std::ostream &os, const foo obj)
+{
+  os << "<foo: " << obj.d_double << ", " << obj.d_int << ">";
+  return os;
+}
+
 void
+qa_pmt_prims::test_any()
+{
+  boost::any a0;
+  boost::any a1;
+  boost::any a2;
+
+  a0 = std::string("Hello!");
+  a1 = 42;
+  a2 = foo(3.250, 21);
+
+  pmt_t p0 = pmt_make_any(a0);
+  pmt_t p1 = pmt_make_any(a1);
+  pmt_t p2 = pmt_make_any(a2);
+
+  CPPUNIT_ASSERT_EQUAL(std::string("Hello!"),
+                      boost::any_cast<std::string>(pmt_any_ref(p0)));
+
+  CPPUNIT_ASSERT_EQUAL(42,
+                      boost::any_cast<int>(pmt_any_ref(p1)));
+
+  CPPUNIT_ASSERT_EQUAL(foo(3.250, 21),
+                      boost::any_cast<foo>(pmt_any_ref(p2)));
+}
+
+// ------------------------------------------------------------------------
+
+void
 qa_pmt_prims::test_serialize()
 {
   std::stringbuf sb;           // fake channel

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.h      
2007-04-18 22:00:47 UTC (rev 5047)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/qa_pmt_prims.h      
2007-04-19 03:54:02 UTC (rev 5048)
@@ -38,6 +38,7 @@
   CPPUNIT_TEST(test_equivalence);
   CPPUNIT_TEST(test_misc);
   CPPUNIT_TEST(test_dict);
+  CPPUNIT_TEST(test_any);
   CPPUNIT_TEST(test_io);
   CPPUNIT_TEST(test_serialize);
   CPPUNIT_TEST_SUITE_END();
@@ -53,6 +54,7 @@
   void test_equivalence();
   void test_misc();
   void test_dict();
+  void test_any();
   void test_io();
   void test_serialize();
 };





reply via email to

[Prev in Thread] Current Thread [Next in Thread]