Skip to content

Commit 051560d

Browse files
author
Bo Gao
committed
Merge branch 'feature/format_code' into 'master'
Format code See merge request lidar/cppbase!17
2 parents 8b18bcd + 6fcc0ee commit 051560d

37 files changed

+2007
-1827
lines changed

.clang-format

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
Language: Cpp
44
BasedOnStyle: Google
55
AccessModifierOffset: -4
6-
AllowShortEnumsOnASingleLine: true
7-
AllowShortBlocksOnASingleLine: Never
8-
AllowShortCaseLabelsOnASingleLine: false
96
AllowShortFunctionsOnASingleLine: Inline
10-
AllowShortLambdasOnASingleLine: All
117
AllowShortIfStatementsOnASingleLine: Never
128
AllowShortLoopsOnASingleLine: false
13-
AlwaysBreakBeforeMultilineStrings: false
14-
AlwaysBreakTemplateDeclarations: Yes
159
BreakBeforeBraces: Custom
1610
BraceWrapping:
1711
AfterCaseLabel: true
@@ -26,14 +20,14 @@ BraceWrapping:
2620
AfterExternBlock: true
2721
BeforeCatch: false
2822
BeforeElse: false
29-
BeforeLambdaBody: false
30-
BeforeWhile: false
23+
# BeforeLambdaBody: false
24+
# BeforeWhile: false
3125
IndentBraces: false
3226
SplitEmptyFunction: false
3327
SplitEmptyRecord: false
3428
SplitEmptyNamespace: false
3529
BreakBeforeTernaryOperators: false
36-
ColumnLimit: 129
30+
ColumnLimit: 100
3731
CompactNamespaces: true
3832
IndentWidth: 4
3933
...

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[![pipeline status](http://192.168.10.1/lidar/cppbase/badges/master/pipeline.svg)](http://192.168.10.1/lidar/cppbase/commits/master)
22

33
# cppbase
4-
54
cppbase是一个跨平台C++17基础库,提供一些C++项目常用的功能,如日志、线程池、TCP/UDP客户端以及服务端等。采用CMake编译和集成一系列第三封库(详见thirdparty)。
65

76
Cross-platform C++17 base library providing common features for C++ projects, such as logging, thread pool, TCP/UDP clien and server, etc. It uses CMake to build and integrate a set of thirdparty libraries (see thirdparty folder for details).
87

8+
## 在QtCreator里配置clang-format (Setup clang-format in QtCreator)
9+
* 帮助->关于插件->勾选Beautifier 重启QtCreator (Help->Plugin->Beautifier Restart QtCreator)
10+
* 工具->选项->Beautifier->Clang-Format 添加程序所在目录,并配置风格 (Tools->Options->Beautifier->Clang-Format Choose command and style)
11+
* 工具->选项->环境->键盘->ClangFormat 配置快捷键 (Tools->Options->environment->keyboard->ClangFormat Set hotkeys)

common/BlockingQueue.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ class BlockingQueue
7878
return true;
7979
}
8080

81-
bool Empty() const
82-
{
83-
return m_queue.empty();
84-
}
81+
bool Empty() const { return m_queue.empty(); }
8582

8683
void Clear()
8784
{
@@ -90,15 +87,12 @@ class BlockingQueue
9087
std::swap(m_queue, empty);
9188
}
9289

93-
size_t Size() const
94-
{
95-
return m_queue.size();
96-
}
90+
size_t Size() const { return m_queue.size(); }
9791

9892
private:
9993
std::queue<T> m_queue;
10094
mutable std::mutex m_mutex;
10195
std::condition_variable m_signal;
10296
};
10397

104-
} // namespace cppbase
98+
} // namespace cppbase

common/Encoding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ std::string Base64Encode(std::string_view str);
8080
*/
8181
std::string Base64Decode(std::string_view str);
8282

83-
}}
83+
}} // namespace cppbase::encoding
8484

8585
#include "Encoding_intl.h"

common/Encoding_intl.h

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ std::string ToUTF8(std::wstring_view wstr)
2020
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
2121
return convert.to_bytes((char16_t*)wstr.data(), (char16_t*)wstr.data() + wstr.size());
2222
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
23-
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
23+
std::wstring_convert<std::codecvt_utf8<wchar_t> > convert;
2424
return convert.to_bytes(wstr.data(), wstr.data() + wstr.size());
2525
#elif defined(_WIN32) || defined(_WIN64)
26-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
26+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > convert;
2727
return convert.to_bytes(wstr.data(), wstr.data() + wstr.size());
2828
#endif
2929
}
@@ -35,10 +35,10 @@ std::wstring FromUTF8(std::string_view str)
3535
auto tmp = convert.from_bytes(str.data(), str.data() + str.size());
3636
return std::wstring(tmp.data(), tmp.data() + tmp.size());
3737
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
38-
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
38+
std::wstring_convert<std::codecvt_utf8<wchar_t> > convert;
3939
return convert.from_bytes(str.data(), str.data() + str.size());
4040
#elif defined(_WIN32) || defined(_WIN64)
41-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
41+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > convert;
4242
return convert.from_bytes(str.data(), str.data() + str.size());
4343
#endif
4444
}
@@ -132,7 +132,7 @@ std::u16string UTF32toUTF16(std::u32string_view str)
132132
std::string Base64Encode(std::string_view str)
133133
{
134134
const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
135-
const size_t mods[] = { 0, 2, 1 };
135+
const size_t mods[] = {0, 2, 1};
136136

137137
size_t ilength = str.length();
138138
size_t olength = 4 * ((ilength + 2) / 3);
@@ -162,25 +162,25 @@ std::string Base64Encode(std::string_view str)
162162

163163
std::string Base64Decode(std::string_view str)
164164
{
165-
static const unsigned char base64[256] =
166-
{
167-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
169-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
170-
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171-
0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
172-
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
173-
0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
174-
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
175-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
183-
};
165+
static const unsigned char base64[256] = {
166+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
169+
0x00, 0x00, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00,
170+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
171+
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
172+
0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
173+
0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
174+
0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183+
0x00};
184184

185185
size_t ilength = str.length();
186186

@@ -189,8 +189,10 @@ std::string Base64Decode(std::string_view str)
189189

190190
size_t olength = ilength / 4 * 3;
191191

192-
if (str[ilength - 1] == '=') olength--;
193-
if (str[ilength - 2] == '=') olength--;
192+
if (str[ilength - 1] == '=')
193+
olength--;
194+
if (str[ilength - 2] == '=')
195+
olength--;
194196

195197
std::string result;
196198
result.resize(olength, 0);
@@ -202,15 +204,18 @@ std::string Base64Decode(std::string_view str)
202204
uint32_t sextet_c = str[i] == '=' ? 0 & i++ : base64[(uint8_t)str[i++]];
203205
uint32_t sextet_d = str[i] == '=' ? 0 & i++ : base64[(uint8_t)str[i++]];
204206

205-
uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
206-
207-
if (j < olength) result[j++] = (triple >> 2 * 8) & 0xFF;
208-
if (j < olength) result[j++] = (triple >> 1 * 8) & 0xFF;
209-
if (j < olength) result[j++] = (triple >> 0 * 8) & 0xFF;
207+
uint32_t triple =
208+
(sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
210209

210+
if (j < olength)
211+
result[j++] = (triple >> 2 * 8) & 0xFF;
212+
if (j < olength)
213+
result[j++] = (triple >> 1 * 8) & 0xFF;
214+
if (j < olength)
215+
result[j++] = (triple >> 0 * 8) & 0xFF;
211216
}
212217

213218
return result;
214219
}
215220

216-
}}
221+
}} // namespace cppbase::encoding

common/FileSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ inline std::string GetLogDir() noexcept
5151
return "./log";
5252
}
5353

54-
}} // namespace cppbase::filesystem
54+
}} // namespace cppbase::filesystem

common/Global.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77

88
#pragma once
99

10-
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
11-
# define DECL_EXPORT __declspec(dllexport)
12-
# define DECL_IMPORT __declspec(dllimport)
10+
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
11+
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
12+
#define DECL_EXPORT __declspec(dllexport)
13+
#define DECL_IMPORT __declspec(dllimport)
1314
#else
14-
# define DECL_EXPORT __attribute__((visibility("default")))
15-
# define DECL_IMPORT __attribute__((visibility("default")))
15+
#define DECL_EXPORT __attribute__((visibility("default")))
16+
#define DECL_IMPORT __attribute__((visibility("default")))
1617
#endif
1718

1819
#if defined(API_EXPORTS)
19-
# define DLLEXPORT DECL_EXPORT
20+
#define DLLEXPORT DECL_EXPORT
2021
#else
21-
# define DLLEXPORT DECL_IMPORT
22+
#define DLLEXPORT DECL_IMPORT
2223
#endif
2324

24-
#define DISALLOW_COPY_AND_ASSIGN(T) \
25-
T(const T&) = delete; \
26-
T& operator=(const T&) = delete; \
27-
T(const T&&)= delete; \
28-
T& operator=(const T&&) = delete
25+
#define DISALLOW_COPY_AND_ASSIGN(T) \
26+
T(const T&) = delete; \
27+
T& operator=(const T&) = delete; \
28+
T(const T&&) = delete; \
29+
T& operator=(const T&&) = delete

common/Semaphore.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,25 @@ namespace cppbase {
1616
class Semaphore
1717
{
1818
public:
19-
Semaphore(int32_t count = 0)
20-
: m_count(count)
21-
{}
19+
Semaphore(int32_t count = 0) : m_count(count) {}
2220

23-
~Semaphore()
24-
{
25-
Reset();
26-
}
21+
~Semaphore() { Reset(); }
2722

2823
void Notify()
2924
{
3025
{
3126
std::unique_lock<std::mutex> lock(m_mutex);
3227
m_count++;
3328
}
34-
//notify the waiting thread
29+
// notify the waiting thread
3530
m_cv.notify_one();
3631
}
3732
void Wait()
3833
{
3934
std::unique_lock<std::mutex> lock(m_mutex);
4035
while (m_count == 0)
4136
{
42-
//wait on the mutex until notify is called
37+
// wait on the mutex until notify is called
4338
m_cv.wait(lock);
4439
}
4540
m_count--;
@@ -62,12 +57,11 @@ class Semaphore
6257
m_cv.notify_all();
6358
m_count = 0;
6459
}
65-
6660

6761
private:
6862
std::mutex m_mutex;
6963
std::condition_variable m_cv;
7064
int32_t m_count;
7165
};
7266

73-
}
67+
} // namespace cppbase

common/ThreadPool.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
#pragma once
99

10-
#include <vector>
11-
#include <queue>
12-
#include <memory>
13-
#include <thread>
14-
#include <mutex>
10+
#include <chrono>
1511
#include <condition_variable>
16-
#include <future>
1712
#include <functional>
18-
#include <stdexcept>
13+
#include <future>
1914
#include <iostream>
20-
#include <chrono>
15+
#include <memory>
16+
#include <mutex>
17+
#include <queue>
18+
#include <stdexcept>
19+
#include <thread>
20+
#include <vector>
2121

2222
#include "Global.h"
2323

@@ -61,18 +61,17 @@ class ThreadPool
6161
ThreadPool() = delete;
6262
~ThreadPool();
6363

64-
template<class F, class... Args>
65-
auto Enqueue(F&& f, Args&&... args)
66-
-> std::future<typename std::result_of<F(Args...)>::type>;
64+
template <class F, class... Args>
65+
auto Enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>;
6766

6867
ThreadPriority GetThreadPriority() const;
6968
uint32_t GetReservedCpu() const;
7069

7170
private:
7271
// need to keep track of threads so we can join them
73-
std::vector< std::thread > m_threads;
72+
std::vector<std::thread> m_threads;
7473
// the task queue
75-
std::queue< std::function<void()> > m_tasks;
74+
std::queue<std::function<void()> > m_tasks;
7675

7776
// synchronization
7877
std::mutex m_queue_mutex;
@@ -83,25 +82,24 @@ class ThreadPool
8382
};
8483

8584
// add new work item to the pool
86-
template<class F, class... Args>
85+
template <class F, class... Args>
8786
auto ThreadPool::Enqueue(F&& f, Args&&... args)
8887
-> std::future<typename std::result_of<F(Args...)>::type>
8988
{
9089
using return_type = typename std::result_of<F(Args...)>::type;
9190

92-
auto task = std::make_shared< std::packaged_task<return_type()> >(
93-
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
94-
);
91+
auto task = std::make_shared<std::packaged_task<return_type()> >(
92+
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
9593

9694
std::future<return_type> res = task->get_future();
9795
{
9896
std::unique_lock<std::mutex> lock(m_queue_mutex);
9997

10098
// don't allow enqueueing after stopping the pool
101-
if(m_stop)
99+
if (m_stop)
102100
throw std::runtime_error("enqueue on stopped ThreadPool");
103101

104-
m_tasks.emplace([task](){
102+
m_tasks.emplace([task]() {
105103
using namespace std::chrono;
106104
auto start = high_resolution_clock::now();
107105
(*task)();
@@ -113,6 +111,6 @@ auto ThreadPool::Enqueue(F&& f, Args&&... args)
113111
return res;
114112
}
115113

116-
} // namespace cppbase
114+
} // namespace cppbase
117115

118116
#include "ThreadPool_intl.h"

0 commit comments

Comments
 (0)