Skip to content

Commit

Permalink
Light all-around fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
deqyra committed Aug 5, 2024
1 parent 982b376 commit 8bf639d
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 79 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ jobs:
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --build-config ${{ matrix.build_type }} --test-dir tests
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
.vscode
build*
compile_commands.json
cpptools/cpptools.cpp
cpptools/_internal/debug_macros.hpp
cpptools/_internal/debug_macros.hpp
cpptools/_internal/undef_debug_macros.hpp
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ on at present.

> **The unit tests for stuff in `cli` fail: _file "xxx" not found_.**
Copy folder `resources` (located in folder `tests`) next to the unit tests executable. CMake should be doing that as post-build task, so if you're encountering that problem as part of building through CMake, please file an issue.
Make sure you're running the test executable through CTest. If for some reason
you need to run the test executable manually, make sure that folder `resources`
(located in folder `tests`) is copied next to the unit tests executable. CMake
should be doing that as a post-build task though, so if you're encountering that
problem as part of building through CMake, please file an issue.

[tree]: https://github.com/deqyra/CppTools/blob/master/cpptools/container
[ranges]: https://github.com/deqyra/CppTools/blob/master/cpptools/utility/ranges.hpp
Expand Down
32 changes: 23 additions & 9 deletions cpptools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ set( CPPTOOLS_TEMPLATE_CONFIG_FILE "${CMAKE_CURRENT_LIST_DIR}/../cmake/cpptools-
set( CPPTOOLS_OUTPUT_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/cpptools-config.cmake" )
set( CPPTOOLS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/cpptools-config-version.cmake" )

# Compile library
add_library( cpptools_objects OBJECT
_internal/assume.hpp
_internal/debug_log.hpp
_internal/debug_macros.hpp
_internal/undef_debug_macros.hpp
_internal/utility_macros.hpp
set( CPPTOOLS_HEADERS
cli/argument_parsing.hpp
cli/command.hpp
cli/command_sequence.hpp
Expand All @@ -34,7 +28,6 @@ add_library( cpptools_objects OBJECT
exception/lookup_exception.hpp
math/sine_generator.hpp
thread/interruptible.hpp
thread/worker.cpp
thread/worker.hpp
utility/bitwise_enum_ops.hpp
utility/clamped_value.hpp
Expand All @@ -47,7 +40,6 @@ add_library( cpptools_objects OBJECT
utility/monitored_value.hpp
utility/predicate.hpp
utility/ranges.hpp
utility/string.cpp
utility/string.hpp
utility/to_string.hpp
utility/type_traits.hpp
Expand All @@ -57,6 +49,28 @@ add_library( cpptools_objects OBJECT
utility/detail/range_base.hpp
)

set( CPPTOOLS_INCLUDE_HEADERS "" )

foreach( HEADER ${CPPTOOLS_HEADERS} )
set( CPPTOOLS_INCLUDE_HEADERS "${CPPTOOLS_INCLUDE_HEADERS}\n#include \"${HEADER}\"")
endforeach()

# Trick to force compilation of inline definitions located in headers
configure_file( cpptools.config.cpp ${CMAKE_CURRENT_LIST_DIR}/cpptools.cpp )

# Compile library
add_library( cpptools_objects OBJECT
cpptools.cpp
_internal/assume.hpp
_internal/debug_log.hpp
_internal/debug_macros.hpp
_internal/undef_debug_macros.hpp
_internal/utility_macros.hpp
${CPPTOOLS_HEADERS}
thread/worker.cpp
utility/string.cpp
)

set( CPPTOOLS_ENABLE_DEBUG $<CONFIG:Debug,RelWithDebInfo> )

target_include_directories( cpptools_objects
Expand Down
12 changes: 12 additions & 0 deletions cpptools/cli/argument_parsing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ class basic_argument_value_map {
return *this;
}

operator std::basic_string_view<Char>() {
return at(0);
}

[[nodiscard]] constexpr decltype(auto) operator[](this auto&& self, std::size_t idx) noexcept {
return self._values[idx];
}

[[nodiscard]] constexpr decltype(auto) at(this auto&& self, std::size_t idx) {
return self._values.at(idx);
}

[[nodiscard]] constexpr auto begin(this auto&& self) noexcept {
return self._values.begin();
}
Expand Down
90 changes: 43 additions & 47 deletions cpptools/cli/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,66 @@

#include "streams.hpp"

namespace tools {
namespace tools::cli {

namespace detail {

template<typename T>
T parse_as(std::string_view input) = delete;
template<typename T>
T parse_as(std::string_view input) = delete;

template<typename T>
std::string_view type_name() = delete;
template<typename T>
std::string_view type_name() = delete;

//
// Specializations of parse_string
//
//
// Specializations of parse_string
//

template<>
CPPTOOLS_API inline std::string parse_as(std::string_view input) {
return std::string(input);
}

template<>
CPPTOOLS_API inline int parse_as(std::string_view input) {
if (!is_integer(input)) {
throw std::invalid_argument("parse_string<int>: String to parse is not exclusively made of digits and a minus sign, or it is at a wrong position.");
template<>
CPPTOOLS_API inline std::string parse_as(std::string_view input) {
return std::string(input);
}
return std::stoi(std::string(input));
}

template<>
CPPTOOLS_API inline bool parse_as(std::string_view input) {
if (input == "y" || input == "yes" || input == "true") {
return true;
template<>
CPPTOOLS_API inline int parse_as(std::string_view input) {
if (!is_integer(input)) {
throw std::invalid_argument("parse_string<int>: String to parse is not exclusively made of digits and a minus sign, or it is at a wrong position.");
}
return std::stoi(std::string(input));
}

if (input == "n" || input == "no" || input == "false") {
return false;
}
template<>
CPPTOOLS_API inline bool parse_as(std::string_view input) {
if (input == "y" || input == "yes" || input == "true") {
return true;
}

throw std::invalid_argument("parse_string<bool>: Invalid string value for expected bool input.");
}
if (input == "n" || input == "no" || input == "false") {
return false;
}

//
// Specializations of type_name
//
throw std::invalid_argument("parse_string<bool>: Invalid string value for expected bool input.");
}

template <>
CPPTOOLS_API inline std::string_view type_name<std::string>() {
return "string";
}
//
// Specializations of type_name
//

template <>
CPPTOOLS_API inline std::string_view type_name<int>() {
return "integer";
}
template <>
CPPTOOLS_API inline std::string_view type_name<std::string>() {
return "string";
}

template <>
CPPTOOLS_API inline std::string_view type_name<bool>() {
return "boolean (\"y\", \"yes\", \"true\", \"n\", \"no\", \"false\")";
}
template <>
CPPTOOLS_API inline std::string_view type_name<int>() {
return "integer";
}

} // namespace detail
template <>
CPPTOOLS_API inline std::string_view type_name<bool>() {
return "boolean (\"y\", \"yes\", \"true\", \"n\", \"no\", \"false\")";
}

namespace cli {
} // namespace detail

template<typename T>
T read_input(cli::streams& streams) {
Expand Down Expand Up @@ -134,8 +132,6 @@ T prompt_bounded(std::string_view title, T min, T max, cli::streams& streams) {
}
}

} // namespace cli

} // namespace tools

#endif//CPPTOOLS_CLI_INPUT
1 change: 1 addition & 0 deletions cpptools/cpptools.config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@CPPTOOLS_INCLUDE_HEADERS@
2 changes: 1 addition & 1 deletion cpptools/exception/parameter_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class parameter_exception : public base_exception {
CPPTOOLS_API parameter_exception(std::string_view parameter_name, std::string parameter_value = "<undefined>") :
base_exception(),
_parameter_name(parameter_name),
_parameter_value(parameter_value)
_parameter_value(std::move(parameter_value))
{
}

Expand Down
34 changes: 21 additions & 13 deletions cpptools/utility/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void strip_cr(std::string& str) {
void strip_c_comments(std::string& str) {
const auto npos = std::string::npos;

// character ranges that belong to comments, in formet {start, count}
std::vector<std::pair<std::size_t, std::size_t>> comment_ranges;

// find all comments
Expand All @@ -66,20 +67,27 @@ void strip_c_comments(std::string& str) {
if (str.at(pos + 1) == '/') { // found "//"
// find eol
auto end_pos = str.find('\n', pos + 2);
if (end_pos != npos) ++end_pos;

comment_ranges.push_back(std::make_pair(pos, end_pos - pos));
pos = end_pos + 1;
} else if (str.at(pos + 1) == '*') {// found "/*"
auto count = (end_pos != npos)
? end_pos - pos
: npos;

comment_ranges.push_back(std::make_pair(pos, count));
pos = (end_pos != npos)
? end_pos + 1
: npos;
} else if (str.at(pos + 1) == '*') { // found "/*"
// find "*/"
auto end_pos = str.find('*', pos + 2);
if (end_pos != npos) ++end_pos;
if (end_pos == npos || str.at(end_pos) == '/')
{
++end_pos;
comment_ranges.push_back(std::make_pair(pos, end_pos - pos));
}
pos = end_pos;
auto end_pos = str.find("*/", pos + 2);
auto count = (end_pos != npos)
? end_pos - pos + 2
: npos;

comment_ranges.push_back(std::make_pair(pos, count));
pos = (end_pos != npos)
? end_pos + 3
: npos;
} else {
++pos;
}

pos = str.find('/', pos);
Expand Down
6 changes: 2 additions & 4 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include( FetchContent )
FetchContent_Declare(
Catch2
URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.5.1.zip
FIND_PACKAGE_ARGS
)

FetchContent_MakeAvailable( Catch2 )
Expand Down Expand Up @@ -51,15 +52,12 @@ add_executable( cpptools_tests
utility/test_string.cpp
utility/test_wrapping_value.cpp
)
target_include_directories( cpptools_tests PUBLIC
${CMAKE_SOURCE_DIR}
)
target_link_libraries( cpptools_tests
cpptools_static Catch2::Catch2WithMain
)

catch_discover_tests( cpptools_tests
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/tests"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

add_custom_command(
Expand Down

0 comments on commit 8bf639d

Please sign in to comment.