一些自己常用的代码集合

 

持续更新:

1、c++11自己实现的 信号量事件

头文件.h

  1 #pragma once
  2 #include <mutex>
  3 #include <condition_variable>
  4 #include <chrono>
  5 
  6 
  7 namespace utils
  8 {
  9     class Base_mutex_sync
 10     {
 11     public:
 12         explicit Base_mutex_sync() {}
 13         virtual ~Base_mutex_sync() {}
 14 
 15         /* @ brief: to unlock
 16         *  @ return - void
 17         */
 18         virtual void signal() = 0;
 19 
 20         /* @ brief: to lock
 21         *  @ return - void
 22         */
 23         virtual void wait() = 0;
 24 
 25     protected:
 26         std::condition_variable    _cv;
 27 
 28         std::mutex        _mutex;
 29     };
 30 
 31 //-------------------------------------------------------------------------------------------
 32 
 33     class Semphore : public Base_mutex_sync
 34     {
 35     public:
 36         Semphore() noexcept;
 37         explicit Semphore(const  int count_mutex) noexcept;
 38         virtual ~Semphore() noexcept;
 39 
 40         Semphore(const Semphore &instance) = delete;
 41         Semphore& operator = (const Semphore& instance) = delete;
 42 
 43         // count_mutex must be 
 44         Semphore(const Semphore&& instance) = delete;
 45         Semphore&& operator = (const Semphore&& instance) = delete;
 46 
 47 
 48         void set_lock_count(const unsigned int lock_count = 1);
 49 
 50         //-------------------------------------------------------------
 51 
 52         /* @ brief: to unlock
 53         *  @ return - void
 54         */
 55         void signal();
 56 
 57         /* @ brief: to lock 
 58         *  @ return - void
 59         */
 60         void wait();
 61 
 62     private:
 63         // the  count of lock, default value is 1
 64         int    _count_mutex = 1;
 65     };
 66 
 67 
 68 //-------------------------------------------------------------------------------------------
 69 
 70 
 71 
 72     /**
 73     * @brief: 
 74     */
 75     class Event : public Base_mutex_sync
 76     {
 77     public:
 78         Event()noexcept;
 79         virtual ~Event() noexcept;
 80 
 81         Event(const Event& instance) = delete;
 82         Event& operator = (const Event& instance) = delete;
 83 
 84         Event(const Event&& instance) = delete;
 85         Event&& operator == (const Event&& instance) = delete;
 86 
 87 
 88         /* @ brief: to lock
 89         *  @ return - void
 90         */
 91         void wait();
 92 
 93         bool wait(const unsigned int nsec, const unsigned int mill_sec);
 94 
 95         void notify_one();
 96 
 97         void notify_all();
 98     };
 99     
100 }

实现文件.cc

  1/*
  5 *    @brief:
  6 */
  7 utils::Semphore::~Semphore() noexcept
  8 {
  9 
 10 }
 11 
 12 
 13 
 14 /*
 15 *    @brief:
 16 */
 17 utils::Semphore::Semphore(const int count_mutex) noexcept
 18 {
 19     if (0 < count_mutex)
 20         _count_mutex = count_mutex;
 21     else
 22     {
 23         // do nothing, the default value of _count_mutex is 1
 24     }
 25 }
 26 
 27 
 28 /*
 29 *    @brief:
 30 */
 31 utils::Semphore::Semphore() noexcept
 32 {
 33 
 34 }
 35 
 36 /*
 37 *    @brief:
 38 */
 39 void utils::Semphore::set_lock_count(const unsigned int lock_count /*= 1*/)
 40 {
 41     if (0 < lock_count)
 42         _count_mutex = lock_count;
 43 }
 44 
 45 /*
 46 *    @brief:
 47 */
 48 void utils::Semphore::signal()
 49 {
 50     {
 51         std::unique_lock<std::mutex> locker(_mutex);
 52         ++_count_mutex;
 53     }
 54 
 55     _cv.notify_one();
 56 }
 57 
 58 /*
 59 *    @brief:
 60 */
 61 void utils::Semphore::wait()
 62 {
 63     std::unique_lock<std::mutex> locker(_mutex);
 64     
 65     _cv.wait(locker, [this]() {return _count_mutex > 0; });
 66 
 67     --_count_mutex;
 68 }
 69 
 70 /*
 71 *    @brief:
 72 */
 73 utils::Event::Event() noexcept
 74 {
 75 
 76 }
 77 
 78 /*
 79 *    @brief:
 80 */
 81 utils::Event::~Event() noexcept
 82 {
 83 
 84 }
 85 
 86 /*
 87 *    @brief:
 88 */
 89 void utils::Event::wait()
 90 {
 91     std::unique_lock<std::mutex> locker(_mutex);
 92     _cv.wait(locker);
 93 }
 94 
 95 /*
 96 *    @brief:
 97 */
 98 void utils::Event::notify_one()
 99 {
100     _cv.notify_one();
101 }
102 
103 /*
104 *    @brief:
105 */
106 void utils::Event::notify_all()
107 {
108     _cv.notify_all();
109 }
110 
111 /*
112 *    @brief:
113 */
114 bool utils::Event::wait(const unsigned int sec, const unsigned int mill_sec)
115 {
116     std::unique_lock<std::mutex> locker(_mutex);
117     std::chrono::seconds secs(sec);
118     std::chrono::milliseconds millsecs(mill_sec);
119 
120     auto ret_val = _cv.wait_for(locker, secs + millsecs);
121     return (std::cv_status::timeout != ret_val);
122 }

 

2、Windows获取exe所在路径 

 1 std::string utils::get_cwd()
 2     {
 3         const int arr_size_256 = 256;
 4         char path[arr_size_256] = { 0 };
 5 
 6 #ifdef compiler_is_vs
 7     #ifdef UNICODE
 8             TCHAR tpath[arr_size_256] = { 0 };
 9             GetModuleFileName(NULL, tpath, arr_size_256);
10             tchar2char(tpath, path);
11     #else
12             GetModuleFileName(NULL, path, arr_size_256);
13     #endif ///    
14 #endif
15         // win:path = XX\xx\xx\x\F.exe
16         std::string str_ret_val(path);
17         int last_pos = str_ret_val.find_last_of('\\');
18 
19         // 找不到\\,直接返回
20         if (-1 == last_pos)
21         {
22         }
23         else
24         {
25             // 找到了,去除最后的F.exe
26             str_ret_val = str_ret_val.substr(0, last_pos);
27         }
28 
29         return str_ret_val;
30     }

 

3、windows下: std::string转std::wstring

 1     std::wstring utils::str2wstr_win(const std::string &str)
 2     {
 3         if (str.empty())
 4         {
 5             return std::wstring();
 6         }
 7 
 8         int size = MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), NULL, 0);
 9         std::wstring ret = std::wstring(size, 0);
10         MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), &ret[0], size);
11 
12         return ret;
13     }

 

4、windows: tchar* 转 char* 

 1 void utils::tchar2char(const TCHAR *ptchar_arr, char * pchar_arr)
 2     {
 3         if (NULL == ptchar_arr || nullptr == ptchar_arr)
 4             return;
 5 
 6         if (NULL == pchar_arr || nullptr == pchar_arr)
 7             return;
 8 
 9         int iLength = 0;
10         //获取字节长度   
11         iLength = WideCharToMultiByte(CP_ACP, 0, ptchar_arr, -1, NULL, 0, NULL, NULL);
12 
13         //将tchar值赋给_char    
14         WideCharToMultiByte(CP_ACP, 0, ptchar_arr, -1, pchar_arr, iLength, NULL, NULL);
15     }


5、动态库导出符号

 1 // windows
 2 #if defined(_WIN32) || defined(_WIN64) || defined(WIN32)
 3 
 4 //----------------------------------------------------------------------
 5     // 定义 .dll  导出符号
 6     #ifndef _lib_pipe_api_
 7         #define _lib_pipe_api_    __declspec(dllexport)
 8     #else
 9         #define _lib_pipe_api_    __declspec(dllimport)
10     #endif /// !_lib_pipe_api_
11 //----------------------------------------------------------------------
12 #elif defined(_unix_) || defined(_linux_)
13 //----------------------------------------------------------------------
14     // 定义 .dll  导出符号
15     #ifndef _lib_pipe_api_
16         #define _lib_pipe_api_    __attribute__((visibility ("default")))
17     #endif /// !_lib_pipe_api_
18 //----------------------------------------------------------------------
19 #endif /// !

 

6、cmake配置项目文件分类

FILE(GLOB_RECURSE LibFiles "TceLogger.h")

7、unicode与ansi兼容

  主要是Windows,提供了 两个版本的api,。 

// _UNICODE用于C运行库。
#ifdef _UNICODE
    #ifndef UNICODE
        #define UNICODE
    #endif ///! UNICODE
#endif /// !_UNICODE

// UNICODE用于WINAPI
#ifdef UNICODE
    #ifndef _UNICODE
        #define _UNICODE
    #endif///!_UNICODE
#endif ///!UNICODE

  一个例子:

1         // 为了统一使用Windows的Unicode的api
2         TCHAR  * tc_exe_path = NULL;
3 #ifdef UNICODE
4         std::wstring wstr = utils::str2wstr_win(_pipe_param._base._name);
5         tc_exe_path = const_cast<TCHAR*>(wstr.c_str());
6 #else
7         tc_exe_path = const_cast<TCHAR*>(_pipe_param._base._name.c_str());
8 #endif

 

8、激活windows

  地址: https://myfreeproject.com/soft/81-kmsauto-net-2016.html

  地址: https://cmwtat.cloudmoe.com/cn.html

  

--- 

 

posted @ 2020-09-13 13:21  mohist  阅读(585)  评论(0编辑  收藏  举报