From 316e0bd2445c402567911b0b0618e03bfcf6af88 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 7 Dec 2020 09:33:31 +0800 Subject: [PATCH 01/15] msgpack.cpp --- section4/msgpack.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/section4/msgpack.cpp b/section4/msgpack.cpp index dc47b9d..7bc83c1 100644 --- a/section4/msgpack.cpp +++ b/section4/msgpack.cpp @@ -97,7 +97,11 @@ void case3() msgpack::sbuffer sbuf; msgpack::pack(sbuf, book1); - auto obj = msgpack::unpack(sbuf.data(), sbuf.size()).get(); + // may cause std::bad_cast error + //auto obj = msgpack::unpack(sbuf.data(), sbuf.size()).get(); + + auto handle = msgpack::unpack(sbuf.data(), sbuf.size()); + auto obj = handle.get(); Book book2; obj.convert(book2); From ab5db0c7b5c9989c47f3690c54ad7d22f39a751b Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 22 Dec 2020 10:40:08 +0800 Subject: [PATCH 02/15] auto.cpp --- section2/auto.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/section2/auto.cpp b/section2/auto.cpp index 903b04d..4bc872e 100644 --- a/section2/auto.cpp +++ b/section2/auto.cpp @@ -4,12 +4,16 @@ // g++ auto.cpp -std=c++14 -o a.out;./a.out // g++ auto.cpp -std=c++14 -I../common -o a.out;./a.out +#include + #include #include #include #include #include +#include + #include void case1() @@ -97,6 +101,10 @@ void case6() decltype(auto) x1 = (x); decltype(auto) x2 = &x; decltype(auto) x3 = x1; + + assert(std::is_lvalue_reference::value); + assert(std::is_pointer::value); + assert(std::is_lvalue_reference::value); } auto get_a_set() From 49a6d26c4ebbbe52807ceaaaa535d9b6e187c549 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 10 Feb 2021 21:46:12 +0800 Subject: [PATCH 03/15] etcd.hpp --- extra/etcd.hpp | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 extra/etcd.hpp diff --git a/extra/etcd.hpp b/extra/etcd.hpp new file mode 100644 index 0000000..3ded3ed --- /dev/null +++ b/extra/etcd.hpp @@ -0,0 +1,175 @@ +// Copyright (c) 2020 by Chrono +// +// docker pull quay.io/coreos/etcd +// docker pull quay.io/coreos/etcd:v3.3.25 +// +// https://etcd.io/docs/v2/api/ +// +// docker-compose -f etcd.yml -p etcd up -d +// docker-compose -f etcd.yml -p etcd down +// +// curl 127.0.0.1:2379/version +// {"etcdserver":"3.3.8","etcdcluster":"3.3.0"} +// {"etcdserver":"3.3.25","etcdcluster":"3.3.0"} +// +// curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value="Hello xxx" +// curl http://127.0.0.1:2379/v2/keys/foo + +#ifndef _UTILITY_ETCD_HPP_ +#define _UTILITY_ETCD_HPP_ + +#include +#include +#include + +namespace etcd::v2 { + +// sample output +// Version: +// { +// "etcdcluster": "3.3.0", +// "etcdserver": "3.3.8", +// "statusCode": 200 +// } +// Get: +// { +// "action": "get", +// "node": { +// "createdIndex": 29, +// "key": "/test/xxx", +// "modifiedIndex": 29, +// "value": "etcd" +// }, +// "statusCode": 200 +// } +class Client final { +public: + using string_type = std::string; + using string_ref_type = const std::string&; + + using json_type = nlohmann::json; +public: + Client(string_ref_type addr, int port) noexcept { + + m_url = fmt::format("http://{}:{}", addr, port); + //m_url = "http://" + addr + std::to_string(port); + + m_prefix = m_url + "/v2/keys"; + } + + ~Client() noexcept = default; +public: + json_type Version() noexcept { + + auto res = cpr::Get( + m_url + "/version" + ); + + return wrap_reply(res); + } + + // statusCode=2xx => OK + json_type Set(string_ref_type key, + string_ref_type value) noexcept { + + auto res = cpr::Put( + m_prefix + key, + cpr::Body("value=" + value) + ); + + return wrap_reply(res); + } + + // statusCode=200 => OK + json_type Get(string_ref_type key) noexcept { + + auto res = cpr::Get( + m_prefix + key + ); + + return wrap_reply(res); + } + + // statusCode=200 => OK + json_type Delete(string_ref_type key) noexcept { + + auto res = cpr::Delete( + m_prefix + key + ); + + return wrap_reply(res); + } + +public: + // statusCode=2xx => OK + json_type Set(string_ref_type key, + string_ref_type value, int ttl) noexcept { + + auto res = cpr::Put( + m_prefix + key, + cpr::Body(fmt::format("value={};ttl={}", value, ttl)) + ); + + return wrap_reply(res); + } + +public: + // It will block the thread until event happens + json_type Watch(string_ref_type key) noexcept { + + auto watch_url = m_prefix + key + "?wait=true"; + + // waiting for event + auto res = cpr::Get(watch_url); + + // fatal error + if (res.error.code != cpr::ErrorCode::OK) { + + return json_type { + {"error", res.error.message} + }; + } + + // http ok but timed out + if (res.text.empty()) { + + return json_type { + {"error", "timedout"} + }; + } + + return wrap_reply(res); + } + +private: + template + json_type wrap_reply(const T& res) noexcept { + + //std::cout << res.status_code << std::endl; + + //json_type j; + //j["code"] = res.status_code; + //j["value"] = json_type::parse(res.text); + + if (res.error.code != cpr::ErrorCode::OK) { + + return json_type { + //{"status_code", res.error.code}, + {"error", res.error.message} + }; + } + + auto j = json_type::parse(res.text); + j["statusCode"] = res.status_code; + + return j; + } +private: + string_type m_url; + string_type m_prefix; +}; + +} // namespace etcd::v2 + +#endif //_UTILITY_ETCD_HPP_ + From f4e492c69796d1a01349ffd181ce34dbd6134907 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 4 Mar 2021 13:34:54 +0800 Subject: [PATCH 04/15] docker-compose.yml --- docker/docker-compose.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 docker/docker-compose.yml diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..54f39d7 --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,19 @@ +# chrono 2021-03 +# +# docker-compose run --rm cpp_study +# + +version: "3" + +services: + + cpp_study: + + image: chronolaw/cpp_study + + container_name: cpp_study + + working_dir: /root/cpp_study/ + + command: /bin/bash + From 569176c19e41baf1c4793f10d70409ca54129f7a Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 4 Mar 2021 13:52:59 +0800 Subject: [PATCH 05/15] docker-compose.yml --- docker/docker-compose.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 54f39d7..4466347 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,6 +2,8 @@ # # docker-compose run --rm cpp_study # +# docker ps +# docker exec -it xxx bash version: "3" @@ -13,7 +15,10 @@ services: container_name: cpp_study - working_dir: /root/cpp_study/ + working_dir: /root/cpp_study + + stdin_open: true + tty: true command: /bin/bash From 423041ae176c8531afb8a7b435b15a32fe648dcc Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 4 Mar 2021 13:57:11 +0800 Subject: [PATCH 06/15] cpp-study-pod.yml --- docker/cpp-study-pod.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docker/cpp-study-pod.yml diff --git a/docker/cpp-study-pod.yml b/docker/cpp-study-pod.yml new file mode 100644 index 0000000..0324f9d --- /dev/null +++ b/docker/cpp-study-pod.yml @@ -0,0 +1,32 @@ +# kubernetes pod yaml for cpp study +# you could run it in k8s/minikube/kind/... +# chrono @ 2021-03 + +# kubectl apply -f cpp-study-pod.yml +# kubectl delete -f cpp-study-pod.yml +# +# kubectl get pods +# kubectl exec -it k8s-cpp-study -- bash +# kubectl attach -it k8s-cpp-study + +apiVersion: v1 +kind: Pod + +metadata: + name: k8s-cpp-study + labels: + author: chrono + env: study + +spec: + + containers: + + - name: cpp_study + image: chronolaw/cpp_study + workingDir: /root/cpp_study + + # exec or attach into this container + stdin: true + tty: true + From 4564d5ea9ab00acebc0ea66144ef9f4c3dfbd7e6 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 4 Mar 2021 14:18:27 +0800 Subject: [PATCH 07/15] typofix --- docker/cpp-study-pod.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/cpp-study-pod.yml b/docker/cpp-study-pod.yml index 0324f9d..0d627b8 100644 --- a/docker/cpp-study-pod.yml +++ b/docker/cpp-study-pod.yml @@ -22,7 +22,7 @@ spec: containers: - - name: cpp_study + - name: cpp-study image: chronolaw/cpp_study workingDir: /root/cpp_study From 873c8a5be87a9fba708e51281447006eeae9e10f Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 4 Mar 2021 16:58:24 +0800 Subject: [PATCH 08/15] Dockerfile --- docker/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index d217a8a..e141919 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -43,7 +43,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update \ google-perftools libgoogle-perftools-dev \ && pip3 install pybind11 \ && cd \ - && git clone https://github.com/chronolaw/cpp_study \ + && git clone https://github.com/chronolaw/cpp_study --depth=1 \ && cp ~/cpp_study/env/vimrc ~/.vimrc \ && cp ~/cpp_study/env/bashrc ~/.bashrc \ && cp ~/cpp_study/env/gitconfig ~/.gitconfig \ @@ -69,7 +69,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update \ && curl -fsL https://github.com/gperftools/gperftools/releases/download/gperftools-${GPERF_VERSION}/gperftools-${GPERF_VERSION}.tar.gz -o gperf.tgz \ && tar xfz gperf.tgz \ && ln -s ~/github/gperftools-${GPERF_VERSION}/pprof-symbolize /bin/pprof \ - && git clone https://github.com/brendangregg/FlameGraph.git \ + && git clone https://github.com/brendangregg/FlameGraph.git --depth=1 \ && ln -s ~/github/FlameGraph/flamegraph.pl ~/cpp_study/section4/ \ && rm *.tgz \ && DEBIAN_FRONTEND=noninteractive apt-get autoremove -y From 2daec740dd694f2ee40f3a0ea2a1357c612ec0ef Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 5 Mar 2021 13:56:24 +0800 Subject: [PATCH 09/15] Dockerfile.new --- docker/Dockerfile.new | 22 ++++++++++++ docker/setup.sh | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 docker/Dockerfile.new create mode 100755 docker/setup.sh diff --git a/docker/Dockerfile.new b/docker/Dockerfile.new new file mode 100644 index 0000000..d1d0266 --- /dev/null +++ b/docker/Dockerfile.new @@ -0,0 +1,22 @@ +# Dockerfile +# chrono@2021-03 + +# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun +# sudo service docker start +# sudo usermod -aG docker ${USER} + +# docker build -t chronolaw/cpp_study . + +# docker run -it --rm chronolaw/cpp_study + +ARG SRC_IMAGE="gcc:7" + +FROM ${SRC_IMAGE} + +WORKDIR /root + +COPY setup.sh ./ + +RUN ./setup.sh + +CMD ["/bin/bash"] diff --git a/docker/setup.sh b/docker/setup.sh new file mode 100755 index 0000000..8a57b21 --- /dev/null +++ b/docker/setup.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +# chrono @ 2021-03 + +# apt-get +DEBIAN_FRONTEND=noninteractive apt-get install -y \ + vim cmake gdb pstack strace \ + libmsgpack-dev protobuf-compiler libprotobuf-dev libprotoc-dev \ + libcurl4-openssl-dev libzmq3-dev \ + python3-dev python3-pip \ + google-perftools libgoogle-perftools-dev + +DEBIAN_FRONTEND=noninteractive apt-get autoremove -y + +# python3 pybind11 +pip3 install pybind11 + +#WORKDIR='/root/' + +HOME='/root' + +git clone https://github.com/chronolaw/cpp_study --depth=1 + +# setubp env +cp ${HOME}/cpp_study/env/vimrc ${HOME}/.vimrc \ +cp ${HOME}/cpp_study/env/bashrc ${HOME}/.bashrc \ +cp ${HOME}/cpp_study/env/gitconfig ${HOME}/.gitconfig \ + +# source + +JSON_VERSION="3.9.1" +CPR_VERSION="1.4.0" +LUAJIT_VERSION="2.1-20200102" +LUABRIDAGE_VERSION="2.6" +GPERF_VERSION="2.8" + +#echo ${JSON_VERSION} + +mkdir ${HOME}/github +cd ${HOME}/github + +# json +curl -fsL https://github.com/nlohmann/json/releases/download/v${JSON_VERSION}/json.hpp -o json.hpp +ln -s ~/github/json.hpp ~/cpp_study/common/ + +# curl/cpr +curl -fsL https://github.com/whoshuu/cpr/archive/${CPR_VERSION}.tar.gz -o cpr.tgz +tar xfz cpr.tgz +cd cpr-${CPR_VERSION} +cmake . -DUSE_SYSTEM_CURL=ON -DBUILD_CPR_TESTS=OFF +make && make install && make clean +cd .. + +# luajit +curl -fsL https://github.com/openresty/luajit2/archive/v${LUAJIT_VERSION}.tar.gz -o luajit.tgz +tar xfz luajit.tgz +cd luajit2-${LUAJIT_VERSION} +make && make install && make clean +ln -s /usr/local/lib/libluajit-5.1.so.2 /lib/x86_64-linux-gnu/ +cd .. + +# luabridge +curl -fsL https://github.com/vinniefalco/LuaBridge/archive/${LUABRIDAGE_VERSION}.tar.gz -o LuaBridge.tgz +tar xfz LuaBridge.tgz +ln -s ~/github/LuaBridge-${LUABRIDAGE_VERSION}/Source/LuaBridge/ ~/cpp_study/common/ + +# gperftools +curl -fsL https://github.com/gperftools/gperftools/releases/download/gperftools-${GPERF_VERSION}/gperftools-${GPERF_VERSION}.tar.gz -o gperf.tgz +tar xfz gperf.tgz +ln -s ~/github/gperftools-${GPERF_VERSION}/pprof-symbolize /bin/pprof + +# flame graph +git clone https://github.com/brendangregg/FlameGraph.git --depth=1 +ln -s ~/github/FlameGraph/flamegraph.pl ~/cpp_study/section4/ + +# clean +rm *.tgz + From c4b402f9f9b02b6c6b5881e448a5984be4817058 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 5 Mar 2021 14:07:38 +0800 Subject: [PATCH 10/15] Dockerfile.new --- docker/Dockerfile.new | 2 +- docker/setup.sh | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile.new b/docker/Dockerfile.new index d1d0266..85b1bf1 100644 --- a/docker/Dockerfile.new +++ b/docker/Dockerfile.new @@ -17,6 +17,6 @@ WORKDIR /root COPY setup.sh ./ -RUN ./setup.sh +RUN ./setup.sh && rm ./setup.sh CMD ["/bin/bash"] diff --git a/docker/setup.sh b/docker/setup.sh index 8a57b21..35121d6 100755 --- a/docker/setup.sh +++ b/docker/setup.sh @@ -16,12 +16,12 @@ DEBIAN_FRONTEND=noninteractive apt-get autoremove -y pip3 install pybind11 #WORKDIR='/root/' +#HOME='/root' -HOME='/root' - +# git source code git clone https://github.com/chronolaw/cpp_study --depth=1 -# setubp env +# setup env cp ${HOME}/cpp_study/env/vimrc ${HOME}/.vimrc \ cp ${HOME}/cpp_study/env/bashrc ${HOME}/.bashrc \ cp ${HOME}/cpp_study/env/gitconfig ${HOME}/.gitconfig \ @@ -35,10 +35,14 @@ LUABRIDAGE_VERSION="2.6" GPERF_VERSION="2.8" #echo ${JSON_VERSION} +#echo ${HOME} mkdir ${HOME}/github cd ${HOME}/github +# test +#exit + # json curl -fsL https://github.com/nlohmann/json/releases/download/v${JSON_VERSION}/json.hpp -o json.hpp ln -s ~/github/json.hpp ~/cpp_study/common/ From f436205d807590bd609404049ded34357ada52e5 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 15 Mar 2021 09:07:09 +0800 Subject: [PATCH 11/15] typofix --- section1/oop.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/section1/oop.cpp b/section1/oop.cpp index dece5a1..80b8267 100644 --- a/section1/oop.cpp +++ b/section1/oop.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2050 by Chrono +// Copyright (c) 2020 by Chrono // // g++ oop.cpp -std=c++11 -o a.out;./a.out // g++ oop.cpp -std=c++14 -o a.out;./a.out From 083d0d0ed5f4b7ad3061d90496c5648785840726 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 20 Apr 2021 07:56:02 +0800 Subject: [PATCH 12/15] README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d41522a..4e98e11 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ docker run -it --rm chronolaw/cpp_study * [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines) * [OpenResty Code Style Guide(zh-cn)](http://openresty.org/cn/c-coding-style-guide.html) * [Google Code Style Guide](https://google.github.io/styleguide/cppguide.html) +* [ProtoBuffer C++ Doc](https://developers.google.com/protocol-buffers/docs/reference/cpp-generated) ## Dev Links From a2ff73a53f51c613627537b61621a8266383ef1a Mon Sep 17 00:00:00 2001 From: Chrono Date: Thu, 28 Oct 2021 11:03:40 +0800 Subject: [PATCH 13/15] c++ hopl4 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4e98e11..6b1547c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ docker run -it --rm chronolaw/cpp_study * [Bjarne Stroustrup's FAQ](http://www.stroustrup.com/bs_faq.html) * [Bjarne Stroustrup's C++11 FAQ](http://www.stroustrup.com/C++11FAQ.html) +* [Bjarne Stroustrup's C++ HOPL4 (zh-cn)](https://github.com/Cpp-Club/Cxx_HOPL4_zh) * [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines) * [OpenResty Code Style Guide(zh-cn)](http://openresty.org/cn/c-coding-style-guide.html) * [Google Code Style Guide](https://google.github.io/styleguide/cppguide.html) From 84bc32f2c2d3719f72e92f8202915956f8fc630b Mon Sep 17 00:00:00 2001 From: ek45 <15509988+ytlw@users.noreply.github.com> Date: Sat, 25 May 2024 20:08:11 +0800 Subject: [PATCH 14/15] support compilation at not-linux platform (#15) --- section1/preprocess.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/section1/preprocess.cpp b/section1/preprocess.cpp index bbf19f3..20c6c35 100644 --- a/section1/preprocess.cpp +++ b/section1/preprocess.cpp @@ -26,6 +26,8 @@ void case1() # #if __linux__ # define HAS_LINUX 1 +#else +# define HAS_LINUX 0 #endif # cout << "linux is " << HAS_LINUX << endl; From 3121dcc32e54d1b721762510fb602df5a035e20a Mon Sep 17 00:00:00 2001 From: ek45 <15509988+ytlw@users.noreply.github.com> Date: Mon, 27 May 2024 09:21:23 +0800 Subject: [PATCH 15/15] support compilation at mingw (#16) Co-authored-by: ytlw --- section1/oop.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/section1/oop.cpp b/section1/oop.cpp index 80b8267..9ad4968 100644 --- a/section1/oop.cpp +++ b/section1/oop.cpp @@ -9,6 +9,10 @@ #include #include +#if(defined (__MINGW64__) || (defined __MINGW32__)) +#include +#endif + #if 1 class Interface {};