东方耀AI技术分享
标题:
搞明白gnuradio中大量出现的pmt_t类型
[打印本页]
作者:
东方耀
时间:
2021-7-21 15:41
标题:
搞明白gnuradio中大量出现的pmt_t类型
//注意配置cmake一下:target_link_libraries(main -lboost_system)
//搞明白gnuradio中大量出现的pmt_t类型
//gnuradio/gnuradio-runtime/lib/pmt/pmt.cc
#include <iostream>
#include <string>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <vector>
using namespace std;
//pmt::pmt_t symbol干什么的?
//https://blog.csdn.net/u014779536/article/details/116404088
class pmt_base : boost::noncopyable
{
public:
// 完整的 基类 pmt_base
pmt_base(){}
virtual ~pmt_base(){}
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_uint64() 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; }
virtual bool is_tuple() const { return false; }
virtual bool is_vector() const { return false; }
virtual bool is_dict() const { return false; }
virtual bool is_any() const { return false; }
virtual bool is_uniform_vector() const { return false; }
virtual bool is_u8vector() const { return false; }
virtual bool is_s8vector() const { return false; }
virtual bool is_u16vector() const { return false; }
virtual bool is_s16vector() const { return false; }
virtual bool is_u32vector() const { return false; }
virtual bool is_s32vector() const { return false; }
virtual bool is_u64vector() const { return false; }
virtual bool is_s64vector() const { return false; }
virtual bool is_f32vector() const { return false; }
virtual bool is_f64vector() const { return false; }
virtual bool is_c32vector() const { return false; }
virtual bool is_c64vector() const { return false; }
};
typedef boost::shared_ptr<pmt_base> pmt_t; //包装基类的一个共享指针 很多地方出现这个类型pmt_t
class pmt_symbol : public pmt_base
{
std::string d_name;
pmt_t d_next;
public:
pmt_symbol(const std::string& name): d_name(name) {}
//~pmt_symbol(){}
bool is_symbol() const { return true; }
const std::string name() { return d_name; }
pmt_t next() { return d_next; } // symbol table link
void set_next(pmt_t next) { d_next = next; }
};
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 & 0xff);
g = h & 0xf0000000;
if (g) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
// 把一个字符串 转int类型
cout << "字符串=" << s << endl;
cout << "把一个字符串转int类型=" << h << endl;
return h;
}
const unsigned int get_symbol_hash_table_size()
{
const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
// hash表大小=701
return SYMBOL_HASH_TABLE_SIZE;
}
static std::vector<pmt_t>* get_symbol_hash_table()
{
static std::vector<pmt_t> s_symbol_hash_table(get_symbol_hash_table_size());
// 动态数组的名字 与 其取地址有什么区别吗? 数组首元素的地址?
//stl中的数组,不能用数组名来代表数组首元素的地址,只能通过数组名取地址才行
return &s_symbol_hash_table;
}
pmt_symbol* _symbol(pmt_t x){
cout << "这个方法:将基类指针 转 子类指针" << endl;
return dynamic_cast<pmt_symbol*>(x.get());
}
pmt_t string_to_symbol(const std::string& name)
{
//字符串哈希&子串匹配
unsigned hash = hash_string(name) % get_symbol_hash_table_size();
// Does a symbol with this name already exist?
//(*get_symbol_hash_table()) 指向的就是一个 vector<pmt_t>
cout << "数组下标=" << hash << endl;
for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()) {
if (name == _symbol(sym)->name()){
cout << "一下就找到了吧" << hash << endl;
return sym; // Yes. Return it
}
}
// Lock the table on insert for thread safety:
boost::mutex thread_safety; // mutex 互斥锁
boost::mutex::scoped_lock lock(thread_safety);
// Re-do the search in case another thread inserted this symbol into the table
// before we got the lock
for (pmt_t sym = (*get_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)); // pmt_t的用法 包裹一下指针
cout << "发生多态(父类指针指向子类对象)=" <<sym.get()->is_symbol() << " ,name=" << _symbol(sym)->name() << endl;
_symbol(sym)->set_next((*get_symbol_hash_table())[hash]);
(*get_symbol_hash_table())[hash] = sym;
cout << "这里才返回的" << sym << endl;
return sym;
}
const std::string symbol_to_string(const pmt_t& sym)
{
if (!sym->is_symbol()){
//throw wrong_type("pmt_symbol_to_string", sym);
cout << "无法转 报错了" << endl;
}
return _symbol(sym)->name();
}
int main(){
//注意配置cmake一下:target_link_libraries(main -lboost_system)
//搞明白gnuradio中大量出现的pmt_t类型
//gnuradio/gnuradio-runtime/lib/pmt/pmt.cc
string len_key="packet_length";
pmt_t d_key_len;
d_key_len = string_to_symbol(len_key); // 字符串转符合
cout << "要搞懂pmt到底干啥?发生多态=" << d_key_len.get()->is_symbol() << endl;
cout << "符号转字符串=" << symbol_to_string(d_key_len) << endl;
return 0;
}
复制代码
作者:
zouqiqi
时间:
2021-11-23 19:34
让天下人人学会人工智能!人工智能的前景一片大好!
欢迎光临 东方耀AI技术分享 (http://ai111.vip/)
Powered by Discuz! X3.4