pmt perf improvements: Make function arguments const & (c1257, c1260
[debian/gnuradio] / pmt / src / lib / pmt.h
index b27b7ce1cf4c779c275786e2e6a1d72aa9378234..24963fee71be791b93f44872f03802c7325f8334 100644 (file)
@@ -1,12 +1,12 @@
 /* -*- c++ -*- */
 /*
- * Copyright 2006 Free Software Foundation, Inc.
+ * Copyright 2006,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 2, or (at your option)
+ * 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,
  * 
  * You should have received a copy of the GNU General Public License
  * along with GNU Radio; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifndef INCLUDED_PMT_H
 #define INCLUDED_PMT_H
 
 #include <boost/shared_ptr.hpp>
+#include <boost/any.hpp>
 #include <complex>
 #include <string>
 #include <stdint.h>
-#include <iostream>
+#include <iosfwd>
+#include <stdexcept>
 
 /*!
  * This file defines a polymorphic type and the operations on it.
@@ -49,27 +51,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);
 };
 
 /*
@@ -80,22 +83,22 @@ public:
  * I.e., there is a single false value, #f.
  * ------------------------------------------------------------------------
  */
-extern const pmt_t PMT_BOOL_T; //< #t : boolean true constant
-extern const pmt_t PMT_BOOL_F; //< #f : boolean false constant
+extern const pmt_t PMT_T;      //< \#t : boolean true constant
+extern const pmt_t PMT_F;      //< \#f : boolean false constant
 
-//! Return true if obj is #t or #f, else return false.
+//! Return true if obj is \#t or \#f, else return false.
 bool pmt_is_bool(pmt_t obj);
 
-//! Return false if obj is #f, else return true.
+//! Return false if obj is \#f, else return true.
 bool pmt_is_true(pmt_t obj);
 
-//! Return true if obj is #f, else return true.
+//! Return true if obj is \#f, else return true.
 bool pmt_is_false(pmt_t obj);
 
-//! Return #f is val is false, else return #t.
+//! Return \#f is val is false, else return \#t.
 pmt_t pmt_from_bool(bool val);
 
-//! Return true if val is PMT_BOOL_T, return false when val is PMT_BOOL_F, 
+//! Return true if val is PMT_T, return false when val is PMT_F, 
 // else raise wrong_type exception.
 bool pmt_to_bool(pmt_t val);
 
@@ -106,16 +109,20 @@ bool pmt_to_bool(pmt_t val);
  */
 
 //! Return true if obj is a symbol, else false.
-bool pmt_is_symbol(pmt_t obj);
+bool pmt_is_symbol(const 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.
  */
-const std::string pmt_symbol_to_string(pmt_t sym);
+const std::string pmt_symbol_to_string(const pmt_t& sym);
 
 /*
  * ------------------------------------------------------------------------
@@ -199,19 +206,19 @@ std::complex<double> pmt_to_complex(pmt_t z);
 extern const pmt_t PMT_NIL;    //< the empty list
 
 //! Return true if \p x is the empty list, otherwise return false.
-bool pmt_is_null(pmt_t x);
+bool pmt_is_null(const pmt_t& x);
 
 //! Return true if \p obj is a pair, else false.
-bool pmt_is_pair(pmt_t obj);
+bool pmt_is_pair(const pmt_t& obj);
 
 //! Return a newly allocated pair whose car is \p x and whose cdr is \p y.
-pmt_t pmt_cons(pmt_t x, pmt_t y);
+pmt_t pmt_cons(const pmt_t& x, const pmt_t& y);
 
 //! If \p pair is a pair, return the car of the \p pair, otherwise raise wrong_type.
-pmt_t pmt_car(pmt_t pair);
+pmt_t pmt_car(const pmt_t& pair);
 
 //! If \p pair is a pair, return the cdr of the \p pair, otherwise raise wrong_type.
-pmt_t pmt_cdr(pmt_t pair);
+pmt_t pmt_cdr(const pmt_t& pair);
 
 //! Stores \p value in the car field of \p pair.
 void pmt_set_car(pmt_t pair, pmt_t value);
@@ -219,6 +226,13 @@ void pmt_set_car(pmt_t pair, pmt_t value);
 //! Stores \p value in the cdr field of \p pair.
 void pmt_set_cdr(pmt_t pair, pmt_t value);
 
+pmt_t pmt_caar(pmt_t pair);
+pmt_t pmt_cadr(pmt_t pair);
+pmt_t pmt_cdar(pmt_t pair);
+pmt_t pmt_cddr(pmt_t pair);
+pmt_t pmt_caddr(pmt_t pair);
+pmt_t pmt_cadddr(pmt_t pair);
+
 /*
  * ------------------------------------------------------------------------
  *                            Vectors
@@ -303,18 +317,18 @@ pmt_t pmt_make_f64vector(size_t k, double fill);
 pmt_t pmt_make_c32vector(size_t k, std::complex<float> fill);
 pmt_t pmt_make_c64vector(size_t k, std::complex<double> fill);
 
-pmt_t pmt_init_u8vector(size_t k, uint8_t *data);
-pmt_t pmt_init_s8vector(size_t k, int8_t *data);
-pmt_t pmt_init_u16vector(size_t k, uint16_t *data);
-pmt_t pmt_init_s16vector(size_t k, int16_t *data);
-pmt_t pmt_init_u32vector(size_t k, uint32_t *data);
-pmt_t pmt_init_s32vector(size_t k, int32_t *data);
-pmt_t pmt_init_u64vector(size_t k, uint64_t *data);
-pmt_t pmt_init_s64vector(size_t k, int64_t *data);
-pmt_t pmt_init_f32vector(size_t k, float *data);
-pmt_t pmt_init_f64vector(size_t k, double *data);
-pmt_t pmt_init_c32vector(size_t k, std::complex<float> *data);
-pmt_t pmt_init_c64vector(size_t k, std::complex<double> *data);
+pmt_t pmt_init_u8vector(size_t k, const uint8_t *data);
+pmt_t pmt_init_s8vector(size_t k, const int8_t *data);
+pmt_t pmt_init_u16vector(size_t k, const uint16_t *data);
+pmt_t pmt_init_s16vector(size_t k, const int16_t *data);
+pmt_t pmt_init_u32vector(size_t k, const uint32_t *data);
+pmt_t pmt_init_s32vector(size_t k, const int32_t *data);
+pmt_t pmt_init_u64vector(size_t k, const uint64_t *data);
+pmt_t pmt_init_s64vector(size_t k, const int64_t *data);
+pmt_t pmt_init_f32vector(size_t k, const float *data);
+pmt_t pmt_init_f64vector(size_t k, const double *data);
+pmt_t pmt_init_c32vector(size_t k, const std::complex<float> *data);
+pmt_t pmt_init_c64vector(size_t k, const std::complex<double> *data);
 
 uint8_t  pmt_u8vector_ref(pmt_t v, size_t k);
 int8_t   pmt_s8vector_ref(pmt_t v, size_t k);
@@ -361,7 +375,7 @@ const std::complex<double> *pmt_c64vector_elements(pmt_t v, size_t &len); //< le
 
 // Return non-const pointers to the elements
 
-void *pmt_uniform_vector_writeable_elements(pmt_t v, size_t &len);  //< works with any; len is in bytes
+void *pmt_uniform_vector_writable_elements(pmt_t v, size_t &len);  //< works with any; len is in bytes
 
 uint8_t  *pmt_u8vector_writable_elements(pmt_t v, size_t &len);  //< len is in elements
 int8_t   *pmt_s8vector_writable_elements(pmt_t v, size_t &len);  //< len is in elements
@@ -406,6 +420,28 @@ pmt_t pmt_dict_keys(pmt_t dict);
 //! Return list of values
 pmt_t pmt_dict_values(pmt_t dict);
 
+/*
+ * ------------------------------------------------------------------------
+ *   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
@@ -413,7 +449,7 @@ pmt_t pmt_dict_values(pmt_t dict);
  */
 
 //! Return true if x and y are the same object; otherwise return false.
-bool pmt_eq(pmt_t x, pmt_t y);
+bool pmt_eq(const pmt_t& x, const pmt_t& y);
 
 /*!
  * \brief Return true if x and y should normally be regarded as the same object, else false.
@@ -421,14 +457,14 @@ bool pmt_eq(pmt_t x, pmt_t y);
  * <pre>
  * eqv returns true if:
  *   x and y are the same object.
- *   x and y are both #t or both #f.
+ *   x and y are both \#t or both \#f.
  *   x and y are both symbols and their names are the same.
  *   x and y are both numbers, and are numerically equal.
  *   x and y are both the empty list (nil).
  *   x and y are pairs or vectors that denote same location in store.
  * </pre>
  */
-bool pmt_eqv(pmt_t x, pmt_t y);
+bool pmt_eqv(const pmt_t& x, const pmt_t& y);
 
 /*!
  * pmt_equal recursively compares the contents of pairs and vectors,
@@ -436,18 +472,18 @@ bool pmt_eqv(pmt_t x, pmt_t y);
  * pmt_equal may fail to terminate if its arguments are circular data
  * structures.
  */
-bool pmt_equal(pmt_t x, pmt_t y);
+bool pmt_equal(const pmt_t& x, const pmt_t& y);
 
 
 //! Return the number of elements in v
-size_t pmt_length(pmt_t v);
+size_t pmt_length(const pmt_t& v);
 
 /*!
  * \brief Find the first pair in \p alist whose car field is \p obj
  *  and return that pair.
  *
  * \p alist (for "association list") must be a list of pairs.  If no pair
- * in \p alist has \p obj as its car then #f is returned.
+ * in \p alist has \p obj as its car then \#f is returned.
  * Uses pmt_eq to compare \p obj with car fields of the pairs in \p alist.
  */
 pmt_t pmt_assq(pmt_t obj, pmt_t alist);
@@ -457,7 +493,7 @@ pmt_t pmt_assq(pmt_t obj, pmt_t alist);
  *  and return that pair.
  *
  * \p alist (for "association list") must be a list of pairs.  If no pair
- * in \p alist has \p obj as its car then #f is returned.
+ * in \p alist has \p obj as its car then \#f is returned.
  * Uses pmt_eqv to compare \p obj with car fields of the pairs in \p alist.
  */
 pmt_t pmt_assv(pmt_t obj, pmt_t alist);
@@ -467,7 +503,7 @@ pmt_t pmt_assv(pmt_t obj, pmt_t alist);
  *  and return that pair.
  *
  * \p alist (for "association list") must be a list of pairs.  If no pair
- * in \p alist has \p obj as its car then #f is returned.
+ * in \p alist has \p obj as its car then \#f is returned.
  * Uses pmt_equal to compare \p obj with car fields of the pairs in \p alist.
  */
 pmt_t pmt_assoc(pmt_t obj, pmt_t alist);
@@ -479,7 +515,7 @@ pmt_t pmt_assoc(pmt_t obj, pmt_t alist);
  * \p list must be a list.  The dynamic order in which \p proc is
  * applied to the elements of \p list is unspecified.
  */
-pmt_t pmt_map(pmt_t proc(pmt_t), pmt_t list);
+pmt_t pmt_map(pmt_t proc(const pmt_t&), pmt_t list);
 
 /*!
  * \brief reverse \p list.
@@ -504,6 +540,81 @@ 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);
+
+/*!
+ * \brief Return a list of length 1 containing \p x1
+ */
+pmt_t pmt_list1(const pmt_t& x1);
+
+/*!
+ * \brief Return a list of length 2 containing \p x1, \p x2
+ */
+pmt_t pmt_list2(const pmt_t& x1, const pmt_t& x2);
+
+/*!
+ * \brief Return a list of length 3 containing \p x1, \p x2, \p x3
+ */
+pmt_t pmt_list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3);
+
+/*!
+ * \brief Return a list of length 4 containing \p x1, \p x2, \p x3, \p x4
+ */
+pmt_t pmt_list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4);
+
+/*!
+ * \brief Return a list of length 5 containing \p x1, \p x2, \p x3, \p x4, \p x5
+ */
+pmt_t pmt_list5(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5);
+
+/*!
+ * \brief Return a list of length 6 containing \p x1, \p x2, \p x3, \p x4, \p
+ * x5, \p x6
+ */
+pmt_t pmt_list6(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5, const pmt_t& x6);
+
+/*!
+ * \brief Return \p list with \p item added to it.
+ */
+pmt_t pmt_list_add(pmt_t list, const pmt_t& item);
+
+
 /*
  * ------------------------------------------------------------------------
  *                          read / write
@@ -536,6 +647,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
@@ -544,11 +665,14 @@ void pmt_write(pmt_t obj, std::ostream &port);
 /*!
  * \brief Write portable byte-serial representation of \p obj to \p sink
  */
-void pmt_serialize(pmt_t obj, std::ostream &sink);
+bool pmt_serialize(pmt_t obj, std::streambuf &sink);
 
 /*!
  * \brief Create obj from portable byte-serial representation
  */
-pmt_t pmt_deserialize(std::istream &source);
+pmt_t pmt_deserialize(std::streambuf &source);
+
+
+void pmt_dump_sizeof();        // debugging
 
 #endif /* INCLUDED_PMT_H */