diff options
author | eb <eb@221aa14e-8319-0410-a670-987f0aec2ac5> | 2007-01-13 06:39:33 +0000 |
---|---|---|
committer | eb <eb@221aa14e-8319-0410-a670-987f0aec2ac5> | 2007-01-13 06:39:33 +0000 |
commit | 29c73e79ef50525b56d8e9f89808baace75fae82 (patch) | |
tree | a7e535126b041d6479e1afa5ff0740cfd9384218 /pmt | |
parent | 77a328cd9cc198de890787e000985baf976af32a (diff) |
Merged latest pmt and mblock into trunk (eb/wip -r4262:4268)
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@4269 221aa14e-8319-0410-a670-987f0aec2ac5
Diffstat (limited to 'pmt')
-rw-r--r-- | pmt/src/lib/Makefile.am | 1 | ||||
-rw-r--r-- | pmt/src/lib/pmt.cc | 100 | ||||
-rw-r--r-- | pmt/src/lib/pmt.h | 72 | ||||
-rw-r--r-- | pmt/src/lib/pmt_int.h | 11 | ||||
-rw-r--r-- | pmt/src/lib/pmt_io.cc | 138 | ||||
-rw-r--r-- | pmt/src/lib/qa_pmt_prims.cc | 11 | ||||
-rw-r--r-- | pmt/src/lib/qa_pmt_prims.h | 2 |
7 files changed, 314 insertions, 21 deletions
diff --git a/pmt/src/lib/Makefile.am b/pmt/src/lib/Makefile.am index 672424edeb..60f889c4b4 100644 --- a/pmt/src/lib/Makefile.am +++ b/pmt/src/lib/Makefile.am @@ -59,6 +59,7 @@ EXTRA_DIST = \ # These are the source files that go into the pmt shared library libpmt_la_SOURCES = \ pmt.cc \ + pmt_io.cc \ pmt_unv.cc # magic flags diff --git a/pmt/src/lib/pmt.cc b/pmt/src/lib/pmt.cc index 0f2ceec51e..036a5f8ba1 100644 --- a/pmt/src/lib/pmt.cc +++ b/pmt/src/lib/pmt.cc @@ -36,18 +36,23 @@ pmt_base::~pmt_base() // Exceptions //////////////////////////////////////////////////////////////////////////// -pmt_exception::pmt_exception(const char *msg, pmt_t obj) - : d_msg(msg), d_obj(obj) +pmt_exception::pmt_exception(const std::string &msg, pmt_t obj) + : logic_error(msg + ": " + pmt_write_string(obj)) { } -pmt_wrong_type::pmt_wrong_type(const char *msg, pmt_t obj) - : pmt_exception(msg, obj) +pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj) + : pmt_exception(msg + ": wrong_type ", obj) { } -pmt_out_of_range::pmt_out_of_range(const char *msg, pmt_t obj) - : pmt_exception(msg, obj) +pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj) + : pmt_exception(msg + ": out of range ", obj) +{ +} + +pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj) + : pmt_exception(msg + ": notimplemented ", obj) { } @@ -197,6 +202,13 @@ pmt_string_to_symbol(const std::string &name) return sym; } +// alias... +pmt_t +pmt_intern(const std::string &name) +{ + return pmt_string_to_symbol(name); +} + const std::string pmt_symbol_to_string(pmt_t sym) { @@ -206,6 +218,8 @@ pmt_symbol_to_string(pmt_t sym) return _symbol(sym)->name(); } + + //////////////////////////////////////////////////////////////////////////// // Number //////////////////////////////////////////////////////////////////////////// @@ -753,3 +767,77 @@ pmt_reverse_x(pmt_t list) return pmt_reverse(list); } +pmt_t +pmt_nth(size_t n, pmt_t list) +{ + pmt_t t = pmt_nthcdr(n, list); + if (pmt_is_pair(t)) + return pmt_car(t); + else + return PMT_NIL; +} + +pmt_t +pmt_nthcdr(size_t n, pmt_t list) +{ + if (!(pmt_is_null(list) || pmt_is_pair(list))) + throw pmt_wrong_type("pmt_nthcdr", list); + + while (n > 0){ + if (pmt_is_pair(list)){ + list = pmt_cdr(list); + n--; + continue; + } + if (pmt_is_null(list)) + return PMT_NIL; + else + throw pmt_wrong_type("pmt_nthcdr: not a LIST", list); + } + return list; +} + +pmt_t +pmt_memq(pmt_t obj, pmt_t list) +{ + while (pmt_is_pair(list)){ + if (pmt_eq(obj, pmt_car(list))) + return list; + list = pmt_cdr(list); + } + return PMT_BOOL_F; +} + +pmt_t +pmt_memv(pmt_t obj, pmt_t list) +{ + while (pmt_is_pair(list)){ + if (pmt_eqv(obj, pmt_car(list))) + return list; + list = pmt_cdr(list); + } + return PMT_BOOL_F; +} + +pmt_t +pmt_member(pmt_t obj, pmt_t list) +{ + while (pmt_is_pair(list)){ + if (pmt_equal(obj, pmt_car(list))) + return list; + list = pmt_cdr(list); + } + return PMT_BOOL_F; +} + +bool +pmt_subsetp(pmt_t list1, pmt_t list2) +{ + while (pmt_is_pair(list1)){ + pmt_t p = pmt_car(list1); + if (pmt_is_false(pmt_memv(p, list2))) + return false; + list1 = pmt_cdr(list1); + } + return true; +} diff --git a/pmt/src/lib/pmt.h b/pmt/src/lib/pmt.h index 0499a16c11..2cb032e5b8 100644 --- a/pmt/src/lib/pmt.h +++ b/pmt/src/lib/pmt.h @@ -28,6 +28,7 @@ #include <string> #include <stdint.h> #include <iostream> +#include <stdexcept> /*! * This file defines a polymorphic type and the operations on it. @@ -49,27 +50,28 @@ class pmt_base; typedef boost::shared_ptr<pmt_base> pmt_t; -class pmt_exception +class pmt_exception : public std::logic_error { - const char *d_msg; - pmt_t d_obj; - public: - pmt_exception(const char *msg, pmt_t obj); - const char *msg() { return d_msg; } - pmt_t obj() { return d_obj; } + pmt_exception(const std::string &msg, pmt_t obj); }; class pmt_wrong_type : public pmt_exception { public: - pmt_wrong_type(const char *msg, pmt_t obj); + pmt_wrong_type(const std::string &msg, pmt_t obj); }; class pmt_out_of_range : public pmt_exception { public: - pmt_out_of_range(const char *msg, pmt_t obj); + pmt_out_of_range(const std::string &msg, pmt_t obj); +}; + +class pmt_notimplemented : public pmt_exception +{ +public: + pmt_notimplemented(const std::string &msg, pmt_t obj); }; /* @@ -111,6 +113,10 @@ bool pmt_is_symbol(pmt_t obj); //! Return the symbol whose name is \p s. pmt_t pmt_string_to_symbol(const std::string &s); +//! Alias for pmt_string_to_symbol +pmt_t pmt_intern(const std::string &s); + + /*! * If \p is a symbol, return the name of the symbol as a string. * Otherwise, raise the wrong_type exception. @@ -504,6 +510,44 @@ pmt_acons(pmt_t x, pmt_t y, pmt_t a) return pmt_cons(pmt_cons(x, y), a); } +/*! + * \brief locates \p nth element of \n list where the car is the 'zeroth' element. + */ +pmt_t pmt_nth(size_t n, pmt_t list); + +/*! + * \brief returns the tail of \p list that would be obtained by calling + * cdr \p n times in succession. + */ +pmt_t pmt_nthcdr(size_t n, pmt_t list); + +/*! + * \brief Return the first sublist of \p list whose car is \p obj. + * If \p obj does not occur in \p list, then #f is returned. + * pmt_memq use pmt_eq to compare \p obj with the elements of \p list. + */ +pmt_t pmt_memq(pmt_t obj, pmt_t list); + +/*! + * \brief Return the first sublist of \p list whose car is \p obj. + * If \p obj does not occur in \p list, then #f is returned. + * pmt_memv use pmt_eqv to compare \p obj with the elements of \p list. + */ +pmt_t pmt_memv(pmt_t obj, pmt_t list); + +/*! + * \brief Return the first sublist of \p list whose car is \p obj. + * If \p obj does not occur in \p list, then #f is returned. + * pmt_member use pmt_equal to compare \p obj with the elements of \p list. + */ +pmt_t pmt_member(pmt_t obj, pmt_t list); + +/*! + * \brief Return true if every element of \p list1 appears in \p list2, and false otherwise. + * Comparisons are done with pmt_eqv. + */ +bool pmt_subsetp(pmt_t list1, pmt_t list2); + /* * ------------------------------------------------------------------------ * read / write @@ -536,6 +580,16 @@ pmt_t pmt_read(std::istream &port); */ void pmt_write(pmt_t obj, std::ostream &port); +/*! + * Return a string representation of \p obj. + * This is the same output as would be generated by pmt_write. + */ +std::string pmt_write_string(pmt_t obj); + + +std::ostream& operator<<(std::ostream &os, pmt_t obj); + + /* * ------------------------------------------------------------------------ * portable byte stream representation diff --git a/pmt/src/lib/pmt_int.h b/pmt/src/lib/pmt_int.h index 5a178523c2..458443eafa 100644 --- a/pmt/src/lib/pmt_int.h +++ b/pmt/src/lib/pmt_int.h @@ -23,6 +23,7 @@ #define INCLUDED_PMT_INT_H #include <pmt.h> +#include <boost/utility.hpp> /* * EVERYTHING IN THIS FILE IS PRIVATE TO THE IMPLEMENTATION! @@ -30,16 +31,11 @@ * See pmt.h for the public interface */ -class pmt_base { +class pmt_base : boost::noncopyable { protected: pmt_base(){}; virtual ~pmt_base(); -private: - pmt_base(const pmt_base& rhs); // NOT IMPLEMENTED - pmt_base& operator=(const pmt_base& rhs); // NOT IMPLEMENTED - - public: virtual bool is_bool() const { return false; } virtual bool is_symbol() const { return false; } @@ -102,6 +98,7 @@ public: pmt_integer(long value); //~pmt_integer(){} + bool is_number() const { return true; } bool is_integer() const { return true; } long value() const { return d_value; } }; @@ -114,6 +111,7 @@ public: pmt_real(double value); //~pmt_real(){} + bool is_number() const { return true; } bool is_real() const { return true; } double value() const { return d_value; } }; @@ -126,6 +124,7 @@ public: pmt_complex(std::complex<double> value); //~pmt_complex(){} + bool is_number() const { return true; } bool is_complex() const { return true; } std::complex<double> value() const { return d_value; } }; diff --git a/pmt/src/lib/pmt_io.cc b/pmt/src/lib/pmt_io.cc new file mode 100644 index 0000000000..2d7af68b2b --- /dev/null +++ b/pmt/src/lib/pmt_io.cc @@ -0,0 +1,138 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 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 2, 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. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <vector> +#include <pmt.h> +#include "pmt_int.h" +#include <sstream> + +static void +pmt_write_list_tail(pmt_t obj, std::ostream &port) +{ + pmt_write(pmt_car(obj), port); // write the car + obj = pmt_cdr(obj); // step to cdr + + if (pmt_is_null(obj)) // () + port << ")"; + + else if (pmt_is_pair(obj)){ // normal list + port << " "; + pmt_write_list_tail(obj, port); + } + else { // dotted pair + port << " . "; + pmt_write(obj, port); + port << ")"; + } +} + +void +pmt_write(pmt_t obj, std::ostream &port) +{ + if (pmt_is_bool(obj)){ + if (pmt_is_true(obj)) + port << "#t"; + else + port << "#f"; + } + else if (pmt_is_symbol(obj)){ + port << pmt_symbol_to_string(obj); + } + else if (pmt_is_number(obj)){ + if (pmt_is_integer(obj)) + port << pmt_to_long(obj); + else if (pmt_is_real(obj)) + port << pmt_to_double(obj); + else if (pmt_is_complex(obj)){ + std::complex<double> c = pmt_to_complex(obj); + port << c.real() << '+' << c.imag() << 'i'; + } + else + goto error; + } + else if (pmt_is_null(obj)){ + port << "()"; + } + else if (pmt_is_pair(obj)){ + port << "("; + pmt_write_list_tail(obj, port); + } + else if (pmt_is_dict(obj)){ + // FIXME + // port << "#<dict " << obj << ">"; + port << "#<dict>"; + } + else if (pmt_is_vector(obj)){ + // FIXME + // port << "#<vector " << obj << ">"; + port << "#<vector>"; + } + else if (pmt_is_uniform_vector(obj)){ + // FIXME + // port << "#<uniform-vector " << obj << ">"; + port << "#<uniform-vector>"; + } + else { + error: + // FIXME + // port << "#<" << obj << ">"; + port << "#<unknown>"; + } +} + +std::ostream& operator<<(std::ostream &os, pmt_t obj) +{ + pmt_write(obj, os); + return os; +} + +std::string +pmt_write_string(pmt_t obj) +{ + std::ostringstream s; + s << obj; + return s.str(); +} + +pmt_t +pmt_read(std::istream &port) +{ + throw pmt_notimplemented("notimplemented: pmt_read", PMT_NIL); +} + +void +pmt_serialize(pmt_t obj, std::ostream &sink) +{ + throw pmt_notimplemented("notimplemented: pmt_serialize", obj); +} + +/*! + * \brief Create obj from portable byte-serial representation + */ +pmt_t +pmt_deserialize(std::istream &source) +{ + throw pmt_notimplemented("notimplemented: pmt_deserialize", PMT_NIL); +} + diff --git a/pmt/src/lib/qa_pmt_prims.cc b/pmt/src/lib/qa_pmt_prims.cc index 09669c3185..cb687b0e1b 100644 --- a/pmt/src/lib/qa_pmt_prims.cc +++ b/pmt/src/lib/qa_pmt_prims.cc @@ -282,3 +282,14 @@ qa_pmt_prims::test_dict() CPPUNIT_ASSERT(pmt_equal(keys, pmt_dict_keys(dict))); CPPUNIT_ASSERT(pmt_equal(vals, pmt_dict_values(dict))); } + +void +qa_pmt_prims::test_io() +{ + pmt_t k0 = pmt_string_to_symbol("k0"); + pmt_t k1 = pmt_string_to_symbol("k1"); + pmt_t k2 = pmt_string_to_symbol("k2"); + pmt_t k3 = pmt_string_to_symbol("k3"); + + CPPUNIT_ASSERT_EQUAL(std::string("k0"), pmt_write_string(k0)); +} diff --git a/pmt/src/lib/qa_pmt_prims.h b/pmt/src/lib/qa_pmt_prims.h index d5e8e7530f..8d379c5afa 100644 --- a/pmt/src/lib/qa_pmt_prims.h +++ b/pmt/src/lib/qa_pmt_prims.h @@ -38,6 +38,7 @@ class qa_pmt_prims : public CppUnit::TestCase { CPPUNIT_TEST(test_equivalence); CPPUNIT_TEST(test_misc); CPPUNIT_TEST(test_dict); + CPPUNIT_TEST(test_io); CPPUNIT_TEST_SUITE_END(); private: @@ -51,6 +52,7 @@ class qa_pmt_prims : public CppUnit::TestCase { void test_equivalence(); void test_misc(); void test_dict(); + void test_io(); }; #endif /* INCLUDED_QA_PMT_PRIMS_H */ |