基於qt creator,camke使用boost庫中的signals2實現訂閱者發佈者模式

                                                                        心 火!


幾個要點先羅列如下:

1)  可以直接使用boost的源文件,不用編譯。

2)在CMakeLists.txt文件中指出boost源文件的所在位置,然後就可以直接在自己的代碼中include boost的源文件。   另外有一種直接使用cmake的find_package功能的方法,見文末。

3)學習boost庫的使用方法,一定要參照正規book或者官網文檔,切莫嘗試從別人blog上系統學習,blog只能起到啓發作用。

 

我的環境:

Qt creator版本:4.6.2, Camke 版本:3.12.1 , Ubuntu: 14.04

Step1:

下載boost庫源文件:https://www.boost.org/users/download/ ,存儲到本地目錄。boost目錄中存儲着hpp源文件。

Step2:

使用qt creator新建一個plain c++ project,選擇編譯方式時,選擇cmake。

在CMakeLists.txt文件中指出boost源文件的所在位置:

if(NOT DEFINED BOOST_PATH)

        set(BOOST_PATH  /home/xx /boost_1_70_0) #boost文件夾在此目錄下面

endif(NOT DEFINED BOOST_PATH)

include_directories( ${BOOST_PATH} )

Step3

在自己的文件中,可以直接include boost源文件了:例如:

#include <boost/signals2/signal.hpp>

#include <boost/random.hpp>

using namespace boost::signals2;

using namespace boost;


舉個例子:

來自《Boost程序庫完全開發指南》 羅劍鋒,chapter 11,signals2部分:

class.h:

#ifndef CLASSES_H_
#define CLASSES_H_

#include <boost/signals2/signal.hpp>
//#include <boost/signals2.hpp>
#include <boost/random.hpp>

using namespace std;
using namespace boost::signals2;
using namespace boost;

class nurse
{
public:
    nurse(string name)
    {
        _name = name;
    }
    nurse(){}

    void action(string str1,string str2)
    {
        cout<< _name << "wake up and to open the door\n";
    }
private:
    string _name;
};


class baby
{
public:
    baby(string name){_name = name;}
    baby(){}

    void action(string str1,string str2)
    {
        cout<< _name << "wake up and crying ....... \n";
    }
private:
    string _name;
};


class ring
{
public:
    ring(){}
    typedef signal<void(string str1,string str2)> signal_t;
    typedef signal_t::slot_type slot_t;

    connection connect(const slot_t& s)
    {
        return _alarm.connect(s);
    }
    void Press(string str1,string str2)
    {
        cout << "Ring alrams ......\n";
        _alarm(str1,str2);
    }
private:
    signal_t _alarm;
};

#endif

main.cpp

#include <iostream>

#include <boost/signals2/signal.hpp>
//#include <boost/signals2.hpp>
#include <boost/bind/bind.hpp>

#include "classes.h"

using namespace std;
using namespace boost::signals2;
using namespace boost;


int main()
{
    cout << "Hello World!" << endl;

    ring r;
    nurse n1("Nurse1 Miss CaoHong");
    nurse n2("Nurse2 Miss YuLin");
    baby b1("Baby1 GuoGuo");
    baby b2("Baby2 NiuNiu");

    r.connect(boost::bind(&nurse::action,n1,_2,_1));
    r.connect(boost::bind(&nurse::action,n2,_2,_1));
    r.connect(boost::bind(&baby::action,b1,_1,_2));
    r.connect(boost::bind(&baby::action,b2,_1,_2));

    r.Press("Frank F","Bluce Lee");

}


CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

project(TryBoostWithCmake)

###########################################

###
set(CMAKE_CXX_STANDARD 11)


if(NOT DEFINED BOOST_PATH)
    set(BOOST_PATH /home/xxxxxxxxx/downLoaedeBoost/boost_1_70_0)
endif(NOT DEFINED BOOST_PATH)

# must add the following part to tell cmake where to find
# included files, otherwise,
#  #include "xxx.hpp" in your own file
# and #include <boost/noncopyable.hpp> in original boost file
# will not find the hpp file.
#include_directories(${BOOST_PATH}
#                      )

##need to study it###
set(BOOST_ROOT /home/xxxxx/downLoaedeBoost/boost_1_70_0)
find_package(Boost
             )
if(NOT Boost_FOUND)
    message("Not found Boost lalalalalaalal------------------------")
endif()

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})

    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
endif()
###

#message("========================")
#message("${CMAKE_MODULE_PATH}")
###


set(
    INC ${BOOST_PATH}/boost/signals2/signal.hpp
    "classes.h"
    #CRB_dev.h
    )
set(SRC
    "main.cpp"
  )
####################################

add_executable(${PROJECT_NAME} ${SRC}
               ${INC})

set( CMAKE_BUILD_TYPE Debug )#fg add

其他:

1.爲什麼boost.signals2是線程安全的,官網說明: https://www.boost.org/doc/libs/1_70_0/doc/html/signals2/thread-safety.html

2.使用cmake的find_package功能找到boost的源文件

參考了: https://www.jianshu.com/p/1827cd86d576https://www.jianshu.com/p/1827cd86d576  , https://blog.csdn.net/haluoluo211/article/details/80559341

##using camke find package###

set(BOOST_ROOT /home/xxx/boost_1_70_0) # tell cmake where to find your boost  files

find_package(Boost   )

if(NOT Boost_FOUND)

    message("Not found Boost lalalalalaalal------------------------")

endif()

if(Boost_FOUND)

    include_directories(${Boost_INCLUDE_DIRS})#cmake將自動找到的頭文件目標進行include

    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")

    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")#可能編譯過的boost纔有效

    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")

endif()

###


Ref:

《Boost程序庫完全開發指南》 羅劍鋒,第四版

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章