东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1775|回复: 1
打印 上一主题 下一主题

[C/C++] 搞明白gnuradio中大量出现的pmt_t类型

[复制链接]

1366

主题

1857

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14459
QQ
跳转到指定楼层
楼主
发表于 2021-7-21 15:41:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式





    //注意配置cmake一下:target_link_libraries(main -lboost_system)
    //搞明白gnuradio中大量出现的pmt_t类型
    //gnuradio/gnuradio-runtime/lib/pmt/pmt.cc





  1. #include <iostream>
  2. #include <string>
  3. #include <boost/noncopyable.hpp>
  4. #include <boost/shared_ptr.hpp>
  5. #include <boost/thread.hpp>
  6. #include <vector>

  7. using namespace std;

  8. //pmt::pmt_t  symbol干什么的?
  9. //https://blog.csdn.net/u014779536/article/details/116404088



  10. class pmt_base : boost::noncopyable
  11. {

  12. public:
  13.     // 完整的 基类 pmt_base
  14.     pmt_base(){}
  15.     virtual ~pmt_base(){}

  16.     virtual bool is_bool() const { return false; }
  17.     virtual bool is_symbol() const { return false; }
  18.     virtual bool is_number() const { return false; }
  19.     virtual bool is_integer() const { return false; }
  20.     virtual bool is_uint64() const { return false; }
  21.     virtual bool is_real() const { return false; }
  22.     virtual bool is_complex() const { return false; }
  23.     virtual bool is_null() const { return false; }
  24.     virtual bool is_pair() const { return false; }
  25.     virtual bool is_tuple() const { return false; }
  26.     virtual bool is_vector() const { return false; }
  27.     virtual bool is_dict() const { return false; }
  28.     virtual bool is_any() const { return false; }

  29.     virtual bool is_uniform_vector() const { return false; }
  30.     virtual bool is_u8vector() const { return false; }
  31.     virtual bool is_s8vector() const { return false; }
  32.     virtual bool is_u16vector() const { return false; }
  33.     virtual bool is_s16vector() const { return false; }
  34.     virtual bool is_u32vector() const { return false; }
  35.     virtual bool is_s32vector() const { return false; }
  36.     virtual bool is_u64vector() const { return false; }
  37.     virtual bool is_s64vector() const { return false; }
  38.     virtual bool is_f32vector() const { return false; }
  39.     virtual bool is_f64vector() const { return false; }
  40.     virtual bool is_c32vector() const { return false; }
  41.     virtual bool is_c64vector() const { return false; }
  42. };


  43. typedef boost::shared_ptr<pmt_base> pmt_t;   //包装基类的一个共享指针 很多地方出现这个类型pmt_t

  44. class pmt_symbol : public pmt_base
  45. {
  46.     std::string d_name;
  47.     pmt_t d_next;

  48. public:
  49.     pmt_symbol(const std::string& name): d_name(name) {}
  50.     //~pmt_symbol(){}

  51.     bool is_symbol() const { return true; }
  52.     const std::string name() { return d_name; }

  53.     pmt_t next() { return d_next; } // symbol table link
  54.     void set_next(pmt_t next) { d_next = next; }
  55. };

  56. unsigned int hash_string(const std::string& s)
  57. {
  58.     unsigned int h = 0;
  59.     unsigned int g = 0;

  60.     for (std::string::const_iterator p = s.begin(); p != s.end(); ++p) {
  61.         h = (h << 4) + (*p & 0xff);
  62.         g = h & 0xf0000000;
  63.         if (g) {
  64.             h = h ^ (g >> 24);
  65.             h = h ^ g;
  66.         }
  67.     }
  68.     // 把一个字符串 转int类型
  69.     cout << "字符串=" << s << endl;
  70.     cout << "把一个字符串转int类型=" << h << endl;
  71.     return h;
  72. }


  73. const unsigned int get_symbol_hash_table_size()
  74. {
  75.     const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
  76.     // hash表大小=701
  77.     return SYMBOL_HASH_TABLE_SIZE;
  78. }


  79. static std::vector<pmt_t>* get_symbol_hash_table()
  80. {
  81.     static std::vector<pmt_t> s_symbol_hash_table(get_symbol_hash_table_size());
  82.     // 动态数组的名字 与 其取地址有什么区别吗? 数组首元素的地址?
  83.     //stl中的数组,不能用数组名来代表数组首元素的地址,只能通过数组名取地址才行
  84.     return &s_symbol_hash_table;
  85. }


  86. pmt_symbol* _symbol(pmt_t x){
  87.     cout << "这个方法:将基类指针 转 子类指针" << endl;
  88.     return dynamic_cast<pmt_symbol*>(x.get());
  89. }


  90. pmt_t string_to_symbol(const std::string& name)
  91. {
  92.     //字符串哈希&子串匹配
  93.     unsigned hash = hash_string(name) % get_symbol_hash_table_size();

  94.     // Does a symbol with this name already exist?
  95.     //(*get_symbol_hash_table()) 指向的就是一个 vector<pmt_t>
  96.     cout << "数组下标=" << hash << endl;
  97.     for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()) {
  98.         if (name == _symbol(sym)->name()){
  99.             cout << "一下就找到了吧" << hash << endl;
  100.             return sym; // Yes.  Return it

  101.         }
  102.             
  103.     }

  104.     // Lock the table on insert for thread safety:
  105.     boost::mutex thread_safety;     // mutex 互斥锁
  106.     boost::mutex::scoped_lock lock(thread_safety);
  107.     // Re-do the search in case another thread inserted this symbol into the table
  108.     // before we got the lock
  109.     for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()) {
  110.         if (name == _symbol(sym)->name())
  111.             return sym; // Yes.  Return it
  112.     }

  113.     // Nope.  Make a new one.
  114.     pmt_t sym = pmt_t(new pmt_symbol(name));  // pmt_t的用法 包裹一下指针
  115.     cout << "发生多态(父类指针指向子类对象)=" <<sym.get()->is_symbol() << "  ,name=" << _symbol(sym)->name() << endl;
  116.     _symbol(sym)->set_next((*get_symbol_hash_table())[hash]);
  117.     (*get_symbol_hash_table())[hash] = sym;
  118.     cout << "这里才返回的" << sym << endl;
  119.     return sym;
  120. }

  121. const std::string symbol_to_string(const pmt_t& sym)
  122. {
  123.     if (!sym->is_symbol()){
  124.         //throw wrong_type("pmt_symbol_to_string", sym);
  125.         cout << "无法转 报错了" << endl;

  126.     }

  127.     return _symbol(sym)->name();
  128. }

  129. int main(){

  130.     //注意配置cmake一下:target_link_libraries(main -lboost_system)
  131.     //搞明白gnuradio中大量出现的pmt_t类型
  132.     //gnuradio/gnuradio-runtime/lib/pmt/pmt.cc

  133.     string len_key="packet_length";
  134.     pmt_t d_key_len;

  135.     d_key_len = string_to_symbol(len_key);   // 字符串转符合

  136.     cout << "要搞懂pmt到底干啥?发生多态=" << d_key_len.get()->is_symbol() << endl;

  137.     cout << "符号转字符串=" << symbol_to_string(d_key_len) << endl;

  138.     return 0;
  139. }
复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
沙发
发表于 2021-11-23 19:34:56 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-6-28 14:04 , Processed in 0.186178 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表