├── MD5 ├── README.md ├── main.cpp └── CMakeLists.txt ├── Args ├── README.md ├── main.cpp ├── CMakeLists.txt └── Args.hpp ├── Any ├── README.md ├── CMakeLists.txt └── main.cpp ├── Aop ├── README.md ├── CMakeLists.txt ├── AopTimer.hpp └── main.cpp ├── Base64 ├── README.md ├── CMakeLists.txt └── main.cpp ├── Daemon ├── README.md ├── CMakeLists.txt └── main.cpp ├── Factory ├── README.md ├── Message.hpp ├── CMakeLists.txt ├── main.cpp └── Factory.hpp ├── Lazy ├── README.md ├── main.cpp ├── CMakeLists.txt └── Lazy.hpp ├── Meta ├── README.md ├── CMakeLists.txt └── main.cpp ├── Range ├── README.md ├── CMakeLists.txt └── main.cpp ├── String ├── README.md ├── CMakeLists.txt └── main.cpp ├── .gitignore ├── Connect ├── README.md ├── CMakeLists.txt ├── Connect.hpp └── main.cpp ├── Ioc ├── README.md ├── CMakeLists.txt └── main.cpp ├── LinqCpp ├── README.md └── CMakeLists.txt ├── Timer ├── README.md ├── CMakeLists.txt ├── main.cpp └── Timer.hpp ├── DateTime ├── README.md ├── CMakeLists.txt ├── main.cpp └── DateTime.hpp ├── Optional ├── README.md ├── CMakeLists.txt └── main.cpp ├── Signal ├── README.md ├── CMakeLists.txt ├── Signal.hpp └── main.cpp ├── Singleton ├── README.md ├── CMakeLists.txt ├── Singleton.hpp └── main.cpp ├── Variant ├── README.md ├── main.cpp └── CMakeLists.txt ├── thread_pool ├── README.md ├── CMakeLists.txt └── main.cpp ├── DllParser ├── README.md ├── Caculate │ ├── CMakeLists.txt │ ├── Caculate.h │ └── Caculate.cpp ├── DllParser │ ├── CMakeLists.txt │ └── main.cpp └── CMakeLists.txt ├── ObjectPool ├── README.md ├── CMakeLists.txt ├── main.cpp └── ObjectPool.hpp ├── PlacementNew ├── README.md ├── CMakeLists.txt └── main.cpp ├── ScopeGuard ├── README.md ├── CMakeLists.txt ├── main.cpp └── ScopeGuard.hpp ├── Traits ├── README.md ├── CMakeLists.txt └── main.cpp ├── lexical_cast ├── README.md ├── CMakeLists.txt └── main.cpp ├── BindCPU ├── README.md └── CMakeLists.txt ├── FunctionTraits ├── README.md ├── CMakeLists.txt └── main.cpp ├── TupleHelper ├── README.md ├── CMakeLists.txt └── main.cpp ├── DaemonAndWatchDog ├── README.md └── CMakeLists.txt ├── SingleApplication ├── README.md ├── CMakeLists.txt └── main.cpp ├── README.md ├── framework ├── net-thrift │ ├── RPCMessage.thrift │ ├── tests │ │ ├── CMakeLists.txt │ │ ├── MessageDef │ │ │ ├── ProtocolDef.h │ │ │ └── MessageDefHead.h │ │ ├── ClientTest │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ └── ServerTest │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ ├── CMakeLists.txt │ ├── src │ │ ├── RPCMessage_types.cpp │ │ ├── RPCMessage_constants.cpp │ │ ├── RPCMessage_constants.h │ │ ├── RPCMessage_types.h │ │ ├── ThriftServerWrapper.cpp │ │ ├── CMakeLists.txt │ │ └── ThriftClientWrapper.cpp │ └── include │ │ ├── ThriftServerWrapper.h │ │ ├── Message.h │ │ └── ThriftClientWrapper.h ├── .gitignore ├── net-tcpserver │ ├── tests │ │ ├── CMakeLists.txt │ │ ├── TcpClientTest │ │ │ └── CMakeLists.txt │ │ ├── TcpServerTest │ │ │ └── CMakeLists.txt │ │ └── MessageDefine │ │ │ └── PeopleInfoMessage.h │ ├── CMakeLists.txt │ ├── src │ │ ├── CRealJob.cpp │ │ ├── TcpServerWrapper.cpp │ │ ├── TcpClientWrapper.cpp │ │ ├── CThreadManage.cpp │ │ └── CWorkerThread.cpp │ └── include │ │ ├── CJob.h │ │ ├── CRealJob.h │ │ ├── Message.h │ │ ├── CThreadManage.h │ │ ├── CWorkerThread.h │ │ ├── TcpClientWrapper.h │ │ ├── TcpServerWrapper.h │ │ ├── CThread.h │ │ └── CThreadPool.h ├── utils │ ├── tests │ │ ├── CMakeLists.txt │ │ ├── String │ │ │ └── CMakeLists.txt │ │ ├── System │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ └── FileSystem │ │ │ └── CMakeLists.txt │ ├── CMakeLists.txt │ ├── include │ │ ├── Utils.h │ │ ├── System.h │ │ └── FileSystem.h │ └── src │ │ └── CMakeLists.txt ├── threadpool │ ├── CMakeLists.txt │ ├── src │ │ ├── CMakeLists.txt │ │ ├── CRealJob.cpp │ │ ├── main.cpp │ │ ├── CThreadManage.cpp │ │ └── CWorkerThread.cpp │ └── include │ │ ├── CRealJob.h │ │ ├── CJob.h │ │ ├── CThreadManage.h │ │ ├── CWorkerThread.h │ │ ├── CThread.h │ │ └── CThreadPool.h ├── clog │ ├── CMakeLists.txt │ ├── tests │ │ ├── main.cpp │ │ └── CMakeLists.txt │ ├── src │ │ ├── LogStream.cpp │ │ ├── CMakeLists.txt │ │ └── LogImpl.h │ └── include │ │ ├── LogStream.h │ │ └── LogWrapper.h ├── net-rcf │ ├── tests │ │ ├── CMakeLists.txt │ │ ├── MessageDef │ │ │ ├── RPCProtocolDefine.h │ │ │ ├── PubSubProtocolDefine.h │ │ │ ├── WeatherInfoMessage.h │ │ │ └── PeopleInfoMessage.h │ │ ├── ClientTest │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ ├── DBServerTest │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ ├── CenterNetServerTest │ │ │ ├── CMakeLists.txt │ │ │ ├── main.cpp │ │ │ └── CenterNetServer.h │ │ ├── PublisherTest │ │ │ └── CMakeLists.txt │ │ └── SubscriberTest │ │ │ └── CMakeLists.txt │ ├── CMakeLists.txt │ └── include │ │ └── RCFServerWrapper.hpp ├── CMakeLists.txt └── LICENSE ├── DaemonAndRunSingleInstance ├── README.md └── CMakeLists.txt ├── event_loop ├── main.cpp ├── CMakeLists.txt ├── event_loop.hpp └── server.hpp ├── WatchDog ├── README.md ├── CMakeLists.txt └── main.cpp ├── invoke ├── CMakeLists.txt ├── main.cpp └── invoke.hpp ├── NonCopyable └── NonCopyable.hpp ├── table_tuple ├── CMakeLists.txt ├── main.cpp └── table_tuple.hpp ├── singletion_v2 ├── CMakeLists.txt ├── main.cpp └── singletion.h ├── random_generator ├── CMakeLists.txt ├── main.cpp └── random_generator.h ├── task_thread ├── CMakeLists.txt ├── main.cpp ├── task_thread.cpp └── task_thread.h ├── thread_guard ├── CMakeLists.txt ├── main.cpp └── thread_guard.hpp ├── cyclic_barrier ├── CMakeLists.txt ├── cyclic_barrier.hpp └── main.cpp ├── message_buffer ├── CMakeLists.txt ├── main.cpp └── message_buffer.hpp ├── shared_mutex ├── CMakeLists.txt ├── main.cpp └── shared_mutex.h ├── count_down_latch ├── CMakeLists.txt ├── count_down_latch.hpp └── main.cpp ├── format ├── fmt │ ├── ostr.h │ ├── fmt.h │ └── bundled │ │ ├── printf.cc │ │ ├── ostream.cc │ │ └── LICENSE.rst ├── CMakeLists.txt ├── timer.hpp └── format.h ├── task_timer ├── CMakeLists.txt ├── main.cpp └── task_timer.h ├── variant_visitor ├── CMakeLists.txt ├── main.cpp └── variant_visitor.hpp ├── subscribe_center ├── subscribe_center.h └── subscribe_center.cpp ├── LICENSE ├── login_copy ├── py │ ├── lg.py │ ├── ul.py │ └── dl.py └── sh │ ├── login.sh │ ├── upload.sh │ └── download.sh └── qt_connect └── qt_connect.h /MD5/README.md: -------------------------------------------------------------------------------- 1 | Md5 sample 2 | =================== 3 | 4 | md5的实现 5 | 6 | -------------------------------------------------------------------------------- /Args/README.md: -------------------------------------------------------------------------------- 1 | Args sample 2 | =================== 3 | 4 | 可变模板参数的一些例子 5 | 6 | -------------------------------------------------------------------------------- /Any/README.md: -------------------------------------------------------------------------------- 1 | Any sample 2 | =================== 3 | 4 | 该例程讲解用C++11来通用数据类型Any 5 | 6 | -------------------------------------------------------------------------------- /Aop/README.md: -------------------------------------------------------------------------------- 1 | Aop sample 2 | =================== 3 | 4 | 该例程讲解用C++11实现AOP框架 5 | 6 | -------------------------------------------------------------------------------- /Base64/README.md: -------------------------------------------------------------------------------- 1 | Base64 sample 2 | =================== 3 | 4 | 该例程讲解base64接口的调用 5 | 6 | -------------------------------------------------------------------------------- /Daemon/README.md: -------------------------------------------------------------------------------- 1 | Daemon sample 2 | =================== 3 | 4 | 该例程讲解Linux守护进程的编程方法 5 | 6 | -------------------------------------------------------------------------------- /Factory/README.md: -------------------------------------------------------------------------------- 1 | Factory sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个工厂类 5 | 6 | -------------------------------------------------------------------------------- /Lazy/README.md: -------------------------------------------------------------------------------- 1 | Lazy sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个Lazy类 5 | 6 | -------------------------------------------------------------------------------- /Meta/README.md: -------------------------------------------------------------------------------- 1 | Meta sample 2 | =================== 3 | 4 | 该例程讲解C++11实现一个获取类成员的Meta 5 | 6 | -------------------------------------------------------------------------------- /Range/README.md: -------------------------------------------------------------------------------- 1 | Range sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个python的range 5 | -------------------------------------------------------------------------------- /String/README.md: -------------------------------------------------------------------------------- 1 | String sample 2 | =================== 3 | 4 | 实现一个string utils类 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | CMakeCache.txt 3 | CMakeFiles/ 4 | Makefile 5 | cmake_install.cmake 6 | -------------------------------------------------------------------------------- /Connect/README.md: -------------------------------------------------------------------------------- 1 | Connect sample 2 | =================== 3 | 4 | 该例程讲解用C++11来实现Qt信号槽机制 5 | 6 | -------------------------------------------------------------------------------- /Ioc/README.md: -------------------------------------------------------------------------------- 1 | IocContainer sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个IoC容器 5 | 6 | -------------------------------------------------------------------------------- /LinqCpp/README.md: -------------------------------------------------------------------------------- 1 | LinqCpp sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个C#的linq库 5 | 6 | -------------------------------------------------------------------------------- /Timer/README.md: -------------------------------------------------------------------------------- 1 | Timer sample 2 | =================== 3 | 4 | 该例程讲解用C++11实现一个计时器Timer 5 | 6 | -------------------------------------------------------------------------------- /DateTime/README.md: -------------------------------------------------------------------------------- 1 | DateTime sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个DateTime类 5 | 6 | -------------------------------------------------------------------------------- /Optional/README.md: -------------------------------------------------------------------------------- 1 | Optional sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个Optional 5 | 6 | -------------------------------------------------------------------------------- /Signal/README.md: -------------------------------------------------------------------------------- 1 | Signal sample 2 | =================== 3 | 4 | 该例程讲解用C++11来封装Linux下的信号注册函数 5 | 6 | -------------------------------------------------------------------------------- /Singleton/README.md: -------------------------------------------------------------------------------- 1 | Singleton sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个通用的单列类 5 | 6 | -------------------------------------------------------------------------------- /Variant/README.md: -------------------------------------------------------------------------------- 1 | Variant sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个Variant容器 5 | 6 | -------------------------------------------------------------------------------- /thread_pool/README.md: -------------------------------------------------------------------------------- 1 | ThreadPool sample 2 | =================== 3 | 4 | 该例程讲解用C++11来实现一个通用线程池 5 | 6 | -------------------------------------------------------------------------------- /DllParser/README.md: -------------------------------------------------------------------------------- 1 | DllParser sample 2 | =================== 3 | 4 | 该例程讲解用C++11来实现一个通用的动态库解析器 5 | 6 | -------------------------------------------------------------------------------- /ObjectPool/README.md: -------------------------------------------------------------------------------- 1 | ObjectPool sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个ObjectPool 5 | 6 | -------------------------------------------------------------------------------- /PlacementNew/README.md: -------------------------------------------------------------------------------- 1 | PlacementNew sample 2 | =================== 3 | 4 | 该例程讲解placement new的用法 5 | 6 | -------------------------------------------------------------------------------- /ScopeGuard/README.md: -------------------------------------------------------------------------------- 1 | ScopeGuard sample 2 | =================== 3 | 4 | 该例程讲解用C++11来实现一个ScopeGuard类 5 | 6 | -------------------------------------------------------------------------------- /Traits/README.md: -------------------------------------------------------------------------------- 1 | Traits sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现C++标准库里面没有的类型的Traits 5 | 6 | -------------------------------------------------------------------------------- /lexical_cast/README.md: -------------------------------------------------------------------------------- 1 | lexical_cast sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个类型转换函数 5 | 6 | -------------------------------------------------------------------------------- /BindCPU/README.md: -------------------------------------------------------------------------------- 1 | BindCPU sample 2 | =================== 3 | 4 | 该例程讲解了进程、线程绑定到固定的cpu核心上运行,来提高程序运行效率 5 | 6 | -------------------------------------------------------------------------------- /FunctionTraits/README.md: -------------------------------------------------------------------------------- 1 | FunctionTraits sample 2 | =================== 3 | 4 | 该例程讲解用C++11实现一个函数萃取类 5 | 6 | -------------------------------------------------------------------------------- /TupleHelper/README.md: -------------------------------------------------------------------------------- 1 | TupleHelper sample 2 | =================== 3 | 4 | 该例程讲解C++11来实现一个TupleHelper 5 | 6 | -------------------------------------------------------------------------------- /DaemonAndWatchDog/README.md: -------------------------------------------------------------------------------- 1 | DaemonAndWatchDog sample 2 | =================== 3 | 4 | 该例程是守护进程和看门狗进程的编程实现 5 | 6 | -------------------------------------------------------------------------------- /SingleApplication/README.md: -------------------------------------------------------------------------------- 1 | SingleApplication sample 2 | =================== 3 | 4 | 该例程讲解Linux下程序只启动一个实例的编程实现 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | samples 2 | =================== 3 | 4 | 个人博客:http://chengxuan.me/ 5 | cnblogs:http://www.cnblogs.com/highway-9/ 6 | -------------------------------------------------------------------------------- /framework/net-thrift/RPCMessage.thrift: -------------------------------------------------------------------------------- 1 | service RPCMessageService 2 | { 3 | string sendMessage(1: string message); 4 | } 5 | -------------------------------------------------------------------------------- /DaemonAndRunSingleInstance/README.md: -------------------------------------------------------------------------------- 1 | DaemonAndRunSingleInstance sample 2 | =================== 3 | 4 | 该例程讲解Linux下一个守护进程只启动一个实例的编程实现 5 | 6 | -------------------------------------------------------------------------------- /framework/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.so 3 | *.a 4 | *.la 5 | *.o 6 | *.d 7 | *.log 8 | *.bundle 9 | *.cmake 10 | *.user 11 | *.pyc 12 | 13 | -------------------------------------------------------------------------------- /DllParser/Caculate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SHAREDLIBNAME cac) 2 | 3 | aux_source_directory(. DIR_SRCS) 4 | 5 | add_library(${SHAREDLIBNAME} SHARED ${DIR_SRCS}) 6 | 7 | -------------------------------------------------------------------------------- /framework/net-tcpserver/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/TcpServerTest) 2 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/TcpClientTest) 3 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/ServerTest) 2 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/ClientTest) 3 | 4 | 5 | -------------------------------------------------------------------------------- /event_loop/main.cpp: -------------------------------------------------------------------------------- 1 | #include "server.hpp" 2 | 3 | int main() 4 | { 5 | event_loop event; 6 | 7 | server s; 8 | s.start(); 9 | 10 | event.loop(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /WatchDog/README.md: -------------------------------------------------------------------------------- 1 | WatchDog sample 2 | =================== 3 | 4 | 该例程讲解了Linux下软件看门狗程序的优雅编写方法,若想了解一个带有看门狗功能的程序怎么在后台运行请看[DaemonAndWatchDog](https://github.com/chxuan/samples/tree/master/DaemonAndWatchDog) 5 | 6 | -------------------------------------------------------------------------------- /Factory/Message.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _MESSAGE_H 2 | #define _MESSAGE_H 3 | 4 | #include 5 | 6 | struct Message 7 | { 8 | virtual ~Message() {} 9 | virtual void func() = 0; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Variant/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Variant.hpp" 4 | 5 | int main() 6 | { 7 | std::cout << Contains::value << std::endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /framework/utils/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/String) 2 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/FileSystem) 3 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/System) 4 | 5 | 6 | -------------------------------------------------------------------------------- /DllParser/DllParser/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(DllParser) 2 | 3 | set(OUTPUTNAME DllParser) 4 | 5 | aux_source_directory(. DIR_SRCS) 6 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 7 | 8 | target_link_libraries(${OUTPUTNAME} dl) 9 | 10 | -------------------------------------------------------------------------------- /MD5/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "md5.h" 3 | 4 | int main() 5 | { 6 | std::string str = "123"; 7 | std::cout << md5(str) << std::endl; 8 | std::cout << md5("中国") << std::endl; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /DllParser/Caculate/Caculate.h: -------------------------------------------------------------------------------- 1 | #ifndef _CACULATE_H 2 | #define _CACULATE_H 3 | 4 | #include 5 | 6 | extern "C" 7 | { 8 | int add(int a, int b); 9 | int sub(int a, int b); 10 | void print(const std::string& str); 11 | } 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Any/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Any) 4 | 5 | set(OUTPUTNAME Any) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Aop/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Aop) 4 | 5 | set(OUTPUTNAME Aop) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Args/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Args.hpp" 4 | 5 | int main() 6 | { 7 | std::cout << Sum::value << std::endl; 8 | std::cout << typeid(MakeIndexes<3>::type).name() << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /DllParser/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(DllParser) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | 7 | add_subdirectory(${PROJECT_SOURCE_DIR}/Caculate) 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/DllParser) 9 | -------------------------------------------------------------------------------- /MD5/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Md5) 4 | 5 | set(OUTPUTNAME Md5) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Args/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Args) 4 | 5 | set(OUTPUTNAME Args) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Meta/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Meta) 4 | 5 | set(OUTPUTNAME Meta) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++14") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Base64/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Base64) 4 | 5 | set(OUTPUTNAME Base64) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Daemon/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Daemon) 4 | 5 | set(OUTPUTNAME Daemon) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Range/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Range) 4 | 5 | set(OUTPUTNAME Range) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Signal/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Signal) 4 | 5 | set(OUTPUTNAME Signal) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Timer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Timer) 4 | 5 | set(OUTPUTNAME Timer) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Traits/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Traits) 4 | 5 | set(OUTPUTNAME Traits) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /framework/threadpool/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(threadpool) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | 7 | aux_source_directory(. SRC_LIST) 8 | 9 | add_subdirectory(${PROJECT_SOURCE_DIR}/src) 10 | 11 | -------------------------------------------------------------------------------- /invoke/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(invoke) 4 | 5 | set(OUTPUTNAME invoke) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++14") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Connect/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Connect) 4 | 5 | set(OUTPUTNAME Connect) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /DateTime/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(DateTime) 4 | 5 | set(OUTPUTNAME DateTime) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Factory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Factory) 4 | 5 | set(OUTPUTNAME Factory) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Optional/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Optional) 4 | 5 | set(OUTPUTNAME Optional) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Variant/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Variant) 4 | 5 | set(OUTPUTNAME Variant) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /WatchDog/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(WatchDog) 4 | 5 | set(OUTPUTNAME WatchDog) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Ioc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(IocContainer) 4 | 5 | set(OUTPUTNAME IocContainer) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /NonCopyable/NonCopyable.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _NONCOPYABLE_H 2 | #define _NONCOPYABLE_H 3 | 4 | class NonCopyable 5 | { 6 | public: 7 | NonCopyable() = default; 8 | NonCopyable(const NonCopyable&) = delete; 9 | NonCopyable& operator=(const NonCopyable&) = delete; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /ObjectPool/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(ObjectPool) 4 | 5 | set(OUTPUTNAME ObjectPool) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /invoke/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "invoke.hpp" 3 | 4 | int add(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | 9 | int main() 10 | { 11 | std::tuple t = std::make_tuple(1, 2); 12 | std::cout << invoke(add, t) << std::endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /table_tuple/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(table_tuple) 4 | 5 | set(OUTPUTNAME table_tuple) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O3 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /PlacementNew/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(PlacementNew) 4 | 5 | set(OUTPUTNAME PlacementNew) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /TupleHelper/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(tuple_helper) 4 | 5 | set(OUTPUTNAME tuple_helper) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++14") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /lexical_cast/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(lexical_cast) 4 | 5 | set(OUTPUTNAME lexical_cast) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /FunctionTraits/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(FunctionTraits) 4 | 5 | set(OUTPUTNAME FunctionTraits) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /Singleton/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Singleton) 4 | 5 | set(OUTPUTNAME Singleton) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -fpermissive -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /singletion_v2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(singleton) 4 | 5 | set(OUTPUTNAME singleton) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -fpermissive -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /random_generator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(random_generator) 4 | 5 | set(OUTPUTNAME random_generator) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /DaemonAndWatchDog/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(DaemonAndWatchDog) 4 | 5 | set(OUTPUTNAME DaemonAndWatchDog) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /DllParser/Caculate/Caculate.cpp: -------------------------------------------------------------------------------- 1 | #include "Caculate.h" 2 | #include 3 | 4 | int add(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | 9 | int sub(int a, int b) 10 | { 11 | return a - b; 12 | } 13 | 14 | void print(const std::string& str) 15 | { 16 | std::cout << str << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /framework/clog/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Clog) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | #set(CMAKE_VERBOSE_MAKEFILE ON) 7 | 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/src) 9 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests) 10 | 11 | 12 | -------------------------------------------------------------------------------- /String/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(String) 4 | 5 | set(OUTPUTNAME String) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-function -Wno-write-strings -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /framework/utils/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Utils) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | #set(CMAKE_VERBOSE_MAKEFILE ON) 7 | 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/src) 9 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests) 10 | 11 | 12 | -------------------------------------------------------------------------------- /framework/net-thrift/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Net-thrift) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | #set(CMAKE_VERBOSE_MAKEFILE ON) 7 | 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/src) 9 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests) 10 | 11 | 12 | -------------------------------------------------------------------------------- /SingleApplication/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(SingleApplication) 4 | 5 | set(OUTPUTNAME SingleApplication) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-result -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /BindCPU/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(BindCPU) 4 | 5 | set(OUTPUTNAME BindCPU) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | 13 | -------------------------------------------------------------------------------- /framework/threadpool/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME ThreadPoolTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/include) 7 | 8 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 9 | 10 | target_link_libraries(${OUTPUTNAME} pthread) 11 | -------------------------------------------------------------------------------- /thread_pool/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(thread_pool) 4 | 5 | set(OUTPUTNAME thread_pool) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O0 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/CenterNetServerTest) 2 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/DBServerTest) 3 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/ClientTest) 4 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/PublisherTest) 5 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests/SubscriberTest) 6 | -------------------------------------------------------------------------------- /task_thread/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(task_thread) 4 | 5 | set(OUTPUTNAME task_thread) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | 13 | -------------------------------------------------------------------------------- /thread_guard/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(thread_guard) 4 | 5 | set(OUTPUTNAME thread_guard) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | -------------------------------------------------------------------------------- /thread_guard/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "thread_guard.hpp" 3 | 4 | void func() 5 | { 6 | std::cout << "Hello std::thread" << std::endl; 7 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 8 | } 9 | 10 | int main() 11 | { 12 | std::thread t(func); 13 | thread_guard guard(t); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /cyclic_barrier/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(cyclic_barrier) 4 | 5 | set(OUTPUTNAME cyclic_barrier) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | -------------------------------------------------------------------------------- /event_loop/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(event_loop) 4 | 5 | set(OUTPUTNAME event_loop.out) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | 13 | -------------------------------------------------------------------------------- /message_buffer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(message_buffer) 4 | 5 | set(OUTPUTNAME message_buffer) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | -------------------------------------------------------------------------------- /shared_mutex/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(shared_mutex) 4 | 5 | set(OUTPUTNAME shared_mutex) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | 13 | -------------------------------------------------------------------------------- /DaemonAndRunSingleInstance/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(DaemonAndRunSingleInstance) 4 | 5 | set(OUTPUTNAME DaemonAndRunSingleInstance) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-result -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | -------------------------------------------------------------------------------- /count_down_latch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(count_down_latch) 4 | 5 | set(OUTPUTNAME count_down_latch) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | aux_source_directory(. DIR_SRCS) 9 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 10 | 11 | target_link_libraries(${OUTPUTNAME} pthread) 12 | -------------------------------------------------------------------------------- /framework/net-thrift/src/RPCMessage_types.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.9.3) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #include "RPCMessage_types.h" 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ScopeGuard/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(ScopeGuard) 4 | 5 | set(OUTPUTNAME ScopeGuard) 6 | #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++14") 8 | 9 | aux_source_directory(. DIR_SRCS) 10 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 11 | 12 | -------------------------------------------------------------------------------- /table_tuple/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "table_tuple.hpp" 4 | 5 | int main() 6 | { 7 | table_tuple test("id", "name", "address"); 8 | for (int i = 0; i < 1000000; ++i) 9 | { 10 | test.insert(1, "Jack", "ChengDu"); 11 | } 12 | /* test.print(); */ 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Lazy/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Lazy.hpp" 3 | 4 | int main() 5 | { 6 | Lazy lazyer = lazy([]{ return 10; }); 7 | std::cout << lazyer.value() << std::endl; 8 | 9 | std::function f = [](int i){ return i; }; 10 | auto lazyer2 = lazy(f, 100); 11 | std::cout << lazyer2.value() << std::endl; 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/MessageDef/RPCProtocolDefine.h: -------------------------------------------------------------------------------- 1 | #ifndef _RPCPROTOCOLDEFINE_H 2 | #define _RPCPROTOCOLDEFINE_H 3 | 4 | #include 5 | #include "PeopleInfoMessage.h" 6 | 7 | RCF_BEGIN(I_RPCMessageHandler, "I_RPCMessageHandler") 8 | RCF_METHOD_R2(bool, queryPeopleInfoByID, int, PeopleInfoMessage&) 9 | RCF_END(I_RPCMessageHandler) 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Lazy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Lazy) 4 | 5 | set(OUTPUTNAME Lazy) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories($ENV{BOOST_LIB_PATH}) 11 | 12 | aux_source_directory(. DIR_SRCS) 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | -------------------------------------------------------------------------------- /DateTime/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DateTime.hpp" 3 | 4 | int main() 5 | { 6 | std::string timeStr = DateTime::convert(time(nullptr)); 7 | std::cout << timeStr << std::endl; 8 | std::cout << DateTime::convert(timeStr) << std::endl; 9 | std::cout << DateTime::currentTime() << std::endl << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /LinqCpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(LinqCpp) 4 | 5 | set(OUTPUTNAME LinqCpp) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 7 | 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories($ENV{BOOST_LIB_PATH}) 11 | 12 | aux_source_directory(. DIR_SRCS) 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | -------------------------------------------------------------------------------- /framework/net-thrift/src/RPCMessage_constants.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.9.3) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #include "RPCMessage_constants.h" 8 | 9 | 10 | 11 | const RPCMessageConstants g_RPCMessage_constants; 12 | 13 | RPCMessageConstants::RPCMessageConstants() { 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /framework/utils/include/Utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file Utils.h 8 | * @brief 常用工具实现 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-28 12 | */ 13 | 14 | #ifndef _UTILS_H 15 | #define _UTILS_H 16 | 17 | #include "String.h" 18 | #include "FileSystem.h" 19 | #include "System.h" 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /framework/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Framework) 4 | 5 | add_subdirectory(${PROJECT_SOURCE_DIR}/utils) 6 | add_subdirectory(${PROJECT_SOURCE_DIR}/clog) 7 | add_subdirectory(${PROJECT_SOURCE_DIR}/net-thrift) 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/net-rcf) 9 | add_subdirectory(${PROJECT_SOURCE_DIR}/net-tcpserver) 10 | add_subdirectory(${PROJECT_SOURCE_DIR}/threadpool) 11 | -------------------------------------------------------------------------------- /task_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "task_thread.h" 3 | 4 | void print() 5 | { 6 | std::cout << "hello world" << std::endl; 7 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 8 | } 9 | 10 | int main() 11 | { 12 | auto t = std::make_shared(std::bind(print)); 13 | 14 | std::cin.get(); 15 | /* t->stop(); */ 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /framework/net-tcpserver/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(net-tcpserver) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2 -std=c++11") 6 | #set(CMAKE_VERBOSE_MAKEFILE ON) 7 | 8 | aux_source_directory(. SRC_LIST) 9 | #add_executable(${PROJECT_NAME} ${SRC_LIST}) 10 | 11 | add_subdirectory(${PROJECT_SOURCE_DIR}/src) 12 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests) 13 | 14 | -------------------------------------------------------------------------------- /format/fmt/ostr.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright(c) 2016 Gabi Melman. 3 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) 4 | // 5 | 6 | #pragma once 7 | // 8 | // include bundled or external copy of fmtlib's ostream support 9 | // 10 | #if !defined(SPDLOG_FMT_EXTERNAL) 11 | #ifndef FMT_HEADER_ONLY 12 | #define FMT_HEADER_ONLY 13 | #endif 14 | #include "bundled/ostream.h" 15 | #include "fmt.h" 16 | #else 17 | #include 18 | #endif 19 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/MessageDef/PubSubProtocolDefine.h: -------------------------------------------------------------------------------- 1 | #ifndef _PUBSUBPROTOCOLDEFINE_H 2 | #define _PUBSUBPROTOCOLDEFINE_H 3 | 4 | #include 5 | #include 6 | #include "WeatherInfoMessage.h" 7 | 8 | RCF_BEGIN(I_PubSubMessageHandler, "I_PubSubMessageHandler") 9 | RCF_METHOD_V1(void, pushWeather, const WeatherInfoMessage&) 10 | RCF_METHOD_V1(void, pushNews, const std::string&) 11 | RCF_END(I_PubSubMessageHandler) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Meta/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Meta.hpp" 5 | 6 | struct Person 7 | { 8 | std::string name; 9 | int age; 10 | 11 | META(name, age) 12 | }; 13 | 14 | template 15 | void func(T& t) 16 | { 17 | auto ret = t.meta(); 18 | std::cout << typeid(ret).name() << std::endl; 19 | } 20 | 21 | int main() 22 | { 23 | Person p {"Jack", 10}; 24 | func(p); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /PlacementNew/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A 5 | { 6 | A(int a, int b) 7 | { 8 | m_avg = (a + b) / 2; 9 | } 10 | 11 | int m_avg; 12 | }; 13 | 14 | using Aligned_A = std::aligned_storage::type; 15 | 16 | int main() 17 | { 18 | Aligned_A a, b; 19 | new (&a) A(2, 2); 20 | b = a; 21 | std::cout << reinterpret_cast(b).m_avg << std::endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/MessageDef/ProtocolDef.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: ProtocolDef.h 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年11月22日 星期日 22时01分28秒 6 | ************************************************************************/ 7 | 8 | #ifndef _PROTOCALDEF_H 9 | #define _PROTOCALDEF_H 10 | 11 | enum 12 | { 13 | PEOPLE_INFO_MSG = 1000 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /task_timer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(task_timer) 4 | 5 | set(OUTPUTNAME task_timer) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-variable -g -O2 -std=c++11") 7 | 8 | include_directories(/usr/local/include) 9 | 10 | link_directories(/usr/local/lib) 11 | 12 | aux_source_directory(. DIR_SRCS) 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} boost_system-mt) 16 | target_link_libraries(${OUTPUTNAME} pthread) 17 | -------------------------------------------------------------------------------- /Base64/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | *该例程讲解base64接口的调用 3 | ************************************************/ 4 | #include 5 | #include 6 | #include "Base64.hpp" 7 | 8 | int main() 9 | { 10 | char buf[20] = "Hello world!"; 11 | std::string base64Str = Base64::encode(buf, sizeof(buf)); 12 | std::cout << base64Str << std::endl; 13 | std::string buf2 = Base64::decode(base64Str); 14 | std::cout << buf2 << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Any/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Any.hpp" 4 | 5 | int main() 6 | { 7 | Any n; 8 | std::cout << n.isNull() << std::endl; 9 | std::string str = "Hello"; 10 | n = str; 11 | 12 | try 13 | { 14 | n.anyCast(); 15 | } 16 | catch (std::exception& e) 17 | { 18 | std::cout << "Exception: " << e.what() << std::endl; 19 | } 20 | 21 | Any n1 = 1; 22 | std::cout << n1.is() << std::endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /framework/net-thrift/src/RPCMessage_constants.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.9.3) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef RPCMessage_CONSTANTS_H 8 | #define RPCMessage_CONSTANTS_H 9 | 10 | #include "RPCMessage_types.h" 11 | 12 | 13 | 14 | class RPCMessageConstants { 15 | public: 16 | RPCMessageConstants(); 17 | 18 | }; 19 | 20 | extern const RPCMessageConstants g_RPCMessage_constants; 21 | 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /random_generator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "random_generator.h" 3 | 4 | int main() 5 | { 6 | for (int i = 1; i < 10; ++i) 7 | { 8 | random_generator random; 9 | std::cout << random.randint(1, 10) << std::endl; 10 | } 11 | 12 | std::cout << "---------------" << std::endl; 13 | for (int i = 1; i < 10; ++i) 14 | { 15 | random_generator random; 16 | std::cout << random.randreal(1, 2) << std::endl; 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /variant_visitor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(visitor) 4 | 5 | set(OUTPUTNAME visitor) 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-variable -g -O2 -std=c++14") 7 | 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories($ENV{BOOST_LIB_PATH}) 11 | 12 | aux_source_directory(. DIR_SRCS) 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} boost_system) 16 | #target_link_libraries(${OUTPUTNAME} pthread) 17 | -------------------------------------------------------------------------------- /Signal/Signal.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | namespace 7 | { 8 | std::unordered_map> signalMap; 9 | void handleSignal(int sig) 10 | { 11 | signalMap[sig](); 12 | } 13 | } 14 | 15 | class Signal 16 | { 17 | public: 18 | static void signal(int sig, const std::function& func) 19 | { 20 | signalMap.emplace(sig, func); 21 | assert(::signal(sig, handleSignal) != SIG_ERR); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /thread_guard/thread_guard.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_GUARD_H 2 | #define _THREAD_GUARD_H 3 | 4 | #include 5 | 6 | class thread_guard 7 | { 8 | public: 9 | thread_guard(const thread_guard&) = delete; 10 | thread_guard& operator=(const thread_guard&) = delete; 11 | explicit thread_guard(std::thread& t) : _t(t) {} 12 | ~thread_guard() 13 | { 14 | if (_t.joinable()) 15 | { 16 | _t.join(); 17 | } 18 | } 19 | 20 | private: 21 | std::thread& _t; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /singletion_v2/main.cpp: -------------------------------------------------------------------------------- 1 | #include "singletion.h" 2 | #include 3 | 4 | class test 5 | { 6 | public: 7 | void print() 8 | { 9 | std::cout << "hello world" << std::endl; 10 | } 11 | 12 | test() 13 | { 14 | std::cout << "test" << std::endl; 15 | } 16 | 17 | ~test() 18 | { 19 | std::cout << "~test" << std::endl; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | singletion::get_instance().print(); 26 | singletion::get_instance().print(); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /singletion_v2/singletion.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file singletion.h 3 | * @brief 单例模板 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2017-12-01 7 | */ 8 | #pragma once 9 | 10 | template 11 | class singletion 12 | { 13 | public: 14 | static T& get_instance() 15 | { 16 | // C++11保证静态变量的线程安全 17 | static T t; 18 | return t; 19 | } 20 | 21 | singletion() = delete; 22 | singletion(const singletion&) = delete; 23 | singletion& operator=(const singletion&) = delete; 24 | }; 25 | -------------------------------------------------------------------------------- /variant_visitor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "variant_visitor.hpp" 5 | 6 | int main() 7 | { 8 | auto visitor = make_visitor 9 | ( 10 | [](int& i) { std::cout << i << std::endl; }, 11 | [](std::string& i) { std::cout << i << std::endl; } 12 | ); 13 | boost::variant v; 14 | v = "Hello world"; 15 | boost::apply_visitor(visitor, v); 16 | v = 100; 17 | boost::apply_visitor(visitor, v); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /framework/net-thrift/src/RPCMessage_types.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.9.3) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef RPCMessage_TYPES_H 8 | #define RPCMessage_TYPES_H 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | 20 | 21 | 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /lexical_cast/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lexical_cast.h" 3 | 4 | int main() 5 | { 6 | std::string str = "1000"; 7 | int n = 200; 8 | std::cout << lexical_cast(str) << std::endl; 9 | std::cout << lexical_cast(n) << std::endl; 10 | std::cout << lexical_cast(n) << std::endl; 11 | std::cout << lexical_cast(-1) << std::endl; 12 | 13 | std::cout << lexical_cast(str) << std::endl; 14 | std::cout << lexical_cast("hello") << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/ClientTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME RCFClientTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/ServerClient") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{RCF_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{RCF_LIB_PATH}) 12 | 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} RcfDll) 16 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/DBServerTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME DBServerTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/ServerClient") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{RCF_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{RCF_LIB_PATH}) 12 | 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} RcfDll) 16 | -------------------------------------------------------------------------------- /Signal/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解用C++11来封装Linux下的信号注册函数 3 | ************************************************/ 4 | #include 5 | #include "Signal.hpp" 6 | 7 | class A 8 | { 9 | public: 10 | void handleExit() 11 | { 12 | std::cout << "Process exit..." << std::endl; 13 | exit(0); 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | std::cout << "Process start..." << std::endl; 20 | A a; 21 | Signal::signal(SIGINT, std::bind(&A::handleExit, &a)); 22 | std::cin.get(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/CenterNetServerTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME CenterNetServerTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/ServerClient") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{RCF_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{RCF_LIB_PATH}) 12 | 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} RcfDll) 16 | -------------------------------------------------------------------------------- /framework/utils/tests/String/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME StringTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/include) 7 | 8 | link_directories(${PROJECT_SOURCE_DIR}/lib) 9 | link_directories($ENV{UUID_LIB_PATH}) 10 | link_directories($ENV{OPENSSL_LIB_PATH}) 11 | 12 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 13 | 14 | target_link_libraries(${OUTPUTNAME} libutils_wrapper.a) 15 | target_link_libraries(${OUTPUTNAME} libuuid.a) 16 | target_link_libraries(${OUTPUTNAME} libcrypto.a) 17 | -------------------------------------------------------------------------------- /framework/utils/tests/System/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME SystemTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/include) 7 | 8 | link_directories(${PROJECT_SOURCE_DIR}/lib) 9 | link_directories($ENV{UUID_LIB_PATH}) 10 | link_directories($ENV{OPENSSL_LIB_PATH}) 11 | 12 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 13 | 14 | target_link_libraries(${OUTPUTNAME} libutils_wrapper.a) 15 | target_link_libraries(${OUTPUTNAME} libuuid.a) 16 | target_link_libraries(${OUTPUTNAME} libcrypto.a) 17 | -------------------------------------------------------------------------------- /framework/utils/tests/FileSystem/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME FileSystemTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/include) 7 | 8 | link_directories(${PROJECT_SOURCE_DIR}/lib) 9 | link_directories($ENV{UUID_LIB_PATH}) 10 | link_directories($ENV{OPENSSL_LIB_PATH}) 11 | 12 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 13 | 14 | target_link_libraries(${OUTPUTNAME} libutils_wrapper.a) 15 | target_link_libraries(${OUTPUTNAME} libuuid.a) 16 | target_link_libraries(${OUTPUTNAME} libcrypto.a) 17 | -------------------------------------------------------------------------------- /SingleApplication/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解Linux下程序只运行一个实例的编程实现 3 | * 4 | * 编写只运行一个实例的程序有很多种方式,比如通过管道 5 | * 共享内存、文件锁等,主要是要有一个全局flag标志该程序 6 | * 已经在运行了,本程序使用文件锁来实现单实例 7 | ************************************************/ 8 | #include "SingleApplication.hpp" 9 | 10 | int main() 11 | { 12 | if (SingleApplication::isRunning()) 13 | { 14 | printf("Process is already running\n"); 15 | return 1; 16 | } 17 | 18 | printf("Process start...\n"); 19 | sleep(5); 20 | printf("Process end...\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /framework/threadpool/src/CRealJob.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CRealJob.cpp 8 | * @brief 具体的任务类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #include "CRealJob.h" 15 | #include 16 | 17 | CRealJob::CRealJob(const OnDoTask& func, void* jobData) 18 | : CJob(), 19 | m_jobData(jobData) 20 | { 21 | assert(func); 22 | m_doTask = func; 23 | } 24 | 25 | void CRealJob::run() 26 | { 27 | if (m_doTask) 28 | { 29 | m_doTask(m_jobData); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ScopeGuard/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解用C++11来实现一个ScopeGuard类 3 | ************************************************/ 4 | #include "ScopeGuard.hpp" 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::function func = [] 11 | { 12 | std::cout << "Cleanup from unnormal exit" << std::endl; 13 | }; 14 | 15 | { 16 | // 正常退出 17 | auto ret = makeGuard(func); 18 | ret.dismiss(); 19 | } 20 | 21 | { 22 | // 非正常退出,调用func函数 23 | auto ret = makeGuard(func); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /cyclic_barrier/cyclic_barrier.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class cyclic_barrier 7 | { 8 | public: 9 | cyclic_barrier(int count) : count_(count) {} 10 | 11 | void await() 12 | { 13 | std::unique_lock lock(mutex_); 14 | 15 | --count_; 16 | 17 | while (count_ > 0) 18 | { 19 | cond_.wait(lock); 20 | } 21 | 22 | if (count_ <= 0) 23 | { 24 | cond_.notify_all(); 25 | } 26 | } 27 | 28 | private: 29 | std::mutex mutex_; 30 | std::condition_variable cond_; 31 | int count_; 32 | }; 33 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/MessageDef/WeatherInfoMessage.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: WeatherInfoMessage.h 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月17日 星期日 09时28分11秒 6 | ************************************************************************/ 7 | 8 | #ifndef _WEATHERINFOMESSAGE_H 9 | #define _WEATHERINFOMESSAGE_H 10 | 11 | #include 12 | #include 13 | 14 | class WeatherInfoMessage 15 | { 16 | public: 17 | void serialize(SF::Archive& ar) 18 | { 19 | ar & m_weatherDescription; 20 | } 21 | 22 | std::string m_weatherDescription; 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /invoke/invoke.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _INVOKE_H 2 | #define _INVOKE_H 3 | 4 | #include 5 | #include 6 | 7 | template 8 | decltype(auto) invoke_impl(Function&& func, Tuple&& t, std::index_sequence) 9 | { 10 | return func(std::get(std::forward(t))...); 11 | } 12 | 13 | template 14 | decltype(auto) invoke(Function&& func, Tuple&& t) 15 | { 16 | constexpr auto size = std::tuple_size::type>::value; 17 | return invoke_impl(std::forward(func), std::forward(t), std::make_index_sequence{}); 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/MessageDef/PeopleInfoMessage.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: PeopleInfoMessage.h 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月10日 星期日 19时58分32秒 6 | ************************************************************************/ 7 | 8 | #ifndef _PEOPLEINFOMESSAGE_H 9 | #define _PEOPLEINFOMESSAGE_H 10 | 11 | #include 12 | #include 13 | 14 | class PeopleInfoMessage 15 | { 16 | public: 17 | void serialize(SF::Archive& ar) 18 | { 19 | ar & m_name & m_age; 20 | } 21 | 22 | std::string m_name; 23 | unsigned int m_age; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /framework/net-tcpserver/tests/TcpClientTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME TcpClientTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDefine) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories(${PROJECT_SOURCE_DIR}/lib) 11 | link_directories($ENV{BOOST_LIB_PATH}) 12 | 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} tcpserver_wrapper) 16 | target_link_libraries(${OUTPUTNAME} boost_serialization) 17 | target_link_libraries(${OUTPUTNAME} boost_system) 18 | -------------------------------------------------------------------------------- /framework/net-tcpserver/tests/TcpServerTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME TcpServerTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDefine) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories(${PROJECT_SOURCE_DIR}/lib) 11 | link_directories($ENV{BOOST_LIB_PATH}) 12 | 13 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 14 | 15 | target_link_libraries(${OUTPUTNAME} tcpserver_wrapper) 16 | target_link_libraries(${OUTPUTNAME} boost_serialization) 17 | target_link_libraries(${OUTPUTNAME} boost_system) 18 | -------------------------------------------------------------------------------- /task_thread/task_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "task_thread.h" 2 | 3 | task_thread::task_thread(const std::function& func) 4 | : func_(func) 5 | { 6 | thread_ = std::make_shared(std::bind(&task_thread::task_func, this)); 7 | } 8 | 9 | task_thread::~task_thread() 10 | { 11 | stop(); 12 | } 13 | 14 | void task_thread::stop() 15 | { 16 | active_ = false; 17 | if (thread_->joinable()) 18 | { 19 | thread_->join(); 20 | } 21 | } 22 | 23 | std::thread::id task_thread::get_thread_id() 24 | { 25 | return thread_->get_id(); 26 | } 27 | 28 | void task_thread::task_func() 29 | { 30 | while (active_) 31 | { 32 | func_(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/PublisherTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME PublisherTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/PubSub") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{RCF_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{RCF_LIB_PATH}) 12 | link_directories($ENV{BOOST_LIB_PATH}) 13 | 14 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 15 | 16 | target_link_libraries(${OUTPUTNAME} RcfDll) 17 | target_link_libraries(${OUTPUTNAME} boost_thread) 18 | target_link_libraries(${OUTPUTNAME} boost_system) 19 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/SubscriberTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME SubscriberTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/PubSub") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{RCF_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{RCF_LIB_PATH}) 12 | link_directories($ENV{BOOST_LIB_PATH}) 13 | 14 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 15 | 16 | target_link_libraries(${OUTPUTNAME} RcfDll) 17 | target_link_libraries(${OUTPUTNAME} boost_thread) 18 | target_link_libraries(${OUTPUTNAME} boost_system) 19 | -------------------------------------------------------------------------------- /ObjectPool/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ObjectPool.hpp" 3 | 4 | class A 5 | { 6 | public: 7 | void print() 8 | { 9 | std::cout << "Hello world" << std::endl; 10 | } 11 | }; 12 | 13 | int main() 14 | { 15 | ObjectPool pool; 16 | pool.add(std::unique_ptr(new A())); 17 | pool.add(std::unique_ptr(new A())); 18 | std::cout << "Object pool size: " << pool.size() << std::endl; 19 | 20 | { 21 | auto a = pool.get(); 22 | a->print(); 23 | pool.get(); 24 | std::cout << "Object pool size: " << pool.size() << std::endl; 25 | } 26 | 27 | std::cout << "Object pool size: " << pool.size() << std::endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Aop/AopTimer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _AOPTIMER_H 2 | #define _AOPTIMER_H 3 | 4 | #include 5 | #include 6 | 7 | class AopTimer 8 | { 9 | public: 10 | void before() 11 | { 12 | m_begin = std::chrono::high_resolution_clock::now(); 13 | } 14 | 15 | void after() 16 | { 17 | long long elapsed = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() 18 | - m_begin).count(); 19 | std::cout << "Used time: " << elapsed << std::endl; 20 | } 21 | 22 | private: 23 | std::chrono::time_point m_begin; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /framework/net-tcpserver/tests/MessageDefine/PeopleInfoMessage.h: -------------------------------------------------------------------------------- 1 | #ifndef _PEOPLEINFOMESSAGE_H 2 | #define _PEOPLEINFOMESSAGE_H 3 | 4 | #include "Message.h" 5 | 6 | class PeopleInfoMessage 7 | { 8 | public: 9 | PeopleInfoMessage() 10 | { 11 | m_age = 0; 12 | m_sex = 0; 13 | } 14 | 15 | template 16 | void serialize(Archive& ar, const unsigned int version) 17 | { 18 | (void)version; 19 | ar & m_name; 20 | ar & m_age; 21 | ar & m_sex; 22 | } 23 | 24 | TCP_MESSAGE_HEADER; 25 | 26 | std::string m_name; 27 | unsigned int m_age; 28 | unsigned int m_sex; 29 | }; 30 | 31 | using PeopleInfoMessagePtr = std::shared_ptr; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /subscribe_center/subscribe_center.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file subscribe_center.h 3 | * @brief 消息订阅中心 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2018-01-01 7 | */ 8 | #pragma once 9 | 10 | #include 11 | #include 12 | #include 13 | #include "lock_shared.h" 14 | 15 | class subscribe_center 16 | { 17 | public: 18 | void add_message(const std::string& message_name, const std::string& session_id); 19 | std::vector get_session_id(const std::string& message_name); 20 | void remove_message(const std::string& session_id); 21 | void clear(); 22 | 23 | private: 24 | std::unordered_multimap message_to_session_; 25 | shared_mutex mutex_; 26 | }; 27 | -------------------------------------------------------------------------------- /Timer/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | *该例程讲解用C++11实现一个计时器Timer 3 | ************************************************/ 4 | #include 5 | #include 6 | #include "Timer.hpp" 7 | 8 | int main() 9 | { 10 | Timer t; // 开始计时 11 | 12 | sleep(2); 13 | 14 | std::cout << "milliseconds: " << t.elapsed() << std::endl; 15 | std::cout << "microseconds: " << t.elapsedMicroseconds() << std::endl; 16 | std::cout << "nanoseconds: " << t.elapsedNanoSeconds() << std::endl; 17 | std::cout << "seconds: " << t.elapsedSeconds() << std::endl; 18 | std::cout << "minutes: " << t.elapsedMinutes() << std::endl; 19 | std::cout << "hours: " << t.elapsedHours() << std::endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /count_down_latch/count_down_latch.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class count_down_latch 7 | { 8 | public: 9 | count_down_latch(int count) : count_(count) {} 10 | 11 | void count_down() 12 | { 13 | std::unique_lock lock(mutex_); 14 | 15 | --count_; 16 | if (count_ <= 0) 17 | { 18 | cond_.notify_one(); 19 | } 20 | } 21 | 22 | void await() 23 | { 24 | std::unique_lock lock(mutex_); 25 | 26 | while (count_ > 0) 27 | { 28 | cond_.wait(lock); 29 | } 30 | } 31 | 32 | private: 33 | std::mutex mutex_; 34 | std::condition_variable cond_; 35 | int count_; 36 | }; 37 | -------------------------------------------------------------------------------- /framework/clog/tests/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年12月07日 星期一 21时10分35秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include "LogWrapper.h" 11 | 12 | int main() 13 | { 14 | int a = 1; 15 | int b = 2; 16 | std::string str = "Hello world"; 17 | 18 | // C风格 19 | logError("Error log"); 20 | logWarn("Warn log"); 21 | logInfo("a + b = %d", a + b); 22 | 23 | // C++风格 24 | logError() << "Hello world"; 25 | logWarn() << "Hello world"; 26 | logDebug() << "message: " << str; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /format/fmt/fmt.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright(c) 2016 Gabi Melman. 3 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) 4 | // 5 | 6 | #pragma once 7 | 8 | // 9 | // Include a bundled header-only copy of fmtlib or an external one. 10 | // By default spdlog include its own copy. 11 | // 12 | 13 | #if !defined(SPDLOG_FMT_EXTERNAL) 14 | 15 | #ifndef FMT_HEADER_ONLY 16 | #define FMT_HEADER_ONLY 17 | #endif 18 | #ifndef FMT_USE_WINDOWS_H 19 | #define FMT_USE_WINDOWS_H 0 20 | #endif 21 | #include "bundled/format.h" 22 | #if defined(SPDLOG_FMT_PRINTF) 23 | #include "bundled/printf.h" 24 | #endif 25 | 26 | #else // external fmtlib 27 | 28 | #include 29 | #if defined(SPDLOG_FMT_PRINTF) 30 | #include 31 | #endif 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /cyclic_barrier/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "cyclic_barrier.hpp" 4 | 5 | cyclic_barrier barrier(3); 6 | 7 | void worker_thread(const std::string& name) 8 | { 9 | std::cout << name << "正在打桩,毕竟不轻松..." << std::endl; 10 | 11 | std::this_thread::sleep_for(std::chrono::seconds(3)); 12 | 13 | std::cout << name << "不容易,终于把桩打完了..." << std::endl; 14 | 15 | barrier.await(); 16 | 17 | std::cout << name << ":其他逗b把桩都打完了,又得忙活了..." << std::endl; 18 | } 19 | 20 | int main() 21 | { 22 | std::thread w1([]{ worker_thread("张三"); }); 23 | std::thread w2([]{ worker_thread("李四"); }); 24 | std::thread w3([]{ worker_thread("王五"); }); 25 | 26 | w1.join(); 27 | w2.join(); 28 | w3.join(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/ClientTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME ThriftClientTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories(${PROJECT_SOURCE_DIR}/lib) 11 | link_directories($ENV{THRIFT_LIB_PATH}) 12 | link_directories($ENV{BOOST_LIB_PATH}) 13 | 14 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 15 | 16 | target_link_libraries(${OUTPUTNAME} libthrift_wrapper.a) 17 | target_link_libraries(${OUTPUTNAME} libthrift.a) 18 | target_link_libraries(${OUTPUTNAME} libboost_serialization.a) 19 | target_link_libraries(${OUTPUTNAME} libpthread.so) 20 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/ServerTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME ThriftServerTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/tests/MessageDef) 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{BOOST_INCLUDE_PATH}) 9 | 10 | link_directories(${PROJECT_SOURCE_DIR}/lib) 11 | link_directories($ENV{THRIFT_LIB_PATH}) 12 | link_directories($ENV{BOOST_LIB_PATH}) 13 | 14 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 15 | 16 | target_link_libraries(${OUTPUTNAME} libthrift_wrapper.a) 17 | target_link_libraries(${OUTPUTNAME} libthrift.a) 18 | target_link_libraries(${OUTPUTNAME} libboost_serialization.a) 19 | target_link_libraries(${OUTPUTNAME} libpthread.so) 20 | -------------------------------------------------------------------------------- /task_thread/task_thread.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file task_thread.h 3 | * @brief 任务线程对象 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2018-01-02 7 | */ 8 | #pragma once 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | class task_thread 16 | { 17 | public: 18 | task_thread(const std::function& func); 19 | ~task_thread(); 20 | 21 | void stop(); 22 | std::thread::id get_thread_id(); 23 | 24 | private: 25 | void task_func(); 26 | task_thread(const task_thread&) = delete; 27 | task_thread& operator=(const task_thread&) = delete; 28 | 29 | private: 30 | std::function func_; 31 | std::shared_ptr thread_; 32 | std::atomic active_{ true }; 33 | }; 34 | -------------------------------------------------------------------------------- /Optional/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Optional.hpp" 4 | 5 | struct A 6 | { 7 | A(int a, int b) : m_a(a), m_b(b) {} 8 | 9 | int m_a; 10 | int m_b; 11 | 12 | void print() 13 | { 14 | std::cout << "Hello world" << std::endl; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | Optional a("ok"); 21 | if (a) 22 | { 23 | std::cout << *a << std::endl; 24 | } 25 | 26 | Optional b("ok"); 27 | std::cout << (a == b) << std::endl; 28 | 29 | Optional op; 30 | op.emplace(1, 2); 31 | A op2(2, 3); 32 | if (op) 33 | { 34 | op2 = *op; 35 | std::cout << op2.m_a << ", " << op2.m_b << std::endl; 36 | op->print(); 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Daemon/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解Linux守护进程的编程方法 3 | ************************************************/ 4 | #include "Daemon.hpp" 5 | 6 | int main() 7 | { 8 | // 启动守护进程 9 | bool ret = Daemon::start(); 10 | if (!ret) 11 | { 12 | printf("Init daemon failed\n"); 13 | return 1; 14 | } 15 | 16 | FILE* file = NULL; 17 | time_t t = 0; 18 | 19 | // 每隔10秒向test.log报告运行状态 20 | while (true) 21 | { 22 | sleep(10); 23 | file = fopen("./var/test.log", "a+"); 24 | if (file != NULL) 25 | { 26 | t = time(NULL); 27 | fprintf(file, "I am here at %s\n", asctime(localtime(&t))); 28 | fclose(file); 29 | } 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /framework/net-tcpserver/src/CRealJob.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CRealJob.cpp 8 | * @brief 具体的任务类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #include "CRealJob.h" 15 | #include 16 | 17 | CRealJob::CRealJob(const OnReciveMessage& func, MessagePtr message, const std::string& remoteAddress) 18 | : CJob(), 19 | m_message(message), 20 | m_remoteAddress(remoteAddress) 21 | { 22 | assert(func); 23 | m_onReciveMessage = func; 24 | } 25 | 26 | CRealJob::~CRealJob() 27 | { 28 | // Do nothing 29 | } 30 | 31 | void CRealJob::run() 32 | { 33 | if (m_onReciveMessage != nullptr) 34 | { 35 | m_onReciveMessage(m_message, m_remoteAddress); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /message_buffer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "message_buffer.hpp" 5 | 6 | message_buffer buffer(500); 7 | std::atomic is_stop{ false }; 8 | 9 | void test1() 10 | { 11 | for (int i = 1; i < 10000; ++i) 12 | { 13 | buffer.put(i); 14 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); 15 | } 16 | } 17 | 18 | void test2() 19 | { 20 | while (!is_stop) 21 | { 22 | std::list result = buffer.get(); 23 | std::cout << "size: " << result.size() << ", " << result.back() << std::endl; 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | std::thread t1(test1); 30 | std::thread t2(test2); 31 | t1.join(); 32 | is_stop = true; 33 | buffer.notify_one(); 34 | t2.join(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /framework/threadpool/include/CRealJob.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CRealJob.h 8 | * @brief 具体的任务类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #ifndef _CREALJOB_H 15 | #define _CREALJOB_H 16 | 17 | #include "CJob.h" 18 | #include 19 | 20 | using OnDoTask = std::function; 21 | 22 | /** 23 | * @brief 任务类,继承自CJob 24 | */ 25 | class CRealJob : public CJob 26 | { 27 | public: 28 | CRealJob(const OnDoTask& func, void* jobData); 29 | 30 | /** 31 | * @brief run 执行任务 32 | */ 33 | virtual void run(); 34 | 35 | private: 36 | OnDoTask m_doTask = nullptr; 37 | void* m_jobData = nullptr; 38 | }; 39 | 40 | using CRealJobPtr = std::shared_ptr; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /framework/clog/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(OUTPUTNAME LogTest) 2 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") 3 | 4 | aux_source_directory(. DIR_SRCS) 5 | 6 | include_directories(${PROJECT_SOURCE_DIR}/include) 7 | 8 | link_directories(${PROJECT_SOURCE_DIR}/lib) 9 | link_directories(${PROJECT_SOURCE_DIR}/../utils/lib) 10 | link_directories($ENV{LOG4CPP_LIB_PATH}) 11 | link_directories($ENV{UUID_LIB_PATH}) 12 | link_directories($ENV{OPENSSL_LIB_PATH}) 13 | 14 | add_executable(${OUTPUTNAME} ${DIR_SRCS}) 15 | 16 | target_link_libraries(${OUTPUTNAME} liblog_wrapper.a) 17 | target_link_libraries(${OUTPUTNAME} liblog4cpp.a) 18 | target_link_libraries(${OUTPUTNAME} libutils_wrapper.a) 19 | target_link_libraries(${OUTPUTNAME} libuuid.a) 20 | target_link_libraries(${OUTPUTNAME} libcrypto.a) 21 | target_link_libraries(${OUTPUTNAME} libpthread.so) 22 | -------------------------------------------------------------------------------- /Singleton/Singleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _SINGLETON_H 2 | #define _SINGLETON_H 3 | 4 | #include 5 | 6 | template 7 | class Singleton 8 | { 9 | public: 10 | Singleton() = delete; 11 | virtual ~Singleton() = delete; 12 | Singleton(const Singleton&) = delete; 13 | Singleton& operator=(const Singleton&) = delete; 14 | 15 | template 16 | static T& get_instance(Args&&... args) 17 | { 18 | static T t{ std::forward(args)... }; 19 | return t; 20 | } 21 | }; 22 | 23 | #define DEFINE_SINGLETON(class_name) \ 24 | public: \ 25 | friend class Singleton; \ 26 | using singleton = Singleton; \ 27 | private: \ 28 | virtual ~class_name() {} \ 29 | class_name(const class_name&) = delete; \ 30 | class_name& operator=(const class_name&) = delete; \ 31 | public: 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /format/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(format) 3 | 4 | # 程序名 5 | set(OUT_NAME test) 6 | 7 | # debug或release模式 8 | if(CMAKE_BUILD_TYPE MATCHES "debug") 9 | message("Build debug...") 10 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O0 -std=c++14") 11 | else() 12 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O3 -std=c++14") 13 | endif() 14 | 15 | # 遍历主目录源文件,删除cmake自动生成的main源文件 16 | file(GLOB_RECURSE SOURCE_FILES "*.cpp") 17 | file(GLOB_RECURSE DEBUG_REMOVE_FILE "debug/*") 18 | file(GLOB_RECURSE RELEASE_REMOVE_FILE "release/*") 19 | list(REMOVE_ITEM SOURCE_FILES ${DEBUG_REMOVE_FILE} ${RELEASE_REMOVE_FILE}) 20 | 21 | # 依赖包含 22 | include_directories(./) 23 | 24 | # 可执行程序 25 | add_executable(${OUT_NAME} ${SOURCE_FILES}) 26 | 27 | # 连接库 28 | set(LINK_LIST pthread) 29 | target_link_libraries(${OUT_NAME} ${LINK_LIST}) 30 | -------------------------------------------------------------------------------- /event_loop/event_loop.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | class event_loop 8 | { 9 | public: 10 | event_loop() 11 | { 12 | set_sigint_handle(); 13 | } 14 | 15 | void loop() 16 | { 17 | sleep(-1); 18 | } 19 | 20 | static bool is_active() 21 | { 22 | return active_; 23 | } 24 | 25 | private: 26 | static void sigint_handle(int sig) 27 | { 28 | std::cout << "end, sig: " << sig << std::endl; 29 | active_ = false; 30 | } 31 | 32 | void set_sigint_handle() 33 | { 34 | struct sigaction act; 35 | act.sa_handler = sigint_handle; 36 | act.sa_flags = SA_INTERRUPT; 37 | 38 | sigaction(SIGINT, &act, nullptr); 39 | } 40 | 41 | private: 42 | static bool active_; 43 | }; 44 | 45 | bool event_loop::active_ = true; 46 | -------------------------------------------------------------------------------- /format/fmt/bundled/printf.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Formatting library for C++ 3 | 4 | Copyright (c) 2012 - 2016, Victor Zverovich 5 | All rights reserved. 6 | 7 | For the license information refer to format.h. 8 | */ 9 | 10 | #include "format.h" 11 | #include "printf.h" 12 | 13 | namespace fmt { 14 | 15 | template 16 | void printf(BasicWriter &w, BasicCStringRef format, ArgList args); 17 | 18 | FMT_FUNC int fprintf(std::FILE *f, CStringRef format, ArgList args) { 19 | MemoryWriter w; 20 | printf(w, format, args); 21 | std::size_t size = w.size(); 22 | return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast(size); 23 | } 24 | 25 | #ifndef FMT_HEADER_ONLY 26 | 27 | template void PrintfFormatter::format(CStringRef format); 28 | template void PrintfFormatter::format(WCStringRef format); 29 | 30 | #endif // FMT_HEADER_ONLY 31 | 32 | } // namespace fmt 33 | -------------------------------------------------------------------------------- /Factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Message.hpp" 4 | #include "Factory.hpp" 5 | 6 | struct PersonMessage : public Message 7 | { 8 | PersonMessage() {} 9 | PersonMessage(int id, const std::string& name, const std::string& address) 10 | : m_id(id), m_name(name), m_address(address) {} 11 | 12 | void func() override 13 | { 14 | std::cout << "id: " << m_id << std::endl; 15 | std::cout << "name: " << m_name << std::endl; 16 | std::cout << "address: " << m_address << std::endl; 17 | } 18 | 19 | private: 20 | int m_id = 0; 21 | std::string m_name; 22 | std::string m_address; 23 | }; 24 | 25 | int main() 26 | { 27 | const int messageType = 1000; 28 | Factory::registerMessage(messageType, 100, "Jack", "Chengdu"); 29 | auto p = Factory::getUnique(messageType); 30 | p->func(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Lazy/Lazy.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _LAZY_H 2 | #define _LAZY_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | class Lazy 10 | { 11 | public: 12 | template 13 | Lazy(Function&& func, Args&&... args) 14 | { 15 | m_func = [&func, &args...]{ return func(std::forward(args)...); }; 16 | } 17 | 18 | R& value() 19 | { 20 | m_value = m_func(); 21 | return *m_value; 22 | } 23 | 24 | private: 25 | std::function m_func; 26 | boost::optional m_value; 27 | }; 28 | 29 | template 30 | Lazy::type> lazy(Function&& func, Args&&... args) 31 | { 32 | return Lazy::type>(std::forward(func), std::forward(args)...); 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /random_generator/random_generator.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file random_generator.h 3 | * @brief 随机数生成器 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2018-01-05 7 | */ 8 | #pragma once 9 | 10 | #include 11 | #include 12 | 13 | class random_generator 14 | { 15 | public: 16 | random_generator() 17 | { 18 | engine_.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count()); 19 | } 20 | 21 | // 生成int类型随机数,返回范围[min, max] 22 | int randint(int min, int max) 23 | { 24 | std::uniform_int_distribution u(min, max); 25 | return u(engine_); 26 | } 27 | 28 | // 生成double类型随机数,返回范围[min, max] 29 | double randreal(double min, double max) 30 | { 31 | std::uniform_real_distribution u(min, max); 32 | return u(engine_); 33 | } 34 | 35 | private: 36 | std::default_random_engine engine_; 37 | }; 38 | -------------------------------------------------------------------------------- /event_loop/server.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "event_loop.hpp" 4 | #include 5 | #include 6 | 7 | class server 8 | { 9 | public: 10 | server() 11 | { 12 | std::cout << "server starting..." << std::endl; 13 | } 14 | 15 | ~server() 16 | { 17 | thread_->join(); 18 | std::cout << "server stoped..." << std::endl; 19 | } 20 | 21 | void start() 22 | { 23 | thread_ = std::make_shared(std::bind(&server::task_thread, this)); 24 | } 25 | 26 | private: 27 | void task_thread() 28 | { 29 | while (event_loop::is_active()) 30 | { 31 | std::cout << "runing..." << std::endl; 32 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 33 | } 34 | std::cout << "thread end..." << std::endl; 35 | } 36 | 37 | private: 38 | std::shared_ptr thread_; 39 | }; 40 | -------------------------------------------------------------------------------- /count_down_latch/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "count_down_latch.hpp" 4 | 5 | count_down_latch latch(3); 6 | 7 | void worker_thread(const std::string& name) 8 | { 9 | std::cout << name << "正在干活..." << std::endl; 10 | 11 | std::this_thread::sleep_for(std::chrono::seconds(3)); 12 | latch.count_down(); 13 | 14 | std::cout << name << "干完活了!" << std::endl; 15 | } 16 | 17 | void boss_thread() 18 | { 19 | std::cout << "老板正在等所有的工人干完活..." << std::endl; 20 | 21 | latch.await(); 22 | 23 | std::cout << "工人活都干完了,老板开始检查了!" << std::endl; 24 | } 25 | 26 | int main() 27 | { 28 | std::thread b(boss_thread); 29 | 30 | std::thread w1([]{ worker_thread("张三"); }); 31 | std::thread w2([]{ worker_thread("李四"); }); 32 | std::thread w3([]{ worker_thread("王五"); }); 33 | 34 | b.join(); 35 | w1.join(); 36 | w2.join(); 37 | w3.join(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/MessageDef/MessageDefHead.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MessageDefHead.h 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年11月22日 星期日 21时21分19秒 6 | ************************************************************************/ 7 | 8 | #ifndef _MESSAGEDEFHEAD_H 9 | #define _MESSAGEDEFHEAD_H 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /framework/threadpool/include/CJob.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CJob.h 8 | * @brief 任务父类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #ifndef _CJOB_H 15 | #define _CJOB_H 16 | 17 | #include 18 | #include 19 | 20 | /** 21 | * @brief 任务类 22 | */ 23 | class CJob 24 | { 25 | public: 26 | virtual ~CJob() = default; 27 | 28 | /** 29 | * @brief run 执行任务 30 | */ 31 | virtual void run() = 0; 32 | 33 | public: 34 | int jobNo() const { return m_jobNo; } 35 | void setJobNo(int jobNo) { m_jobNo = jobNo; } 36 | 37 | std::string jobName() const { return m_jobName; } 38 | void setJobName(const std::string& jobName) { m_jobName = jobName; } 39 | 40 | private: 41 | int m_jobNo = 0; 42 | std::string m_jobName; 43 | }; 44 | 45 | using CJobPtr = std::shared_ptr; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CJob.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CJob.h 8 | * @brief 任务父类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #ifndef _CJOB_H 15 | #define _CJOB_H 16 | 17 | #include 18 | #include 19 | 20 | /** 21 | * @brief 任务类 22 | */ 23 | class CJob 24 | { 25 | public: 26 | virtual ~CJob() = default; 27 | 28 | /** 29 | * @brief run 执行任务 30 | */ 31 | virtual void run() = 0; 32 | 33 | public: 34 | int jobNo() const { return m_jobNo; } 35 | void setJobNo(int jobNo) { m_jobNo = jobNo; } 36 | 37 | std::string jobName() const { return m_jobName; } 38 | void setJobName(const std::string& jobName) { m_jobName = jobName; } 39 | 40 | private: 41 | int m_jobNo = 0; 42 | std::string m_jobName; 43 | }; 44 | 45 | using CJobPtr = std::shared_ptr; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /task_timer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "task_timer.h" 3 | 4 | void test() 5 | { 6 | std::cout << "Timer thread id: " << std::this_thread::get_id() << std::endl; 7 | } 8 | 9 | int main() 10 | { 11 | std::cout << "Main thread id: " << std::this_thread::get_id() << std::endl; 12 | 13 | task_timer<> t0; 14 | t0.set_single_shot(true); 15 | t0.bind(test); 16 | t0.start(100); 17 | 18 | task_timer<> t; 19 | t.bind(test); 20 | t.bind([]{ std::cout << "Hello C++" << std::endl; }); 21 | t.start(1000); 22 | 23 | std::cin.get(); 24 | t.stop(); 25 | std::cout << "Timer stop" << std::endl; 26 | 27 | std::cin.get(); 28 | t.start(); 29 | std::cout << "Timer restart" << std::endl; 30 | 31 | std::cin.get(); 32 | t0.destroy(); 33 | t.destroy(); 34 | std::cout << "Timer destroy" << std::endl; 35 | 36 | std::cin.get(); 37 | std::cout << "Process end" << std::endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Singleton/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解用C++11来实现一个通用的单列类 3 | ************************************************/ 4 | #include 5 | #include 6 | #include "Singleton.hpp" 7 | 8 | class A 9 | { 10 | DEFINE_SINGLETON(A); 11 | public: 12 | A(const std::string& str, int a) 13 | : m_str(str), m_a(a) 14 | { 15 | // Do nothing 16 | } 17 | 18 | void print() 19 | { 20 | std::cout << m_str << std::endl; 21 | std::cout << m_a << std::endl; 22 | } 23 | 24 | std::string m_str = "nihao"; 25 | int m_a; 26 | }; 27 | 28 | class B 29 | { 30 | DEFINE_SINGLETON(B); 31 | public: 32 | B() = default; 33 | void print() 34 | { 35 | std::cout << "Hello world" << std::endl; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | std::string str = "Hello C++11"; 42 | A::singleton::get_instance(str, 1000).print(); 43 | B::singleton::get_instance().print(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/CenterNetServerTest/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月09日 星期六 20时26分44秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "CenterNetServer.h" 10 | 11 | int main() 12 | { 13 | CenterNetServer server; 14 | bool ok = server.start(); 15 | if (ok) 16 | { 17 | std::cout << "Center net server start..." << std::endl; 18 | } 19 | else 20 | { 21 | std::cout << "Center net server start failed" << std::endl; 22 | return -1; 23 | } 24 | 25 | std::cin.get(); 26 | 27 | ok = server.stop(); 28 | if (ok) 29 | { 30 | std::cout << "Center net server stoped..." << std::endl; 31 | } 32 | else 33 | { 34 | std::cout << "Center net server stoped failed" << std::endl; 35 | return -1; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /WatchDog/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解Linux软件看门狗的优雅编写方法 3 | * 4 | * 编写WatchDog有很多种方式: 5 | * a.一个WatchDog.sh脚本程序 6 | * b.一个WatchDog.exe可执行程序 7 | * c.一个可执行程序里面包含WatchDog 8 | * 9 | * 本例程就是使用的c方式,通过父进程监控子进程的运行状态来实现的 10 | * 其中父进程就是子进程(具体的任务进程)的WatchDog 11 | ************************************************/ 12 | #include "WatchDog.hpp" 13 | 14 | void childProcess() 15 | { 16 | int i = 0; 17 | while (true) 18 | { 19 | ++i; 20 | printf("i: %d, pid: %d, ppid: %d\n", i, getpid(), getppid()); 21 | if (i == 10) 22 | { 23 | // 子进程主动结束 24 | //exit(0); 25 | char* p = NULL; 26 | *p = 1; 27 | } 28 | sleep(1); 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | printf("Main pid: %d\n", getpid()); 35 | 36 | // 启动看门狗进程 37 | bool ret = WatchDog::start(childProcess); 38 | if (!ret) 39 | { 40 | printf("Init watch dog failed\n"); 41 | return 1; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CRealJob.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CRealJob.h 8 | * @brief 具体的任务类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-20 12 | */ 13 | 14 | #ifndef _CREALJOB_H 15 | #define _CREALJOB_H 16 | 17 | #include "CJob.h" 18 | #include 19 | 20 | class Message; 21 | using MessagePtr = std::shared_ptr; 22 | using OnReciveMessage = std::function; 23 | 24 | /** 25 | * @brief 任务类,继承自CJob 26 | */ 27 | class CRealJob : public CJob 28 | { 29 | public: 30 | CRealJob(const OnReciveMessage& func, MessagePtr message, const std::string& remoteAddress); 31 | virtual ~CRealJob(); 32 | 33 | /** 34 | * @brief run 执行任务 35 | */ 36 | virtual void run(); 37 | 38 | private: 39 | OnReciveMessage m_onReciveMessage = nullptr; 40 | MessagePtr m_message; 41 | std::string m_remoteAddress; 42 | }; 43 | 44 | using CRealJobPtr = std::shared_ptr; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /format/fmt/bundled/ostream.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Formatting library for C++ - std::ostream support 3 | 4 | Copyright (c) 2012 - 2016, Victor Zverovich 5 | All rights reserved. 6 | 7 | For the license information refer to format.h. 8 | */ 9 | 10 | #include "ostream.h" 11 | 12 | namespace fmt { 13 | 14 | namespace internal { 15 | FMT_FUNC void write(std::ostream &os, Writer &w) { 16 | const char *data = w.data(); 17 | typedef internal::MakeUnsigned::Type UnsignedStreamSize; 18 | UnsignedStreamSize size = w.size(); 19 | UnsignedStreamSize max_size = 20 | internal::to_unsigned((std::numeric_limits::max)()); 21 | do { 22 | UnsignedStreamSize n = size <= max_size ? size : max_size; 23 | os.write(data, static_cast(n)); 24 | data += n; 25 | size -= n; 26 | } while (size != 0); 27 | } 28 | } 29 | 30 | FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { 31 | MemoryWriter w; 32 | w.write(format_str, args); 33 | internal::write(os, w); 34 | } 35 | } // namespace fmt 36 | -------------------------------------------------------------------------------- /ScopeGuard/ScopeGuard.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _SCOPEGUARD_H 2 | #define _SCOPEGUARD_H 3 | 4 | #include 5 | 6 | template 7 | class ScopeGuard 8 | { 9 | public: 10 | ScopeGuard() = default; 11 | ScopeGuard(const ScopeGuard&) = delete; 12 | ScopeGuard& operator=(const ScopeGuard&) = delete; 13 | 14 | explicit ScopeGuard(T&& func) : m_func(std::move(func)) {} 15 | explicit ScopeGuard(const T& func) : m_func(func) {} 16 | 17 | ~ScopeGuard() 18 | { 19 | if (!m_dismiss) 20 | { 21 | m_func(); 22 | } 23 | } 24 | 25 | ScopeGuard(ScopeGuard&& other) 26 | : m_func(std::move(other.m_func)) 27 | { 28 | other.dismiss(); 29 | } 30 | 31 | void dismiss() 32 | { 33 | m_dismiss = true; 34 | } 35 | 36 | private: 37 | T m_func; 38 | bool m_dismiss = false; 39 | }; 40 | 41 | template 42 | ScopeGuard::type> makeGuard(T&& func) 43 | { 44 | return ScopeGuard::type>(std::forward(func)); 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /framework/net-tcpserver/src/TcpServerWrapper.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file TcpServerImpl.cpp 8 | * @brief tcp服务器包装类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-31 12 | */ 13 | 14 | #include "TcpServerWrapper.h" 15 | 16 | TcpServerWrapper::TcpServerWrapper(unsigned short port) 17 | { 18 | m_impl.reset(); 19 | if (m_impl.use_count() == 0) 20 | { 21 | m_impl = std::make_shared(port); 22 | } 23 | } 24 | 25 | bool TcpServerWrapper::start() 26 | { 27 | assert(m_impl.use_count() != 0); 28 | return m_impl->start(); 29 | } 30 | 31 | bool TcpServerWrapper::stop() 32 | { 33 | assert(m_impl.use_count() != 0); 34 | return m_impl->stop(); 35 | } 36 | 37 | void TcpServerWrapper::setThreadPoolNum(unsigned int num) 38 | { 39 | assert(m_impl.use_count() != 0); 40 | m_impl->setThreadPoolNum(num); 41 | } 42 | 43 | void TcpServerWrapper::setServerParam(const ServerParam ¶m) 44 | { 45 | assert(m_impl.use_count() != 0); 46 | m_impl->setServerParam(param); 47 | } 48 | -------------------------------------------------------------------------------- /framework/threadpool/src/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年02月17日 星期三 22时01分17秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "CThreadManage.h" 13 | #include "CRealJob.h" 14 | 15 | void doTask(void* jobData) 16 | { 17 | (void)jobData; 18 | std::cout << "Hello world" << std::endl; 19 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); 20 | } 21 | 22 | int main() 23 | { 24 | CThreadManagePtr manage(new CThreadManage); 25 | manage->initThreadNum(10); 26 | 27 | for (int i = 0; i < 100; ++i) 28 | { 29 | CRealJobPtr job(new CRealJob(std::bind(doTask, std::placeholders::_1), nullptr)); 30 | manage->run(job); 31 | } 32 | 33 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 34 | std::cout << "##############END###################" << std::endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/Message.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file Message.h 8 | * @brief 用于接收tcp消息 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-03 12 | */ 13 | 14 | #ifndef _MESSAGE_H 15 | #define _MESSAGE_H 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #define TCP_MESSAGE_HEADER unsigned int m_messageType 31 | 32 | /** 33 | * @brief 用于tcp服务器接收的消息结构 34 | */ 35 | class Message 36 | { 37 | public: 38 | TCP_MESSAGE_HEADER; 39 | 40 | std::string m_data; 41 | }; 42 | 43 | using MessagePtr = std::shared_ptr; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /framework/net-tcpserver/src/TcpClientWrapper.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file TcpClientImpl.h 8 | * @brief tcp客户端包装类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-31 12 | */ 13 | 14 | #include "TcpClientWrapper.h" 15 | 16 | TcpClientWrapper::TcpClientWrapper(const std::string &ip, unsigned short port) 17 | { 18 | m_impl.reset(); 19 | if (m_impl.use_count() == 0) 20 | { 21 | m_impl = std::make_shared(ip, port); 22 | } 23 | } 24 | 25 | bool TcpClientWrapper::start() 26 | { 27 | assert(m_impl.use_count() != 0); 28 | return m_impl->start(); 29 | } 30 | 31 | bool TcpClientWrapper::stop() 32 | { 33 | assert(m_impl.use_count() != 0); 34 | return m_impl->stop(); 35 | } 36 | 37 | void TcpClientWrapper::setThreadPoolNum(unsigned int num) 38 | { 39 | assert(m_impl.use_count() != 0); 40 | m_impl->setThreadPoolNum(num); 41 | } 42 | 43 | void TcpClientWrapper::setClientParam(const ClientParam ¶m) 44 | { 45 | assert(m_impl.use_count() != 0); 46 | m_impl->setClientParam(param); 47 | } 48 | -------------------------------------------------------------------------------- /framework/net-tcpserver/src/CThreadManage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadManage.cpp 8 | * @brief 线程池管理类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #include "CThreadManage.h" 15 | #include "CThreadPool.h" 16 | #include "CJob.h" 17 | 18 | CThreadManage::CThreadManage() 19 | { 20 | if (m_threadPool.use_count() == 0) 21 | { 22 | m_threadPool = std::make_shared(); 23 | } 24 | } 25 | 26 | CThreadManage::~CThreadManage() 27 | { 28 | assert(m_threadPool.use_count() != 0); 29 | m_threadPool->terminateAll(); 30 | } 31 | 32 | void CThreadManage::initThreadNum(unsigned int initNumOfThread) 33 | { 34 | assert(m_threadPool.use_count() != 0); 35 | m_threadPool->initThreadNum(initNumOfThread); 36 | } 37 | 38 | void CThreadManage::run(CJobPtr job) 39 | { 40 | assert(m_threadPool.use_count() != 0); 41 | m_threadPool->run(job); 42 | } 43 | 44 | void CThreadManage::terminateAll() 45 | { 46 | assert(m_threadPool.use_count() != 0); 47 | m_threadPool->terminateAll(); 48 | } 49 | -------------------------------------------------------------------------------- /framework/threadpool/src/CThreadManage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadManage.cpp 8 | * @brief 线程池管理类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #include "CThreadManage.h" 15 | #include "CThreadPool.h" 16 | #include "CJob.h" 17 | 18 | CThreadManage::CThreadManage() 19 | { 20 | if (m_threadPool.use_count() == 0) 21 | { 22 | m_threadPool = std::make_shared(); 23 | } 24 | } 25 | 26 | CThreadManage::~CThreadManage() 27 | { 28 | assert(m_threadPool.use_count() != 0); 29 | m_threadPool->terminateAll(); 30 | } 31 | 32 | void CThreadManage::initThreadNum(unsigned int initNumOfThread) 33 | { 34 | assert(m_threadPool.use_count() != 0); 35 | m_threadPool->initThreadNum(initNumOfThread); 36 | } 37 | 38 | void CThreadManage::run(CJobPtr job) 39 | { 40 | assert(m_threadPool.use_count() != 0); 41 | m_threadPool->run(job); 42 | } 43 | 44 | void CThreadManage::terminateAll() 45 | { 46 | assert(m_threadPool.use_count() != 0); 47 | m_threadPool->terminateAll(); 48 | } 49 | -------------------------------------------------------------------------------- /shared_mutex/main.cpp: -------------------------------------------------------------------------------- 1 | #include "shared_mutex.h" 2 | #include 3 | #include 4 | 5 | class ThreadSafeCounter 6 | { 7 | public: 8 | ThreadSafeCounter() = default; 9 | 10 | unsigned int get() 11 | { 12 | lock_shared lock(mutex_, true); 13 | return value_; 14 | } 15 | 16 | void increment() 17 | { 18 | lock_shared lock(mutex_); 19 | value_++; 20 | } 21 | 22 | void reset() 23 | { 24 | lock_shared lock(mutex_); 25 | value_ = 0; 26 | } 27 | 28 | private: 29 | shared_mutex mutex_; 30 | unsigned int value_ = 0; 31 | }; 32 | 33 | int main() 34 | { 35 | ThreadSafeCounter counter; 36 | 37 | auto increment_and_print = [&counter]() 38 | { 39 | for (int i = 0; i < 3; i++) { 40 | counter.increment(); 41 | std::cout << std::this_thread::get_id() << ' ' << counter.get() << '\n'; 42 | } 43 | }; 44 | 45 | std::thread thread1(increment_and_print); 46 | std::thread thread2(increment_and_print); 47 | 48 | thread1.join(); 49 | thread2.join(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 chxuan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /framework/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 chxuan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /String/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "StringUtil.hpp" 3 | #include "Format.hpp" 4 | 5 | int main() 6 | { 7 | std::string str = "Hello world"; 8 | std::cout << StringUtil::trimLeft(str, "Hello") << std::endl; 9 | std::cout << StringUtil::trimRight(str, "world") << std::endl; 10 | str = " nihao "; 11 | std::cout << StringUtil::trim(str) << std::endl; 12 | std::cout << StringUtil::toUpper(str) << std::endl; 13 | std::cout << StringUtil::toLower(StringUtil::toUpper(str)) << std::endl; 14 | str = "Hello world"; 15 | std::cout << StringUtil::startsWith(str, "Hello") << std::endl; 16 | std::cout << StringUtil::endsWith(str, "a") << std::endl; 17 | std::vector result = StringUtil::split(str, " "); 18 | for (auto& iter : result) 19 | { 20 | std::cout << iter << std::endl; 21 | } 22 | 23 | try 24 | { 25 | std::string str = "a = {0}, b = {1}"; 26 | std::cout << format(const_cast(str.c_str()), 10.23, 200) << std::endl; 27 | } 28 | catch (std::exception& e) 29 | { 30 | std::cout << e.what() << std::endl; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/ClientTest/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月10日 星期日 14时00分15秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "RPCProtocolDefine.h" 10 | #include "RCFClientWrapper.hpp" 11 | 12 | int main() 13 | { 14 | RCFClientWrapper client("127.0.0.1", 50001); 15 | 16 | try 17 | { 18 | PeopleInfoMessage peopleInfo; 19 | int id = 1000; 20 | 21 | bool ok = client.rcfClientObject()->queryPeopleInfoByID(id, peopleInfo); 22 | if (ok) 23 | { 24 | std::cout << "name: " << peopleInfo.m_name << std::endl; 25 | std::cout << "age: " << peopleInfo.m_age << std::endl; 26 | } 27 | else 28 | { 29 | std::cout << "queryPeopleInfoByID failed" << std::endl; 30 | } 31 | } 32 | catch (const RCF::Exception& e) 33 | { 34 | std::cout << "Error: " << e.getErrorString() << std::endl; 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /framework/clog/src/LogStream.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file LogStream.cpp 8 | * @brief 日志流格式化类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-23 12 | */ 13 | 14 | #include "LogStream.h" 15 | #include "LogImpl.h" 16 | 17 | LogStream::LogStream(const std::string& filePath, 18 | const std::string& function, 19 | unsigned long line, 20 | unsigned int priorityLevel) 21 | : m_priorityLevel(priorityLevel) 22 | { 23 | if (m_buffer.use_count() == 0) 24 | { 25 | m_buffer = std::make_shared(); 26 | } 27 | 28 | std::string strFilePath = filePath; 29 | int pos = strFilePath.find_last_of("/"); 30 | (*m_buffer) << strFilePath.substr(pos + 1) << " " << function << "(" << line << ") "; 31 | } 32 | 33 | LogStream::~LogStream() 34 | { 35 | assert(m_buffer.use_count() != 0); 36 | bool ok = LogImpl::getInstance()->logPrint(m_priorityLevel, m_buffer->str()); 37 | if (!ok) 38 | { 39 | std::cout << "Print log failed, log content: " << m_buffer->str() << std::endl; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /framework/threadpool/include/CThreadManage.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadManage.h 8 | * @brief 线程池管理类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREADMANAGE_H 15 | #define _CTHREADMANAGE_H 16 | 17 | #include 18 | 19 | class CThreadPool; 20 | class CJob; 21 | 22 | /** 23 | * @brief 线程池管理类,管理CThreadPool 24 | */ 25 | class CThreadManage 26 | { 27 | public: 28 | using CThreadPoolPtr = std::shared_ptr; 29 | using CJobPtr = std::shared_ptr; 30 | 31 | CThreadManage(); 32 | ~CThreadManage(); 33 | 34 | public: 35 | /** 36 | * @brief initThreadNum 初始化线程数量 37 | * 38 | * @param initNumOfThread 线程数量 39 | */ 40 | void initThreadNum(unsigned int initNumOfThread); 41 | 42 | /** 43 | * @brief run 执行具体的job 44 | * 45 | * @param job 具体的任务 46 | */ 47 | void run(CJobPtr job); 48 | 49 | /** 50 | * @brief terminateAll 终止全部的线程 51 | */ 52 | void terminateAll(); 53 | 54 | private: 55 | CThreadPoolPtr m_threadPool; 56 | }; 57 | 58 | using CThreadManagePtr = std::shared_ptr; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CThreadManage.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadManage.h 8 | * @brief 线程池管理类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREADMANAGE_H 15 | #define _CTHREADMANAGE_H 16 | 17 | #include 18 | 19 | class CThreadPool; 20 | class CJob; 21 | 22 | /** 23 | * @brief 线程池管理类,管理CThreadPool 24 | */ 25 | class CThreadManage 26 | { 27 | public: 28 | using CThreadPoolPtr = std::shared_ptr; 29 | using CJobPtr = std::shared_ptr; 30 | 31 | CThreadManage(); 32 | ~CThreadManage(); 33 | 34 | public: 35 | /** 36 | * @brief initThreadNum 初始化线程数量 37 | * 38 | * @param initNumOfThread 线程数量 39 | */ 40 | void initThreadNum(unsigned int initNumOfThread); 41 | 42 | /** 43 | * @brief run 执行具体的job 44 | * 45 | * @param job 具体的任务 46 | */ 47 | void run(CJobPtr job); 48 | 49 | /** 50 | * @brief terminateAll 终止全部的线程 51 | */ 52 | void terminateAll(); 53 | 54 | private: 55 | CThreadPoolPtr m_threadPool; 56 | }; 57 | 58 | using CThreadManagePtr = std::shared_ptr; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Aop/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | *该例程讲解用C++11实现AOP框架 3 | ************************************************/ 4 | #include 5 | #include 6 | #include "Aop.hpp" 7 | #include "AopTimer.hpp" 8 | 9 | class A 10 | { 11 | public: 12 | void before(int n) 13 | { 14 | std::cout << "before from A: " << n << std::endl; 15 | } 16 | 17 | void after(int n) 18 | { 19 | std::cout << "after from A: " << n << std::endl; 20 | } 21 | }; 22 | 23 | class B 24 | { 25 | public: 26 | void before(int n) 27 | { 28 | std::cout << "before from B: " << n << std::endl; 29 | } 30 | 31 | void after(int n) 32 | { 33 | std::cout << "after from B: " << n << std::endl; 34 | } 35 | }; 36 | 37 | class C 38 | { 39 | public: 40 | void before(int n) 41 | { 42 | std::cout << "before from C: " << n << std::endl; 43 | } 44 | 45 | }; 46 | 47 | void coreFunc(int n) 48 | { 49 | std::cout << "invoke coreFunc: " << n << std::endl; 50 | } 51 | 52 | void coreFunc2() 53 | { 54 | usleep(2000 * 1000); 55 | } 56 | 57 | int main() 58 | { 59 | aopInvoke(&coreFunc, 100); 60 | aopInvoke(&coreFunc2); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Connect/Connect.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _CONNECT_H 2 | #define _CONNECT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define emit 9 | #define slots 10 | #define signals public 11 | #define connect(sender, signal, slot) ((sender)->signal.bind(slot)) 12 | 13 | template 14 | class Slot 15 | { 16 | public: 17 | using OnFunc = std::function; 18 | 19 | Slot(const OnFunc& func) : m_func(func) {} 20 | 21 | void exec(Args&&... args) 22 | { 23 | m_func(std::forward(args)...); 24 | } 25 | 26 | private: 27 | OnFunc m_func = nullptr; 28 | }; 29 | 30 | template 31 | class Signal 32 | { 33 | public: 34 | using SlotPtr = std::shared_ptr>; 35 | using OnFunc = std::function; 36 | 37 | void bind(const OnFunc& func) 38 | { 39 | SlotPtr s = std::make_shared>(func); 40 | m_slotVec.emplace_back(s); 41 | } 42 | 43 | void operator()(Args&&... args) 44 | { 45 | for (auto& iter : m_slotVec) 46 | { 47 | iter->exec(std::forward(args)...); 48 | } 49 | } 50 | 51 | private: 52 | std::vector m_slotVec; 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /message_buffer/message_buffer.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | class message_buffer 9 | { 10 | public: 11 | message_buffer() = default; 12 | message_buffer(std::size_t buffer_size) : buffer_size_(buffer_size) {} 13 | 14 | void reset(std::size_t buffer_size) 15 | { 16 | buffer_size_ = buffer_size; 17 | } 18 | 19 | void notify_one() 20 | { 21 | std::unique_lock lock(mutex_); 22 | cond_.notify_one(); 23 | } 24 | 25 | void put(const T& message) 26 | { 27 | std::unique_lock lock(mutex_); 28 | 29 | buffer_.emplace_back(message); 30 | if (buffer_.size() >= buffer_size_) 31 | { 32 | cond_.notify_one(); 33 | } 34 | } 35 | 36 | std::list get() 37 | { 38 | std::unique_lock lock(mutex_); 39 | 40 | while (buffer_.empty()) 41 | { 42 | cond_.wait(lock); 43 | } 44 | 45 | return std::move(buffer_); 46 | } 47 | 48 | private: 49 | std::mutex mutex_; 50 | std::list buffer_; 51 | std::condition_variable cond_; 52 | std::size_t buffer_size_ = 1; 53 | }; 54 | -------------------------------------------------------------------------------- /shared_mutex/shared_mutex.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file shared_mutex.h 3 | * @brief 封装读写锁 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2018-01-05 7 | */ 8 | #pragma once 9 | 10 | #include 11 | 12 | class shared_mutex 13 | { 14 | public: 15 | shared_mutex() 16 | { 17 | pthread_rwlock_init(&mutex_, nullptr); 18 | } 19 | 20 | ~shared_mutex() 21 | { 22 | pthread_rwlock_destroy(&mutex_); 23 | } 24 | 25 | void lock_read() 26 | { 27 | pthread_rwlock_rdlock(&mutex_); 28 | } 29 | 30 | void lock_write() 31 | { 32 | pthread_rwlock_wrlock(&mutex_); 33 | } 34 | 35 | void unlock() 36 | { 37 | pthread_rwlock_unlock(&mutex_); 38 | } 39 | 40 | private: 41 | pthread_rwlock_t mutex_; 42 | }; 43 | 44 | class lock_shared 45 | { 46 | public: 47 | lock_shared(shared_mutex& mt, bool shared = false) 48 | : mutex_(mt) 49 | { 50 | if (shared) 51 | { 52 | mutex_.lock_read(); 53 | } 54 | else 55 | { 56 | mutex_.lock_write(); 57 | } 58 | } 59 | 60 | ~lock_shared() 61 | { 62 | mutex_.unlock(); 63 | } 64 | 65 | private: 66 | shared_mutex& mutex_; 67 | }; 68 | -------------------------------------------------------------------------------- /framework/net-rcf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Net-rcf) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs -g -O2") 6 | #set(CMAKE_VERBOSE_MAKEFILE ON) 7 | 8 | add_subdirectory(${PROJECT_SOURCE_DIR}/tests) 9 | 10 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFServerWrapper.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 11 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFServerImpl.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 12 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFClientWrapper.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 13 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFClientImpl.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 14 | 15 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFPublisherWrapper.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 16 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFPublisherImpl.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 17 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFSubscriberWrapper.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 18 | install(FILES ${PROJECT_SOURCE_DIR}/include/RCFSubscriberImpl.hpp DESTINATION /usr/local/framework/net-rcf/include/net-rcf) 19 | 20 | 21 | -------------------------------------------------------------------------------- /login_copy/py/lg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import os 5 | 6 | # 配置文件路径 7 | file_path="/usr/local/etc/ssh_ip.conf" 8 | 9 | # 读取文件 10 | def readlines(): 11 | results = [] 12 | f = open(file_path) 13 | lines = f.readlines() 14 | for line in lines: 15 | line = line.strip() 16 | if (len(line) != 0): 17 | results.append(line) 18 | 19 | f.close() 20 | return results 21 | 22 | 23 | # 打印IP地址列表 24 | def print_ip_address(ips): 25 | print '\033[1;32m%-10s %-20s %-10s %-15s %9s\033[0m' % ("ID", "IP", "PORT", "用户名", "备注") 26 | i = 0 27 | for line in ips: 28 | i = i + 1 29 | arr = line.split(",") 30 | print '%-10d %-20s %-10s %-15s %s' % (i, arr[0], arr[1], arr[2], arr[3]) 31 | 32 | 33 | # 选择IP地址 34 | def choice_ip_address(): 35 | i = input("登录到:") 36 | return i 37 | 38 | # 登录到服务器 39 | def login_to_server(ips, i): 40 | if (i >= 1 and i <= len(ips)): 41 | arr = ips[i - 1].split(",") 42 | os.system("ssh -p " + arr[1] + " " + arr[2] + "@" + arr[0]) 43 | else: 44 | print("无效的IP地址或用户名!") 45 | 46 | 47 | # main函数 48 | if __name__ == "__main__": 49 | ips = readlines() 50 | print_ip_address(ips) 51 | i = choice_ip_address() 52 | login_to_server(ips, i) 53 | -------------------------------------------------------------------------------- /qt_connect/qt_connect.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file qt_connect.h 3 | * @brief 仿Qt的信号槽机制,用于类之间的解耦 4 | * @author chxuan, 787280310@qq.com 5 | * @version 1.0.0 6 | * @date 2017-11-25 7 | */ 8 | #pragma once 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #define emit 15 | #define slots 16 | #define qt_connect(qt_sig, qt_slt) (qt_sig.bind(qt_slt)) 17 | 18 | template 19 | class qt_slot 20 | { 21 | public: 22 | using func_t = std::function; 23 | qt_slot(const func_t& func) : func_(func) {} 24 | 25 | void exec(const Args&... args) 26 | { 27 | func_(args...); 28 | } 29 | 30 | private: 31 | func_t func_; 32 | }; 33 | 34 | template 35 | class qt_signal 36 | { 37 | public: 38 | using slot_ptr = std::shared_ptr>; 39 | using func_t = std::function; 40 | 41 | void bind(const func_t& func) 42 | { 43 | auto s = std::make_shared>(func); 44 | slots_.emplace_back(s); 45 | } 46 | 47 | void operator()(const Args&... args) 48 | { 49 | for (auto& s : slots_) 50 | { 51 | s->exec(args...); 52 | } 53 | } 54 | 55 | private: 56 | std::vector slots_; 57 | }; 58 | 59 | -------------------------------------------------------------------------------- /framework/clog/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(STATICLIBNAME log_wrapper) 2 | set(SHAREDLIBNAME log_wrapper_so) 3 | set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") 4 | 5 | aux_source_directory(. DIR_SRCS) 6 | 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories(${PROJECT_SOURCE_DIR}/../utils/include) 9 | include_directories($ENV{LOG4CPP_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{LOG4CPP_LIB_PATH}) 12 | 13 | add_library(${STATICLIBNAME} STATIC ${DIR_SRCS}) 14 | add_library(${SHAREDLIBNAME} SHARED ${DIR_SRCS}) 15 | 16 | set_target_properties(${SHAREDLIBNAME} PROPERTIES OUTPUT_NAME ${STATICLIBNAME}) 17 | set_target_properties(${SHAREDLIBNAME} PROPERTIES VERSION 1.1.0 SOVERSION 1) 18 | 19 | target_link_libraries(${SHAREDLIBNAME} -Wl,--whole-archive -llog4cpp -Wl,--no-whole-archive) 20 | 21 | install(TARGETS ${STATICLIBNAME} ARCHIVE DESTINATION /usr/local/framework/clog/lib) 22 | install(TARGETS ${SHAREDLIBNAME} LIBRARY DESTINATION /usr/local/framework/clog/lib) 23 | install(FILES ${PROJECT_SOURCE_DIR}/include/LogWrapper.h DESTINATION /usr/local/framework/clog/include/clog) 24 | install(FILES ${PROJECT_SOURCE_DIR}/include/LogMessage.h DESTINATION /usr/local/framework/clog/include/clog) 25 | install(FILES ${PROJECT_SOURCE_DIR}/include/LogStream.h DESTINATION /usr/local/framework/clog/include/clog) 26 | -------------------------------------------------------------------------------- /Range/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Range.hpp" 3 | 4 | int main() 5 | { 6 | std::cout << "range(5): "; 7 | for (int i : range(5)) 8 | { 9 | std::cout << i << " "; 10 | } 11 | std::cout << std::endl; 12 | 13 | std::cout << "range(2, 6): "; 14 | for (int i : range(2, 6)) 15 | { 16 | std::cout << i << " "; 17 | } 18 | std::cout << std::endl; 19 | 20 | std::cout << "range(10.5, 15.5): "; 21 | for (int i : range(10.5, 15.5)) 22 | { 23 | std::cout << i << " "; 24 | } 25 | std::cout << std::endl; 26 | 27 | std::cout << "range(35, 27, -1): "; 28 | for (int i : range(35, 27, -1)) 29 | { 30 | std::cout << i << " "; 31 | } 32 | std::cout << std::endl; 33 | 34 | std::cout << "range(2, 8, 0.5): "; 35 | for (float i : range(2, 8, 0.5)) 36 | { 37 | std::cout << i << " "; 38 | } 39 | std::cout << std::endl; 40 | 41 | std::cout << "range(8, 7, -0.1): "; 42 | for (auto i : range(8, 7, -0.1)) 43 | { 44 | std::cout << i << " "; 45 | } 46 | std::cout << std::endl; 47 | 48 | std::cout << "range('a', 'z'): "; 49 | for (auto i : range('a', 'z')) 50 | { 51 | std::cout << i << " "; 52 | } 53 | std::cout << std::endl; 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Timer/Timer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H 2 | #define _TIMER_H 3 | 4 | #include 5 | 6 | class Timer 7 | { 8 | public: 9 | Timer() : m_begin(std::chrono::high_resolution_clock::now()) {} 10 | 11 | void reset() 12 | { 13 | m_begin = std::chrono::high_resolution_clock::now(); 14 | } 15 | 16 | // 默认输出毫秒. 17 | template 18 | long long elapsed() const 19 | { 20 | return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_begin).count(); 21 | } 22 | 23 | // 微秒. 24 | long long elapsedMicroseconds() const 25 | { 26 | return elapsed(); 27 | } 28 | 29 | // 纳秒. 30 | long long elapsedNanoSeconds() const 31 | { 32 | return elapsed(); 33 | } 34 | 35 | // 秒. 36 | long long elapsedSeconds() const 37 | { 38 | return elapsed(); 39 | } 40 | 41 | // 分. 42 | long long elapsedMinutes() const 43 | { 44 | return elapsed(); 45 | } 46 | 47 | // 时. 48 | long long elapsedHours() const 49 | { 50 | return elapsed(); 51 | } 52 | 53 | private: 54 | std::chrono::time_point m_begin; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /format/timer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _TIMER_H 2 | #define _TIMER_H 3 | 4 | #include 5 | 6 | class timer 7 | { 8 | public: 9 | timer() : _begin(std::chrono::high_resolution_clock::now()) {} 10 | 11 | void reset() 12 | { 13 | _begin = std::chrono::high_resolution_clock::now(); 14 | } 15 | 16 | // 默认输出毫秒. 17 | template 18 | long long elapsed() const 19 | { 20 | return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - _begin).count(); 21 | } 22 | 23 | // 微秒. 24 | long long elapsed_microseconds() const 25 | { 26 | return elapsed(); 27 | } 28 | 29 | // 纳秒. 30 | long long elapsed_nanoseconds() const 31 | { 32 | return elapsed(); 33 | } 34 | 35 | // 秒. 36 | long long elapsed_seconds() const 37 | { 38 | return elapsed(); 39 | } 40 | 41 | // 分. 42 | long long elapsed_minutes() const 43 | { 44 | return elapsed(); 45 | } 46 | 47 | // 时. 48 | long long elapsed_hours() const 49 | { 50 | return elapsed(); 51 | } 52 | 53 | private: 54 | std::chrono::time_point _begin; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /format/fmt/bundled/LICENSE.rst: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 - 2016, Victor Zverovich 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /framework/net-thrift/src/ThriftServerWrapper.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file ThriftServerWrapper.cpp 8 | * @brief thrift服务器端通信包装实现文件 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-25 12 | */ 13 | 14 | #include "ThriftServerWrapper.h" 15 | #include "ThriftServerImpl.h" 16 | 17 | ThriftServerWrapper::ThriftServerWrapper() 18 | { 19 | if (m_impl.use_count() == 0) 20 | { 21 | m_impl = std::make_shared(); 22 | } 23 | } 24 | 25 | void ThriftServerWrapper::init(unsigned int port) 26 | { 27 | if (m_impl.use_count() != 0) 28 | { 29 | m_impl->init(port); 30 | } 31 | } 32 | 33 | bool ThriftServerWrapper::start() 34 | { 35 | if (m_impl.use_count() != 0) 36 | { 37 | return m_impl->start(); 38 | } 39 | 40 | return false; 41 | } 42 | 43 | bool ThriftServerWrapper::stop() 44 | { 45 | if (m_impl.use_count() != 0) 46 | { 47 | return m_impl->stop(); 48 | } 49 | 50 | return false; 51 | } 52 | 53 | void ThriftServerWrapper::deinit() 54 | { 55 | if (m_impl.use_count() != 0) 56 | { 57 | m_impl->deinit(); 58 | } 59 | } 60 | 61 | void ThriftServerWrapper::setMessageCallback(MESSAGE_CALLBACK func) 62 | { 63 | if (m_impl.use_count() != 0) 64 | { 65 | m_impl->setMessageCallback(func); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /framework/threadpool/include/CWorkerThread.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CWorkerThread.h 8 | * @brief 工作线程类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CWORKERTHREAD_H 15 | #define _CWORKERTHREAD_H 16 | 17 | #include "CThread.h" 18 | #include 19 | 20 | class CThreadPool; 21 | class CJob; 22 | 23 | using CThreadPoolPtr =std::shared_ptr; 24 | using CJobPtr = std::shared_ptr; 25 | 26 | /** 27 | * @brief 工作线程类,继承自CThread,执行具体的job 28 | */ 29 | class CWorkerThread 30 | : public CThread, 31 | public std::enable_shared_from_this 32 | { 33 | public: 34 | CWorkerThread(); 35 | 36 | /** 37 | * @brief run 实现run函数,在run函数里面等待并执行job 38 | */ 39 | virtual void run(); 40 | 41 | public: 42 | /** 43 | * @brief setThreadPool 设置工作线程的线程池 44 | * 45 | * @param threadPool 线程池 46 | */ 47 | void setThreadPool(CThreadPoolPtr threadPool); 48 | 49 | /** 50 | * @brief stopWorkThread 设置是否需要停止工作线程 51 | * 52 | * @param isStopWorkThread 是否停止工作线程的标志变量 53 | */ 54 | void stopWorkThread(bool isStopWorkThread); 55 | 56 | private: 57 | CThreadPoolPtr m_threadPool; 58 | std::atomic m_isStopWorkThread; 59 | }; 60 | 61 | using CWorkerThreadPtr = std::shared_ptr; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CWorkerThread.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CWorkerThread.h 8 | * @brief 工作线程类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CWORKERTHREAD_H 15 | #define _CWORKERTHREAD_H 16 | 17 | #include "CThread.h" 18 | #include 19 | 20 | class CThreadPool; 21 | class CJob; 22 | 23 | using CThreadPoolPtr = std::shared_ptr; 24 | using CJobPtr = std::shared_ptr; 25 | 26 | /** 27 | * @brief 工作线程类,继承自CThread,执行具体的job 28 | */ 29 | class CWorkerThread 30 | : public CThread, 31 | public std::enable_shared_from_this 32 | { 33 | public: 34 | CWorkerThread(); 35 | 36 | /** 37 | * @brief run 实现run函数,在run函数里面等待并执行job 38 | */ 39 | virtual void run(); 40 | 41 | public: 42 | /** 43 | * @brief setThreadPool 设置工作线程的线程池 44 | * 45 | * @param threadPool 线程池 46 | */ 47 | void setThreadPool(CThreadPoolPtr threadPool); 48 | 49 | /** 50 | * @brief stopWorkThread 设置是否需要停止工作线程 51 | * 52 | * @param isStopWorkThread 是否停止工作线程的标志变量 53 | */ 54 | void stopWorkThread(bool isStopWorkThread); 55 | 56 | private: 57 | CThreadPoolPtr m_threadPool; 58 | std::atomic m_isStopWorkThread; 59 | }; 60 | 61 | using CWorkerThreadPtr = std::shared_ptr; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /login_copy/sh/login.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 配置文件路径 4 | config_file="/usr/local/etc/ssh_ip.conf" 5 | 6 | # 读取配置文件 7 | function read_config_file() 8 | { 9 | section=$1 10 | key=$2 11 | value=`awk -F '=' '/\['$section'\]/{a=1}a==1&&$1~/'$key'/{print $2;exit}' $config_file` 12 | echo "$value" 13 | } 14 | 15 | # 打印IP地址列表 16 | function print_ip_address() 17 | { 18 | item_count=100 19 | echo "IP地址列表:" 20 | for ((i=1; i<=${item_count}; i++)) 21 | do 22 | section="item""$i" 23 | ip=`read_config_file "$section" "ip"` 24 | if [[ ${ip} != "" ]]; then 25 | echo "[""$i""]:"${ip} 26 | fi 27 | done 28 | } 29 | 30 | # 选择IP地址 31 | function choice_ip_address() 32 | { 33 | read -p "登录到:" input 34 | echo ${input} 35 | } 36 | 37 | # 登录到服务器 38 | function login_to_server() 39 | { 40 | item_index=$1 41 | section="item""$item_index" 42 | ip=`read_config_file "$section" "ip"` 43 | port=`read_config_file "$section" "port"` 44 | user_name=`read_config_file "$section" "user_name"` 45 | 46 | if [[ ${ip} != "" && ${port} != "" && ${user_name} != "" ]]; then 47 | ssh -p $port "$user_name"@"$ip" 48 | else 49 | echo "无效的IP地址或用户名!" 50 | fi 51 | } 52 | 53 | # main函数 54 | function main() 55 | { 56 | print_ip_address 57 | item_index=`choice_ip_address` 58 | login_to_server "$item_index" 59 | } 60 | 61 | # 调用main函数 62 | main 63 | 64 | -------------------------------------------------------------------------------- /framework/net-thrift/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(STATICLIBNAME thrift_wrapper) 2 | set(SHAREDLIBNAME thrift_wrapper_so) 3 | set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") 4 | 5 | aux_source_directory(. DIR_SRCS) 6 | 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{THRIFT_INCLUDE_PATH}) 9 | include_directories($ENV{BOOST_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{THRIFT_LIB_PATH}) 12 | link_directories($ENV{BOOST_LIB_PATH}) 13 | 14 | add_library(${STATICLIBNAME} STATIC ${DIR_SRCS}) 15 | add_library(${SHAREDLIBNAME} SHARED ${DIR_SRCS}) 16 | 17 | set_target_properties(${SHAREDLIBNAME} PROPERTIES OUTPUT_NAME ${STATICLIBNAME}) 18 | set_target_properties(${SHAREDLIBNAME} PROPERTIES VERSION 1.1.0 SOVERSION 1) 19 | 20 | target_link_libraries(${SHAREDLIBNAME} -Wl,--whole-archive -lthrift -lboost_serialization -Wl,--no-whole-archive) 21 | 22 | install(TARGETS ${STATICLIBNAME} ARCHIVE DESTINATION /usr/local/framework/net-thrift/lib) 23 | install(TARGETS ${SHAREDLIBNAME} LIBRARY DESTINATION /usr/local/framework/net-thrift/lib) 24 | install(FILES ${PROJECT_SOURCE_DIR}/include/Message.h DESTINATION /usr/local/framework/net-thrift/include/net-thrift) 25 | install(FILES ${PROJECT_SOURCE_DIR}/include/ThriftServerWrapper.h DESTINATION /usr/local/framework/net-thrift/include/net-thrift) 26 | install(FILES ${PROJECT_SOURCE_DIR}/include/ThriftClientWrapper.h DESTINATION /usr/local/framework/net-thrift/include/net-thrift) 27 | -------------------------------------------------------------------------------- /Traits/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Traits.hpp" 3 | 4 | /* template struct is_tuple : is_specialization_of, std::tuple>{}; */ 5 | /* template struct is_queue : is_specialization_of, std::queue>{}; */ 6 | /* template struct is_stack : is_specialization_of, std::stack>{}; */ 7 | /* template struct is_set : is_specialization_of, std::set> {}; */ 8 | /* template struct is_multiset : is_specialization_of, std::multiset> {}; */ 9 | /* template struct is_unordered_set : is_specialization_of, std::unordered_set> {}; */ 10 | /* template struct is_priority_queue : is_specialization_of, std::priority_queue>{}; */ 11 | /* template struct is_pair : is_specialization_of, std::pair> {}; */ 12 | 13 | template 14 | typename std::enable_if::value>::type testTraits(T&& t) 15 | { 16 | (void)t; 17 | std::cout << "is tuple" << std::endl; 18 | } 19 | 20 | template 21 | typename std::enable_if::value>::type testTraits(T&& t) 22 | { 23 | (void)t; 24 | std::cout << "is set" << std::endl; 25 | } 26 | 27 | int main() 28 | { 29 | testTraits(std::forward_as_tuple(1, "Hello")); 30 | std::set s {1}; 31 | testTraits(s); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /framework/utils/include/System.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file System.h 8 | * @brief 系统相关工具实现 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-28 12 | */ 13 | 14 | #ifndef _SYSTEM_H 15 | #define _SYSTEM_H 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | namespace utils 22 | { 23 | /** 24 | * @brief 系统相关操作 25 | */ 26 | class System 27 | { 28 | public: 29 | /** 30 | * @brief uuid 获取uuid 31 | * 32 | * @return uuid字符串 33 | */ 34 | static std::string uuid(); 35 | 36 | /** 37 | * @brief md5 对字符串进行md5加密 38 | * 39 | * @param str 待加密的字符串 40 | * 41 | * @return 加密后的字符串 42 | * 43 | * @note 若加密失败,则返回空字符串 44 | */ 45 | static std::string md5(const std::string& str); 46 | 47 | /** 48 | * @brief sha1 对字符串进行sha1加密 49 | * 50 | * @param str 待加密的字符串 51 | * 52 | * @return 加密后的字符串 53 | * 54 | * @note 若加密失败,则返回空字符串 55 | */ 56 | static std::string sha1(const std::string& str); 57 | 58 | /** 59 | * @brief totalMemery 获取系统总内存大小 60 | * 61 | * @return 成功返回总内存大小,失败返回0 62 | */ 63 | static unsigned long totalMemery(); 64 | 65 | /** 66 | * @brief freeMemery 获取系统可用内存大小 67 | * 68 | * @return 成功返回可用内存大小,失败返回0 69 | */ 70 | static unsigned long freeMemery(); 71 | }; 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /framework/utils/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(STATICLIBNAME utils_wrapper) 2 | set(SHAREDLIBNAME utils_wrapper_so) 3 | set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") 4 | 5 | aux_source_directory(. DIR_SRCS) 6 | 7 | include_directories(${PROJECT_SOURCE_DIR}/include) 8 | include_directories($ENV{UUID_INCLUDE_PATH}) 9 | include_directories($ENV{OPENSSL_INCLUDE_PATH}) 10 | 11 | link_directories($ENV{UUID_LIB_PATH}) 12 | link_directories($ENV{OPENSSL_LIB_PATH}) 13 | 14 | add_library(${STATICLIBNAME} STATIC ${DIR_SRCS}) 15 | add_library(${SHAREDLIBNAME} SHARED ${DIR_SRCS}) 16 | 17 | set_target_properties(${SHAREDLIBNAME} PROPERTIES OUTPUT_NAME ${STATICLIBNAME}) 18 | set_target_properties(${SHAREDLIBNAME} PROPERTIES VERSION 1.1.0 SOVERSION 1) 19 | 20 | target_link_libraries(${SHAREDLIBNAME} -Wl,--whole-archive -luuid -lcrypto -Wl,--no-whole-archive) 21 | 22 | install(TARGETS ${STATICLIBNAME} ARCHIVE DESTINATION /usr/local/framework/utils/lib) 23 | install(TARGETS ${SHAREDLIBNAME} LIBRARY DESTINATION /usr/local/framework/utils/lib) 24 | install(FILES ${PROJECT_SOURCE_DIR}/include/Utils.h DESTINATION /usr/local/framework/utils/include/utils) 25 | install(FILES ${PROJECT_SOURCE_DIR}/include/String.h DESTINATION /usr/local/framework/utils/include/utils) 26 | install(FILES ${PROJECT_SOURCE_DIR}/include/FileSystem.h DESTINATION /usr/local/framework/utils/include/utils) 27 | install(FILES ${PROJECT_SOURCE_DIR}/include/System.h DESTINATION /usr/local/framework/utils/include/utils) 28 | -------------------------------------------------------------------------------- /framework/clog/include/LogStream.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file LogStream.h 8 | * @brief 日志流格式化类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-23 12 | */ 13 | 14 | #ifndef _LOGSTREAM_H 15 | #define _LOGSTREAM_H 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | /** 23 | * @brief 日志流对象 24 | */ 25 | class LogStream 26 | { 27 | public: 28 | /** 29 | * @brief LogStream 构造函数 30 | * 31 | * @param filePath 文件路径 32 | * @param function 函数名 33 | * @param line 代码行数 34 | * @param priorityLevel 日志优先级 35 | */ 36 | LogStream(const std::string& filePath, 37 | const std::string& function, 38 | unsigned long line, 39 | unsigned int priorityLevel); 40 | ~LogStream(); 41 | 42 | /** 43 | * @brief << 从载输入操作符 44 | * 45 | * @tparam T 模板类型 46 | * @param t 模板类对象 47 | * 48 | * @return 日志流对象 49 | */ 50 | template 51 | LogStream& operator << (const T& t) 52 | { 53 | assert(m_buffer.use_count() != 0); 54 | (*m_buffer) << t; 55 | return *this; 56 | } 57 | 58 | private: 59 | using OStringStreamPtr = std::shared_ptr; 60 | OStringStreamPtr m_buffer; ///< 字符串输入流对象 61 | 62 | unsigned int m_priorityLevel = 0; ///< 日志优先级 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /framework/clog/include/LogWrapper.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file LogWrapper.h 8 | * @brief 日志库包装接口定义 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-12-05 12 | */ 13 | 14 | #ifndef _LOGWRAPPER_H 15 | #define _LOGWRAPPER_H 16 | 17 | #include "LogMessage.h" 18 | 19 | /// 日志优先级,值越低优先级越高 20 | /// LogWrapper的优先级的值和log4cpp的优先级的值一样,方便映射 21 | enum LogWrapperPriorityLevel 22 | { 23 | LogWrapperFatalLevel = 0, 24 | LogWrapperAlertLevel = 100, 25 | LogWrapperCritLevel = 200, 26 | LogWrapperErrorLevel = 300, 27 | LogWrapperWarnLevel = 400, 28 | LogWrapperNoticeLevel = 500, 29 | LogWrapperInfoLevel = 600, 30 | LogWrapperDebugLevel = 700 31 | }; 32 | 33 | #define LOCATION_INFO __FILE__, __FUNCTION__, __LINE__ 34 | 35 | #define logFatal LogMessage(LOCATION_INFO, LogWrapperFatalLevel).log 36 | #define logAlert LogMessage(LOCATION_INFO, LogWrapperAlertLevel).log 37 | #define logCrit LogMessage(LOCATION_INFO, LogWrapperCritLevel).log 38 | #define logError LogMessage(LOCATION_INFO, LogWrapperErrorLevel).log 39 | #define logWarn LogMessage(LOCATION_INFO, LogWrapperWarnLevel).log 40 | #define logNotice LogMessage(LOCATION_INFO, LogWrapperNoticeLevel).log 41 | #define logInfo LogMessage(LOCATION_INFO, LogWrapperInfoLevel).log 42 | #define logDebug LogMessage(LOCATION_INFO, LogWrapperDebugLevel).log 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/DBServerTest/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月09日 星期六 20时26分44秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "RPCProtocolDefine.h" 10 | #include "RCFServerWrapper.hpp" 11 | 12 | class RCFMessageHandler 13 | { 14 | public: 15 | bool queryPeopleInfoByID(int id, PeopleInfoMessage& peopleInfo) 16 | { 17 | if (id == 1000) 18 | { 19 | peopleInfo.m_name = "Jack"; 20 | peopleInfo.m_age = 24; 21 | 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | RCFMessageHandler rcfMessageHandler; 32 | RCFServerWrapper server(50002); 33 | 34 | bool ok = server.start(rcfMessageHandler); 35 | if (ok) 36 | { 37 | std::cout << "DB server start..." << std::endl; 38 | } 39 | else 40 | { 41 | std::cout << "DB server start failed" << std::endl; 42 | return -1; 43 | } 44 | 45 | std::cin.get(); 46 | 47 | ok = server.stop(); 48 | if (ok) 49 | { 50 | std::cout << "DB server stoped..." << std::endl; 51 | } 52 | else 53 | { 54 | std::cout << "DB server stoped failed" << std::endl; 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /DllParser/DllParser/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解用C++11来实现一个通用的动态库解析器 3 | * 使用到的C++11特性有: 4 | * 1.可变参数模板类 5 | * 2.智能指针 6 | * 3.函数相关std::function 7 | * 4.using关键字 8 | * 5.判断函数的返回值类型std::result_of 9 | * 6.完美转发std::forward 10 | ************************************************/ 11 | #include "DllParser.hpp" 12 | 13 | int main() 14 | { 15 | std::string dllFilePath = "../Caculate/libcac.so"; 16 | 17 | DllParser parser; 18 | bool ret = parser.load(dllFilePath); 19 | if (!ret) 20 | { 21 | return 1; 22 | } 23 | 24 | int a = 20; 25 | int b = 10; 26 | 27 | try 28 | { 29 | auto addRet = parser.excecuteFunction("add", a, b); 30 | std::cout << "a + b = " << addRet << std::endl; 31 | } 32 | catch (std::exception& e) 33 | { 34 | std::cout << "Exception: " << e.what() << std::endl; 35 | } 36 | 37 | try 38 | { 39 | auto subRet = parser.excecuteFunction("sub", a, b); 40 | std::cout << "a - b = " << subRet << std::endl; 41 | } 42 | catch (std::exception& e) 43 | { 44 | std::cout << "Exception: " << e.what() << std::endl; 45 | } 46 | 47 | std::string str = "Hello C++11"; 48 | try 49 | { 50 | parser.excecuteFunction("print", str); 51 | } 52 | catch (std::exception& e) 53 | { 54 | std::cout << "Exception: " << e.what() << std::endl; 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /framework/net-rcf/tests/CenterNetServerTest/CenterNetServer.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: CenterNetServer.h 3 | > Author: 4 | > Mail: 5 | > Created Time: 2016年01月10日 星期日 20时30分04秒 6 | ************************************************************************/ 7 | 8 | #ifndef _CENTERNETSERVER_H 9 | #define _CENTERNETSERVER_H 10 | 11 | #include "RPCProtocolDefine.h" 12 | #include "RCFServerWrapper.hpp" 13 | #include "RCFClientWrapper.hpp" 14 | 15 | class CenterNetServer; 16 | 17 | class RCFMessageHandler 18 | { 19 | public: 20 | RCFMessageHandler(CenterNetServer* centerNetServer); 21 | ~RCFMessageHandler(); 22 | 23 | public: 24 | bool queryPeopleInfoByID(int id, PeopleInfoMessage& peopleInfo); 25 | 26 | private: 27 | CenterNetServer* m_centerNetServer; 28 | }; 29 | 30 | class CenterNetServer 31 | { 32 | public: 33 | CenterNetServer(); 34 | ~CenterNetServer(); 35 | 36 | public: 37 | bool start(); 38 | bool stop(); 39 | 40 | public: 41 | typedef boost::shared_ptr > RCFServerWrapperPtr; 42 | RCFServerWrapperPtr m_rcfServerWrapper; 43 | 44 | typedef boost::shared_ptr > RCFClientWrapperPtr; 45 | RCFClientWrapperPtr m_rcfDBServerClientWrapper; 46 | 47 | private: 48 | typedef boost::shared_ptr RCFMessageHandlerPtr; 49 | RCFMessageHandlerPtr m_rcfMessageHandler; 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /thread_pool/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "thread_pool.hpp" 5 | 6 | void test_task(const std::string& str) 7 | { 8 | std::cout << "Current thread id: " << std::this_thread::get_id() << ", str: " << str << std::endl; 9 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); 10 | } 11 | 12 | class Test 13 | { 14 | public: 15 | void print(const std::string& str, int i) 16 | { 17 | std::cout << "Test: " << str << ", i: " << i << std::endl; 18 | } 19 | }; 20 | 21 | class Test2 22 | { 23 | public: 24 | void operator()(const std::string& str, int i) 25 | { 26 | std::cout << "Test2: " << str << ", i: " << i << std::endl; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | Test t; 33 | Test2 t2; 34 | 35 | try 36 | { 37 | thread_pool pool; 38 | pool.init_thread_num(10); 39 | 40 | std::string str = "Hello world"; 41 | 42 | for (int i = 0; i < 1000; ++i) 43 | { 44 | pool.add_task([]{ std::cout << "Hello ThreadPool" << std::endl; }); 45 | pool.add_task(test_task, str); 46 | pool.add_task(t2, str, i); 47 | pool.add_task(&Test::print, &t, str, i); 48 | } 49 | 50 | std::cin.get(); 51 | std::cout << "##############END###################" << std::endl; 52 | } 53 | catch (std::exception& e) 54 | { 55 | std::cout << "Exception: " << e.what() << std::endl; 56 | } 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /TupleHelper/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tuple_helper.hpp" 4 | 5 | int add(int a, int b) 6 | { 7 | return a + b; 8 | } 9 | 10 | struct person 11 | { 12 | int age = 0; 13 | std::string name; 14 | }; 15 | 16 | int main() 17 | { 18 | std::cout << "******tuple_printer*********" << std::endl; 19 | std::tuple tp = std::make_tuple(1, 2, 3, 'a'); 20 | print_tuple(tp); 21 | print_tuple(std::forward_as_tuple("Hello", "world")); 22 | 23 | std::cout << "******std::cout*********" << std::endl; 24 | std::cout << tp << std::endl; 25 | 26 | std::cout << "******get_element_by_type*********" << std::endl; 27 | auto a = get_element_by_type(tp); 28 | std::cout << "double: " << a << std::endl; 29 | auto b = get_element_by_type(tp); 30 | std::cout << "char: " << b << std::endl; 31 | 32 | 33 | std::cout << "******std::array to std::tuple*********" << std::endl; 34 | std::array arr { 1, 2, 3, 4 }; 35 | std::cout << array2tuple(arr) << std::endl; 36 | 37 | std::cout << "******std::tuple invoke*********" << std::endl; 38 | std::tuple t = std::make_tuple(1, 2); 39 | std::cout << invoke(add, t) << std::endl; 40 | 41 | std::cout << "******make_from_tuple*********" << std::endl; 42 | std::tuple p = std::make_tuple(20, "Jack"); 43 | auto pp = make_from_tuple(p); 44 | std::cout << pp.age << ", " << pp.name << std::endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Args/Args.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _ARGS_H 2 | #define _ARGS_H 3 | 4 | #include 5 | 6 | /**********模板递归和特化方式展开参数包******************/ 7 | 8 | // 前向申明 9 | template 10 | struct Sum; 11 | 12 | // 基本定义 13 | #if 0 14 | // 枚举编译器常量 15 | template 16 | struct Sum 17 | { 18 | enum 19 | { 20 | value = Sum::value + Sum::value 21 | }; 22 | }; 23 | #else 24 | // 继承了编译器常量std::integral_constant 25 | template 26 | struct Sum : std::integral_constant::value + Sum::value> {}; 27 | #endif 28 | 29 | // 终止递归 30 | #if 0 31 | template 32 | struct Sum 33 | { 34 | enum 35 | { 36 | value = sizeof(Last) 37 | }; 38 | }; 39 | #else 40 | template 41 | struct Sum : std::integral_constant {}; 42 | #endif 43 | 44 | /**********继承和特化方式展开参数包******************/ 45 | 46 | // 整型序列的定义 47 | template 48 | struct IndexSeq {}; 49 | 50 | #if 0 51 | // 继承方式,开始展开参数包 52 | template 53 | struct MakeIndexes : MakeIndexes {}; 54 | #else 55 | // using实现来展开参数包 56 | template 57 | struct MakeIndexes 58 | { 59 | using type = typename MakeIndexes::type; 60 | }; 61 | #endif 62 | 63 | // 模板特化,终止展开参数包的条件 64 | template 65 | struct MakeIndexes<0, Indexes...> 66 | { 67 | using type = IndexSeq; 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /framework/net-thrift/include/ThriftServerWrapper.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file ThriftServerWrapper.h 8 | * @brief thrift服务器通信包装 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-25 12 | */ 13 | 14 | #ifndef _THRIFTSERVERWRAPPER_H 15 | #define _THRIFTSERVERWRAPPER_H 16 | 17 | #include "Message.h" 18 | #include 19 | #include 20 | 21 | class ThriftServerImpl; 22 | 23 | /// 消息回调函数定义 24 | using MESSAGE_CALLBACK = void (*) (Message* message, Message* retMessage); 25 | 26 | 27 | /** 28 | * @brief thrift服务器通信包装类 29 | */ 30 | class ThriftServerWrapper 31 | { 32 | public: 33 | ThriftServerWrapper(); 34 | 35 | /** 36 | * @brief init 初始化thrift服务器端 37 | * 38 | * @param port 监听端口,默认为9090 39 | */ 40 | void init(unsigned int port = 9090); 41 | 42 | /** 43 | * @brief start 开始服务器 44 | * 45 | * @return 成功返回true,否则返回false 46 | */ 47 | bool start(); 48 | 49 | /** 50 | * @brief stop 停止服务器 51 | * 52 | * @return 成功返回true,否则返回false 53 | */ 54 | bool stop(); 55 | 56 | /** 57 | * @brief deinit 反初始化,释放一些资源 58 | */ 59 | void deinit(); 60 | 61 | /** 62 | * @brief setMessageCallback 设置消息回调函数 63 | * 64 | * @param func 回调函数指针 65 | */ 66 | void setMessageCallback(MESSAGE_CALLBACK func); 67 | 68 | private: 69 | using ThriftServerImplPtr = std::shared_ptr; 70 | ThriftServerImplPtr m_impl; ///< thrift服务器实现类指针 71 | }; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /subscribe_center/subscribe_center.cpp: -------------------------------------------------------------------------------- 1 | #include "subscribe_center.h" 2 | 3 | void subscribe_center::add_message(const std::string& message_name, const std::string& session_id) 4 | { 5 | lock_shared lock(mutex_); 6 | auto range = message_to_session_.equal_range(message_name); 7 | for (auto iter = range.first; iter != range.second; ++iter) 8 | { 9 | if (iter->second == session_id) 10 | { 11 | return; 12 | } 13 | } 14 | 15 | message_to_session_.emplace(message_name, session_id); 16 | } 17 | 18 | std::vector subscribe_center::get_session_id(const std::string& message_name) 19 | { 20 | lock_shared lock(mutex_, true); 21 | std::vector session_ids; 22 | auto range = message_to_session_.equal_range(message_name); 23 | for (auto iter = range.first; iter != range.second; ++iter) 24 | { 25 | session_ids.emplace_back(iter->second); 26 | } 27 | 28 | return session_ids; 29 | } 30 | 31 | void subscribe_center::remove_message(const std::string& session_id) 32 | { 33 | lock_shared lock(mutex_); 34 | auto begin = message_to_session_.begin(); 35 | while (begin != message_to_session_.end()) 36 | { 37 | if (begin->second == session_id) 38 | { 39 | begin = message_to_session_.erase(begin); 40 | } 41 | else 42 | { 43 | ++begin; 44 | } 45 | } 46 | } 47 | 48 | void subscribe_center::clear() 49 | { 50 | lock_shared lock(mutex_); 51 | message_to_session_.clear(); 52 | } 53 | -------------------------------------------------------------------------------- /FunctionTraits/main.cpp: -------------------------------------------------------------------------------- 1 | #include "FunctionTraits.hpp" 2 | #include 3 | #if defined(__GNUC__) 4 | #include 5 | #endif 6 | #include 7 | #include 8 | 9 | template 10 | void printType() 11 | { 12 | #if defined(__GNUC__) 13 | std::cout << abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr) << std::endl; 14 | #else 15 | std::cout << typeid(T).name() << std::endl; 16 | #endif 17 | } 18 | 19 | float (*func)(std::string, int); 20 | float func2(const std::string& str, int b) 21 | { 22 | return static_cast(str.size() + b); 23 | } 24 | 25 | struct AA 26 | { 27 | int func(int a, int b) volatile {return a + b;} 28 | int operator()(int) const {return 0;} 29 | }; 30 | 31 | int main() 32 | { 33 | std::function f = [](int a){return a;}; 34 | 35 | printType>::functionType>(); 36 | printType>::args<0>::type>(); 37 | printType>::returnType>(); 38 | std::cout << "count: " << FunctionTraits>::arity << std::endl; 39 | 40 | printType::functionType>(); 41 | printType::functionType>(); 42 | printType::functionType>(); 43 | 44 | printType::functionType>(); 45 | printType(); 46 | printType::functionType>(); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /DateTime/DateTime.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _DATETIME_H 2 | #define _DATETIME_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class DateTime 9 | { 10 | public: 11 | template 12 | static typename std::enable_if::value, std::string>::type convert(time_t t) 13 | { 14 | return time2string(t); 15 | } 16 | 17 | template 18 | static typename std::enable_if::value, time_t>::type convert(const std::string& timeStr) 19 | { 20 | return string2time(timeStr); 21 | } 22 | 23 | static std::string currentTime() 24 | { 25 | return time2string(time(nullptr)); 26 | } 27 | 28 | private: 29 | static std::string time2string(time_t t) 30 | { 31 | struct tm* tmNow = localtime(&t); 32 | char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = {'\0'}; 33 | std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tmNow); 34 | return timeStr; 35 | } 36 | 37 | static time_t string2time(const std::string& timeStr) 38 | { 39 | struct tm stTm; 40 | sscanf(timeStr.c_str(), "%d-%d-%d %d:%d:%d", 41 | &(stTm.tm_year), 42 | &(stTm.tm_mon), 43 | &(stTm.tm_mday), 44 | &(stTm.tm_hour), 45 | &(stTm.tm_min), 46 | &(stTm.tm_sec)); 47 | 48 | stTm.tm_year -= 1900; 49 | stTm.tm_mon--; 50 | stTm.tm_isdst = -1; 51 | 52 | return mktime(&stTm); 53 | } 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /Ioc/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IocContainer.hpp" 3 | 4 | class ICar 5 | { 6 | public: 7 | virtual ~ICar() 8 | { 9 | // Do nothing 10 | } 11 | 12 | virtual void test() = 0; 13 | }; 14 | 15 | class Bus: public ICar 16 | { 17 | public: 18 | virtual void test() override 19 | { 20 | std::cout << "Bus::test" << std::endl; 21 | } 22 | }; 23 | 24 | class Car : public ICar 25 | { 26 | public: 27 | virtual void test() override 28 | { 29 | std::cout << "Car::test" << std::endl; 30 | } 31 | }; 32 | 33 | struct A 34 | { 35 | void func() 36 | { 37 | std::cout << "A::func()" << std::endl; 38 | } 39 | }; 40 | 41 | struct B 42 | { 43 | void func() 44 | { 45 | std::cout << "B::func()" << std::endl; 46 | } 47 | }; 48 | int main() 49 | { 50 | #if 0 51 | IocContainer ioc; 52 | 53 | try 54 | { 55 | ioc.registerType("bus"); 56 | ioc.registerType("car"); 57 | } 58 | catch (std::exception& e) 59 | { 60 | std::cout << "Exception: " << e.what() << std::endl; 61 | return 1; 62 | } 63 | 64 | std::shared_ptr bus = ioc.resolveShared("bus"); 65 | bus->test(); 66 | std::shared_ptr car = ioc.resolveShared("car"); 67 | car->test(); 68 | #endif 69 | IocContainer ioc; 70 | ioc.registerSimple("a"); 71 | ioc.registerSimple("b"); 72 | auto a = ioc.resolveShared("a"); 73 | a->func(); 74 | auto b = ioc.resolveShared("b"); 75 | b->func(); 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /ObjectPool/ObjectPool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECTPOOL_H 2 | #define _OBJECTPOOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | class ObjectPool 10 | { 11 | public: 12 | using DeleterType = std::function; 13 | 14 | void add(std::unique_ptr t) 15 | { 16 | m_pool.emplace_back(std::move(t)); 17 | } 18 | 19 | std::unique_ptr get() 20 | { 21 | if (m_pool.empty()) 22 | { 23 | throw std::logic_error("No more object"); 24 | } 25 | 26 | std::unique_ptr ptr(m_pool.back().release(), [this](T* t) 27 | { 28 | m_pool.emplace_back(std::unique_ptr(t)); 29 | }); 30 | 31 | m_pool.pop_back(); 32 | return std::move(ptr); 33 | } 34 | 35 | std::shared_ptr getShared() 36 | { 37 | if (m_pool.empty()) 38 | { 39 | throw std::logic_error("No more object"); 40 | } 41 | 42 | auto pin = std::unique_ptr(std::move(m_pool.back())); 43 | m_pool.pop_back(); 44 | 45 | return std::shared_ptr(pin.release(), [this](T* t) 46 | { 47 | m_pool.emplace_back(std::unique_ptr(t)); 48 | }); 49 | } 50 | 51 | bool empty() const 52 | { 53 | return m_pool.empty(); 54 | } 55 | 56 | std::size_t size() const 57 | { 58 | return m_pool.size(); 59 | } 60 | 61 | private: 62 | std::vector> m_pool; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/TcpClientWrapper.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file TcpClientImpl.h 8 | * @brief tcp客户端包装类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-31 12 | */ 13 | 14 | #ifndef _TCPCLIENTWRAPPER_H 15 | #define _TCPCLIENTWRAPPER_H 16 | 17 | #include "TcpClientImpl.h" 18 | 19 | /** 20 | * @brief tcp客户端包装 21 | */ 22 | class TcpClientWrapper 23 | { 24 | public: 25 | TcpClientWrapper(const std::string& ip, unsigned short port); 26 | 27 | /** 28 | * @brief start 开始服务 29 | * 30 | * @note 在调用该函数之前,请先调用setThreadPoolNum和setClientParam函数 31 | * 32 | * @return 成功返回true,否则返回false 33 | */ 34 | bool start(); 35 | 36 | /** 37 | * @brief stop 停止服务 38 | * 39 | * @return 成功返回true,否则返回false 40 | */ 41 | bool stop(); 42 | 43 | /** 44 | * @brief setThreadPoolNum 设置线程池数量 45 | * 46 | * @param num 线程池数量 47 | */ 48 | void setThreadPoolNum(unsigned int num); 49 | 50 | /** 51 | * @brief setClientParam 设置客户端参数,主要是回调函数 52 | * 53 | * @param param 客户端参数 54 | */ 55 | void setClientParam(const ClientParam& param); 56 | 57 | /** 58 | * @brief write 同步写数据 59 | * 60 | * @param t 消息结构 61 | */ 62 | template 63 | void write(const T& t) 64 | { 65 | assert(m_impl.use_count() != 0); 66 | m_impl->write(t); 67 | } 68 | 69 | private: 70 | TcpClientImplPtr m_impl; 71 | }; 72 | 73 | using TcpClientWrapperPtr = std::shared_ptr; 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /framework/utils/tests/System/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年11月29日 星期日 18时41分08秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "Utils.h" 10 | 11 | int main() 12 | { 13 | std::cout << "####################uuid###################" << std::endl; 14 | for (int i = 0; i < 10; ++i) 15 | { 16 | std::cout << utils::System::uuid() << std::endl; 17 | } 18 | 19 | std::cout << "########################md5##################" << std::endl; 20 | 21 | std::string str = "hello"; 22 | std::cout << str << ": " << utils::System::md5(str) << std::endl; 23 | str = "中国"; 24 | std::cout << str << ": " << utils::System::md5(str) << std::endl; 25 | 26 | std::cout << "####################sha1#####################" << std::endl; 27 | 28 | str = "hello"; 29 | std::cout << str << ": " << utils::System::sha1(str) << std::endl; 30 | str = "中国"; 31 | std::cout << str << ": " << utils::System::sha1(str) << std::endl; 32 | 33 | unsigned long totalram = utils::System::totalMemery(); 34 | if (totalram != 0) 35 | { 36 | std::cout << "总内存大小:" << totalram / (1024 * 1024 * 1024 * 1.0) << "GB" << std::endl; 37 | } 38 | 39 | unsigned long freeram = utils::System::freeMemery(); 40 | if (freeram != 0) 41 | { 42 | std::cout << "可用内存大小:" << freeram / (1024 * 1024 * 1024 * 1.0) << "GB" << std::endl; 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /login_copy/sh/upload.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 配置文件路径 4 | config_file="/usr/local/etc/ssh_ip.conf" 5 | # 拷贝文件到target_dir 6 | target_dir="~/cx" 7 | 8 | # 读取配置文件 9 | function read_config_file() 10 | { 11 | section=$1 12 | key=$2 13 | value=`awk -F '=' '/\['$section'\]/{a=1}a==1&&$1~/'$key'/{print $2;exit}' $config_file` 14 | echo "$value" 15 | } 16 | 17 | # 打印IP地址列表 18 | function print_ip_address() 19 | { 20 | item_count=100 21 | echo "IP地址列表:" 22 | for ((i=1; i<=${item_count}; i++)) 23 | do 24 | section="item""$i" 25 | ip=`read_config_file "$section" "ip"` 26 | if [[ ${ip} != "" ]]; then 27 | echo "[""$i""]:"${ip} 28 | fi 29 | done 30 | } 31 | 32 | # 选择IP地址 33 | function choice_ip_address() 34 | { 35 | read -p "上传到:" input 36 | echo ${input} 37 | } 38 | 39 | # 拷贝文件 40 | function copy_file() 41 | { 42 | section="item""$ip_index" 43 | ip=`read_config_file "$section" "ip"` 44 | port=`read_config_file "$section" "port"` 45 | user_name=`read_config_file "$section" "user_name"` 46 | 47 | if [[ ${ip} != "" && ${port} != "" && ${user_name} != "" ]]; then 48 | scp -P $port -r $file_names "$user_name"@"$ip":$target_dir 49 | echo "$file_names""已经上传到""$target_dir" 50 | else 51 | echo "无效的IP地址或用户名!" 52 | fi 53 | } 54 | 55 | # 脚本启动点 56 | if [ $# -lt 1 ]; then 57 | echo "请输入参数!" 58 | exit 1 59 | fi 60 | 61 | file_names="" 62 | for arg in $@ 63 | do 64 | file_names="$file_names"" ""$arg" 65 | done 66 | 67 | print_ip_address 68 | ip_index=`choice_ip_address` 69 | 70 | copy_file "$ip_index" "$file_names" 71 | -------------------------------------------------------------------------------- /Factory/Factory.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H 2 | #define _FACTORY_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "Message.hpp" 9 | 10 | class Factory 11 | { 12 | public: 13 | Factory() = delete; 14 | Factory(const Factory&) = delete; 15 | Factory& operator=(const Factory&) = delete; 16 | 17 | template 18 | static void registerMessage(int key) 19 | { 20 | m_messageMap.emplace(key, []{ return new T(); }); 21 | } 22 | 23 | template 24 | static void registerMessage(int key, const Args&... args) 25 | { 26 | m_messageMap.emplace(key, [=] { return new T(args...); }); 27 | } 28 | 29 | static Message* get(int key) 30 | { 31 | auto iter = m_messageMap.find(key); 32 | if (iter == m_messageMap.end()) 33 | { 34 | return nullptr; 35 | } 36 | return iter->second(); 37 | } 38 | 39 | static std::unique_ptr getUnique(int key) 40 | { 41 | return std::unique_ptr(get(key)); 42 | } 43 | 44 | static std::shared_ptr getShared(int key) 45 | { 46 | return std::shared_ptr(get(key)); 47 | } 48 | 49 | using MessageID = int; 50 | using MessageCreationFunc = std::function; 51 | 52 | private: 53 | static std::unordered_map m_messageMap; 54 | }; 55 | // warning : this imply header only include once 56 | std::unordered_map Factory::m_messageMap; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /login_copy/sh/download.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 配置文件路径 4 | config_file="/usr/local/etc/ssh_ip.conf" 5 | # 拷贝文件到target_dir 6 | target_dir="${HOME}""/cx" 7 | 8 | # 读取配置文件 9 | function read_config_file() 10 | { 11 | section=$1 12 | key=$2 13 | value=`awk -F '=' '/\['$section'\]/{a=1}a==1&&$1~/'$key'/{print $2;exit}' $config_file` 14 | echo "$value" 15 | } 16 | 17 | # 打印IP地址列表 18 | function print_ip_address() 19 | { 20 | item_count=100 21 | echo "IP地址列表:" 22 | for ((i=1; i<=${item_count}; i++)) 23 | do 24 | section="item""$i" 25 | ip=`read_config_file "$section" "ip"` 26 | if [[ ${ip} != "" ]]; then 27 | echo "[""$i""]:"${ip} 28 | fi 29 | done 30 | } 31 | 32 | # 选择IP地址 33 | function choice_ip_address() 34 | { 35 | read -p "从哪里下载:" input 36 | echo ${input} 37 | } 38 | 39 | # 拷贝文件 40 | function copy_file() 41 | { 42 | section="item""$ip_index" 43 | ip=`read_config_file "$section" "ip"` 44 | port=`read_config_file "$section" "port"` 45 | user_name=`read_config_file "$section" "user_name"` 46 | 47 | if [[ ${ip} != "" && ${port} != "" && ${user_name} != "" ]]; then 48 | scp -P $port -r "$user_name"@"$ip:$file_names" $target_dir 49 | echo "$file_names""已经下载到""$target_dir" 50 | else 51 | echo "无效的IP地址或用户名!" 52 | fi 53 | } 54 | 55 | # 脚本启动点 56 | if [ $# -lt 1 ]; then 57 | echo "请输入参数!" 58 | exit 1 59 | fi 60 | 61 | file_names="" 62 | for arg in $@ 63 | do 64 | file_names="$file_names"" ""$arg" 65 | done 66 | 67 | print_ip_address 68 | ip_index=`choice_ip_address` 69 | 70 | copy_file "$ip_index" "$file_names" 71 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/TcpServerWrapper.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file TcpServerImpl.h 8 | * @brief tcp服务器包装类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-31 12 | */ 13 | 14 | #ifndef _TCPSERVERWRAPPER_H 15 | #define _TCPSERVERWRAPPER_H 16 | 17 | #include "TcpServerImpl.h" 18 | 19 | /** 20 | * @brief tcp服务器包装类 21 | */ 22 | class TcpServerWrapper 23 | { 24 | public: 25 | TcpServerWrapper(unsigned short port); 26 | 27 | /** 28 | * @brief start 开始服务 29 | * 30 | * @note 在调用该函数之前,请先调用setThreadPoolNum和setClientParam函数 31 | * 32 | * @return 成功返回true,否则返回false 33 | */ 34 | bool start(); 35 | 36 | /** 37 | * @brief stop 停止服务 38 | * 39 | * @return 成功返回true,否则返回false 40 | */ 41 | bool stop(); 42 | 43 | /** 44 | * @brief setThreadPoolNum 设置线程池数量 45 | * 46 | * @param num 线程池数量 47 | */ 48 | void setThreadPoolNum(unsigned int num); 49 | 50 | /** 51 | * @brief setClientParam 设置服务器参数,主要是回调函数 52 | * 53 | * @param param 服务器参数 54 | */ 55 | void setServerParam(const ServerParam& param); 56 | 57 | /** 58 | * @brief write 同步写数据 59 | * 60 | * @tparam T 模版类型 61 | * @param t 消息结构 62 | * @param remoteAddress 客户端地址(地址格式:127.0.0.1:8888) 63 | */ 64 | template 65 | void write(const T& t, const std::string& remoteAddress) 66 | { 67 | assert(m_impl.use_count() != 0); 68 | m_impl->write(t, remoteAddress); 69 | } 70 | 71 | private: 72 | TcpServerImplPtr m_impl; 73 | }; 74 | 75 | using TcpServerWrapperPtr = std::shared_ptr; 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/ClientTest/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年11月15日 星期日 20时46分11秒 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include "ThriftClientWrapper.h" 11 | #include "ProtocolDef.h" 12 | #include "Message.h" 13 | #include "PeopleInfoMessage.h" 14 | 15 | int main() 16 | { 17 | ThriftClientWrapper client; 18 | client.init("127.0.0.1", 9090); 19 | 20 | QueryPeopleInfoMessage queryPeopleInfo; 21 | queryPeopleInfo.m_cardId = "1234567890"; 22 | 23 | Message* message = new Message; 24 | message->m_messageType = PEOPLE_INFO_MSG; 25 | message->m_data = queryPeopleInfo.serializeSelf(); 26 | 27 | Message* retMessage = new Message; 28 | 29 | client.sendMessage(message, retMessage); 30 | 31 | if (retMessage->m_messageType == PEOPLE_INFO_MSG) 32 | { 33 | PeopleInfoMessage peopleInfo; 34 | peopleInfo.deserializeSelf(retMessage->m_data); 35 | 36 | std::cout << "name: " << peopleInfo.m_name << std::endl; 37 | std::cout << "age: " << peopleInfo.m_age << std::endl; 38 | std::cout << "sex: " << peopleInfo.m_sex << std::endl; 39 | 40 | for (auto& cat : peopleInfo.m_cats) 41 | { 42 | std::cout << "cat name: " << cat.m_name << std::endl; 43 | std::cout << "cat age: " << cat.m_age << std::endl; 44 | } 45 | } 46 | 47 | if (retMessage != NULL) 48 | { 49 | delete retMessage; 50 | retMessage = NULL; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /framework/net-thrift/include/Message.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file Message.h 8 | * @brief thrift消息传输定义 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-25 12 | */ 13 | 14 | #ifndef _MESSAGE_H 15 | #define _MESSAGE_H 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | /** 25 | * @brief thrift消息定义 26 | */ 27 | class Message 28 | { 29 | public: 30 | /** 31 | * @brief serializeSelf 序列化 32 | * 33 | * @return 序列化后的字符串 34 | */ 35 | std::string serializeSelf() 36 | { 37 | std::ostringstream os; 38 | boost::archive::binary_oarchive oa(os); 39 | oa << *this; 40 | return os.str(); 41 | } 42 | 43 | /** 44 | * @brief deserializeSelf 反序列化 45 | * 46 | * @param content 需要序列化的字符串mZ 47 | */ 48 | void deserializeSelf(const std::string& content) 49 | { 50 | std::istringstream is(content); 51 | boost::archive::binary_iarchive ia(is); 52 | ia >> *this; 53 | } 54 | 55 | unsigned int m_messageType; ///< 消息类型 56 | std::string m_data; ///< 消息内容 57 | }; 58 | 59 | namespace boost 60 | { 61 | namespace serialization 62 | { 63 | template 64 | void serialize(Archive& ar, Message& message, const unsigned int version) 65 | { 66 | (void)version; 67 | ar & message.m_messageType; 68 | ar & message.m_data; 69 | } 70 | } 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /Connect/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************ 2 | * 该例程讲解用C++11来实现Qt的信号槽机制 3 | * 使用到的C++11特性有: 4 | * 1.可变参数模板类 5 | * 2.智能指针 6 | * 3.函数相关std::function、std::bind 7 | * 4.using关键字 8 | * 5.完美转发std::forward 9 | ************************************************/ 10 | #include "Connect.hpp" 11 | #include 12 | #include 13 | 14 | class A 15 | { 16 | public: 17 | void start() 18 | { 19 | emit m_s1(); 20 | emit m_s2("Hello C++11"); 21 | emit m_s3(100, "Hello C++11"); 22 | } 23 | 24 | signals: 25 | Signal<> m_s1; // 不带参数的信号 26 | Signal m_s2; 27 | Signal m_s3; 28 | }; 29 | 30 | class B 31 | { 32 | public slots: 33 | void func1() 34 | { 35 | std::cout << "func1" << std::endl; 36 | } 37 | 38 | void func2(const std::string& str) 39 | { 40 | std::cout << str << std::endl; 41 | } 42 | 43 | void func3(int n, const std::string& str) 44 | { 45 | std::cout << n << " " << str << std::endl; 46 | } 47 | }; 48 | 49 | void func(const std::string& str) 50 | { 51 | std::cout << "func " << str << std::endl; 52 | } 53 | 54 | int main() 55 | { 56 | A a; 57 | B b; 58 | 59 | // 信号与槽绑定 60 | connect(&a, m_s1, std::bind(&B::func1, &b)); 61 | connect(&a, m_s2, std::bind(&B::func2, &b, std::placeholders::_1)); 62 | connect(&a, m_s3, std::bind(&B::func3, &b, std::placeholders::_1, std::placeholders::_2)); 63 | connect(&a, m_s2, std::bind(func, std::placeholders::_1)); 64 | connect(&a, m_s2, [](const std::string& str) 65 | { 66 | std::cout << "lambda str: " << str << std::endl; 67 | }); 68 | 69 | a.start(); 70 | 71 | return 0; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /login_copy/py/ul.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import os 5 | import sys 6 | 7 | # 配置文件路径 8 | file_path="/usr/local/etc/ssh_ip.conf" 9 | # 文件保存路径 10 | save_path="~/cx" 11 | 12 | # 读取文件 13 | def readlines(): 14 | results = [] 15 | f = open(file_path) 16 | lines = f.readlines() 17 | for line in lines: 18 | line = line.strip() 19 | if (len(line) != 0): 20 | results.append(line) 21 | 22 | f.close() 23 | return results 24 | 25 | 26 | # 打印IP地址列表 27 | def print_ip_address(ips): 28 | print '\033[1;32m%-10s %-20s %-10s %-15s %9s\033[0m' % ("ID", "IP", "PORT", "用户名", "备注") 29 | i = 0 30 | for line in ips: 31 | i = i + 1 32 | arr = line.split(",") 33 | print '%-10d %-20s %-10s %-15s %s' % (i, arr[0], arr[1], arr[2], arr[3]) 34 | 35 | 36 | # 选择IP地址 37 | def choice_ip_address(): 38 | i = input("上传到:") 39 | return i 40 | 41 | 42 | # 上传文件 43 | def upload_file(ips, i, filenames): 44 | if (i >= 1 and i <= len(ips)): 45 | arr = ips[i - 1].split(",") 46 | os.system("scp -P " + arr[1] + " -r " + filenames + " " + arr[2] + "@" + arr[0] + ":" + save_path) 47 | print(filenames + "已经上传到" + save_path) 48 | else: 49 | print("无效的IP地址或用户名!") 50 | 51 | 52 | # main函数 53 | if __name__ == "__main__": 54 | if (len(sys.argv) > 1): 55 | filenames = "" 56 | for filename in sys.argv[1:]: 57 | if (len(filenames) == 0): 58 | filenames = filenames + filename 59 | else: 60 | filenames = filenames + " " + filename 61 | 62 | ips = readlines() 63 | print_ip_address(ips) 64 | i = choice_ip_address() 65 | upload_file(ips, i, filenames) 66 | else: 67 | print("请输入文件名!") 68 | -------------------------------------------------------------------------------- /framework/net-tcpserver/src/CWorkerThread.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CWorkerThread.cpp 8 | * @brief 工作线程类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #include "CWorkerThread.h" 15 | #include "CThreadPool.h" 16 | #include "CJob.h" 17 | 18 | CWorkerThread::CWorkerThread() 19 | : CThread(), 20 | m_isStopWorkThread(false) 21 | { 22 | // Do nothing 23 | } 24 | 25 | void CWorkerThread::run() 26 | { 27 | while (true) 28 | { 29 | CJobPtr job; 30 | { 31 | std::unique_lock locker(m_threadPool->m_jobQueueMutex); 32 | while (m_threadPool->m_jobQueue.empty() && 33 | !m_threadPool->m_isStopThreadPool && 34 | !m_isStopWorkThread) 35 | { 36 | m_threadPool->m_jobQueueGetCond.wait(locker); 37 | } 38 | 39 | if (m_threadPool->m_isStopThreadPool || m_isStopWorkThread) 40 | { 41 | break; 42 | } 43 | 44 | if (!m_threadPool->m_jobQueue.empty()) 45 | { 46 | job = m_threadPool->m_jobQueue.front(); 47 | m_threadPool->m_jobQueue.pop(); 48 | } 49 | } 50 | 51 | if (job.use_count() != 0) 52 | { 53 | job->run(); 54 | job.reset(); 55 | m_threadPool->m_jobQueuePutCond.notify_one(); 56 | } 57 | } 58 | } 59 | 60 | void CWorkerThread::setThreadPool(CThreadPoolPtr threadPool) 61 | { 62 | assert(threadPool != NULL); 63 | m_threadPool = threadPool; 64 | } 65 | 66 | void CWorkerThread::stopWorkThread(bool isStopWorkThread) 67 | { 68 | m_isStopWorkThread = isStopWorkThread; 69 | } 70 | -------------------------------------------------------------------------------- /framework/threadpool/src/CWorkerThread.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CWorkerThread.cpp 8 | * @brief 工作线程类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #include "CWorkerThread.h" 15 | #include "CThreadPool.h" 16 | #include "CJob.h" 17 | 18 | CWorkerThread::CWorkerThread() 19 | : CThread(), 20 | m_isStopWorkThread(false) 21 | { 22 | // Do nothing 23 | } 24 | 25 | void CWorkerThread::run() 26 | { 27 | while (true) 28 | { 29 | CJobPtr job; 30 | { 31 | std::unique_lock locker(m_threadPool->m_jobQueueMutex); 32 | while (m_threadPool->m_jobQueue.empty() && 33 | !m_threadPool->m_isStopThreadPool && 34 | !m_isStopWorkThread) 35 | { 36 | m_threadPool->m_jobQueueGetCond.wait(locker); 37 | } 38 | 39 | if (m_threadPool->m_isStopThreadPool || m_isStopWorkThread) 40 | { 41 | break; 42 | } 43 | 44 | if (!m_threadPool->m_jobQueue.empty()) 45 | { 46 | job = m_threadPool->m_jobQueue.front(); 47 | m_threadPool->m_jobQueue.pop(); 48 | } 49 | } 50 | 51 | if (job.use_count() != 0) 52 | { 53 | job->run(); 54 | job.reset(); 55 | m_threadPool->m_jobQueuePutCond.notify_one(); 56 | } 57 | } 58 | } 59 | 60 | void CWorkerThread::setThreadPool(CThreadPoolPtr threadPool) 61 | { 62 | assert(threadPool != NULL); 63 | m_threadPool = threadPool; 64 | } 65 | 66 | void CWorkerThread::stopWorkThread(bool isStopWorkThread) 67 | { 68 | m_isStopWorkThread = isStopWorkThread; 69 | } 70 | -------------------------------------------------------------------------------- /framework/net-thrift/src/ThriftClientWrapper.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file ThriftClientWrapper.cpp 8 | * @brief thrift客户端通信包装实现文件 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-25 12 | */ 13 | 14 | #include "ThriftClientWrapper.h" 15 | #include "ThriftClientImpl.h" 16 | #include "Message.h" 17 | 18 | ThriftClientWrapper::ThriftClientWrapper() 19 | { 20 | if (m_impl.use_count() == 0) 21 | { 22 | m_impl = std::make_shared(); 23 | } 24 | } 25 | 26 | void ThriftClientWrapper::init(const std::string& ip, unsigned int port) 27 | { 28 | if (m_impl.use_count() != 0) 29 | { 30 | m_impl->init(ip, port); 31 | } 32 | } 33 | 34 | bool ThriftClientWrapper::sendMessage(Message* message, Message* retMessage) 35 | { 36 | if (m_impl.use_count() != 0) 37 | { 38 | return m_impl->sendMessage(message, retMessage); 39 | } 40 | 41 | return false; 42 | } 43 | 44 | void ThriftClientWrapper::deinit() 45 | { 46 | if (m_impl.use_count() != 0) 47 | { 48 | m_impl->deinit(); 49 | } 50 | } 51 | 52 | void ThriftClientWrapper::setConnectMsecTimeoutOnce(unsigned int connectMsecTimeout) 53 | { 54 | if (m_impl.use_count() != 0) 55 | { 56 | m_impl->setConnectMsecTimeoutOnce(connectMsecTimeout); 57 | } 58 | } 59 | 60 | void ThriftClientWrapper::setSendMsecTimeoutOnce(unsigned int sendMsecTimeout) 61 | { 62 | if (m_impl.use_count() != 0) 63 | { 64 | m_impl->setSendMsecTimeoutOnce(sendMsecTimeout); 65 | } 66 | } 67 | 68 | void ThriftClientWrapper::setRecivedMsecTimeoutOnce(unsigned int recivedMsecTimeout) 69 | { 70 | if (m_impl.use_count() != 0) 71 | { 72 | m_impl->setRecivedMsecTimeoutOnce(recivedMsecTimeout); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /variant_visitor/variant_visitor.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _VARIANT_VISITOR_H 2 | #define _VARIANT_VISITOR_H 3 | 4 | #include 5 | #include 6 | 7 | template 8 | struct make_overload_impl : make_overload_impl::type, 9 | make_overload_impl::type 10 | { 11 | using type = make_overload_impl; 12 | using make_overload_impl::type::operator(); 13 | using make_overload_impl::type::operator(); 14 | constexpr explicit make_overload_impl(Function&& func, Args&&... args) 15 | : make_overload_impl::type(std::forward(func)), 16 | make_overload_impl::type(std::forward(args)...) 17 | {} 18 | }; 19 | 20 | template 21 | struct make_overload_impl 22 | { 23 | using type = Function; 24 | }; 25 | 26 | template 27 | struct make_overload_impl 28 | { 29 | using type = make_overload_impl; 30 | using Function = Return(*)(Args...); 31 | constexpr explicit make_overload_impl(const Function&& func) : _func(func) {} 32 | constexpr Return operator()(Args&&... args) const 33 | { 34 | return _func(std::forward(args)...); 35 | } 36 | 37 | private: 38 | Function _func; 39 | }; 40 | 41 | struct make_overload 42 | { 43 | template::type...>::type> 45 | constexpr Overload operator()(Function&&... func) const 46 | { 47 | return Overload(std::forward(func)...); 48 | } 49 | }; 50 | 51 | template 52 | auto make_visitor(Args&&... args) 53 | { 54 | return make_overload()(std::forward(args)...); 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /login_copy/py/dl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import os 5 | import sys 6 | 7 | # 配置文件路径 8 | file_path="/usr/local/etc/ssh_ip.conf" 9 | # 文件保存路径 10 | save_path="~/cx" 11 | 12 | # 读取文件 13 | def readlines(): 14 | results = [] 15 | f = open(file_path) 16 | lines = f.readlines() 17 | for line in lines: 18 | line = line.strip() 19 | if (len(line) != 0): 20 | results.append(line) 21 | 22 | f.close() 23 | return results 24 | 25 | 26 | # 打印IP地址列表 27 | def print_ip_address(ips): 28 | print '\033[1;32m%-10s %-20s %-10s %-15s %9s\033[0m' % ("ID", "IP", "PORT", "用户名", "备注") 29 | i = 0 30 | for line in ips: 31 | i = i + 1 32 | arr = line.split(",") 33 | print '%-10d %-20s %-10s %-15s %s' % (i, arr[0], arr[1], arr[2], arr[3]) 34 | 35 | 36 | # 选择IP地址 37 | def choice_ip_address(): 38 | i = input("从哪里下载:") 39 | return i 40 | 41 | 42 | # 下载文件 43 | def download_file(ips, i, filenames): 44 | if (i >= 1 and i <= len(ips)): 45 | arr = ips[i - 1].split(",") 46 | os.system("scp -P " + arr[1] + " -r " + arr[2] + "@" + arr[0] + ":" + filenames + " " + save_path) 47 | print(filenames + "已经下载到" + save_path) 48 | else: 49 | print("无效的IP地址或用户名!") 50 | 51 | 52 | # main函数 53 | if __name__ == "__main__": 54 | if (len(sys.argv) > 1): 55 | filenames = "" 56 | for filename in sys.argv[1:]: 57 | if (len(filenames) == 0): 58 | filenames = filenames + filename 59 | else: 60 | filenames = filenames + " " + filename 61 | 62 | filenames = "'" + filenames + "'" 63 | ips = readlines() 64 | print_ip_address(ips) 65 | i = choice_ip_address() 66 | download_file(ips, i, filenames) 67 | else: 68 | print("请输入文件名!") 69 | -------------------------------------------------------------------------------- /table_tuple/table_tuple.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | struct element 11 | { 12 | element(const std::string& in_field_name, const T& in_field_value) 13 | : field_name(in_field_name), field_value(in_field_value) {} 14 | std::string field_name; 15 | T field_value; 16 | }; 17 | 18 | template 19 | class table_tuple 20 | { 21 | public: 22 | table_tuple() = default; 23 | 24 | template 25 | table_tuple(Args&&... args) 26 | { 27 | static_assert(sizeof...(Args) == sizeof...(T), "Error: parameter number is wrong"); 28 | create_table_structure(std::forward(args)...); 29 | } 30 | 31 | void insert(T&&... args) 32 | { 33 | std::lock_guard lock(mutex_); 34 | tuple_list_.emplace_back(std::forward_as_tuple(args...)); 35 | } 36 | 37 | void print() 38 | { 39 | for (auto& iter : tuple_list_) 40 | { 41 | std::cout << std::get<0>(iter) << ", " << std::get<1>(iter) 42 | << ", " << std::get<2>(iter) << std::endl; 43 | } 44 | } 45 | 46 | private: 47 | void create_table_structure() {} 48 | template 49 | void create_table_structure(const std::string& first, Args&&... rest) 50 | { 51 | std::size_t size = table_structure_map_.size(); 52 | std::cout << "first: " << first << ", " << size << std::endl; 53 | table_structure_map_.emplace(first, size); 54 | create_table_structure(std::forward(rest)...); 55 | } 56 | 57 | private: 58 | std::unordered_map table_structure_map_; 59 | std::list> tuple_list_; 60 | std::mutex mutex_; 61 | }; 62 | -------------------------------------------------------------------------------- /framework/net-thrift/include/ThriftClientWrapper.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file ThriftClientWrapper.h 8 | * @brief thrift客户端通信包装 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-25 12 | */ 13 | 14 | #ifndef _THRIFTCLIENTWRAPPER_H 15 | #define _THRIFTCLIENTWRAPPER_H 16 | 17 | #include 18 | #include 19 | 20 | class Message; 21 | class ThriftClientImpl; 22 | 23 | /** 24 | * @brief thrift客户端通信包装类 25 | */ 26 | class ThriftClientWrapper 27 | { 28 | public: 29 | ThriftClientWrapper(); 30 | 31 | /** 32 | * @brief init 初始化thrift客户端 33 | * 34 | * @param ip 服务器ip地址 35 | * @param port 服务器端口号,默认为9090 36 | */ 37 | void init(const std::string& ip, unsigned int port = 9090); 38 | 39 | /** 40 | * @brief sendMessage 发送消息到服务器端 41 | * 42 | * @param message 发送的消息 43 | * @param retMessage 接收的消息 44 | * 45 | * @return 成功返回true,否则返回false 46 | */ 47 | bool sendMessage(Message* message, Message* retMessage); 48 | 49 | /** 50 | * @brief deinit 反初始化,释放一些资源 51 | */ 52 | void deinit(); 53 | 54 | /** 55 | * @brief setConnectMsecTimeoutOnce 设置单次连接超时 56 | * 57 | * @param connectMsecTimeout 超时时间,单位:毫秒 58 | */ 59 | void setConnectMsecTimeoutOnce(unsigned int connectMsecTimeout); 60 | 61 | /** 62 | * @brief setSendMsecTimeoutOnce 设置单次发送超时 63 | * 64 | * @param sendMsecTimeout 超时时间,单位:毫秒 65 | */ 66 | void setSendMsecTimeoutOnce(unsigned int sendMsecTimeout); 67 | 68 | /** 69 | * @brief setRecivedMsecTimeoutOnce 设置单次接收超时 70 | * 71 | * @param recivedMsecTimeout 超时时间,单位:毫秒 72 | */ 73 | void setRecivedMsecTimeoutOnce(unsigned int recivedMsecTimeout); 74 | 75 | private: 76 | using ThriftClientImplPtr = std::shared_ptr; 77 | ThriftClientImplPtr m_impl; ///< thrift客户端实现类指针 78 | }; 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /framework/threadpool/include/CThread.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThread.h 8 | * @brief 线程父类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREAD_H 15 | #define _CTHREAD_H 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | /** 22 | * @brief 线程类,封装了std::thread的常用函数 23 | */ 24 | class CThread 25 | { 26 | public: 27 | using ThreadPtr = std::shared_ptr; 28 | 29 | virtual ~CThread() 30 | { 31 | if (m_thread.use_count() != 0) 32 | { 33 | if (m_thread->joinable()) 34 | { 35 | m_thread->join(); 36 | } 37 | } 38 | } 39 | 40 | /** 41 | * @brief run 继承自CThread的类都要实现run函数 42 | */ 43 | virtual void run() = 0; 44 | 45 | public: 46 | /** 47 | * @brief start 启动线程是调用handleThread,handleThread再调用run函数 48 | */ 49 | void start() 50 | { 51 | if (m_thread.use_count() == 0) 52 | { 53 | m_thread = std::make_shared(std::bind(&CThread::handleThread, this)); 54 | } 55 | } 56 | 57 | bool joinable() const 58 | { 59 | assert(m_thread.use_count() != 0); 60 | return m_thread->joinable(); 61 | } 62 | 63 | void join() 64 | { 65 | assert(m_thread.use_count() != 0); 66 | m_thread->join(); 67 | } 68 | 69 | void detach() 70 | { 71 | assert(m_thread.use_count() != 0); 72 | m_thread->detach(); 73 | } 74 | 75 | std::thread::id threadID() const 76 | { 77 | assert(m_thread.use_count() != 0); 78 | return m_thread->get_id(); 79 | } 80 | 81 | private: 82 | void handleThread() 83 | { 84 | run(); 85 | } 86 | 87 | private: 88 | ThreadPtr m_thread; 89 | }; 90 | 91 | using CThreadPtr = std::shared_ptr; 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /framework/clog/src/LogImpl.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file LogImpl.h 8 | * @brief 日志库接口定义 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-12-05 12 | */ 13 | 14 | #ifndef _LOGIMPL_H 15 | #define _LOGIMPL_H 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | /** 26 | * @brief 日志实现类 27 | */ 28 | class LogImpl 29 | { 30 | public: 31 | LogImpl(); 32 | ~LogImpl(); 33 | LogImpl(const LogImpl& var) = delete; 34 | LogImpl& operator=(const LogImpl& var) = delete; 35 | 36 | /** 37 | * @brief getInstance 得到日志实现类指针 38 | * 39 | * @return 日志实现类指针 40 | */ 41 | static LogImpl* getInstance(); 42 | 43 | /** 44 | * @brief logPrint 打印日志 45 | * 46 | * @param priorityLevel 日志优先级 47 | * @param logContent 日志内容 48 | * 49 | * @return 成功返回true,否则返回false 50 | */ 51 | bool logPrint(unsigned int priorityLevel, const std::string& logContent); 52 | 53 | private: 54 | /** 55 | * @brief init 初始化日志框架 56 | * 57 | * @return 成功返回true,否则返回false 58 | */ 59 | bool init(); 60 | 61 | /** 62 | * @brief deinit 释放资源 63 | */ 64 | void deinit(); 65 | 66 | /** 67 | * @brief createLogFile 创建日志输出文件 68 | * 69 | * @return 日志文件路径 70 | */ 71 | std::string createLogFile(); 72 | 73 | /** 74 | * @brief initLogCore 初始化日志框架核心 75 | * 76 | * @param logFileName 日志文件路径 77 | * 78 | * @return 成功返回true,否则返回false 79 | */ 80 | bool initLogCore(const std::string& logFileName); 81 | 82 | private: 83 | log4cpp::Category* m_root = nullptr; ///< 日志类别指针 84 | bool m_isInitSuccess = false; ///< 日志框架是否初始化成功 85 | }; 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CThread.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThread.h 8 | * @brief 线程父类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREAD_H 15 | #define _CTHREAD_H 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | /** 22 | * @brief 线程类,封装了std::thread的常用函数 23 | */ 24 | class CThread 25 | { 26 | public: 27 | using ThreadPtr = std::shared_ptr; 28 | 29 | virtual ~CThread() 30 | { 31 | if (m_thread.use_count() != 0) 32 | { 33 | if (m_thread->joinable()) 34 | { 35 | m_thread->join(); 36 | } 37 | } 38 | } 39 | 40 | /** 41 | * @brief run 继承自CThread的类都要实现run函数 42 | */ 43 | virtual void run() = 0; 44 | 45 | public: 46 | /** 47 | * @brief start 启动线程是调用handleThread,handleThread再调用run函数 48 | */ 49 | void start() 50 | { 51 | if (m_thread.use_count() == 0) 52 | { 53 | m_thread = std::make_shared(std::bind(&CThread::handleThread, this)); 54 | } 55 | } 56 | 57 | bool joinable() const 58 | { 59 | assert(m_thread.use_count() != 0); 60 | return m_thread->joinable(); 61 | } 62 | 63 | void join() 64 | { 65 | assert(m_thread.use_count() != 0); 66 | m_thread->join(); 67 | } 68 | 69 | void detach() 70 | { 71 | assert(m_thread.use_count() != 0); 72 | m_thread->detach(); 73 | } 74 | 75 | std::thread::id threadID() const 76 | { 77 | assert(m_thread.use_count() != 0); 78 | return m_thread->get_id(); 79 | } 80 | 81 | private: 82 | void handleThread() 83 | { 84 | run(); 85 | } 86 | 87 | private: 88 | ThreadPtr m_thread; 89 | }; 90 | 91 | using CThreadPtr = std::shared_ptr; 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /framework/net-tcpserver/include/CThreadPool.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadPool.h 8 | * @brief 线程池类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREADPOOL_H 15 | #define _CTHREADPOOL_H 16 | 17 | #include "CThread.h" 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | class CWorkerThread; 25 | class CJob; 26 | 27 | using CWorkerThreadPtr = std::shared_ptr; 28 | using CJobPtr = std::shared_ptr; 29 | 30 | /** 31 | * @brief 线程池类,管理并发线程 32 | */ 33 | class CThreadPool : public std::enable_shared_from_this 34 | { 35 | public: 36 | friend class CWorkerThread; 37 | 38 | CThreadPool(); 39 | ~CThreadPool(); 40 | 41 | public: 42 | /** 43 | * @brief initThreadNum 初始化线程数量 44 | * 45 | * @param initNumOfThread 线程数量 46 | */ 47 | void initThreadNum(unsigned int initNumOfThread); 48 | 49 | /** 50 | * @brief run 执行具体的job 51 | * 52 | * @param job 具体的任务 53 | */ 54 | void run(CJobPtr job); 55 | 56 | /** 57 | * @brief terminateAll 终止全部的线程 58 | * 等待正在执行的线程执行完毕,等待执行的任务将不会再执行了 59 | */ 60 | void terminateAll(); 61 | 62 | private: 63 | /** 64 | * @brief createIdleThread 创建空闲线程 65 | * 66 | * @param num 线程数量 67 | */ 68 | void createIdleThread(unsigned int num); 69 | 70 | /** 71 | * @brief cleanJobQueue 清除任务队列 72 | */ 73 | void cleanJobQueue(); 74 | 75 | private: 76 | std::vector m_idleList; 77 | 78 | std::condition_variable_any m_jobQueuePutCond; 79 | std::condition_variable_any m_jobQueueGetCond; 80 | std::mutex m_jobQueueMutex; 81 | std::queue m_jobQueue; 82 | 83 | std::atomic m_isStopThreadPool; 84 | }; 85 | 86 | using CThreadPoolPtr = std::shared_ptr; 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /framework/threadpool/include/CThreadPool.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file CThreadPool.h 8 | * @brief 线程池类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-02-17 12 | */ 13 | 14 | #ifndef _CTHREADPOOL_H 15 | #define _CTHREADPOOL_H 16 | 17 | #include "CThread.h" 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | class CWorkerThread; 25 | class CJob; 26 | 27 | using CWorkerThreadPtr = std::shared_ptr; 28 | using CJobPtr = std::shared_ptr; 29 | 30 | /** 31 | * @brief 线程池类,管理并发线程 32 | */ 33 | class CThreadPool : public std::enable_shared_from_this 34 | { 35 | public: 36 | friend class CWorkerThread; 37 | 38 | CThreadPool(); 39 | ~CThreadPool(); 40 | 41 | public: 42 | /** 43 | * @brief initThreadNum 初始化线程数量 44 | * 45 | * @param initNumOfThread 线程数量 46 | */ 47 | void initThreadNum(unsigned int initNumOfThread); 48 | 49 | /** 50 | * @brief run 执行具体的job 51 | * 52 | * @param job 具体的任务 53 | */ 54 | void run(CJobPtr job); 55 | 56 | /** 57 | * @brief terminateAll 终止全部的线程 58 | * 等待正在执行的线程执行完毕,等待执行的任务将不会再执行了 59 | */ 60 | void terminateAll(); 61 | 62 | private: 63 | /** 64 | * @brief createIdleThread 创建空闲线程 65 | * 66 | * @param num 线程数量 67 | */ 68 | void createIdleThread(unsigned int num); 69 | 70 | /** 71 | * @brief cleanJobQueue 清除任务队列 72 | */ 73 | void cleanJobQueue(); 74 | 75 | private: 76 | std::vector m_idleList; 77 | 78 | std::condition_variable_any m_jobQueuePutCond; 79 | std::condition_variable_any m_jobQueueGetCond; 80 | std::mutex m_jobQueueMutex; 81 | std::queue m_jobQueue; 82 | 83 | std::atomic m_isStopThreadPool; 84 | }; 85 | 86 | using CThreadPoolPtr = std::shared_ptr; 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /framework/net-rcf/include/RCFServerWrapper.hpp: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file RCFServerWrapper.hpp 8 | * @brief RCF服务端通信框架包装类 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2016-01-04 12 | */ 13 | 14 | #ifndef _RCFSERVERWRAPPER_H 15 | #define _RCFSERVERWRAPPER_H 16 | 17 | #include "RCFServerImpl.hpp" 18 | 19 | /** 20 | * @brief RCF服务端通信框架包装类 21 | * 22 | * @tparam I_RCFMessageHander 类类型 23 | */ 24 | template 25 | class RCFServerWrapper 26 | { 27 | public: 28 | /** 29 | * @brief RCFServerWrapper 构造函数 30 | * 31 | * @param port 端口号 32 | */ 33 | RCFServerWrapper(unsigned int port) 34 | { 35 | m_impl.reset(); 36 | if (m_impl == NULL) 37 | { 38 | m_impl = boost::make_shared >(port); 39 | } 40 | } 41 | 42 | /** 43 | * @brief start 开启服务器 44 | * 45 | * @tparam RCFMessageHandler 类类型 46 | * @param rcfMessageHandler 消息处理对象 47 | * 48 | * @return 成功返回true,否则返回false 49 | */ 50 | template 51 | bool start(RCFMessageHandler& rcfMessageHandler) 52 | { 53 | assert(m_impl != NULL); 54 | return m_impl->start(rcfMessageHandler); 55 | } 56 | 57 | /** 58 | * @brief stop 停止服务器 59 | * 60 | * @return 成功返回true,否则返回false 61 | */ 62 | bool stop() 63 | { 64 | assert(m_impl != NULL); 65 | return m_impl->stop(); 66 | } 67 | 68 | /** 69 | * @brief clientAddress 获取客户端的ip地址和端口号 70 | * 71 | * @return 返回客户端的ip地址和端口号 72 | */ 73 | std::string clientAddress() const 74 | { 75 | assert(m_impl != NULL); 76 | return m_impl->clientAddress(); 77 | } 78 | 79 | private: 80 | typedef typename boost::shared_ptr > RCFServerImplPtr; 81 | RCFServerImplPtr m_impl; ///< RCF服务器实现类指针 82 | }; 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /framework/net-thrift/tests/ServerTest/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author: 4 | > Mail: 5 | > Created Time: 2015年11月15日 星期日 09时46分39秒 6 | ************************************************************************/ 7 | 8 | #include "ThriftServerWrapper.h" 9 | #include "ProtocolDef.h" 10 | #include "PeopleInfoMessage.h" 11 | #include 12 | #include 13 | 14 | static void messageTask(Message* message, Message* retMessage) 15 | { 16 | if (message == NULL || retMessage == NULL) 17 | { 18 | std::cout << "message or retMessage is NULL" << std::endl; 19 | return; 20 | } 21 | 22 | std::cout << "Recived message type: " << message->m_messageType << std::endl; 23 | 24 | if (message->m_messageType == PEOPLE_INFO_MSG) 25 | { 26 | QueryPeopleInfoMessage queryPeopleInfo; 27 | queryPeopleInfo.deserializeSelf(message->m_data); 28 | 29 | if (queryPeopleInfo.m_cardId == "1234567890") 30 | { 31 | PeopleInfoMessage peopleInfo; 32 | peopleInfo.m_name = "Jack"; 33 | peopleInfo.m_age = 20; 34 | peopleInfo.m_sex = 1; 35 | 36 | Cat cat1; 37 | cat1.m_name = "Tom"; 38 | cat1.m_age = 1; 39 | 40 | Cat cat2; 41 | cat2.m_name = "John"; 42 | cat2.m_age = 2; 43 | 44 | peopleInfo.m_cats.push_back(cat1); 45 | peopleInfo.m_cats.push_back(cat2); 46 | 47 | retMessage->m_data = peopleInfo.serializeSelf(); 48 | } 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | ThriftServerWrapper server; 55 | server.setMessageCallback(messageTask); 56 | server.init(9090); 57 | server.start(); 58 | 59 | std::cout << "Server start..." << std::endl; 60 | 61 | getchar(); 62 | 63 | server.stop(); 64 | server.deinit(); 65 | 66 | std::cout << "Server stoped..." << std::endl; 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /framework/utils/include/FileSystem.h: -------------------------------------------------------------------------------- 1 | /* Copyright(C) 2 | * For free 3 | * All right reserved 4 | * 5 | */ 6 | /** 7 | * @file FileSystem.h 8 | * @brief 文件系统工具实现 9 | * @author highway-9, 787280310@qq.com 10 | * @version 1.1.0 11 | * @date 2015-11-28 12 | */ 13 | 14 | #ifndef _FILESYSTEM_H 15 | #define _FILESYSTEM_H 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | namespace utils 22 | { 23 | /** 24 | * @brief 文件、路径操作 25 | */ 26 | class FileSystem 27 | { 28 | public: 29 | /** 30 | * @brief currentWorkPath 获取当前程序的工作目录 31 | * 32 | * @return 工作目录 33 | * 34 | * @note 失败将返回空字符串 35 | */ 36 | static std::string currentWorkPath(); 37 | 38 | /** 39 | * @brief setCurrentWorkPath 设置当前程序的工作目录 40 | * 41 | * @param path 工作目录 42 | * 43 | * @return 成功返回true,否则返回false 44 | */ 45 | static bool setCurrentWorkPath(const std::string& path); 46 | 47 | /** 48 | * @brief currentExePath 获得当前可执行文件所在目录 49 | * 50 | * @return 可执行文件所在目录 51 | * 52 | * @note 失败将返回空字符串 53 | */ 54 | static std::string currentExePath(); 55 | 56 | /** 57 | * @brief currentExeName 获得当前可执行文件名 58 | * 59 | * @return 可执行文件名 60 | * 61 | * @note 失败将返回空字符串 62 | */ 63 | static std::string currentExeName(); 64 | 65 | /** 66 | * @brief isExists 判断一个文件或目录是否存在 67 | * 68 | * @param path 文件或目录路径 69 | * 70 | * @return 存在返回true,否则返回false 71 | */ 72 | static bool isExists(const std::string& path); 73 | 74 | /** 75 | * @brief mkdir 创建多级目录 76 | * 77 | * @param path 路径 78 | * 79 | * @return 成功返回true,否则返回false 80 | */ 81 | static bool mkdir(const std::string& path); 82 | 83 | /** 84 | * @brief remove 删除文件或目录 85 | * 86 | * @param path 路径 87 | * 88 | * @note 目录里面有内容,返回false,并且不能够递归删除目录 89 | * 90 | * @return 成功返回true,否则返回false 91 | */ 92 | static bool remove(const std::string& path); 93 | }; 94 | } 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /format/format.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /* 3 | 功能:格式化字符串 4 | 日期:2018-06-22 5 | 作者:chxuan <787280310@qq.com> 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "lexical_cast.h" 12 | 13 | template 14 | inline typename std::enable_if<(i == std::tuple_size::value), std::string>::type 15 | get_arg_by_index(Tuple&, std::size_t) 16 | { 17 | throw std::invalid_argument("Arg index out of range"); 18 | } 19 | 20 | template 21 | inline typename std::enable_if<(i < std::tuple_size::value), std::string>::type 22 | get_arg_by_index(Tuple& tp, std::size_t index) 23 | { 24 | if (i == index) 25 | { 26 | return lexical_cast(std::get(tp)); 27 | } 28 | else 29 | { 30 | return get_arg_by_index(tp, index); 31 | } 32 | } 33 | 34 | template 35 | inline std::string format(const char* str, Args&&... args) 36 | { 37 | std::string buf; 38 | auto tp = std::tuple(args...); 39 | char* curr = const_cast(str); 40 | char* last = curr; 41 | int pos = -1; 42 | 43 | while (true) 44 | { 45 | if (*curr == '{' && *(curr + 1) == '}') 46 | { 47 | int len = curr - last; 48 | if (len != 0) 49 | { 50 | buf.append(last, len); 51 | } 52 | 53 | buf.append(get_arg_by_index<0>(tp, ++pos)); 54 | 55 | last = curr + 2; 56 | ++curr; 57 | } 58 | else if (*curr == '\0') 59 | { 60 | int len = curr - last; 61 | if (len != 0) 62 | { 63 | buf.append(last, len); 64 | } 65 | break; 66 | } 67 | 68 | ++curr; 69 | } 70 | 71 | return buf; 72 | } 73 | 74 | template 75 | inline std::string format(const std::string& str, Args&&... args) 76 | { 77 | return format(str.c_str(), std::forward(args)...); 78 | } 79 | -------------------------------------------------------------------------------- /task_timer/task_timer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | class task_timer 12 | { 13 | public: 14 | task_timer() : work_(ios_), timer_(ios_) {} 15 | ~task_timer() 16 | { 17 | destroy(); 18 | } 19 | 20 | void start(std::size_t duration) 21 | { 22 | if (ios_.stopped()) 23 | { 24 | return; 25 | } 26 | 27 | duration_ = duration; 28 | thread_ = std::thread([this]{ ios_.run(); }); 29 | start(); 30 | } 31 | 32 | void start() 33 | { 34 | timer_.expires_from_now(Duration(duration_)); 35 | timer_.async_wait([this](const boost::system::error_code& ec) 36 | { 37 | if (ec) 38 | { 39 | return; 40 | } 41 | 42 | for (auto& func : func_vec_) 43 | { 44 | func(); 45 | } 46 | 47 | if (!is_single_shot_) 48 | { 49 | start(); 50 | } 51 | }); 52 | } 53 | 54 | void stop() 55 | { 56 | timer_.cancel(); 57 | } 58 | 59 | void destroy() 60 | { 61 | stop(); 62 | ios_.stop(); 63 | if (thread_.joinable()) 64 | { 65 | thread_.join(); 66 | } 67 | } 68 | 69 | void bind(const std::function& func) 70 | { 71 | func_vec_.emplace_back(func); 72 | } 73 | 74 | void set_single_shot(bool is_single_shot) 75 | { 76 | is_single_shot_ = is_single_shot; 77 | } 78 | 79 | private: 80 | boost::asio::io_service ios_; 81 | boost::asio::io_service::work work_; 82 | boost::asio::deadline_timer timer_; 83 | std::vector> func_vec_; 84 | std::thread thread_; 85 | std::size_t duration_ = 0; 86 | std::atomic is_single_shot_ { false }; 87 | }; 88 | --------------------------------------------------------------------------------