Skip to content

Commit e4c7e42

Browse files
committedNov 27, 2021
[common] Improvements
1 parent 567cc29 commit e4c7e42

File tree

12 files changed

+584
-40
lines changed

12 files changed

+584
-40
lines changed
 

‎.github/workflows/deploy_docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: sudo pip install poxy
2525

2626
- name: Configure CMake
27-
run: cmake -B ${{ github.workspace }}/build -S ${{ github.workspace }} -DBETHUTIL-COMMON_BUILD_DOCS=ON -D@BETHUTIL-TEMPLATE@_BUILD_SRC=OFF -DBUILD_TESTING=OFF
27+
run: cmake -B ${{ github.workspace }}/build -S ${{ github.workspace }} -DBETHUTIL-COMMON_BUILD_DOCS=ON -DBETHUTIL-BETHUTIL-COMMON_BUILD_SRC=OFF -DBUILD_TESTING=OFF
2828

2929
- name: Build
3030
run: cmake --build ${{ github.workspace }}/build --target docs

‎LICENSE

+375-21
Large diffs are not rendered by default.

‎docs/poxy.toml.in

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ navbar = [ 'classes', 'namespaces' ]
1616
'https://app.codecov.io/gh/Guekka/bethutil-common'
1717
]
1818

19-
[images]
20-
paths = [ '@DOCS_DIR@/images' ]
21-
2219
[macros]
2320
'protected' = 'private'
2421
'BETHUTIL_VISIBLE' = ''

‎examples/empty.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#include <iostream>
22

3-
int main()
4-
{
5-
std::cout << "Hello, World";
6-
return 0;
7-
}
3+
void unused() {}

‎include/btu/common/algorithms.hpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Copyright (C) 2021 Edgar B
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
#pragma once
6+
7+
#include <algorithm>
8+
#include <cassert>
9+
#include <cmath>
10+
#include <filesystem>
11+
#include <iterator>
12+
#include <string>
13+
#include <string_view>
14+
#include <type_traits>
15+
16+
namespace btu::common {
17+
//Expects a range sorted in descending order
18+
template<class It, class Predicate, class Sum>
19+
[[nodiscard]] inline It merge_if(It first, It last, const Predicate &predicate, const Sum &sum)
20+
{
21+
if (first == last)
22+
return last;
23+
24+
last--;
25+
while (first != last)
26+
{
27+
while (predicate(*first, *last))
28+
{
29+
*first = sum(*first, *last);
30+
last--;
31+
if (first == last)
32+
return ++first;
33+
}
34+
first++;
35+
}
36+
return ++first;
37+
}
38+
39+
template<class It, class Predicate>
40+
[[nodiscard]] inline It merge_if(It first, It last, Predicate &&predicate)
41+
{
42+
using Type = typename std::iterator_traits<It>::value_type;
43+
auto plus = [](const Type &first, const Type &second) { return first + second; };
44+
return merge_if(first, last, std::forward<Predicate>(predicate), plus);
45+
}
46+
47+
template<class Container, class Predicate>
48+
[[nodiscard]] inline auto merge_if(Container &cont, Predicate &&predicate)
49+
{
50+
using namespace std;
51+
return merge_if(begin(cont), end(cont), forward<Predicate>(predicate));
52+
}
53+
54+
template<class Container, class Predicate, class Sum>
55+
[[nodiscard]] inline auto merge_if(Container &cont, Predicate &&pred, Sum &&sum)
56+
{
57+
using namespace std;
58+
return merge_if(begin(cont), end(cont), forward<Predicate>(pred), forward<Sum>(sum));
59+
}
60+
61+
template<class Container, class ValueType>
62+
[[nodiscard]] inline auto contains(const Container &cont, const ValueType &val)
63+
{
64+
using namespace std;
65+
return find(begin(cont), end(cont), val) != end(cont);
66+
}
67+
68+
template<class Container, class Predicate>
69+
[[nodiscard]] inline auto find_if(const Container &cont, const Predicate &pred)
70+
{
71+
using namespace std;
72+
return find_if(begin(cont), end(cont), pred);
73+
}
74+
75+
template<typename Cont, typename Pred>
76+
void erase_if(Cont &cont, const Pred &pred)
77+
{
78+
using namespace std;
79+
cont.erase(remove_if(begin(cont), end(cont), pred), end(cont));
80+
}
81+
} // namespace btu::common

‎include/btu/common/games.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "path.hpp"
4+
5+
namespace btu::common {
6+
enum class Game
7+
{
8+
TES3,
9+
TES4,
10+
FNV,
11+
SLE,
12+
SSE,
13+
FO4,
14+
Custom
15+
};
16+
17+
} // namespace btu::common

‎include/btu/common/path.hpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright (C) 2021 Edgar B
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
6+
#pragma once
7+
8+
#include "string.hpp"
9+
10+
#include <filesystem>
11+
12+
namespace btu::common {
13+
using Path = std::filesystem::path;
14+
using OsString = Path::string_type;
15+
using OsChar = OsString::value_type;
16+
17+
inline Path to_lower(const Path &path)
18+
{
19+
return to_lower<OsChar>(path.native());
20+
}
21+
22+
} // namespace btu::common

‎include/btu/common/string.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (C) 2021 Edgar B
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
6+
#pragma once
7+
8+
#include <algorithm>
9+
#include <cctype>
10+
#include <iterator>
11+
#include <string>
12+
13+
namespace btu::common {
14+
15+
template<typename CharT>
16+
using StrComparePred = bool (*)(CharT, CharT);
17+
18+
template<typename CharT>
19+
StrComparePred<CharT> str_compare_pred(bool case_sensitive = true)
20+
{
21+
if (case_sensitive)
22+
return [](CharT ch1, CharT ch2) { return ch1 == ch2; };
23+
24+
return [](CharT ch1, CharT ch2) {
25+
ch1 = ::tolower(ch1);
26+
ch2 = ::tolower(ch2);
27+
28+
return ch1 == ch2;
29+
};
30+
}
31+
32+
template<class CharT>
33+
bool str_compare(std::basic_string_view<CharT> string,
34+
std::basic_string_view<CharT> other,
35+
bool case_sensitive = true)
36+
{
37+
auto pred = str_compare_pred<CharT>(case_sensitive);
38+
39+
using namespace std;
40+
return string.size() == other.size() && equal(cbegin(other), cend(other), cbegin(string), pred);
41+
}
42+
43+
template<class CharT>
44+
std::basic_string<CharT> to_lower(std::basic_string_view<CharT> str)
45+
{
46+
std::basic_string<CharT> res;
47+
res.reserve(str.size());
48+
std::transform(str.begin(), str.end(), std::back_inserter(res), [](auto &&c) { return ::tolower(c); });
49+
return res;
50+
}
51+
} // namespace btu::common

‎include/btu/common/template.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (C) 2021 Edgar B
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5+
6+
#pragma once
7+
8+
#include <type_traits>
9+
#include <vector>
10+
11+
namespace btu::common {
12+
template<typename T, typename U>
13+
using is_equiv = std::is_same<std::remove_cvref<T>, std::remove_cvref<U>>;
14+
15+
template<typename T, typename U>
16+
constexpr bool is_equiv_v = is_equiv<T, U>::value;
17+
18+
} // namespace btu::common

‎src/CMakeLists.txt

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
set(ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
22

3-
set(INCLUDE_DIR "${ROOT_DIR}/include/btu")
3+
set(INCLUDE_DIR "${ROOT_DIR}/include")
44
set(HEADER_FILES
5-
"${INCLUDE_DIR}/common.hpp"
5+
"${INCLUDE_DIR}/btu/common.hpp"
6+
7+
"${INCLUDE_DIR}/btu/common/algorithms.hpp"
8+
"${INCLUDE_DIR}/btu/common/games.hpp"
9+
"${INCLUDE_DIR}/btu/common/path.hpp"
10+
"${INCLUDE_DIR}/btu/common/string.hpp"
611
)
712

813
set(SOURCE_DIR "${ROOT_DIR}/src")
914
set(SOURCE_FILES
10-
15+
"${SOURCE_DIR}/empty.cpp"
1116
)
1217

1318
source_group(
@@ -54,11 +59,12 @@ endif()
5459

5560
target_include_directories(
5661
"${PROJECT_NAME}"
57-
PUBLIC
62+
SYSTEM PUBLIC
5863
"$<BUILD_INTERFACE:${INCLUDE_DIR}>"
5964
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
60-
PRIVATE
61-
"${SOURCE_DIR}"
65+
PRIVATE
66+
"$<BUILD_INTERFACE:${INCLUDE_DIR}>"
67+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
6268
)
6369

6470
install(
@@ -68,7 +74,7 @@ install(
6874

6975
install(
7076
EXPORT "${PROJECT_NAME}-targets"
71-
NAMESPACE "${PROJECT_NAME}::"
77+
NAMESPACE "btu::"
7278
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
7379
)
7480

@@ -87,13 +93,12 @@ install(
8793
FILES
8894
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
8995
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
90-
"${ROOT_DIR}/cmake/FindLZ4.cmake"
9196
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
9297
)
9398

9499
install(
95-
DIRECTORY "${INCLUDE_DIR}/bethutil/template"
96-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/bethutil"
100+
DIRECTORY "${INCLUDE_DIR}/btu/common"
101+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/btu"
97102
)
98103

99104
install(

‎src/main.cpp ‎src/empty.cpp

File renamed without changes.

‎tests/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ add_executable(
2323
${HEADER_FILES}
2424
${SOURCE_FILES}
2525
)
26+
27+
find_package(Catch2 CONFIG REQUIRED)
28+
include(Catch)
2629
catch_discover_tests(tests)
2730

2831
target_compile_definitions(

0 commit comments

Comments
 (0)
Please sign in to comment.