From 7b803212e0f67800c76a8f4ef1d6027c02ccf696 Mon Sep 17 00:00:00 2001 From: yangsoon Date: Fri, 26 Jun 2020 20:44:53 +0800 Subject: [PATCH] add smart-point --- C++Primer/Template/com.cpp | 2 +- C++Primer/smart_point/strblob.cpp | 29 +++++++++++++++++++ C++Primer/smart_point/strblob.h | 20 +++++++++++++ .../{tcpcliser => tcpclistener}/tcpcli01.c | 0 .../{tcpcliser => tcpclistener}/tcpcli04.c | 0 .../{tcpcliser => tcpclistener}/tcpserv01.c | 0 .../{tcpcliser => tcpclistener}/tcpserv02.c | 0 .../{tcpcliser => tcpclistener}/tcpserv04.c | 0 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 C++Primer/smart_point/strblob.cpp create mode 100644 C++Primer/smart_point/strblob.h rename muduo/unp/{tcpcliser => tcpclistener}/tcpcli01.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpcli04.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv01.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv02.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv04.c (100%) diff --git a/C++Primer/Template/com.cpp b/C++Primer/Template/com.cpp index e0e5231..8f76be3 100644 --- a/C++Primer/Template/com.cpp +++ b/C++Primer/Template/com.cpp @@ -9,5 +9,5 @@ int compare(const T &v1, const T &v2) { } int main() { - + cout << compare(1,2); } \ No newline at end of file diff --git a/C++Primer/smart_point/strblob.cpp b/C++Primer/smart_point/strblob.cpp new file mode 100644 index 0000000..6d0cf4d --- /dev/null +++ b/C++Primer/smart_point/strblob.cpp @@ -0,0 +1,29 @@ +#include "strblob.h" + +// make_shared 在动态内存中分配一个对象并初始化它 +StrBlob::StrBlob(): data(std::make_shared>()) {}; + +StrBlob::StrBlob(std::initializer_list il): data(std::make_shared>(il)){}; + +void StrBlob::check(size_type i, const std::string &msg) const { + if (i >= data->size()) { + throw std::out_of_range(msg); + } +} + +std::string& StrBlob::front() { + check(0, "front on empty StrBlob"); + return data->front(); +} + +std::string& StrBlob::back() { + check(0, "back on empty StrBlob"); + return data->back(); +} + +void StrBlob::pop_back() { + check(0, "pop_back on empty StrBlob"); + data->pop_back(); +} + + diff --git a/C++Primer/smart_point/strblob.h b/C++Primer/smart_point/strblob.h new file mode 100644 index 0000000..4ae85b4 --- /dev/null +++ b/C++Primer/smart_point/strblob.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +class StrBlob { +public: + typedef std::vector::size_type size_type; + StrBlob(); + StrBlob(std::initializer_list il); + size_type size() const {return data->size();} + bool empty() const {return data->empty();} + void push_back(const std::string &t) {data->push_back(t)}; + void pop_back(); + std::string& front(); + std::string& back(); +private: + std::shared_ptr> data; + void check(size_type i, const std::string &msg) const; +}; diff --git a/muduo/unp/tcpcliser/tcpcli01.c b/muduo/unp/tcpclistener/tcpcli01.c similarity index 100% rename from muduo/unp/tcpcliser/tcpcli01.c rename to muduo/unp/tcpclistener/tcpcli01.c diff --git a/muduo/unp/tcpcliser/tcpcli04.c b/muduo/unp/tcpclistener/tcpcli04.c similarity index 100% rename from muduo/unp/tcpcliser/tcpcli04.c rename to muduo/unp/tcpclistener/tcpcli04.c diff --git a/muduo/unp/tcpcliser/tcpserv01.c b/muduo/unp/tcpclistener/tcpserv01.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv01.c rename to muduo/unp/tcpclistener/tcpserv01.c diff --git a/muduo/unp/tcpcliser/tcpserv02.c b/muduo/unp/tcpclistener/tcpserv02.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv02.c rename to muduo/unp/tcpclistener/tcpserv02.c diff --git a/muduo/unp/tcpcliser/tcpserv04.c b/muduo/unp/tcpclistener/tcpserv04.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv04.c rename to muduo/unp/tcpclistener/tcpserv04.c