gnss-sdr代码解读(1)

5 篇文章 0 订阅

gnss-sdr代码解读(1)

作者微信公众号:小卫星

 

1、main.cc

      看一个庞大的程序,首先是聚焦主要框架,因此main里面这句最重要:

std::unique_ptr<ControlThread> control_thread(new ControlThread());

          unique_ptr(C++11)

         unique_ptr指不共享它的指针。它无法复制到其他 unique_ptr,无法通过值传递到函数,也无法用于需要副本的任何标准模板库 (STL) 算法。只能移动unique_ptr。这意味着,内存资源所有权将转移到另一 unique_ptr,并且原始 unique_ptr 不再拥有此资源。 

        std::unique_ptr实现了独享所有权的语义。一个非空的std::unique_ptr总是拥有它所指向的资源。转移一个std::unique_ptr将会把所有权也从源指针转移给目标指针(源指针被置空)。拷贝一个std::unique_ptr将不被允许。因此std::unique_ptr是一个仅能移动(move_only)的类型。当指针析构时,它所拥有的资源也被销毁。默认情况下,资源的析构是伴随着调用std::unique_ptr内部的原始指针的delete操作的。 

        unique_ptr不像shared_ptr一样拥有标准库函数make_shared来创建一个shared_ptr实例。要想创建一个unique_ptr,我们需要将一个new 操作符返回的指针传递给unique_ptr的构造函数。

示例:(引自https://www.cnblogs.com/DswCnblog/p/5628195.html)

//

int main()

{

    unique_ptr<int> pInt(new int(5));

    cout << *pInt;

}

//

int main()

{

    unique_ptr<int> pInt(new int(5));

    unique_ptr<int> pInt2 = std::move(pInt);    // 转移所有权

    //cout << *pInt << endl; // 出错,pInt为空

    cout << *pInt2 << endl;

    unique_ptr<int> pInt3(std::move(pInt2));

}

//

unique_ptr<int> clone(int p)

{

    unique_ptr<int> pInt(new int(p));

    return pInt;    // 返回unique_ptr

}

int main() {

    int p = 5;

    unique_ptr<int> ret = clone(p);

    cout << *ret << endl;

}

control_thread->run();

2、control_thread.cc

       在该类的构造函数中实现了

configuration_ = std::make_shared<FileConfiguration>(FLAGS_config_file);

     make_shared函数的主要功能是在动态内存中分配一个对象并初始化它,返回指向此对象的shared_ptr;由于是通过shared_ptr管理内存,因此一种安全分配和使用动态内存的方法。

       FLAGS_config_file是在哪里定义的呢?(不好意思,暂时还没找到)

       在该类的构造函数中调用了它的函数init(),里面主要有两句:

control_queue_ = gr::msg_queue::make(0);

flowgraph_ = std::make_shared<GNSSFlowgraph>(configuration_, control_queue_);

       第一句在

https://www.gnuradio.org/doc/doxygen/classgr_1_1msg__queue.html

里有说明,好吧,其实没有说明,就这一句:static sptr gr::msg_queue::make(unsigned int limit = 0)

而第二句直接调用了GNSSFlowgraph

3、gnss_flowgraph.cc

       在该类的构造函数中调用了它的函数init(),里面主要有两句:

std::unique_ptr<GNSSBlockFactory> block_factory_(new GNSSBlockFactory());

std::shared_ptr<std::vector<std::unique_ptr<GNSSBlockInterface>>> channels = block_factory_->GetChannels(configuration_, queue_);

      第句直接调用了GNSSBlockFactory

      句直接调用了GNSSBlockInterface。注意第二句的结构:

              Ø 第一层:std::unique_ptr<GNSSBlockInterface>

              Ø 第二层:std::vector<...>

              Ø 第三层:std::shared_ptr<...>

      因此channels 是一个vector的指针,因此channels->at(i)传回索引i所指的数据的指针。

std::shared_ptr<GNSSBlockInterface> chan_ = std::move(channels->at(i));

      这句就是把这个指针移动给chan_,因为它是unique_ptr,因此只能move

      接着这句有一句:

channels_.push_back(std::dynamic_pointer_cast<ChannelInterface>(chan_));

      channels_gnss_flowgraph.h中定义:std::vector<std::shared_ptr<ChannelInterface>> channels_;

      vectorpush_back是将数据加入到vector尾部。

      C++ 中提供了四种强制类型转换操作符:static_cast, dynamic_cast, const_cast, reinterpret_cast。而shared_ptr 无法利用这些原始的操作符进行转换,其定义了自己的类型转换操作符:static_pointer_cast, dynamic_pointer_cast, const_pointer_cast

     可见在这里chan_被强制从GNSSBlockInterface类型转换为了ChannelInterface 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值