1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2009,2010 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "pmt_int.h"
28 #include <gnuradio/messages/msg_accepter.h>
29 #include <pmt/pmt.h>
30 #include <pmt/pmt_pool.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <vector>
34 
35 namespace pmt {
36 
~pmt_base()37 pmt_base::~pmt_base()
38 {
39     // nop -- out of line virtual destructor
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////
43 //                         Exceptions
44 ////////////////////////////////////////////////////////////////////////////
45 
exception(const std::string & msg,pmt_t obj)46 exception::exception(const std::string& msg, pmt_t obj)
47     : logic_error(msg + ": " + write_string(obj))
48 {
49 }
50 
wrong_type(const std::string & msg,pmt_t obj)51 wrong_type::wrong_type(const std::string& msg, pmt_t obj)
52     : exception(msg + ": wrong_type ", obj)
53 {
54 }
55 
out_of_range(const std::string & msg,pmt_t obj)56 out_of_range::out_of_range(const std::string& msg, pmt_t obj)
57     : exception(msg + ": out of range ", obj)
58 {
59 }
60 
notimplemented(const std::string & msg,pmt_t obj)61 notimplemented::notimplemented(const std::string& msg, pmt_t obj)
62     : exception(msg + ": notimplemented ", obj)
63 {
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////
67 //                          Dynamic Casts
68 ////////////////////////////////////////////////////////////////////////////
69 
_symbol(pmt_t x)70 static pmt_symbol* _symbol(pmt_t x) { return dynamic_cast<pmt_symbol*>(x.get()); }
71 
_integer(pmt_t x)72 static pmt_integer* _integer(pmt_t x) { return dynamic_cast<pmt_integer*>(x.get()); }
73 
_uint64(pmt_t x)74 static pmt_uint64* _uint64(pmt_t x) { return dynamic_cast<pmt_uint64*>(x.get()); }
75 
_real(pmt_t x)76 static pmt_real* _real(pmt_t x) { return dynamic_cast<pmt_real*>(x.get()); }
77 
_complex(pmt_t x)78 static pmt_complex* _complex(pmt_t x) { return dynamic_cast<pmt_complex*>(x.get()); }
79 
_pair(pmt_t x)80 static pmt_pair* _pair(pmt_t x) { return dynamic_cast<pmt_pair*>(x.get()); }
81 
_vector(pmt_t x)82 static pmt_vector* _vector(pmt_t x) { return dynamic_cast<pmt_vector*>(x.get()); }
83 
_tuple(pmt_t x)84 static pmt_tuple* _tuple(pmt_t x) { return dynamic_cast<pmt_tuple*>(x.get()); }
85 
_uniform_vector(pmt_t x)86 static pmt_uniform_vector* _uniform_vector(pmt_t x)
87 {
88     return dynamic_cast<pmt_uniform_vector*>(x.get());
89 }
90 
_any(pmt_t x)91 static pmt_any* _any(pmt_t x) { return dynamic_cast<pmt_any*>(x.get()); }
92 
93 ////////////////////////////////////////////////////////////////////////////
94 //                           Globals
95 ////////////////////////////////////////////////////////////////////////////
96 
pmt_null()97 pmt_null::pmt_null() {}
98 
get_PMT_NIL()99 pmt_t get_PMT_NIL()
100 {
101     static pmt_t _NIL = pmt_t(new pmt_null());
102     return _NIL;
103 }
104 
get_PMT_T()105 pmt_t get_PMT_T()
106 {
107     static const pmt_t _T = pmt_t(new pmt_bool());
108     return _T;
109 }
110 
get_PMT_F()111 pmt_t get_PMT_F()
112 {
113     static const pmt_t _F = pmt_t(new pmt_bool());
114     return _F;
115 }
116 
get_PMT_EOF()117 pmt_t get_PMT_EOF()
118 {
119     static const pmt_t _EOF = cons(get_PMT_NIL(), get_PMT_NIL());
120     return _EOF;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////
124 //                           Booleans
125 ////////////////////////////////////////////////////////////////////////////
126 
pmt_bool()127 pmt_bool::pmt_bool() {}
128 
is_true(pmt_t obj)129 bool is_true(pmt_t obj) { return obj != PMT_F; }
130 
is_false(pmt_t obj)131 bool is_false(pmt_t obj) { return obj == PMT_F; }
132 
is_bool(pmt_t obj)133 bool is_bool(pmt_t obj) { return obj->is_bool(); }
134 
from_bool(bool val)135 pmt_t from_bool(bool val) { return val ? PMT_T : PMT_F; }
136 
to_bool(pmt_t val)137 bool to_bool(pmt_t val)
138 {
139     if (val == PMT_T)
140         return true;
141     if (val == PMT_F)
142         return false;
143     throw wrong_type("pmt_to_bool", val);
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////
147 //                             Symbols
148 ////////////////////////////////////////////////////////////////////////////
149 
get_symbol_hash_table_size()150 static const unsigned int get_symbol_hash_table_size()
151 {
152     static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
153     return SYMBOL_HASH_TABLE_SIZE;
154 }
155 
get_symbol_hash_table()156 static std::vector<pmt_t>* get_symbol_hash_table()
157 {
158     static std::vector<pmt_t> s_symbol_hash_table(get_symbol_hash_table_size());
159     return &s_symbol_hash_table;
160 }
161 
pmt_symbol(const std::string & name)162 pmt_symbol::pmt_symbol(const std::string& name) : d_name(name) {}
163 
164 
hash_string(const std::string & s)165 static unsigned int hash_string(const std::string& s)
166 {
167     unsigned int h = 0;
168     unsigned int g = 0;
169 
170     for (std::string::const_iterator p = s.begin(); p != s.end(); ++p) {
171         h = (h << 4) + (*p & 0xff);
172         g = h & 0xf0000000;
173         if (g) {
174             h = h ^ (g >> 24);
175             h = h ^ g;
176         }
177     }
178     return h;
179 }
180 
is_symbol(const pmt_t & obj)181 bool is_symbol(const pmt_t& obj) { return obj->is_symbol(); }
182 
string_to_symbol(const std::string & name)183 pmt_t string_to_symbol(const std::string& name)
184 {
185     unsigned hash = hash_string(name) % get_symbol_hash_table_size();
186 
187     // Does a symbol with this name already exist?
188     for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()) {
189         if (name == _symbol(sym)->name())
190             return sym; // Yes.  Return it
191     }
192 
193     // Lock the table on insert for thread safety:
194     static boost::mutex thread_safety;
195     boost::mutex::scoped_lock lock(thread_safety);
196     // Re-do the search in case another thread inserted this symbol into the table
197     // before we got the lock
198     for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()) {
199         if (name == _symbol(sym)->name())
200             return sym; // Yes.  Return it
201     }
202 
203     // Nope.  Make a new one.
204     pmt_t sym = pmt_t(new pmt_symbol(name));
205     _symbol(sym)->set_next((*get_symbol_hash_table())[hash]);
206     (*get_symbol_hash_table())[hash] = sym;
207     return sym;
208 }
209 
210 // alias...
intern(const std::string & name)211 pmt_t intern(const std::string& name) { return string_to_symbol(name); }
212 
symbol_to_string(const pmt_t & sym)213 const std::string symbol_to_string(const pmt_t& sym)
214 {
215     if (!sym->is_symbol())
216         throw wrong_type("pmt_symbol_to_string", sym);
217 
218     return _symbol(sym)->name();
219 }
220 
221 
222 ////////////////////////////////////////////////////////////////////////////
223 //                             Number
224 ////////////////////////////////////////////////////////////////////////////
225 
is_number(pmt_t x)226 bool is_number(pmt_t x) { return x->is_number(); }
227 
228 ////////////////////////////////////////////////////////////////////////////
229 //                             Integer
230 ////////////////////////////////////////////////////////////////////////////
231 
pmt_integer(long value)232 pmt_integer::pmt_integer(long value) : d_value(value) {}
233 
is_integer(pmt_t x)234 bool is_integer(pmt_t x) { return x->is_integer(); }
235 
236 
from_long(long x)237 pmt_t from_long(long x) { return pmt_t(new pmt_integer(x)); }
238 
to_long(pmt_t x)239 long to_long(pmt_t x)
240 {
241     pmt_integer* i = dynamic_cast<pmt_integer*>(x.get());
242     if (i)
243         return i->value();
244 
245     throw wrong_type("pmt_to_long", x);
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////
249 //                             Uint64
250 ////////////////////////////////////////////////////////////////////////////
251 
pmt_uint64(uint64_t value)252 pmt_uint64::pmt_uint64(uint64_t value) : d_value(value) {}
253 
is_uint64(pmt_t x)254 bool is_uint64(pmt_t x) { return x->is_uint64(); }
255 
256 
from_uint64(uint64_t x)257 pmt_t from_uint64(uint64_t x) { return pmt_t(new pmt_uint64(x)); }
258 
to_uint64(pmt_t x)259 uint64_t to_uint64(pmt_t x)
260 {
261     if (x->is_uint64())
262         return _uint64(x)->value();
263     if (x->is_integer()) {
264         long tmp = _integer(x)->value();
265         if (tmp >= 0)
266             return (uint64_t)tmp;
267     }
268 
269     throw wrong_type("pmt_to_uint64", x);
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////
273 //                              Real
274 ////////////////////////////////////////////////////////////////////////////
275 
pmt_real(double value)276 pmt_real::pmt_real(double value) : d_value(value) {}
277 
is_real(pmt_t x)278 bool is_real(pmt_t x) { return x->is_real(); }
279 
from_double(double x)280 pmt_t from_double(double x) { return pmt_t(new pmt_real(x)); }
281 
from_float(float x)282 pmt_t from_float(float x) { return pmt_t(new pmt_real(x)); }
283 
to_double(pmt_t x)284 double to_double(pmt_t x)
285 {
286     if (x->is_real())
287         return _real(x)->value();
288     if (x->is_integer())
289         return _integer(x)->value();
290 
291     throw wrong_type("pmt_to_double", x);
292 }
293 
to_float(pmt_t x)294 float to_float(pmt_t x) { return float(to_double(x)); }
295 
296 ////////////////////////////////////////////////////////////////////////////
297 //                              Complex
298 ////////////////////////////////////////////////////////////////////////////
299 
pmt_complex(std::complex<double> value)300 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
301 
is_complex(pmt_t x)302 bool is_complex(pmt_t x) { return x->is_complex(); }
303 
make_rectangular(double re,double im)304 pmt_t make_rectangular(double re, double im) { return from_complex(re, im); }
305 
from_complex(double re,double im)306 pmt_t from_complex(double re, double im) { return pmt_from_complex(re, im); }
307 
pmt_from_complex(double re,double im)308 pmt_t pmt_from_complex(double re, double im)
309 {
310     return pmt_t(new pmt_complex(std::complex<double>(re, im)));
311 }
312 
from_complex(const std::complex<double> & z)313 pmt_t from_complex(const std::complex<double>& z) { return pmt_t(new pmt_complex(z)); }
314 
to_complex(pmt_t x)315 std::complex<double> to_complex(pmt_t x)
316 {
317     if (x->is_complex())
318         return _complex(x)->value();
319     if (x->is_real())
320         return _real(x)->value();
321     if (x->is_integer())
322         return _integer(x)->value();
323 
324     throw wrong_type("pmt_to_complex", x);
325 }
326 
327 ////////////////////////////////////////////////////////////////////////////
328 //                              Pairs
329 ////////////////////////////////////////////////////////////////////////////
330 
pmt_pair(const pmt_t & car,const pmt_t & cdr)331 pmt_pair::pmt_pair(const pmt_t& car, const pmt_t& cdr) : d_car(car), d_cdr(cdr) {}
332 
is_null(const pmt_t & x)333 bool is_null(const pmt_t& x) { return x == PMT_NIL; }
334 
is_pair(const pmt_t & obj)335 bool is_pair(const pmt_t& obj) { return obj->is_pair(); }
336 
cons(const pmt_t & x,const pmt_t & y)337 pmt_t cons(const pmt_t& x, const pmt_t& y) { return pmt_t(new pmt_pair(x, y)); }
338 
car(const pmt_t & pair)339 pmt_t car(const pmt_t& pair)
340 {
341     pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
342     if (p)
343         return p->car();
344 
345     throw wrong_type("pmt_car", pair);
346 }
347 
cdr(const pmt_t & pair)348 pmt_t cdr(const pmt_t& pair)
349 {
350     pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
351     if (p)
352         return p->cdr();
353 
354     throw wrong_type("pmt_cdr", pair);
355 }
356 
set_car(pmt_t pair,pmt_t obj)357 void set_car(pmt_t pair, pmt_t obj)
358 {
359     if (pair->is_pair())
360         _pair(pair)->set_car(obj);
361     else
362         throw wrong_type("pmt_set_car", pair);
363 }
364 
set_cdr(pmt_t pair,pmt_t obj)365 void set_cdr(pmt_t pair, pmt_t obj)
366 {
367     if (pair->is_pair())
368         _pair(pair)->set_cdr(obj);
369     else
370         throw wrong_type("pmt_set_cdr", pair);
371 }
372 
373 ////////////////////////////////////////////////////////////////////////////
374 //                             Vectors
375 ////////////////////////////////////////////////////////////////////////////
376 
pmt_vector(size_t len,pmt_t fill)377 pmt_vector::pmt_vector(size_t len, pmt_t fill) : d_v(len)
378 {
379     for (size_t i = 0; i < len; i++)
380         d_v[i] = fill;
381 }
382 
ref(size_t k) const383 pmt_t pmt_vector::ref(size_t k) const
384 {
385     if (k >= length())
386         throw out_of_range("pmt_vector_ref", from_long(k));
387     return d_v[k];
388 }
389 
set(size_t k,pmt_t obj)390 void pmt_vector::set(size_t k, pmt_t obj)
391 {
392     if (k >= length())
393         throw out_of_range("pmt_vector_set", from_long(k));
394     d_v[k] = obj;
395 }
396 
fill(pmt_t obj)397 void pmt_vector::fill(pmt_t obj)
398 {
399     for (size_t i = 0; i < length(); i++)
400         d_v[i] = obj;
401 }
402 
is_vector(pmt_t obj)403 bool is_vector(pmt_t obj) { return obj->is_vector(); }
404 
make_vector(size_t k,pmt_t fill)405 pmt_t make_vector(size_t k, pmt_t fill) { return pmt_t(new pmt_vector(k, fill)); }
406 
vector_ref(pmt_t vector,size_t k)407 pmt_t vector_ref(pmt_t vector, size_t k)
408 {
409     if (!vector->is_vector())
410         throw wrong_type("pmt_vector_ref", vector);
411     return _vector(vector)->ref(k);
412 }
413 
vector_set(pmt_t vector,size_t k,pmt_t obj)414 void vector_set(pmt_t vector, size_t k, pmt_t obj)
415 {
416     if (!vector->is_vector())
417         throw wrong_type("pmt_vector_set", vector);
418     _vector(vector)->set(k, obj);
419 }
420 
vector_fill(pmt_t vector,pmt_t obj)421 void vector_fill(pmt_t vector, pmt_t obj)
422 {
423     if (!vector->is_vector())
424         throw wrong_type("pmt_vector_set", vector);
425     _vector(vector)->fill(obj);
426 }
427 
428 ////////////////////////////////////////////////////////////////////////////
429 //                             Tuples
430 ////////////////////////////////////////////////////////////////////////////
431 
pmt_tuple(size_t len)432 pmt_tuple::pmt_tuple(size_t len) : d_v(len) {}
433 
ref(size_t k) const434 pmt_t pmt_tuple::ref(size_t k) const
435 {
436     if (k >= length())
437         throw out_of_range("pmt_tuple_ref", from_long(k));
438     return d_v[k];
439 }
440 
is_tuple(pmt_t obj)441 bool is_tuple(pmt_t obj) { return obj->is_tuple(); }
442 
tuple_ref(const pmt_t & tuple,size_t k)443 pmt_t tuple_ref(const pmt_t& tuple, size_t k)
444 {
445     if (!tuple->is_tuple())
446         throw wrong_type("pmt_tuple_ref", tuple);
447     return _tuple(tuple)->ref(k);
448 }
449 
450 // for (i=0; i < 10; i++)
451 //   make_constructor()
452 
make_tuple()453 pmt_t make_tuple() { return pmt_t(new pmt_tuple(0)); }
454 
make_tuple(const pmt_t & e0)455 pmt_t make_tuple(const pmt_t& e0)
456 {
457     pmt_tuple* t = new pmt_tuple(1);
458     t->_set(0, e0);
459     return pmt_t(t);
460 }
461 
make_tuple(const pmt_t & e0,const pmt_t & e1)462 pmt_t make_tuple(const pmt_t& e0, const pmt_t& e1)
463 {
464     pmt_tuple* t = new pmt_tuple(2);
465     t->_set(0, e0);
466     t->_set(1, e1);
467     return pmt_t(t);
468 }
469 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2)470 pmt_t make_tuple(const pmt_t& e0, const pmt_t& e1, const pmt_t& e2)
471 {
472     pmt_tuple* t = new pmt_tuple(3);
473     t->_set(0, e0);
474     t->_set(1, e1);
475     t->_set(2, e2);
476     return pmt_t(t);
477 }
478 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3)479 pmt_t make_tuple(const pmt_t& e0, const pmt_t& e1, const pmt_t& e2, const pmt_t& e3)
480 {
481     pmt_tuple* t = new pmt_tuple(4);
482     t->_set(0, e0);
483     t->_set(1, e1);
484     t->_set(2, e2);
485     t->_set(3, e3);
486     return pmt_t(t);
487 }
488 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4)489 pmt_t make_tuple(
490     const pmt_t& e0, const pmt_t& e1, const pmt_t& e2, const pmt_t& e3, const pmt_t& e4)
491 {
492     pmt_tuple* t = new pmt_tuple(5);
493     t->_set(0, e0);
494     t->_set(1, e1);
495     t->_set(2, e2);
496     t->_set(3, e3);
497     t->_set(4, e4);
498     return pmt_t(t);
499 }
500 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4,const pmt_t & e5)501 pmt_t make_tuple(const pmt_t& e0,
502                  const pmt_t& e1,
503                  const pmt_t& e2,
504                  const pmt_t& e3,
505                  const pmt_t& e4,
506                  const pmt_t& e5)
507 {
508     pmt_tuple* t = new pmt_tuple(6);
509     t->_set(0, e0);
510     t->_set(1, e1);
511     t->_set(2, e2);
512     t->_set(3, e3);
513     t->_set(4, e4);
514     t->_set(5, e5);
515     return pmt_t(t);
516 }
517 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4,const pmt_t & e5,const pmt_t & e6)518 pmt_t make_tuple(const pmt_t& e0,
519                  const pmt_t& e1,
520                  const pmt_t& e2,
521                  const pmt_t& e3,
522                  const pmt_t& e4,
523                  const pmt_t& e5,
524                  const pmt_t& e6)
525 {
526     pmt_tuple* t = new pmt_tuple(7);
527     t->_set(0, e0);
528     t->_set(1, e1);
529     t->_set(2, e2);
530     t->_set(3, e3);
531     t->_set(4, e4);
532     t->_set(5, e5);
533     t->_set(6, e6);
534     return pmt_t(t);
535 }
536 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4,const pmt_t & e5,const pmt_t & e6,const pmt_t & e7)537 pmt_t make_tuple(const pmt_t& e0,
538                  const pmt_t& e1,
539                  const pmt_t& e2,
540                  const pmt_t& e3,
541                  const pmt_t& e4,
542                  const pmt_t& e5,
543                  const pmt_t& e6,
544                  const pmt_t& e7)
545 {
546     pmt_tuple* t = new pmt_tuple(8);
547     t->_set(0, e0);
548     t->_set(1, e1);
549     t->_set(2, e2);
550     t->_set(3, e3);
551     t->_set(4, e4);
552     t->_set(5, e5);
553     t->_set(6, e6);
554     t->_set(7, e7);
555     return pmt_t(t);
556 }
557 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4,const pmt_t & e5,const pmt_t & e6,const pmt_t & e7,const pmt_t & e8)558 pmt_t make_tuple(const pmt_t& e0,
559                  const pmt_t& e1,
560                  const pmt_t& e2,
561                  const pmt_t& e3,
562                  const pmt_t& e4,
563                  const pmt_t& e5,
564                  const pmt_t& e6,
565                  const pmt_t& e7,
566                  const pmt_t& e8)
567 {
568     pmt_tuple* t = new pmt_tuple(9);
569     t->_set(0, e0);
570     t->_set(1, e1);
571     t->_set(2, e2);
572     t->_set(3, e3);
573     t->_set(4, e4);
574     t->_set(5, e5);
575     t->_set(6, e6);
576     t->_set(7, e7);
577     t->_set(8, e8);
578     return pmt_t(t);
579 }
580 
make_tuple(const pmt_t & e0,const pmt_t & e1,const pmt_t & e2,const pmt_t & e3,const pmt_t & e4,const pmt_t & e5,const pmt_t & e6,const pmt_t & e7,const pmt_t & e8,const pmt_t & e9)581 pmt_t make_tuple(const pmt_t& e0,
582                  const pmt_t& e1,
583                  const pmt_t& e2,
584                  const pmt_t& e3,
585                  const pmt_t& e4,
586                  const pmt_t& e5,
587                  const pmt_t& e6,
588                  const pmt_t& e7,
589                  const pmt_t& e8,
590                  const pmt_t& e9)
591 {
592     pmt_tuple* t = new pmt_tuple(10);
593     t->_set(0, e0);
594     t->_set(1, e1);
595     t->_set(2, e2);
596     t->_set(3, e3);
597     t->_set(4, e4);
598     t->_set(5, e5);
599     t->_set(6, e6);
600     t->_set(7, e7);
601     t->_set(8, e8);
602     t->_set(9, e9);
603     return pmt_t(t);
604 }
605 
to_tuple(const pmt_t & x)606 pmt_t to_tuple(const pmt_t& x)
607 {
608     if (x->is_tuple()) // already one
609         return x;
610 
611     size_t len = length(x);
612     pmt_tuple* t = new pmt_tuple(len);
613     pmt_t r = pmt_t(t);
614 
615     if (x->is_vector()) {
616         for (size_t i = 0; i < len; i++)
617             t->_set(i, _vector(x)->ref(i));
618         return r;
619     }
620 
621     if (x->is_pair()) {
622         pmt_t y = x;
623         for (size_t i = 0; i < len; i++) {
624             t->_set(i, car(y));
625             y = cdr(y);
626         }
627         return r;
628     }
629 
630     throw wrong_type("pmt_to_tuple", x);
631 }
632 
633 
634 ////////////////////////////////////////////////////////////////////////////
635 //                       Uniform Numeric Vectors
636 ////////////////////////////////////////////////////////////////////////////
637 
is_uniform_vector(pmt_t x)638 bool is_uniform_vector(pmt_t x) { return x->is_uniform_vector(); }
639 
uniform_vector_itemsize(pmt_t vector)640 size_t uniform_vector_itemsize(pmt_t vector)
641 {
642     if (!vector->is_uniform_vector())
643         throw wrong_type("pmt_uniform_vector_itemsize", vector);
644     return _uniform_vector(vector)->itemsize();
645 }
646 
uniform_vector_elements(pmt_t vector,size_t & len)647 const void* uniform_vector_elements(pmt_t vector, size_t& len)
648 {
649     if (!vector->is_uniform_vector())
650         throw wrong_type("pmt_uniform_vector_elements", vector);
651     return _uniform_vector(vector)->uniform_elements(len);
652 }
653 
uniform_vector_writable_elements(pmt_t vector,size_t & len)654 void* uniform_vector_writable_elements(pmt_t vector, size_t& len)
655 {
656     if (!vector->is_uniform_vector())
657         throw wrong_type("pmt_uniform_vector_writable_elements", vector);
658     return _uniform_vector(vector)->uniform_writable_elements(len);
659 }
660 
661 
662 ////////////////////////////////////////////////////////////////////////////
663 //                            Dictionaries
664 ////////////////////////////////////////////////////////////////////////////
665 
666 /*
667  * This is an a-list implementation.
668  *
669  * When we need better performance for large dictionaries, consider implementing
670  * persistent Red-Black trees as described in "Purely Functional Data Structures",
671  * Chris Okasaki, 1998, section 3.3.
672  */
673 
pmt_dict(const pmt_t & car,const pmt_t & cdr)674 pmt_dict::pmt_dict(const pmt_t& car, const pmt_t& cdr) : pmt_pair::pmt_pair(car, cdr) {}
675 
is_dict(const pmt_t & obj)676 bool is_dict(const pmt_t& obj) { return is_null(obj) || obj->is_dict(); }
677 
make_dict()678 pmt_t make_dict() { return PMT_NIL; }
679 
dcons(const pmt_t & x,const pmt_t & y)680 pmt_t dcons(const pmt_t& x, const pmt_t& y)
681 {
682     // require arguments to be a PMT pair and PMT dictionary respectively
683     if (!is_pair(x))
684         throw wrong_type("pmt_dcons: not a pair", x);
685     if (!is_dict(y))
686         throw wrong_type("pmt_dcons: not a dict", y);
687 
688     return pmt_t(new pmt_dict(x, y));
689 }
690 
dict_add(const pmt_t & dict,const pmt_t & key,const pmt_t & value)691 pmt_t dict_add(const pmt_t& dict, const pmt_t& key, const pmt_t& value)
692 {
693     if (is_null(dict))
694         return acons(key, value, PMT_NIL);
695 
696     if (dict_has_key(dict, key))
697         return acons(key, value, dict_delete(dict, key));
698 
699     return acons(key, value, dict);
700 }
701 
dict_update(const pmt_t & dict1,const pmt_t & dict2)702 pmt_t dict_update(const pmt_t& dict1, const pmt_t& dict2)
703 {
704     pmt_t d(dict1);
705     pmt_t k(dict_keys(dict2));
706     while (is_pair(k)) {
707         d = dict_add(d, car(k), dict_ref(dict2, car(k), PMT_NIL));
708         k = cdr(k);
709     }
710     return d;
711 }
712 
713 
dict_delete(const pmt_t & dict,const pmt_t & key)714 pmt_t dict_delete(const pmt_t& dict, const pmt_t& key)
715 {
716     if (is_null(dict))
717         return dict;
718 
719     if (eqv(caar(dict), key))
720         return cdr(dict);
721 
722     return dcons(car(dict), dict_delete(cdr(dict), key));
723 }
724 
dict_ref(const pmt_t & dict,const pmt_t & key,const pmt_t & not_found)725 pmt_t dict_ref(const pmt_t& dict, const pmt_t& key, const pmt_t& not_found)
726 {
727     pmt_t p = assv(key, dict); // look for (key . value) pair
728     if (is_pair(p))
729         return cdr(p);
730     else
731         return not_found;
732 }
733 
dict_has_key(const pmt_t & dict,const pmt_t & key)734 bool dict_has_key(const pmt_t& dict, const pmt_t& key)
735 {
736     return is_pair(assv(key, dict));
737 }
738 
dict_items(pmt_t dict)739 pmt_t dict_items(pmt_t dict)
740 {
741     if (!is_dict(dict))
742         throw wrong_type("pmt_dict_values", dict);
743 
744     return dict; // equivalent to dict in the a-list case
745 }
746 
dict_keys(pmt_t dict)747 pmt_t dict_keys(pmt_t dict)
748 {
749     if (!is_dict(dict))
750         throw wrong_type("pmt_dict_keys", dict);
751 
752     return map(car, dict);
753 }
754 
dict_values(pmt_t dict)755 pmt_t dict_values(pmt_t dict)
756 {
757     if (!is_dict(dict))
758         throw wrong_type("pmt_dict_keys", dict);
759 
760     return map(cdr, dict);
761 }
762 
763 ////////////////////////////////////////////////////////////////////////////
764 //                                 Any
765 ////////////////////////////////////////////////////////////////////////////
766 
pmt_any(const boost::any & any)767 pmt_any::pmt_any(const boost::any& any) : d_any(any) {}
768 
is_any(pmt_t obj)769 bool is_any(pmt_t obj) { return obj->is_any(); }
770 
make_any(const boost::any & any)771 pmt_t make_any(const boost::any& any) { return pmt_t(new pmt_any(any)); }
772 
any_ref(pmt_t obj)773 boost::any any_ref(pmt_t obj)
774 {
775     if (!obj->is_any())
776         throw wrong_type("pmt_any_ref", obj);
777     return _any(obj)->ref();
778 }
779 
any_set(pmt_t obj,const boost::any & any)780 void any_set(pmt_t obj, const boost::any& any)
781 {
782     if (!obj->is_any())
783         throw wrong_type("pmt_any_set", obj);
784     _any(obj)->set(any);
785 }
786 
787 ////////////////////////////////////////////////////////////////////////////
788 //               msg_accepter -- built from "any"
789 ////////////////////////////////////////////////////////////////////////////
790 
is_msg_accepter(const pmt_t & obj)791 bool is_msg_accepter(const pmt_t& obj)
792 {
793     if (!is_any(obj))
794         return false;
795 
796     boost::any r = any_ref(obj);
797     return boost::any_cast<gr::messages::msg_accepter_sptr>(&r) != 0;
798 }
799 
800 //! make a msg_accepter
make_msg_accepter(gr::messages::msg_accepter_sptr ma)801 pmt_t make_msg_accepter(gr::messages::msg_accepter_sptr ma) { return make_any(ma); }
802 
803 //! Return underlying msg_accepter
msg_accepter_ref(const pmt_t & obj)804 gr::messages::msg_accepter_sptr msg_accepter_ref(const pmt_t& obj)
805 {
806     try {
807         return boost::any_cast<gr::messages::msg_accepter_sptr>(any_ref(obj));
808     } catch (boost::bad_any_cast& e) {
809         throw wrong_type("pmt_msg_accepter_ref", obj);
810     }
811 }
812 
813 
814 ////////////////////////////////////////////////////////////////////////////
815 //             Binary Large Object -- currently a u8vector
816 ////////////////////////////////////////////////////////////////////////////
817 
is_blob(pmt_t x)818 bool is_blob(pmt_t x) { return is_u8vector(x); }
819 
make_blob(const void * buf,size_t len_in_bytes)820 pmt_t make_blob(const void* buf, size_t len_in_bytes)
821 {
822     return init_u8vector(len_in_bytes, (const uint8_t*)buf);
823 }
824 
blob_data(pmt_t blob)825 const void* blob_data(pmt_t blob)
826 {
827     size_t len;
828     return uniform_vector_elements(blob, len);
829 }
830 
blob_length(pmt_t blob)831 size_t blob_length(pmt_t blob)
832 {
833     size_t len;
834     uniform_vector_elements(blob, len);
835     return len;
836 }
837 
838 
839 ////////////////////////////////////////////////////////////////////////////
840 //                          General Functions
841 ////////////////////////////////////////////////////////////////////////////
842 
is_pdu(const pmt_t & obj)843 bool is_pdu(const pmt_t& obj)
844 {
845     return is_pair(obj) && is_dict(car(obj)) && is_uniform_vector(cdr(obj));
846 }
847 
eq(const pmt_t & x,const pmt_t & y)848 bool eq(const pmt_t& x, const pmt_t& y) { return x == y; }
849 
eqv(const pmt_t & x,const pmt_t & y)850 bool eqv(const pmt_t& x, const pmt_t& y)
851 {
852     if (x == y)
853         return true;
854 
855     if (x->is_integer() && y->is_integer())
856         return _integer(x)->value() == _integer(y)->value();
857 
858     if (x->is_uint64() && y->is_uint64())
859         return _uint64(x)->value() == _uint64(y)->value();
860 
861     if (x->is_real() && y->is_real())
862         return _real(x)->value() == _real(y)->value();
863 
864     if (x->is_complex() && y->is_complex())
865         return _complex(x)->value() == _complex(y)->value();
866 
867     return false;
868 }
869 
equal(const pmt_t & x,const pmt_t & y)870 bool equal(const pmt_t& x, const pmt_t& y)
871 {
872     if (eqv(x, y))
873         return true;
874 
875     if (x->is_pair() && y->is_pair())
876         return equal(car(x), car(y)) && equal(cdr(x), cdr(y));
877 
878     if (x->is_vector() && y->is_vector()) {
879         pmt_vector* xv = _vector(x);
880         pmt_vector* yv = _vector(y);
881         if (xv->length() != yv->length())
882             return false;
883 
884         for (unsigned i = 0; i < xv->length(); i++)
885             if (!equal(xv->_ref(i), yv->_ref(i)))
886                 return false;
887 
888         return true;
889     }
890 
891     if (x->is_tuple() && y->is_tuple()) {
892         pmt_tuple* xv = _tuple(x);
893         pmt_tuple* yv = _tuple(y);
894         if (xv->length() != yv->length())
895             return false;
896 
897         for (unsigned i = 0; i < xv->length(); i++)
898             if (!equal(xv->_ref(i), yv->_ref(i)))
899                 return false;
900 
901         return true;
902     }
903 
904     if (x->is_uniform_vector() && y->is_uniform_vector()) {
905         pmt_uniform_vector* xv = _uniform_vector(x);
906         pmt_uniform_vector* yv = _uniform_vector(y);
907         if (xv->length() != yv->length())
908             return false;
909 
910         size_t len_x, len_y;
911         const void* x_m = xv->uniform_elements(len_x);
912         const void* y_m = yv->uniform_elements(len_y);
913         if (memcmp(x_m, y_m, len_x) == 0)
914             return true;
915 
916         return false;
917     }
918 
919     // FIXME add other cases here...
920 
921     return false;
922 }
923 
length(const pmt_t & x)924 size_t length(const pmt_t& x)
925 {
926     if (x->is_vector())
927         return _vector(x)->length();
928 
929     if (x->is_uniform_vector())
930         return _uniform_vector(x)->length();
931 
932     if (x->is_tuple())
933         return _tuple(x)->length();
934 
935     if (x->is_null())
936         return 0;
937 
938     // also returns correct result for dictionaries
939     if (x->is_pair()) {
940         size_t length = 1;
941         pmt_t it = cdr(x);
942         while (is_pair(it)) {
943             length++;
944             it = cdr(it);
945         }
946         if (is_null(it))
947             return length;
948 
949         // not a proper list
950         throw wrong_type("pmt_length", x);
951     }
952 
953     throw wrong_type("pmt_length", x);
954 }
955 
assq(pmt_t obj,pmt_t alist)956 pmt_t assq(pmt_t obj, pmt_t alist)
957 {
958     while (is_pair(alist)) {
959         pmt_t p = car(alist);
960         if (!is_pair(p)) // malformed alist
961             return PMT_F;
962 
963         if (eq(obj, car(p)))
964             return p;
965 
966         alist = cdr(alist);
967     }
968     return PMT_F;
969 }
970 
assv(pmt_t obj,pmt_t alist)971 pmt_t assv(pmt_t obj, pmt_t alist)
972 {
973     while (is_pair(alist)) {
974         pmt_t p = car(alist);
975         if (!is_pair(p)) // malformed alist
976             return PMT_F;
977 
978         if (eqv(obj, car(p)))
979             return p;
980 
981         alist = cdr(alist);
982     }
983     return PMT_F;
984 }
985 
986 
assoc(pmt_t obj,pmt_t alist)987 pmt_t assoc(pmt_t obj, pmt_t alist)
988 {
989     while (is_pair(alist)) {
990         pmt_t p = car(alist);
991         if (!is_pair(p)) // malformed alist
992             return PMT_F;
993 
994         if (equal(obj, car(p)))
995             return p;
996 
997         alist = cdr(alist);
998     }
999     return PMT_F;
1000 }
1001 
map(pmt_t proc (const pmt_t &),pmt_t list)1002 pmt_t map(pmt_t proc(const pmt_t&), pmt_t list)
1003 {
1004     pmt_t r = PMT_NIL;
1005 
1006     while (is_pair(list)) {
1007         r = cons(proc(car(list)), r);
1008         list = cdr(list);
1009     }
1010 
1011     return reverse_x(r);
1012 }
1013 
reverse(pmt_t listx)1014 pmt_t reverse(pmt_t listx)
1015 {
1016     pmt_t list = listx;
1017     pmt_t r = PMT_NIL;
1018 
1019     if (is_dict(listx)) {
1020         while (is_pair(list)) {
1021             r = dcons(car(list), r);
1022             list = cdr(list);
1023         }
1024     } else {
1025         while (is_pair(list)) {
1026             r = cons(car(list), r);
1027             list = cdr(list);
1028         }
1029     }
1030     if (is_null(list))
1031         return r;
1032     else
1033         throw wrong_type("pmt_reverse", listx);
1034 }
1035 
reverse_x(pmt_t list)1036 pmt_t reverse_x(pmt_t list)
1037 {
1038     // FIXME do it destructively
1039     return reverse(list);
1040 }
1041 
nth(size_t n,pmt_t list)1042 pmt_t nth(size_t n, pmt_t list)
1043 {
1044     pmt_t t = nthcdr(n, list);
1045     if (is_pair(t))
1046         return car(t);
1047     else
1048         return PMT_NIL;
1049 }
1050 
nthcdr(size_t n,pmt_t list)1051 pmt_t nthcdr(size_t n, pmt_t list)
1052 {
1053     if (!(is_pair(list) || is_null(list)))
1054         throw wrong_type("pmt_nthcdr", list);
1055 
1056     while (n > 0) {
1057         if (is_pair(list)) {
1058             list = cdr(list);
1059             n--;
1060             continue;
1061         }
1062         if (is_null(list))
1063             return PMT_NIL;
1064         else
1065             throw wrong_type("pmt_nthcdr: not a LIST", list);
1066     }
1067     return list;
1068 }
1069 
memq(pmt_t obj,pmt_t list)1070 pmt_t memq(pmt_t obj, pmt_t list)
1071 {
1072     while (is_pair(list)) {
1073         if (eq(obj, car(list)))
1074             return list;
1075         list = cdr(list);
1076     }
1077     return PMT_F;
1078 }
1079 
memv(pmt_t obj,pmt_t list)1080 pmt_t memv(pmt_t obj, pmt_t list)
1081 {
1082     while (is_pair(list)) {
1083         if (eqv(obj, car(list)))
1084             return list;
1085         list = cdr(list);
1086     }
1087     return PMT_F;
1088 }
1089 
member(pmt_t obj,pmt_t list)1090 pmt_t member(pmt_t obj, pmt_t list)
1091 {
1092     while (is_pair(list)) {
1093         if (equal(obj, car(list)))
1094             return list;
1095         list = cdr(list);
1096     }
1097     return PMT_F;
1098 }
1099 
subsetp(pmt_t list1,pmt_t list2)1100 bool subsetp(pmt_t list1, pmt_t list2)
1101 {
1102     while (is_pair(list1)) {
1103         pmt_t p = car(list1);
1104         if (is_false(memv(p, list2)))
1105             return false;
1106         list1 = cdr(list1);
1107     }
1108     return true;
1109 }
1110 
list1(const pmt_t & x1)1111 pmt_t list1(const pmt_t& x1) { return cons(x1, PMT_NIL); }
1112 
list2(const pmt_t & x1,const pmt_t & x2)1113 pmt_t list2(const pmt_t& x1, const pmt_t& x2) { return cons(x1, cons(x2, PMT_NIL)); }
1114 
list3(const pmt_t & x1,const pmt_t & x2,const pmt_t & x3)1115 pmt_t list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3)
1116 {
1117     return cons(x1, cons(x2, cons(x3, PMT_NIL)));
1118 }
1119 
list4(const pmt_t & x1,const pmt_t & x2,const pmt_t & x3,const pmt_t & x4)1120 pmt_t list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4)
1121 {
1122     return cons(x1, cons(x2, cons(x3, cons(x4, PMT_NIL))));
1123 }
1124 
list5(const pmt_t & x1,const pmt_t & x2,const pmt_t & x3,const pmt_t & x4,const pmt_t & x5)1125 pmt_t list5(
1126     const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5)
1127 {
1128     return cons(x1, cons(x2, cons(x3, cons(x4, cons(x5, PMT_NIL)))));
1129 }
1130 
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)1131 pmt_t list6(const pmt_t& x1,
1132             const pmt_t& x2,
1133             const pmt_t& x3,
1134             const pmt_t& x4,
1135             const pmt_t& x5,
1136             const pmt_t& x6)
1137 {
1138     return cons(x1, cons(x2, cons(x3, cons(x4, cons(x5, cons(x6, PMT_NIL))))));
1139 }
1140 
list_add(pmt_t list,const pmt_t & item)1141 pmt_t list_add(pmt_t list, const pmt_t& item)
1142 {
1143     return reverse(cons(item, reverse(list)));
1144 }
1145 
list_rm(pmt_t list,const pmt_t & item)1146 pmt_t list_rm(pmt_t list, const pmt_t& item)
1147 {
1148     if (is_pair(list)) {
1149         pmt_t left = car(list);
1150         pmt_t right = cdr(list);
1151         if (!equal(left, item)) {
1152             return cons(left, list_rm(right, item));
1153         } else {
1154             return list_rm(right, item);
1155         }
1156     } else {
1157         return list;
1158     }
1159 }
1160 
list_has(pmt_t list,const pmt_t & item)1161 bool list_has(pmt_t list, const pmt_t& item)
1162 {
1163     if (is_pair(list)) {
1164         pmt_t left = car(list);
1165         pmt_t right = cdr(list);
1166         if (equal(left, item))
1167             return true;
1168         return list_has(right, item);
1169     } else {
1170         if (is_null(list))
1171             return false;
1172         throw std::runtime_error("list contains invalid format!");
1173     }
1174 }
1175 
caar(pmt_t pair)1176 pmt_t caar(pmt_t pair) { return (car(car(pair))); }
1177 
cadr(pmt_t pair)1178 pmt_t cadr(pmt_t pair) { return car(cdr(pair)); }
1179 
cdar(pmt_t pair)1180 pmt_t cdar(pmt_t pair) { return cdr(car(pair)); }
1181 
cddr(pmt_t pair)1182 pmt_t cddr(pmt_t pair) { return cdr(cdr(pair)); }
1183 
caddr(pmt_t pair)1184 pmt_t caddr(pmt_t pair) { return car(cdr(cdr(pair))); }
1185 
cadddr(pmt_t pair)1186 pmt_t cadddr(pmt_t pair) { return car(cdr(cdr(cdr(pair)))); }
1187 
is_eof_object(pmt_t obj)1188 bool is_eof_object(pmt_t obj) { return eq(obj, PMT_EOF); }
1189 
dump_sizeof()1190 void dump_sizeof()
1191 {
1192     printf("sizeof(pmt_t)              = %3zd\n", sizeof(pmt_t));
1193     printf("sizeof(pmt_base)           = %3zd\n", sizeof(pmt_base));
1194     printf("sizeof(pmt_bool)           = %3zd\n", sizeof(pmt_bool));
1195     printf("sizeof(pmt_symbol)         = %3zd\n", sizeof(pmt_symbol));
1196     printf("sizeof(pmt_integer)        = %3zd\n", sizeof(pmt_integer));
1197     printf("sizeof(pmt_uint64)         = %3zd\n", sizeof(pmt_uint64));
1198     printf("sizeof(pmt_real)           = %3zd\n", sizeof(pmt_real));
1199     printf("sizeof(pmt_complex)        = %3zd\n", sizeof(pmt_complex));
1200     printf("sizeof(pmt_null)           = %3zd\n", sizeof(pmt_null));
1201     printf("sizeof(pmt_pair)           = %3zd\n", sizeof(pmt_pair));
1202     printf("sizeof(pmt_vector)         = %3zd\n", sizeof(pmt_vector));
1203     printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));
1204 }
1205 
1206 } /* namespace pmt */
1207