Skip to content

Commit f4ddc62

Browse files
authored
Merge pull request #1376 from fnc12/code_quality
Another code quality iteration.
2 parents e246862 + 997effc commit f4ddc62

File tree

12 files changed

+308
-245
lines changed

12 files changed

+308
-245
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,5 @@ StatementMacros:
122122
- QT_REQUIRE_VERSION
123123
TabWidth: 4
124124
UseTab: Never
125+
LineEnding: LF
125126
...

dev/column_result_proxy.h

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
#pragma once
2-
3-
#include "type_traits.h"
4-
#include "table_reference.h"
5-
6-
namespace sqlite_orm {
7-
namespace internal {
8-
1+
#pragma once
2+
3+
#include "type_traits.h"
4+
#include "table_reference.h"
5+
6+
namespace sqlite_orm {
7+
namespace internal {
8+
99
/*
1010
* Holder for the type of an unmapped aggregate/structure/object to be constructed ad-hoc from column results.
1111
* `T` must be constructible using direct-list-initialization.
12-
*/
13-
template<class T, class ColResults>
14-
struct structure {
15-
using type = T;
16-
};
17-
}
18-
}
19-
20-
namespace sqlite_orm {
21-
namespace internal {
22-
23-
template<class T, class SFINAE = void>
24-
struct column_result_proxy : std::remove_const<T> {};
25-
12+
*/
13+
template<class T, class ColResults>
14+
struct structure {
15+
using type = T;
16+
};
17+
}
18+
}
19+
20+
namespace sqlite_orm {
21+
namespace internal {
22+
23+
template<class T, class SFINAE = void>
24+
struct column_result_proxy : std::remove_const<T> {};
25+
2626
/*
2727
* Unwrap `table_reference`
28-
*/
29-
template<class P>
30-
struct column_result_proxy<P, match_if<is_table_reference, P>> : decay_table_ref<P> {};
31-
28+
*/
29+
template<class P>
30+
struct column_result_proxy<P, match_if<is_table_reference, P>> : decay_table_ref<P> {};
31+
3232
/*
3333
* Pass through `structure`
34-
*/
35-
template<class P>
36-
struct column_result_proxy<P, match_specialization_of<P, structure>> : P {};
37-
38-
template<class T>
39-
using column_result_proxy_t = typename column_result_proxy<T>::type;
40-
}
41-
}
34+
*/
35+
template<class P>
36+
struct column_result_proxy<P, match_specialization_of<P, structure>> : P {};
37+
38+
template<class T>
39+
using column_result_proxy_t = typename column_result_proxy<T>::type;
40+
}
41+
}

dev/implementations/table_definitions.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
*/
55
#pragma once
66

7-
#include <type_traits> // std::decay_t
7+
#include <type_traits> // std::remove_reference
88
#include <utility> // std::move
99
#include <algorithm> // std::find_if, std::ranges::find
1010

11+
#include "../tuple_helper/tuple_filter.h"
12+
#include "../type_traits.h"
1113
#include "../type_printer.h"
1214
#include "../schema/column.h"
1315
#include "../schema/table.h"
@@ -20,7 +22,7 @@ namespace sqlite_orm {
2022
std::vector<table_xinfo> res;
2123
res.reserve(filter_tuple_sequence_t<elements_type, is_column>::size());
2224
this->for_each_column([&res](auto& column) {
23-
using field_type = field_type_t<std::decay_t<decltype(column)>>;
25+
using field_type = field_type_t<std::remove_reference_t<decltype(column)>>;
2426
std::string dft;
2527
if(auto d = column.default_value()) {
2628
dft = std::move(*d);

dev/journal_mode.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#pragma once
22

3-
#include <iterator> // std::back_inserter
4-
#include <string> // std::string
5-
#include <memory> // std::unique_ptr
63
#include <array> // std::array
7-
#include <algorithm> // std::transform
4+
#include <string> // std::string
5+
#include <utility> // std::pair
6+
#include <algorithm> // std::ranges::transform
87
#include <cctype> // std::toupper
98

109
#include "serialize_result_type.h"
@@ -35,36 +34,49 @@ namespace sqlite_orm {
3534

3635
namespace internal {
3736

38-
inline const serialize_result_type& to_string(journal_mode value) {
39-
static const std::array<serialize_result_type, 6> res = {
37+
inline const serialize_result_type& journal_mode_to_string(journal_mode value) {
38+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
39+
static constexpr std::array<serialize_result_type, 6> idx2str = {
40+
#else
41+
static const std::array<serialize_result_type, 6> idx2str = {
42+
#endif
4043
"DELETE",
4144
"TRUNCATE",
4245
"PERSIST",
4346
"MEMORY",
4447
"WAL",
4548
"OFF",
4649
};
47-
return res.at(static_cast<int>(value));
50+
return idx2str.at(static_cast<int>(value));
4851
}
4952

50-
inline std::pair<bool, journal_mode> journal_mode_from_string(const std::string& string) {
51-
std::string upperString;
52-
std::transform(string.begin(), string.end(), std::back_inserter(upperString), [](char c) {
53-
return static_cast<char>(std::toupper(static_cast<int>(c)));
54-
});
55-
static const std::array<journal_mode, 6> allValues = {{
53+
inline std::pair<bool, journal_mode> journal_mode_from_string(std::string string) {
54+
static constexpr std::array<journal_mode, 6> journalModes = {{
5655
journal_mode::DELETE,
5756
journal_mode::TRUNCATE,
5857
journal_mode::PERSIST,
5958
journal_mode::MEMORY,
6059
journal_mode::WAL,
6160
journal_mode::OFF,
6261
}};
63-
for(auto journalMode: allValues) {
64-
if(to_string(journalMode) == upperString) {
62+
#if __cpp_lib_ranges >= 201911L
63+
std::ranges::transform(string, string.begin(), [](unsigned char c) noexcept {
64+
return std::toupper(c);
65+
});
66+
if(auto found = std::ranges::find(journalModes, string, journal_mode_to_string);
67+
found != journalModes.end()) SQLITE_ORM_CPP_LIKELY {
68+
return {true, *found};
69+
}
70+
#else
71+
std::transform(string.begin(), string.end(), string.begin(), [](unsigned char c) noexcept {
72+
return std::toupper(c);
73+
});
74+
for(auto journalMode: journalModes) {
75+
if(journal_mode_to_string(journalMode) == string) {
6576
return {true, journalMode};
6677
}
6778
}
79+
#endif
6880
return {false, journal_mode::OFF};
6981
}
7082
}

dev/locking_mode.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <array> // std::array
44
#include <string> // std::string
55
#include <utility> // std::pair
6-
#include <iterator> // std::back_inserter
6+
#include <algorithm> // std::ranges::transform
7+
#include <cctype> // std::toupper
78

89
#include "serialize_result_type.h"
910

@@ -14,28 +15,42 @@ namespace sqlite_orm {
1415
};
1516

1617
namespace internal {
17-
inline const serialize_result_type& to_string(locking_mode value) {
18-
static const std::array<serialize_result_type, 2> res = {
18+
inline const serialize_result_type& locking_mode_to_string(locking_mode value) {
19+
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
20+
static constexpr std::array<serialize_result_type, 2> idx2str = {
21+
#else
22+
static const std::array<serialize_result_type, 2> idx2str = {
23+
#endif
1924
"NORMAL",
2025
"EXCLUSIVE",
2126
};
22-
return res.at(static_cast<int>(value));
27+
return idx2str.at(static_cast<int>(value));
2328
}
2429

25-
inline std::pair<bool, locking_mode> locking_mode_from_string(const std::string& string) {
26-
std::string upperString;
27-
std::transform(string.begin(), string.end(), std::back_inserter(upperString), [](char c) {
28-
return static_cast<char>(std::toupper(static_cast<int>(c)));
29-
});
30-
static const std::array<locking_mode, 2> allValues = {{
30+
inline std::pair<bool, locking_mode> locking_mode_from_string(std::string string) {
31+
static constexpr std::array<locking_mode, 2> lockingModes = {{
3132
locking_mode::NORMAL,
3233
locking_mode::EXCLUSIVE,
3334
}};
34-
for(auto lockingMode: allValues) {
35-
if(to_string(lockingMode) == upperString) {
35+
36+
#if __cpp_lib_ranges >= 201911L
37+
std::ranges::transform(string, string.begin(), [](unsigned char c) noexcept {
38+
return std::toupper(c);
39+
});
40+
if(auto found = std::ranges::find(lockingModes, string, locking_mode_to_string);
41+
found != lockingModes.end()) SQLITE_ORM_CPP_LIKELY {
42+
return {true, *found};
43+
}
44+
#else
45+
std::transform(string.begin(), string.end(), string.begin(), [](unsigned char c) noexcept {
46+
return std::toupper(c);
47+
});
48+
for(auto lockingMode: lockingModes) {
49+
if(locking_mode_to_string(lockingMode) == string) {
3650
return {true, lockingMode};
3751
}
3852
}
53+
#endif
3954
return {false, locking_mode::NORMAL};
4055
}
4156
}

dev/pragma.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ namespace sqlite_orm {
7777
}
7878

7979
void journal_mode(sqlite_orm::journal_mode value) {
80-
this->_journal_mode = -1;
80+
this->journal_mode_ = -1;
8181
this->set_pragma("journal_mode", value);
82-
this->_journal_mode = static_cast<decltype(this->_journal_mode)>(value);
82+
this->journal_mode_ = static_cast<decltype(this->journal_mode_)>(value);
8383
}
8484

8585
/**
@@ -101,9 +101,9 @@ namespace sqlite_orm {
101101
}
102102

103103
void synchronous(int value) {
104-
this->_synchronous = -1;
104+
this->synchronous_ = -1;
105105
this->set_pragma("synchronous", value);
106-
this->_synchronous = value;
106+
this->synchronous_ = value;
107107
}
108108

109109
int user_version() {
@@ -222,8 +222,8 @@ namespace sqlite_orm {
222222
private:
223223
friend struct storage_base;
224224

225-
int _synchronous = -1;
226-
signed char _journal_mode = -1; // if != -1 stores static_cast<sqlite_orm::journal_mode>(journal_mode)
225+
int synchronous_ = -1;
226+
signed char journal_mode_ = -1; // if != -1 stores static_cast<sqlite_orm::journal_mode>(journal_mode)
227227
get_connection_t get_connection;
228228

229229
template<class T>
@@ -245,15 +245,15 @@ namespace sqlite_orm {
245245
this->set_pragma_impl(ss.str(), db);
246246
}
247247

248-
void set_pragma(const std::string& name, const sqlite_orm::journal_mode& value, sqlite3* db = nullptr) {
248+
void set_pragma(const std::string& name, sqlite_orm::journal_mode value, sqlite3* db = nullptr) {
249249
std::stringstream ss;
250-
ss << "PRAGMA " << name << " = " << to_string(value);
250+
ss << "PRAGMA " << name << " = " << journal_mode_to_string(value);
251251
this->set_pragma_impl(ss.str(), db);
252252
}
253253

254-
void set_pragma(const std::string& name, const sqlite_orm::locking_mode& value, sqlite3* db = nullptr) {
254+
void set_pragma(const std::string& name, sqlite_orm::locking_mode value, sqlite3* db = nullptr) {
255255
std::stringstream ss;
256-
ss << "PRAGMA " << name << " = " << to_string(value);
256+
ss << "PRAGMA " << name << " = " << locking_mode_to_string(value);
257257
this->set_pragma_impl(ss.str(), db);
258258
}
259259

0 commit comments

Comments
 (0)