commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] pmt/src/lib pmt.cc pmt.h pmt_int.h


From: Eric Blossom
Subject: [Commit-gnuradio] pmt/src/lib pmt.cc pmt.h pmt_int.h
Date: Tue, 04 Jul 2006 03:49:36 +0000

CVSROOT:        /sources/gnuradio
Module name:    pmt
Changes by:     Eric Blossom <eb>       06/07/04 03:49:36

Modified files:
        src/lib        : pmt.cc pmt.h pmt_int.h 

Log message:
        work-in-progress

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/pmt/src/lib/pmt.cc?cvsroot=gnuradio&r1=1.1.1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/pmt/src/lib/pmt.h?cvsroot=gnuradio&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/pmt/src/lib/pmt_int.h?cvsroot=gnuradio&r1=1.1&r2=1.2

Patches:
Index: pmt.cc
===================================================================
RCS file: /sources/gnuradio/pmt/src/lib/pmt.cc,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -b -r1.1.1.1 -r1.2
--- pmt.cc      26 Jun 2006 04:50:48 -0000      1.1.1.1
+++ pmt.cc      4 Jul 2006 03:49:36 -0000       1.2
@@ -23,5 +23,319 @@
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
+#include <vector>
 #include <pmt.h>
+#include "pmt_int.h"
 
+pmt_base::~pmt_base()
+{
+  // nop -- out of line virtual destructor
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                         Exceptions
+////////////////////////////////////////////////////////////////////////////
+
+pmt_exception::pmt_exception(const char *msg, pmt_t obj)
+  : d_msg(msg), d_obj(obj)
+{
+}
+
+pmt_wrong_type::pmt_wrong_type(const char *msg, pmt_t obj)
+  : pmt_exception(msg, obj)
+{
+}
+
+pmt_out_of_range::pmt_out_of_range(const char *msg, pmt_t obj)
+  : pmt_exception(msg, obj)
+{
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                          Dynamic Casts
+////////////////////////////////////////////////////////////////////////////
+
+static pmt_symbol *
+_symbol(pmt_t x)
+{
+  return dynamic_cast<pmt_symbol*>(x.get());
+}
+
+static pmt_integer *
+_integer(pmt_t x)
+{
+  return dynamic_cast<pmt_integer*>(x.get());
+}
+
+static pmt_real *
+_real(pmt_t x)
+{
+  return dynamic_cast<pmt_real*>(x.get());
+}
+
+static pmt_complex *
+_complex(pmt_t x)
+{
+  return dynamic_cast<pmt_complex*>(x.get());
+}
+
+static pmt_pair *
+_pair(pmt_t x)
+{
+  return dynamic_cast<pmt_pair*>(x.get());
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                           Booleans
+////////////////////////////////////////////////////////////////////////////
+
+const pmt_t PMT_BOOL_T = pmt_t(new pmt_bool());                // singleton
+const pmt_t PMT_BOOL_F = pmt_t(new pmt_bool());                // singleton
+
+bool
+pmt_is_true(pmt_t obj)
+{
+  return obj != PMT_BOOL_F;
+}
+
+bool
+pmt_is_false(pmt_t obj)
+{
+  return obj == PMT_BOOL_F;
+}
+
+bool
+pmt_is_bool(pmt_t obj)
+{
+  return obj->is_bool();
+}
+
+pmt_t
+pmt_from_bool(bool val)
+{
+  return val ? PMT_BOOL_T : PMT_BOOL_F;
+}
+
+bool
+pmt_to_bool(pmt_t val)
+{
+  if (val == PMT_BOOL_T)
+    return true;
+  if (val == PMT_BOOL_F)
+    return false;
+  throw pmt_wrong_type("pmt_to_bool", val);
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                             Symbols
+////////////////////////////////////////////////////////////////////////////
+
+static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
+static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
+
+pmt_symbol::pmt_symbol(const std::string &name)
+  : d_name(name)
+{
+}
+
+
+static unsigned int
+hash_string(const std::string &s)
+{
+  unsigned int h = 0;
+  unsigned int g = 0;
+
+  for (std::string::const_iterator p = s.begin(); p != s.end(); p++){
+    h = (h << 4) + *p;
+    g = h & 0xf0000000;
+    if (g){
+      h = h ^ (g >> 24);
+      h = h ^ g;
+    }
+  }
+  return h;
+}
+
+bool 
+pmt_is_symbol(pmt_t obj)
+{
+  return obj->is_symbol();
+}
+
+pmt_t 
+pmt_string_to_symbol(const std::string &name)
+{
+  unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
+
+  // Does a symbol with this name already exist?
+  for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
+    if (name == _symbol(sym)->name())
+      return sym;              // Yes.  Return it
+  }
+
+  // Nope.  Make a new one.
+  pmt_t sym = pmt_t(new pmt_symbol(name));
+  _symbol(sym)->set_next(s_symbol_hash_table[hash]);
+  s_symbol_hash_table[hash] = sym;
+  return sym;
+}
+
+const std::string
+pmt_symbol_to_string(pmt_t sym)
+{
+  if (!sym->is_symbol())
+    throw pmt_wrong_type("pmt_symbol_to_string", sym);
+
+  return _symbol(sym)->name();
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                             Number
+////////////////////////////////////////////////////////////////////////////
+
+bool
+pmt_is_number(pmt_t x)
+{
+  return x->is_number();
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                             Integer
+////////////////////////////////////////////////////////////////////////////
+
+bool
+pmt_is_integer(pmt_t x)
+{
+  return x->is_integer();
+}
+
+pmt_t
+pmt_from_long(long x)
+{
+  return pmt_t(new pmt_integer(x));
+}
+
+long
+pmt_to_long(pmt_t x)
+{
+  if (x->is_integer())
+    return _integer(x)->value();
+
+  throw pmt_wrong_type("pmt_to_long", x);
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                              Real
+////////////////////////////////////////////////////////////////////////////
+
+bool 
+pmt_is_real(pmt_t x)
+{
+  return x->is_real();
+}
+
+pmt_t
+pmt_from_double(double x)
+{
+  return pmt_t(new pmt_real(x));
+}
+
+double
+pmt_to_double(pmt_t x)
+{
+  if (x->is_real())
+    return _real(x)->value();
+  if (x->is_integer())
+    return _integer(x)->value();
+
+  throw pmt_wrong_type("pmt_to_double", x);
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                              Complex
+////////////////////////////////////////////////////////////////////////////
+
+bool 
+pmt_is_complex(pmt_t x)
+{
+  return x->is_complex();
+}
+
+pmt_t
+pmt_make_rectangular(double re, double im)
+{
+  return pmt_t(new pmt_complex(std::complex<double>(re, im)));
+}
+
+std::complex<double>
+pmt_to_complex(pmt_t x)
+{
+  if (x->is_complex())
+    return _complex(x)->value();
+  if (x->is_real())
+    return _real(x)->value();
+  if (x->is_integer())
+    return _integer(x)->value();
+
+  throw pmt_wrong_type("pmt_to_complex", x);
+}
+
+////////////////////////////////////////////////////////////////////////////
+//                              Pairs
+////////////////////////////////////////////////////////////////////////////
+
+const pmt_t PMT_NIL = pmt_t(new pmt_null());           // singleton
+
+bool
+pmt_is_null(pmt_t x)
+{
+  return x == PMT_NIL;
+}
+
+bool
+pmt_is_pair(pmt_t obj)
+{
+  return obj->is_pair();
+}
+
+pmt_t
+pmt_cons(pmt_t x, pmt_t y)
+{
+  return pmt_t(new pmt_pair(x, y));
+}
+
+pmt_t
+pmt_car(pmt_t pair)
+{
+  if (pair->is_pair())
+    return _pair(pair)->car();
+  
+  throw pmt_wrong_type("pmt_car", pair);
+}
+
+pmt_t
+pmt_cdr(pmt_t pair)
+{
+  if (pair->is_pair())
+    return _pair(pair)->cdr();
+  
+  throw pmt_wrong_type("pmt_cdr", pair);
+}
+
+void
+pmt_set_car(pmt_t pair, pmt_t obj)
+{
+  if (pair->is_pair())
+    _pair(pair)->set_car(obj);
+  
+  throw pmt_wrong_type("pmt_set_car", pair);
+}
+
+void
+pmt_set_cdr(pmt_t pair, pmt_t obj)
+{
+  if (pair->is_pair())
+    _pair(pair)->set_cdr(obj);
+  
+  throw pmt_wrong_type("pmt_set_cdr", pair);
+}

Index: pmt.h
===================================================================
RCS file: /sources/gnuradio/pmt/src/lib/pmt.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- pmt.h       1 Jul 2006 23:28:02 -0000       1.4
+++ pmt.h       4 Jul 2006 03:49:36 -0000       1.5
@@ -48,6 +48,30 @@
  */
 typedef boost::shared_ptr<pmt_base> pmt_t;
 
+
+class pmt_exception
+{
+  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; }
+};
+
+class pmt_wrong_type : public pmt_exception
+{
+public:
+  pmt_wrong_type(const char *msg, pmt_t obj);
+};
+
+class pmt_out_of_range : public pmt_exception
+{
+public:
+  pmt_out_of_range(const char *msg, pmt_t obj);
+};
+
 /*
  * ------------------------------------------------------------------------
  * Booleans.  Two constants, #t and #f.
@@ -59,20 +83,20 @@
 extern const pmt_t PMT_BOOL_T; //< #t : boolean true constant
 extern const pmt_t PMT_BOOL_F; //< #f : boolean false constant
 
+//! 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.
 bool pmt_is_true(pmt_t obj);
 
 //! Return true if obj is #f, else return true.
 bool pmt_is_false(pmt_t obj);
 
-//! Return true if obj is #t or #f, else return false.
-bool pmt_is_bool(pmt_t obj);
-
 //! 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, 
-// else throw 'wrong-type' exception.
+// else raise wrong_type exception.
 bool pmt_to_bool(pmt_t val);
 
 /*
@@ -131,10 +155,6 @@
 
 /*
  * \brief Return true if \p obj is a real number, else false.
- *
- * Note that the sets of integer and rational values form subsets of the set
- * of real numbers, so the predicate will also be fulfilled if \p obj is
- * an integer number or rational number. (Rationals are not implemented.)
  */
 bool pmt_is_real(pmt_t obj);
 
@@ -145,7 +165,7 @@
  * \brief Convert pmt to double if possible.
  *
  * Returns the number closest to \p val that is representable
- * as a double.  The argument \p val must be a real number, otherwise
+ * as a double.  The argument \p val must be a real or integer, otherwise
  * a wrong_type exception is raised.
  */
 double pmt_to_double(pmt_t x);
@@ -158,10 +178,6 @@
 
 /*!
  * \brief return true if \p obj is a complex number, false otherwise.
- *
- * Note that the sets of real, rational and integer values form subsets of
- * the subsets of the set of complex numbers, i.e., the predicate will
- * also be fulfilled if \p obj is a real, rational or integer number.
  */
 bool pmt_is_complex(pmt_t obj);
 
@@ -169,7 +185,7 @@
 pmt_t pmt_make_rectangular(double re, double im);
 
 /*!
- * If \p z is complex, return the closest complex<double>.
+ * If \p z is complex, real or integer, return the closest complex<double>.
  * Otherwise, raise the wrong_type exception.
  */
 std::complex<double> pmt_to_complex(pmt_t z);

Index: pmt_int.h
===================================================================
RCS file: /sources/gnuradio/pmt/src/lib/pmt_int.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- pmt_int.h   1 Jul 2006 23:28:02 -0000       1.1
+++ pmt_int.h   4 Jul 2006 03:49:36 -0000       1.2
@@ -24,38 +24,57 @@
 
 #include <pmt.h>
 
+/*
+ * EVERYTHING IN THIS FILE IS PRIVATE TO THE IMPLEMENTATION!
+ *
+ * See pmt.h for the public interface
+ */
 
 class pmt_base {
 protected:
-  virtual ~pmt_base() = 0;
+  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 pmt_is_bool()    { return false; }
-  virtual bool pmt_is_symbol()  { return false; }
-  virtual bool pmt_is_number()  { return false; }
-  virtual bool pmt_is_integer() { return false; }
-  virtual bool pmt_is_real()    { return false; }
-  virtual bool pmt_is_complex() { return false; }
+  virtual bool is_bool()    const { return false; }
+  virtual bool is_symbol()  const { return false; }
+  virtual bool is_number()  const { return false; }
+  virtual bool is_integer() const { return false; }
+  virtual bool is_real()    const { return false; }
+  virtual bool is_complex() const { return false; }
+  virtual bool is_null()    const { return false; }
+  virtual bool is_pair()    const { return false; }
 };
 
 class pmt_bool : public pmt_base
 {
 public:
-  ~pmt_bool() {}
+  pmt_bool();
+  //~pmt_bool(){}
 
-  bool pmt_is_bool() { return true; }
+  bool is_bool() const { return true; }
 };
 
 
 class pmt_symbol : public pmt_base
 {
   std::string  d_name;
+  pmt_t                d_next;
   
 public:
   pmt_symbol(const std::string &name);
-  ~pmt_symbol() {}
+  //~pmt_symbol(){}
+
+  bool is_symbol() const { return true; }
+  const std::string name() { return d_name; }
 
-  bool pmt_is_symbol() { return true; }
+  pmt_t next() { return d_next; }              // symbol table link
+  void set_next(pmt_t next) { d_next = next; }
 };
 
 class pmt_integer : public pmt_base
@@ -64,11 +83,10 @@
 
 public:
   pmt_integer(long value);
-  ~pmt_integer() {}
+  //~pmt_integer(){}
 
-  bool pmt_is_integer() { return true; }
-  bool pmt_is_real()    { return true; }
-  bool pmt_is_complex() { return true; }
+  bool is_integer() const { return true; }
+  long value() const { return d_value; }
 };
 
 class pmt_real : public pmt_base
@@ -77,10 +95,10 @@
 
 public:
   pmt_real(double value);
-  ~pmt_real() {}
+  //~pmt_real(){}
 
-  bool pmt_is_real()    { return true; }
-  bool pmt_is_complex() { return true; }
+  bool is_real() const { return true; }
+  double value() const { return d_value; }
 };
 
 class pmt_complex : public pmt_base
@@ -88,22 +106,37 @@
   std::complex<double> d_value;
 
 public:
-  pmt_real(std::complex<double> value);
-  ~pmt_real() {}
+  pmt_complex(std::complex<double> value);
+  //~pmt_complex(){}
 
-  bool pmt_is_complex() { return true; }
+  bool is_complex() const { return true; }
+  std::complex<double> value() const { return d_value; }
+};
+
+class pmt_null  : public pmt_base
+{
+public:
+  pmt_null();
+  //~pmt_null(){}
+
+  bool is_null() const { return true; }
 };
 
 class pmt_pair : public pmt_base
 {
   pmt_t                d_car;
   pmt_t                d_cdr;
+
 public:
   pmt_pair(pmt_t car, pmt_t cdr);
-  ~pmt_pair();
+  //~pmt_pair(){};
 
-  bool pmt_is_pair() { return true; }
-};
+  bool is_pair() const { return true; }
+  pmt_t car() const { return d_car; }
+  pmt_t cdr() const { return d_cdr; }
 
+  void set_car(pmt_t car) { d_car = car; }
+  void set_cdr(pmt_t cdr) { d_cdr = cdr; }
+};
 
 #endif /* INCLUDED_PMT_INT_H */




reply via email to

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