diff options
Diffstat (limited to 'gruel/src')
-rw-r--r-- | gruel/src/include/gruel/Makefile.am | 1 | ||||
-rw-r--r-- | gruel/src/include/gruel/pmt.h | 30 | ||||
-rw-r--r-- | gruel/src/include/gruel/pmt_sugar.h | 94 | ||||
-rw-r--r-- | gruel/src/lib/pmt/pmt.cc | 33 | ||||
-rw-r--r-- | gruel/src/lib/pmt/qa_pmt_prims.cc | 19 | ||||
-rw-r--r-- | gruel/src/lib/pmt/qa_pmt_prims.h | 2 |
6 files changed, 178 insertions, 1 deletions
diff --git a/gruel/src/include/gruel/Makefile.am b/gruel/src/include/gruel/Makefile.am index 9aedc7fda..67dd12995 100644 --- a/gruel/src/include/gruel/Makefile.am +++ b/gruel/src/include/gruel/Makefile.am @@ -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 \ diff --git a/gruel/src/include/gruel/pmt.h b/gruel/src/include/gruel/pmt.h index b1cb29f7c..3188aad1d 100644 --- a/gruel/src/include/gruel/pmt.h +++ b/gruel/src/include/gruel/pmt.h @@ -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 index 000000000..830dfc3fe --- /dev/null +++ b/gruel/src/include/gruel/pmt_sugar.h @@ -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 */ diff --git a/gruel/src/lib/pmt/pmt.cc b/gruel/src/lib/pmt/pmt.cc index 42f25b9de..e50e21838 100644 --- a/gruel/src/lib/pmt/pmt.cc +++ b/gruel/src/lib/pmt/pmt.cc @@ -917,6 +917,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 //////////////////////////////////////////////////////////////////////////// diff --git a/gruel/src/lib/pmt/qa_pmt_prims.cc b/gruel/src/lib/pmt/qa_pmt_prims.cc index 04fdc1a43..59d9e14d3 100644 --- a/gruel/src/lib/pmt/qa_pmt_prims.cc +++ b/gruel/src/lib/pmt/qa_pmt_prims.cc @@ -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); +} diff --git a/gruel/src/lib/pmt/qa_pmt_prims.h b/gruel/src/lib/pmt/qa_pmt_prims.h index fd6f70c8e..29ba02f11 100644 --- a/gruel/src/lib/pmt/qa_pmt_prims.h +++ b/gruel/src/lib/pmt/qa_pmt_prims.h @@ -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 */ |