Add blobs and shorthand pmt pseudo-constructors.
authorEric Blossom <eb@comsec.com>
Wed, 19 Aug 2009 05:44:05 +0000 (22:44 -0700)
committerEric Blossom <eb@comsec.com>
Wed, 19 Aug 2009 05:44:05 +0000 (22:44 -0700)
blobs == Binary Large Object.  Very handy for passing around
uninterpreted data.  The shorthand constructors were implemented by
overloading the pmt_t mp(foo) function in the pmt namespace.

I originally called "mp" "pmt", but that caused a conflict with the
pmt namespace.

gruel/src/include/gruel/Makefile.am
gruel/src/include/gruel/pmt.h
gruel/src/include/gruel/pmt_sugar.h [new file with mode: 0644]
gruel/src/lib/pmt/pmt.cc
gruel/src/lib/pmt/qa_pmt_prims.cc
gruel/src/lib/pmt/qa_pmt_prims.h

index 9aedc7fda2c40e5d7e1d5280d81724c93f2145e1..67dd129955c5f7695d438fedfa6a30fb25c9a778 100644 (file)
@@ -35,6 +35,7 @@ gruelinclude_HEADERS = \
        pmt.h \
        pmt_pool.h \
        pmt_serial_tags.h \
+       pmt_sugar.h \
        realtime.h \
        sys_pri.h \
        thread_body_wrapper.h \
index b1cb29f7c3ce5cfc7cf35ac6f28019ce24d4a374..3188aad1d5db76176e453c2ccda0674ea19e8b28 100644 (file)
@@ -304,6 +304,33 @@ void pmt_vector_set(pmt_t vector, size_t k, pmt_t obj);
 //! Store \p fill in every position of \p vector
 void pmt_vector_fill(pmt_t vector, pmt_t fill);
 
+/*
+ * ------------------------------------------------------------------------
+ *                   Binary Large Objects (BLOBs)
+ *
+ * Handy for passing around uninterpreted chunks of memory.
+ * ------------------------------------------------------------------------
+ */
+
+//! Return true if \p x is a blob, othewise false.
+bool pmt_is_blob(pmt_t x);
+
+/*!
+ * \brief Make a blob given a pointer and length in bytes
+ *
+ * \param buf is the pointer to data to use to create blob
+ * \param len is the size of the data in bytes.
+ *
+ * The data is copied into the blob.
+ */
+pmt_t pmt_make_blob(const void *buf, size_t len);
+
+//! Return a pointer to the blob's data
+const void *pmt_blob_data(pmt_t blob);
+
+//! Return the blob's length in bytes
+size_t pmt_blob_length(pmt_t blob);
+
 /*!
  * <pre>
  * ------------------------------------------------------------------------
@@ -736,4 +763,7 @@ void pmt_dump_sizeof();     // debugging
 
 } /* namespace pmt */
 
+
+#include <gruel/pmt_sugar.h>
+
 #endif /* INCLUDED_PMT_H */
diff --git a/gruel/src/include/gruel/pmt_sugar.h b/gruel/src/include/gruel/pmt_sugar.h
new file mode 100644 (file)
index 0000000..830dfc3
--- /dev/null
@@ -0,0 +1,94 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2009 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef INCLUDED_GRUEL_PMT_SUGAR_H
+#define INCLUDED_GRUEL_PMT_SUGAR_H
+
+/*!
+ * This file is included by pmt.h and contains pseudo-constructor
+ * shorthand for making pmt objects
+ */
+
+namespace pmt {
+
+  //! Make pmt symbol
+  static inline pmt_t
+  mp(const std::string &s)
+  {
+    return pmt_string_to_symbol(s);
+  }
+
+  //! Make pmt symbol
+  static inline pmt_t
+  mp(const char *s)
+  {
+    return pmt_string_to_symbol(s);
+  }
+
+  //! Make pmt long
+  static inline pmt_t
+  mp(long x){
+    return pmt_from_long(x);
+  }
+
+  //! Make pmt long
+  static inline pmt_t
+  mp(int x){
+    return pmt_from_long(x);
+  }
+
+  //! Make pmt double
+  static inline pmt_t
+  mp(double x){
+    return pmt_from_double(x);
+  }
+  
+  //! Make pmt complex
+  static inline pmt_t
+  mp(std::complex<double> z)
+  {
+    return pmt_make_rectangular(z.real(), z.imag());
+  }
+
+  //! Make pmt complex
+  static inline pmt_t
+  mp(std::complex<float> z)
+  {
+    return pmt_make_rectangular(z.real(), z.imag());
+  }
+
+  //! Make pmt msg_accepter
+  static inline pmt_t
+  mp(boost::shared_ptr<gruel::msg_accepter> ma)
+  {
+    return pmt_make_msg_accepter(ma);
+  }
+
+  //! Make pmt Binary Large Object (BLOB)
+  static inline pmt_t
+  mp(const void *data, size_t len_in_bytes)
+  {
+    return pmt_make_blob(data, len_in_bytes);
+  }
+
+} /* namespace pmt */
+
+
+#endif /* INCLUDED_GRUEL_PMT_SUGAR_H */
index 42f25b9de9340aae35396c6790a6423611e9a0dc..e50e218380311560a0e91249ac9a9e5c017a2cb0 100644 (file)
@@ -916,6 +916,39 @@ pmt_msg_accepter_ref(const pmt_t &obj)
 }
 
 
+////////////////////////////////////////////////////////////////////////////
+//             Binary Large Object -- currently a u8vector
+////////////////////////////////////////////////////////////////////////////
+
+bool
+pmt_is_blob(pmt_t x)
+{
+  // return pmt_is_u8vector(x);
+  return pmt_is_uniform_vector(x);
+}
+
+pmt_t
+pmt_make_blob(const void *buf, size_t len_in_bytes)
+{
+  return pmt_init_u8vector(len_in_bytes, (const uint8_t *) buf);
+}
+
+const void *
+pmt_blob_data(pmt_t blob)
+{
+  size_t len;
+  return pmt_uniform_vector_elements(blob, len);
+}
+
+size_t
+pmt_blob_length(pmt_t blob)
+{
+  size_t len;
+  pmt_uniform_vector_elements(blob, len);
+  return len;
+}
+
+
 ////////////////////////////////////////////////////////////////////////////
 //                          General Functions
 ////////////////////////////////////////////////////////////////////////////
index 04fdc1a433986070b4e89d9c93db45d16f0ab127..59d9e14d30a7e257b07e6975a08c490e7cf41c34 100644 (file)
@@ -23,7 +23,8 @@
 #include <qa_pmt_prims.h>
 #include <cppunit/TestAssert.h>
 #include <gruel/msg_passing.h>
-#include <stdio.h>
+#include <cstdio>
+#include <cstring>
 #include <sstream>
 
 using namespace pmt;
@@ -557,3 +558,19 @@ qa_pmt_prims::test_sets()
   CPPUNIT_ASSERT(!pmt_subsetp(l3,l2));
 }
 
+void
+qa_pmt_prims::test_sugar()
+{
+  CPPUNIT_ASSERT(pmt_is_symbol(mp("my-symbol")));
+  CPPUNIT_ASSERT_EQUAL((long) 10, pmt_to_long(mp(10)));
+  CPPUNIT_ASSERT_EQUAL((double) 1e6, pmt_to_double(mp(1e6)));
+  CPPUNIT_ASSERT_EQUAL(std::complex<double>(2, 3),
+                      pmt_to_complex(mp(std::complex<double>(2, 3))));
+
+  int buf[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+  pmt_t blob = mp(buf, sizeof(buf));
+  const void *data = pmt_blob_data(blob);
+  size_t nbytes = pmt_blob_length(blob);
+  CPPUNIT_ASSERT_EQUAL(sizeof(buf), nbytes);
+  CPPUNIT_ASSERT(memcmp(buf, data, nbytes) == 0);
+}
index fd6f70c8eab8236970e1ff6509f84a211e162cfb..29ba02f11cb28bcf50d0e547a4d46c593be9047d 100644 (file)
@@ -45,6 +45,7 @@ class qa_pmt_prims : public CppUnit::TestCase {
   CPPUNIT_TEST(test_lists);
   CPPUNIT_TEST(test_serialize);
   CPPUNIT_TEST(test_sets);
+  CPPUNIT_TEST(test_sugar);
   CPPUNIT_TEST_SUITE_END();
 
  private:
@@ -65,6 +66,7 @@ class qa_pmt_prims : public CppUnit::TestCase {
   void test_lists();
   void test_serialize();
   void test_sets();
+  void test_sugar();
 };
 
 #endif /* INCLUDED_QA_PMT_PRIMS_H */