日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

muduo C++网络库的学习笔记

發布時間:2024/3/7 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 muduo C++网络库的学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 10.3.C++鏈接linking
    • 10.3.2 inline函數
    • 10.3.3 模板
    • 10.3.4 虛函數
  • 10.4 工程項目中頭文件的使用規則
    • 10.4.2 頭文件的使用規則
  • 11.1 樸實的C++設計
  • 11.2 程序庫的二進制兼容性
  • 11.3 避免使用虛函數作為庫的接口
  • 11.4 動態庫接口的推薦做法
  • 11.5 std::function和std::bind取代虛函數
    • 11.5.1基本用途
    • 11.5.2 對程序庫的影響
    • 11.5.5 對面向對象程序設計的影響
  • 11.6 iostream的用途與局限
    • 11.6.1 stdio格式化輸入輸出的缺點
    • 11.6.2 iostream設計初衷
    • 11.6.3 iostream與標準庫其他組件的交互
    • 11.6.4 iostream在使用方面的缺點
    • 11.6.5 iostream在設計方面的缺點
    • 11.6.6 一個300行的memory buffer output stream
    • 11.6.7 現實中的C++程序如果做文件IO
  • 11.7 值語義與數據抽象
    • 11.7.1 什么是值語義
    • 11.7.2 值語義與生命期
    • 11.7.3 值語義與標準庫
    • 11.7.4 值語義與C++語言
    • 11.7.5 什么是數據抽象
    • 11.7.6 數據抽象所需的語言措施
    • 11.7.7 數據抽象的例子
  • 12 C++經驗談
    • 12.1 用異或來交換變量是錯誤的
    • 12.2 不要重載全局::operator new()
      • 12.2.1 內存管理的基本要求
      • 12.2.2 重載::operator new()的理由
      • 12.2.3 ::operator new()的兩種重載方式
      • 12.2.4 現實的開發環境
      • 12.2.5 重載::operator new()的困境
      • 12.2.6 解決辦法:替換malloc
      • 12.2.7 為單獨的class重載::operator new()有問題嗎?
      • 12.2.8 有必要自行定制內存分配器嗎?
    • 12.3 帶符號整數的除法與余數
    • 12.4 在單元測試中mock系統調用
      • 12.4.1 系統函數的依賴注入
      • 12.4.2 鏈接器墊片link seam
    • 12.5 慎用匿名namespace
      • 12.5.1 C語言的static關鍵字的兩種用法
      • 12.5.2 C++語言的static關鍵字的四種用法
      • 12.5.3 匿名namespace的不利之處
    • 12.6 采用有利于版本管理的代碼格式
      • 12.6.1 對diff友好的代碼格式
      • 12.6.2 對grep友好的代碼風格
      • 12.6.3 一切為了效率
    • 12.7 再探std::string
      • 12.7.1 直接拷貝eager copy
      • 12.7.2 寫時賦值copy on write
      • 12.7.3 短字符串優化
    • 12.8 用STL algorithm輕松解決算法面試題

10.3.C++鏈接linking

10.3.2 inline函數

inline關鍵字在源文件中不是必須的,編譯器可以自動判斷;
inline在頭文件中還是需要的,可以防止鏈接器重復定義(multipe definition);

然后判斷一個C++可執行文件是debug build還是release build?即判斷:一個可執行文件是-O0編譯的還是-O2編譯的?

  • eg:
#include <cstdio> #include <cmath> #include "printer.h" #include <vector>int main() {std::vector<int> vi;printf("%zd\n", vi.size());return 0; }

my_course/course/12/01_math/01/run.sh

#!/bin/bash set -erm -rf build cmake -B build cmake --build build

my_course/course/12/01_math/01/CMakeLists.txt

cmake_minimum_required(VERSION 3.15) project(hellocmake)add_executable(main main.cpp)
  • 測試:

debug

wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ g++ -Wall main.cpp wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ nm ./a.out |grep size|c++filt 000000000000142c W std::vector<int, std::allocator<int> >::size() const

release

wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ g++ -Wall -O2 main.cpp wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ nm ./a.out |grep size|c++filt

10.3.3 模板

  • my_course/course/12/01_math/01/Request.h
class PrintRequest {int m_user_d_;public:int getUserId() const { return m_user_d_; } };class ScanRequest {int m_userId_;public:int getUserId() const { return m_userId_; } };
  • my_course/course/12/01_math/01/Printer.h
#include "Request.h"class PrintRequest; class ScanRequest;class Printer {// 模板函數解析request公共部分// 實現放在源文件中,好處:Printer的用戶看不到decodeRequest函數模板的定義,也可以加快編譯速度template <typename REQ>void decodeRequest(const REQ &req);void processRequest();int m_currentRequestUserId_;public:void onRequest(const PrintRequest &req);void onRequest(const ScanRequest &req); };
  • my_course/course/12/01_math/01/Printer.cc
#include "Printer.h" #include "Request.h"// 現在編譯器能給看到decodeRequest的定義,也就能給具體化 template <typename REQ> void Printer::decodeRequest(const REQ &req) {m_currentRequestUserId_ = req.getUserId();// decode other parts }void Printer::onRequest(const PrintRequest &req) {decodeRequest(req);processRequest(); } void Printer::onRequest(const ScanRequest &req) {decodeRequest(req);processRequest(); }

C++11 extern template特性

  • 阻止隱式模板具體化,使得std::string和std::
#include <cstdio> #include <cmath> // #include "printer.h" #include <vector> #include <string> #include <iostream>using namespace std;int main() {// std::vector<int> vi;// printf("%zd\n", vi.size());string name;cin >> name;cout << "hello, " << name << endl;return 0; } 目標文件,并沒有具體化iostream和string的兩個大模板 wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ nm build/main |grep " [TW]" 0000000000001410 T __libc_csu_fini 00000000000013a0 T __libc_csu_init 0000000000001418 T _fini 0000000000001180 T _start 0000000000004000 W data_start 0000000000001269 T main wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ man nm而是引用了標準庫中的實現 wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ nm build/main |grep -o " U .*"U _Unwind_Resume@@GCC_3.0 是string的構造函數與析構函數U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev@@GLIBCXX_3.4.21U _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@@GLIBCXX_3.4.21U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4U _ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKNSt7__cxx1112basic_stringIS4_S5_T1_EE@@GLIBCXX_3.4.21U _ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE@@GLIBCXX_3.4.21U __cxa_atexit@@GLIBC_2.2.5U __gxx_personality_v0@@CXXABI_1.3U __libc_start_main@@GLIBC_2.2.5U __stack_chk_fail@@GLIBC_2.4

10.3.4 虛函數

每個多態class都有一份vtable

  • 定義或者繼承了虛函數的對象中會有一個隱含成員:指向vtable的指針vptr
  • 在構造和析構對象的適合,編譯器生成的代碼會修改這個vptr成員,會用到vtable的定義(使用其他地址
  • eg:
#include <cstdio> #include <cmath> // #include "printer.h" #include <vector> #include <string> #include <iostream>using namespace std;class Base { public:virtual ~Base();virtual void doIt(); };int main() {Base *b = new Base();b->doIt();return 0; }
  • 測試:出現這種錯誤的根本原因是:程序中某個虛函數沒有定義。雖然報錯顯示:找不到虛函數表的定義
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o [100%] Linking CXX executable main /usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `Base::Base()': main.cpp:(.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]+0xf): undefined reference to `vtable for Base' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/main.dir/build.make:97: main] Error 1 make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

10.4 工程項目中頭文件的使用規則

頭文件通常分為:C語言系統頭文件,C++標準庫頭文件,C++第三方庫頭文件,本公司的基礎庫頭文件,本項目的頭文件

10.4.2 頭文件的使用規則

  • eg:查找頭文件包含的小技巧。eg:一個程序只包含了<iostream>,但是卻能使用std:string,這個<string>是如何被引入的?
// #include <cstdio> // #include <cmath> // #include "printer.h" // #include <vector> // #include <string> #include <iostream>using namespace std;// class Base // { // public: // virtual ~Base(); // virtual void doIt(); // };int main() {// Base *b = new Base();// b->doIt();// std::cout << sizeof(Base) << std::endl;std::string s = "wangji";return 0; } wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ cat >string #wangji wangji wangji@DESKTOP-QNG23J0:~/test/test/my_course/course/12/01_math/01$ g++ -M -I . main.cpp In file included from /usr/include/c++/9/bits/locale_classes.h:40,from /usr/include/c++/9/bits/ios_base.h:41,from /usr/include/c++/9/ios:42,from /usr/include/c++/9/ostream:38,from /usr/include/c++/9/iostream:39,from main.cpp:6:

11.1 樸實的C++設計

11.2 程序庫的二進制兼容性

11.3 避免使用虛函數作為庫的接口

11.4 動態庫接口的推薦做法

1.暴露的接口里面不要有虛函數,要顯式聲明構造函數、析構函數,并且不能inline

  • graphics.h
class Graphics { public:Graphics();~Graphics();void drawLine(int x0, int y0, int x1, int y1);private:class Impl;std::unique_ptr<Impl> impl; };

2.這部分代碼位于so中,隨著庫的升級一起變化
graphics.cc

#include "graphics.h"class Graphics::Impl { public:void drawLine(int x0, int y0, int x1, int y1); };//編譯器可以看到Impl的定義,編譯通過 Graphics::Graphics() : impl(new Impl) {}//析構函數是空的,也必須放到這里定義。 Graphics::~Graphics() {}void Graphics::drawLine(int x0, int y0, int x1, int y1) {impl->drawLine(x0, y0, x1, y1); }

3.增加新的功能,不必通過繼承的方式,就可以原地修改,容易保持二進制兼容性
graphics.h

#include <memory>class Graphics { public:Graphics();~Graphics();void drawLine(int x0, int y0, int x1, int y1);// 新增的非虛函數不影響現有的可執行文件void drawLine(double x0, double y0, double x1, double y1);private:class Impl;std::unique_ptr<Impl> impl; };

graphics.cc

#include "graphics.h"class Graphics::Impl { public:void drawLine(int x0, int y0, int x1, int y1);void drawLine(double x0, double y0, double x1, double y1); };//編譯器可以看到Impl的定義,編譯通過 Graphics::Graphics() : impl(new Impl) {}//析構函數是空的,也必須放到這里定義。 Graphics::~Graphics() {}void Graphics::drawLine(int x0, int y0, int x1, int y1) {impl->drawLine(x0, y0, x1, y1); }void Graphics::drawLine(double x0, double y0, double x1, double y1) {impl->drawLine(x0, y0, x1, y1); }

pimpl C語言的庫同樣可以用,eg:libevent2中的struct event_base

  • 為什么非虛函數比虛函數更健壯?
    因為虛函數是虛表指針+offset來決定虛函數的,而非虛函數是通過名字找到對應的函數的

11.5 std::function和std::bind取代虛函數

11.5.1基本用途

#include <functional> #include <string>class Foo { public:void methodA();void methodInt(int a);void methodString(std::string const &str); };class Bar { public:void methodB(); };void main() {// 無參,無返回值std::function<void()> f1;std::function<void(int)> f2;Foo foo;f1 = std::bind(&Foo::methodA, &foo);f1();Bar bar;f1 = std::bind(&Bar::methodB, std::ref(bar));f1();f1 = std::bind(&Foo ::methodInt, &foo, 42);f1();f1 = std::bind(&Foo::methodString, &foo, "hello");f1(); //調用foo.methodString("hello")// 要留意bind的實參(const char*)的生命期,她不應該短于f1的生命期// 必要時,可通過f1 = std::bind(&Foo::methodString, &foo, "hello”_s);來保證安全f2 = std::bind(&Foo::methodInt, &foo, std::placeholders::_1);f2(53); }

11.5.2 對程序庫的影響

程序庫的設計不應該給使用者帶來不必要的耦合限制,而繼承是第二強的一種耦合,最強的耦合是友元;

常規OO設計:虛函數+派生覆寫;

基于std::function的設計:以std::function作為接口

  • eg:線程庫
#include <functional> #include <string>// 一個基于std::function的Thread class基本數據結構 class Thread { public:using ThreadCallback = std::function<void()>;Thread(ThreadCallback const &cb) : cb_(cb) {}void start(){// somae magic to call run() in new created thread}private:ThreadCallback cb_;void run(){cb_();} };// 使用方式:不需要繼承 class Foo { public:void runInthread();void runInAnotherThread(int); };void main() {Foo foo;Thread thread1(std::bind(&Foo::runInthread, &foo));Thread thread2(std::bind(&Foo::runInAnotherThread, &foo, 100));thread1.start();thread2.start(); }
  • eg:網絡庫
#include <functional> #include <string>// network library class Connection; class NetServer { public:typedef std::function<void(Connection *)> ConnectionCallback;typedef std::function<void(Connection *, const void *, int len)> MessageCallback;NetServer(std::uint16_t port);~NetServer();void registerConnectionCallback(ConnectionCallback const &);void registerMessageCallback(MessageCallback const &);void sendMessage(Connection *, const void *buf, int len);void run(); };// 以std::function作為橋梁 // user code class EchoService { public:// 符合NetServer::sendMessage原型using SendMessageCallback = std::function<void(Connection *, const void *buf, int len)>;EchoService(const SendMessageCallback &sendMsgCb) : sendMessageCb_(sendMsgCb) {}// 符合NetServer::ConnectionCallback原型void onConnection(Connection *conn){printf("");}// 符合NetServer::MessageCallback原型void onMessage(Connection *conn, const void *buf, int size){sendMessageCb_(conn, buf, size); // echo back}private:SendMessageCallback sendMessageCb_; };// 上帝:把各種部件拼接起來 int main() {NetServer server(7);EchoService echo(std::bind(&NetServer::sendMessage, &server, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));server.registerConnectionCallback(std::bind(&EchoService::onConnection, &echo, std::placeholders::_1));server.registerMessageCallback(std::bind(&EchoService::onMessage, &echo, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));server.run(); }

11.5.5 對面向對象程序設計的影響

用std::function代替虛函數,OO設計模式:行為模式、Factory Method創建型模式、Stratery模式、Command模式、Template Method等都可以不用了

  • 以上述EchoService為例,EchoService需要一個函數原型滿足SendMessageCallback的東西來發送消息,而并不關心數據發送到網絡上還是Mock上

面向對象的接口與實現分離:

先寫一個AbstructDataSink interface,包含sendMessage()這個虛函數,然后派生出兩個class:NetDataSink和MockDataSink;
EchoService的構造函數應該以AbstructDataSink*為參數

基于對象的接口與實現分離:

直接傳入一個SendMessageCallback對象

什么時候使用繼承?
OO中的public繼承,即為了實現接口與實現分離,muduo只會在派生類的數目和功能完全確定的情況下使用;

  • eg:IO multiplexing在不同操作系統下不同有不同的實現方法,數目固定,且功能完全確定。用多態來代替switch-case可以達到簡化代碼的目的;

11.6 iostream的用途與局限

11.6.1 stdio格式化輸入輸出的缺點

#include <stdio.h> #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <iostream>int main() {//緩沖區溢出危險:輸入的name沒有指定大小// char name[80];// scanf("%s\n", name);// // 安全做法:// constexpr int max_name = 80;// char myname[max_name];// char fmt[10];// sprintf(fmt, "%%%%ds", max_name - 1);// scanf(fmt, name);// int64_t在32bit和64bit平臺上是不同的類型int64_t x = 100;printf("%" PRIo64 "\n", x); //輸出的是8進制printf("%06" PRIo64 "\n", x);std::cout << std::dec << x << std::endl;// 等價于printf("%""ld""\n",x); // 64bit OSprintf("%""lld""\n",x); // 32bit OS// 等價于// printf("%ld\n", x); // 64bit OS// printf("%lld", x); // 32bit OSstd::size_t i = 1;printf("%zd\n", i);return 0; }

11.6.2 iostream設計初衷

#include <ostream> #include <iostream>class Date { public:Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}void writeTo(std::ostream &out) const{out << year_ << "-" << month_ << "-" << day_;}friend std::ostream &operator<<(std::ostream &out, const Date &date){out << date.year_ << date.month_ << date.day_ << std::endl;}private:int year_;int month_;int day_; };std::ostream &operator<<(std::ostream &out, const Date &date) {date.writeTo(out);return out; }int main() {Date date{2022, 11, 3};std::cout << date << std::endl;return 0; }

11.6.3 iostream與標準庫其他組件的交互

11.6.4 iostream在使用方面的缺點

#include <ostream> #include <iostream> #include <iomanip> //操作子格式化需要該頭文件 class Date { public:Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}// 輸出2022-11-03// iostream輸出格式繁瑣void writeTo(std::ostream &out) const{out << year_ << "-"<< std::setw(2) << std::setfill('0') << month_ << "-"<< std::setw(2) << std::setfill('0') << day_;}void writeTo(std::ostream &out){out << year_ << "-" << month_ << "-" << day_;char buf[32];snprintf(buf, sizeof(buf), "%d-%02d-%02d", year_, month_, day_);out << buf;}private:int year_;int month_;int day_; };std::ostream &operator<<(std::ostream &out, const Date &date) {date.writeTo(out);return out; }int main() {Date date{2022, 11, 3};std::cout << date << std::endl;const char *name = "wangji";int age = 30;// 注:數字表示替換字符串中要替換的位置,若一個字符串要替換兩個int類型,在替換位置分別 寫%1$d和%2$d.printf("My name is %1$s, I am %2$d years old.\n", name, age);std::cout << "My name is " << name << ", I am " << age << " years old." << std::endl;// 將整數轉為十六進制,會持續影響ostream的狀態int x = 8888;std::cout << std::hex << x << std::endl; // 0x22b8std::cout << 123 << std::endl; // 0x7bdouble d = 123.45;// C風格的格式輸出都不會影響,很舒服// 一般情況考慮用snprintf()打印到棧上緩沖,再用ostream輸出// stdio函數時線程安全的,iostream不是線程安全的:cout.operator<<(a).operator<<(b)兩次調用期間可能會被打斷,造成輸出不連續// fprintf(stdout, "%s %d", a, b);打印的內容不會受其他線程影響printf("\n%8.3f\n", d);std::cout << d << std::endl;using namespace std;cout << d << endl;// setprecision會影響后續輸出的精度,setw則不會cout << setw(8) << fixed << setprecision(3) << d << endl;cout << d << endl;return 0; }

總結

輸入:istream不適合輸入帶格式的數據;推薦做法:std::getline讀入一行數據到std::string,然后用正則表達式判斷正誤,并作分組,最后用strtod,strtol,或者std::string相關函數做類型轉換

輸出:ostream僅做簡單的無格式輸出

不要用ostream來寫log
ostringstream會動態分配內存,不適合性能較高的場合

文件IO,如果用作文本文件的輸入或者輸出,fstream也有上述的缺點
iostream在某些場合比stdio快,某些又慢,對于高性能而言,需自己實現字符串轉換

11.6.5 iostream在設計方面的缺點

面向對象中的public繼承需要滿足Liskov替代原則,就是OO繼承強調的是可替代性,派生類的對象可以替換基類對象;
只有真正的is-a關系采用public繼承,其他均以組合替代;

11.6.6 一個300行的memory buffer output stream

LogStream接口定義見muduo

其他程序如何使用LogStream作為輸出呢?

#include <ostream> #include <iostream> #include <iomanip> //操作子格式化需要該頭文件 class Date { public:Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}// 輸出2022-11-03// iostream輸出格式繁瑣void writeTo(std::ostream &out) const{out << year_ << "-"<< std::setw(2) << std::setfill('0') << month_ << "-"<< std::setw(2) << std::setfill('0') << day_;}template <typename OStream>void writeTo(OStream &out){out << year_ << "-" << month_ << "-" << day_;char buf[32];snprintf(buf, sizeof(buf), "%d-%02d-%02d", year_, month_, day_);out << buf;}private:int year_;int month_;int day_; };template <typename OStream> OStream& operator<<(OStream& out, const Date &date) {date.writeTo(out);return out; }

11.6.7 現實中的C++程序如果做文件IO

在C++項目中,自己寫個File class,把項目用到的文件IO功能簡單封裝一下,通常就能滿足需要。

11.7 值語義與數據抽象

11.7.1 什么是值語義

指的是對象的拷貝與原對象無關,就像拷貝int一樣,eg:

  • C++內置類型bool、int、double、
  • 標準庫:pair<>、vector<>、map<>、string

對象語義:對象拷貝是禁止的,eg:

  • muduo的Thread的拷貝是禁止的,因為Thread代表一個線程,拷貝一個Thread對象并不能讓系統增加一個一模一樣的線程

11.7.2 值語義與生命期

  • eg:若Parent擁有Child,Child的生命期由其Parent控制
#include <memory> class Parent;class Child { public:explicit Child(Parent *myParent) : myParent_(myParent) {}private:Parent *myParent_; };class Parent { public:Parent() {}private:std::unique_ptr<Child> myChild_; };

采用std::shared_ptr寫法:

#include <memory> class Parent; using ParentPtr = std::shared_ptr<Parent>;class Child { public:explicit Child(const ParentPtr &myParent) : myParent_(myParent) {}private:std::weak_ptr<Parent> myParent_; };using ChildPtr = std::shared_ptr<Child>; class Parent : public std::enable_shared_from_this<Parent> { public:Parent() {}void addChild(){myChild.reset(new Child(shared_from_this()));}private:ChildPtr myChild; };int main() {ParentPtr p(new Parent());p->addChild(); }
  • eg:Child持有mom和dad的parents,一個parent持有一個或者多個child;mom知道她的配偶spouse,dad知道她的配偶spouse
#include <memory> #include <vector> // 如哦不使用智能指針,用C++做面向對象編程會困難重重 class Parent; using ParentPtr = std::shared_ptr<Parent>;class Child { public:explicit Child(const ParentPtr &mom, const ParentPtr &dad) : myMom_(mom), myDad_(dad) {}private:std::weak_ptr<Parent> myMom_;std::weak_ptr<Parent> myDad_; };using ChildPtr = std::shared_ptr<Child>; class Parent { public:Parent() {}void addChild(const ChildPtr &child){myChildren.push_back(child);}void setSpouse(const ParentPtr &spouse){mySpouse = spouse;}private:std::vector<ChildPtr> myChildren;std::weak_ptr<Parent> mySpouse; };int main() {ParentPtr mom(new Parent());ParentPtr dad(new Parent());mom->setSpouse(dad);dad->setSpouse(mom);{ChildPtr child(new Child(mom, dad));mom->addChild(child);dad->addChild(child);}{ChildPtr child(new Child(mom, dad));mom->addChild(child);dad->addChild(child);} }

11.7.3 值語義與標準庫

C++編譯器會為class默認提供copy constructor和assignment operator,所以在寫一個C++ class的時候,讓他默認繼承boost::noncopyable,幾乎總是正確的。

11.7.4 值語義與C++語言

C++的設計初衷是讓用戶定義的類型class能像內置類型int一樣工作。

11.7.5 什么是數據抽象

數據抽象data abstraction是與面向對象object oriented并列的一種編程范式。

11.7.6 數據抽象所需的語言措施

11.7.7 數據抽象的例子

12 C++經驗談

12.1 用異或來交換變量是錯誤的

  • eg:將“12345”反轉為“54321”
// C void reverse(char *str, int n) {char *begin = str;char *end = str + n - 1;while (begin < end){auto tmp = *begin;*begin = *end;*end = tmp;begin++;--end;} }// C++ void reverse_by_std(char* str, int n) {std::reverse(str,str+n); }

12.2 不要重載全局::operator new()

12.2.1 內存管理的基本要求

既不重復delete,也不漏掉delete

12.2.2 重載::operator new()的理由

12.2.3 ::operator new()的兩種重載方式

//方式1 #include <new> void *operator new(size_t size); void operator delete(void *p);//方式2 void *operator new(size_t size, const char *file, int line); void operator delete(void *p, const char *file, int line);Foo *p = new (__FILE, __LINE__) Foo;

12.2.4 現實的開發環境

12.2.5 重載::operator new()的困境

12.2.6 解決辦法:替換malloc

12.2.7 為單獨的class重載::operator new()有問題嗎?

12.2.8 有必要自行定制內存分配器嗎?

重載::operator new()或許在某些臨時的場合能應急,但是不應該作為一種策略來使用。
如果需要,可以從malloc層面入手,徹底替換內存分配器。

12.3 帶符號整數的除法與余數

12.4 在單元測試中mock系統調用

12.4.1 系統函數的依賴注入

方法1:
采用傳統的面向對象的手法,借助運行期的遲綁定實現注入與替換。自己寫一個System interface,把程序里面的open,close,write等函數用虛函數封裝一層。

方法2:采用編譯器或者鏈接期的遲綁定,因此程序只會用到一個implementation object,為此虛函數調用的代價有些不值得。(與系統調用相比,虛函數這點開銷可以忽略不計)。

  • 在一個system namespace頭文件,在其中聲明read()和write()等普通函數,然后在.cc文件里轉發給對應系統函數::read()和::write()。
  • 無需用到虛函數,代碼寫起來也比較簡潔,只用前綴sockets::即可。
// real:SocketsOps.h namespace sockets {int connect(int port, const struct sockaddr_in &addr); }// real:SocketsOps.cc int sockets::connect(int port, const struct sockaddr_in &sockaddr) {return ::connect(port, sockaddr_cast(&addr), sizeof(addr)); } // 編譯普通程序 g++ main.cc mynetcat.cc SocketsOps.cc - o mynetcat// stub:單元測試,MockSocketsOps.cc int sockets::connect(int port, const struct sockaddr_in &sockaddr) {errno = EAGIN;return -1; } // 編譯單元測試 g++ main.cc mynetcat.cc MockSocketsOps.cc - o mynetcat

12.4.2 鏈接器墊片link seam

  • 一開始沒有考慮單元測試,如何注入mock系統調用?
// 在鏈接時,會優先采用我們自己定義的函數 connect_func_t connect_func = dlsym(RTDL_NEXT, "connect");bool mock_connect; int mock_connect_errno; // mock connectextern "C" int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {if (mock_connect){errno = mock_connect_errno;return errno == 0 ? 0 : -1;}else{return connect_func(sockfd, addr, addrlen);} }

12.5 慎用匿名namespace

一些小的helper函數會放到匿名namespace中

12.5.1 C語言的static關鍵字的兩種用法

函數內的靜態變量

  • 使用靜態變量的函數是不可重入的,也不是線程安全的

函數體之外修飾變量或者函數

  • 僅對本文件可見
  • 匿名namespace可以達到相同的效果

12.5.2 C++語言的static關鍵字的四種用法

除了以上C語言的兩種用法,有定義了兩種新用法:

  • 修飾class的數據成員
  • 修飾class的成員函數

12.5.3 匿名namespace的不利之處

anon.cc

namespace {void foo() {} } // namespaceint main() {foo(); }

anonlib.cc

namespace {void foo() {} } // namespace

上述兩個文件都定義了匿名空間中的foo()函數,那么gdb則無法區分這兩個函數

  • 匿名namespace中的函數是weak text,鏈接的時候若發生重名,linker不會報錯
g++ -g anon.cc anonlib.cc (gdb) b ' (anonymous namespace)::foo() __cxa_finalize@plt _fini _start anonlib.cc frame_dummy main() __cxa_finalize __do_global_dtors_aux _init anon.cc deregister_tm_clones main register_tm_clones

解決辦法:

  • 調試時使用文件名:行號
  • 使用具體的namespace名字,Boost中就常用boost::detail來存放不應該暴露給用戶,但又不得不放到頭文件里面的函數或者class

12.6 采用有利于版本管理的代碼格式

C和C++代碼中的換行符都被編譯器(預處理之后)當做white space來對待。

  • eg:等價寫法:
foo(1, 2) foo(1,2)

12.6.1 對diff友好的代碼格式

不適用/**/來注釋多行代碼

12.6.2 對grep友好的代碼風格

12.6.3 一切為了效率

12.7 再探std::string

12.7.1 直接拷貝eager copy

12.7.2 寫時賦值copy on write

12.7.3 短字符串優化

12.8 用STL algorithm輕松解決算法面試題

生成N個不同元素的全排列

#include <algorithm> #include <vector> #include <iostream> #include <iterator>using namespace std; int main() {std::vector<int> vec = {1, 2, 3, 4};int count = 0;do{std::cout << ++count << ": ";std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, ","));cout << endl;} while (next_permutation(vec.begin(), vec.end()));return 0; }

生成從N個元素中取出M個的所有組合

  • 對序列{1,1,1,0,0,0,0}做全排列。對于每個排列,輸出數字1對應的位置上的元素。
  • eg:
#include <algorithm> #include <vector> #include <iostream> #include <iterator>using namespace std; int main() {std::vector<int> values = {1, 2, 3, 4, 5, 6, 7};std::vector<int> select = {1, 1, 1, 0, 0, 0, 0};int count = 0;do{std::cout << ++count << ": ";for (auto i = 0; i < select.size(); ++i){if (select[i] == 1){cout << values[i] << std::endl;}}cout << endl;} while (prev_permutation(select.begin(), select.end()));return 0; }

用unique()去除連續的重復空白

  • 給定一個字符串,要求原地把相鄰的多個空格替換為一個,例如a__b,輸出為a_b。
  • 所有針對于STL algorithm都只能調整區間內元素的順序,不能真正刪除容器內的元素
void removeContinousSpaces(std::string &str) {該函數的作用是“去除”容器或者數組中相鄰元素的重復出現的元素,注意 (1) 這里的去除并非真正意義的erase,而是將重復的元素放到容器的末尾,返回值是去重之后的尾地址。 (2) unique針對的是相鄰元素,所以對于順序順序錯亂的數組成員,或者容器成員,需要先進行排序,可以調用std::sort()函數/**/auto last = std::unique(str.end(), str.end(), [](char a, char b){ return a == ' ' && b == ' '; });str.erase(last, str.end()); }

用一臺4GiB內存的機器對磁盤上的單個100GB文件排序

  • 假設要歸并從小到大排序好的32個文件
  • 標準思路是:先分塊排序,然后多路歸并成輸出文件。多路歸并使用heap排序。
  • 用{make,push,pop}_heap實現多路歸并
#include <algorithm> #include <iostream> #include <iterator> #include <vector>typedef int Record; typedef std::vector<Record> File;struct Input {Record value;size_t index;const File* file;explicit Input(const File* f): value(-1),index(0),file(f){ }bool next(){if (index < file->size()){ value = (*file)[index];++index;return true;} else {return false;}}bool operator<(const Input& rhs) const{// make_heap to build min-heap, for mergingreturn value > rhs.value;} };File mergeN(const std::vector<File>& files) {File output;std::vector<Input> inputs;// 構造二叉堆// 二叉堆非常適合解決在連續的插入和刪除操作中,快速定位最大值或最小值的問題//在最大堆中,根節點的值最大; 在最小堆中,根節點的值最小;局部和整體一樣for (size_t i = 0; i < files.size(); ++i) {Input input(&files[i]);if (input.next()) {inputs.push_back(input);}}std::make_heap(inputs.begin(), inputs.end());// 循環結束,即堆為空,說明每個文件都讀取完畢了while (!inputs.empty()) {// 將堆頂元素放到末尾inputs.back()std::pop_heap(inputs.begin(), inputs.end());output.push_back(inputs.back().value);// 從堆頂元素所屬的文件讀入下一條記錄,成功則放回到堆中if (inputs.back().next()) {std::push_heap(inputs.begin(), inputs.end());} else {inputs.pop_back();}}return output; }int main() {// 假設要歸并從小到大排序好的32個文件,構造一個32元素的min heap,每次取出堆頂的元素,將其Record寫入輸出文件const int kFiles = 32;std::vector<File> files(kFiles);for (int i = 0; i < kFiles; ++i) {File file(rand() % 1000);std::generate(file.begin(), file.end(), &rand);std::sort(file.begin(), file.end());files[i].swap(file);}File output = mergeN(files);std::copy(output.begin(), output.end(),std::ostream_iterator<Record>(std::cout, "\n")); }

總結

以上是生活随笔為你收集整理的muduo C++网络库的学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。