Apply clang-format and cmake-format and add style check workflow (#171)

* apply clang-format and cmake-format and add style check workflow

* add declare package definition

* add additional public methods and rename internals

* change development verison tag to 1.0.0

* rename internal method

* rename public method

* rename test var

* update copyright and fix comment

* typo

* run fix-format

* fix test function names
This commit is contained in:
Lars Melchior
2021-01-06 14:40:33 +01:00
committed by GitHub
parent cf3f62b6f2
commit 1ebbac6332
51 changed files with 728 additions and 593 deletions

View File

@@ -12,44 +12,36 @@ CPMAddPackage(
NAME asio
VERSION 1.16.1
GITHUB_REPOSITORY chriskohlhoff/asio
GIT_TAG asio-1-16-1 # asio uses non-standard version tag, we must specify GIT_TAG
GIT_TAG asio-1-16-1 # asio uses non-standard version tag, we must specify GIT_TAG
)
# ASIO doesn't use CMake, we have to configure it manually.
# Extra notes for using on Windows:
# 1) If _WIN32_WINNT is not set, ASIO assumes _WIN32_WINNT=0x0501, i.e. Windows XP target,
# which is definitely not the platform which most users target.
# ASIO doesn't use CMake, we have to configure it manually. Extra notes for using on Windows:
#
# 1) If _WIN32_WINNT is not set, ASIO assumes _WIN32_WINNT=0x0501, i.e. Windows XP target, which is
# definitely not the platform which most users target.
#
# 2) WIN32_LEAN_AND_MEAN is defined to make Winsock2 work.
if(asio_ADDED)
add_library(asio INTERFACE)
target_include_directories(asio
INTERFACE ${asio_SOURCE_DIR}/asio/include
)
target_include_directories(asio INTERFACE ${asio_SOURCE_DIR}/asio/include)
target_compile_definitions(asio
INTERFACE
ASIO_STANDALONE
ASIO_NO_DEPRECATED
)
target_compile_definitions(asio INTERFACE ASIO_STANDALONE ASIO_NO_DEPRECATED)
target_link_libraries(asio
INTERFACE
Threads::Threads
)
target_link_libraries(asio INTERFACE Threads::Threads)
if(WIN32)
# macro see @ https://stackoverflow.com/a/40217291/1746503
macro(get_win32_winnt version)
if (CMAKE_SYSTEM_VERSION)
if(CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if ("${verMajor}" MATCHES "10")
if("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif ("${verMajor}" MATCHES "10")
endif("${verMajor}" MATCHES "10")
# Remove all remaining '.' characters.
string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
@@ -65,11 +57,7 @@ if(asio_ADDED)
message(STATUS "Set _WIN32_WINNET=${_WIN32_WINNT}")
target_compile_definitions(asio
INTERFACE
_WIN32_WINNT=${_WIN32_WINNT}
WIN32_LEAN_AND_MEAN
)
target_compile_definitions(asio INTERFACE _WIN32_WINNT=${_WIN32_WINNT} WIN32_LEAN_AND_MEAN)
endif()
endif()

View File

@@ -20,45 +20,31 @@
using asio::ip::tcp;
class session
: public std::enable_shared_from_this<session>
{
class session : public std::enable_shared_from_this<session> {
public:
session(tcp::socket socket)
: socket_(std::move(socket))
{
}
session(tcp::socket socket) : socket_(std::move(socket)) {}
void start()
{
do_read();
}
void start() { do_read(); }
private:
void do_read()
{
void do_read() {
auto self(shared_from_this());
socket_.async_read_some(asio::buffer(data_, max_length),
[this, self](std::error_code ec, std::size_t length)
{
if (!ec)
{
do_write(length);
}
});
[this, self](std::error_code ec, std::size_t length) {
if (!ec) {
do_write(length);
}
});
}
void do_write(std::size_t length)
{
void do_write(std::size_t length) {
auto self(shared_from_this());
asio::async_write(socket_, asio::buffer(data_, length),
[this, self](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
do_read();
}
});
[this, self](std::error_code ec, std::size_t /*length*/) {
if (!ec) {
do_read();
}
});
}
tcp::socket socket_;
@@ -66,39 +52,30 @@ private:
char data_[max_length];
};
class server
{
class server {
public:
server(asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
{
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)) {
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
[this](std::error_code ec, tcp::socket socket)
{
if (!ec)
{
std::make_shared<session>(std::move(socket))->start();
}
void do_accept() {
acceptor_.async_accept([this](std::error_code ec, tcp::socket socket) {
if (!ec) {
std::make_shared<session>(std::move(socket))->start();
}
do_accept();
});
do_accept();
});
}
tcp::acceptor acceptor_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
int main(int argc, char* argv[]) {
try {
if (argc != 2) {
std::cerr << "Usage: async_tcp_echo_server <port>\n";
return 1;
}
@@ -108,9 +85,7 @@ int main(int argc, char* argv[])
server s(io_context, std::atoi(argv[1]));
io_context.run();
}
catch (std::exception& e)
{
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}

View File

@@ -16,11 +16,10 @@ CPMAddPackage(
NAME benchmark
GITHUB_REPOSITORY google/benchmark
VERSION 1.5.0
OPTIONS
"BENCHMARK_ENABLE_TESTING Off"
OPTIONS "BENCHMARK_ENABLE_TESTING Off"
)
if (benchmark_ADDED)
if(benchmark_ADDED)
# patch google benchmark target
set_target_properties(benchmark PROPERTIES CXX_STANDARD 17)
endif()
@@ -28,5 +27,5 @@ endif()
# ---- Executable ----
add_executable(CPMExampleBenchmark "main.cpp")
set_target_properties(CPMExampleBenchmark PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMExampleBenchmark PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMExampleBenchmark fibonacci benchmark)

View File

@@ -1,14 +1,13 @@
#include <benchmark/benchmark.h>
#include <vector>
#include <algorithm>
#include <random>
#include <fibonacci.h>
#include <algorithm>
#include <random>
#include <vector>
std::vector<unsigned> createTestNumbers(){
std::vector<unsigned> createTestNumbers() {
std::vector<unsigned> v;
for (int i=0;i<25;++i) v.emplace_back(i);
for (int i = 0; i < 25; ++i) v.emplace_back(i);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
@@ -18,7 +17,7 @@ std::vector<unsigned> createTestNumbers(){
void fibonacci(benchmark::State& state) {
auto numbers = createTestNumbers();
for (auto _ : state) {
for (auto v: numbers) benchmark::DoNotOptimize(fibonacci(v));
for (auto v : numbers) benchmark::DoNotOptimize(fibonacci(v));
}
}
@@ -27,7 +26,7 @@ BENCHMARK(fibonacci);
void fastFibonacci(benchmark::State& state) {
auto numbers = createTestNumbers();
for (auto _ : state) {
for (auto v: numbers) benchmark::DoNotOptimize(fastFibonacci(v));
for (auto v : numbers) benchmark::DoNotOptimize(fastFibonacci(v));
}
}

View File

@@ -8,17 +8,13 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!" << std::endl;
}
void print(const boost::system::error_code& /*e*/) { std::cout << "Hello, world!" << std::endl; }
int main()
{
int main() {
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));

View File

@@ -26,5 +26,5 @@ set_target_properties(CPMExampleCatch2 PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ----
ENABLE_TESTING()
ADD_TEST(CPMExampleCatch2 CPMExampleCatch2)
enable_testing()
add_test(CPMExampleCatch2 CPMExampleCatch2)

View File

@@ -1,9 +1,10 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <fibonacci.h>
TEST_CASE("fibonacci"){
#include <catch2/catch.hpp>
TEST_CASE("fibonacci") {
REQUIRE(fibonacci(0) == 0);
REQUIRE(fibonacci(1) == 1);
REQUIRE(fibonacci(2) == 1);
@@ -13,8 +14,8 @@ TEST_CASE("fibonacci"){
REQUIRE(fibonacci(13) == 233);
}
TEST_CASE("fastFibonacci"){
for (unsigned i=0; i<25; ++i){
TEST_CASE("fastFibonacci") {
for (unsigned i = 0; i < 25; ++i) {
REQUIRE(fibonacci(i) == fastFibonacci(i));
}
}

View File

@@ -10,13 +10,11 @@ CPMAddPackage(
NAME cereal
VERSION 1.2.2
GITHUB_REPOSITORY USCiLab/cereal
OPTIONS
"SKIP_PORTABILITY_TEST ON"
"JUST_INSTALL_CEREAL ON"
OPTIONS "SKIP_PORTABILITY_TEST ON" "JUST_INSTALL_CEREAL ON"
)
# ---- Create binary ----
add_executable(CPMExampleCereal main.cpp)
target_link_libraries(CPMExampleCereal cereal )
target_link_libraries(CPMExampleCereal cereal)
set_target_properties(CPMExampleCereal PROPERTIES CXX_STANDARD 17)

View File

@@ -1,28 +1,20 @@
#define CATCH_CONFIG_MAIN
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>
#include <string>
#include <cereal/cereal.hpp>
#include <sstream>
#include <string>
struct player_data
{
struct player_data {
int id{-1};
std::string name{};
};
template<typename Archive>
void serialize(Archive& archive, player_data const &data)
{
archive(
cereal::make_nvp("id", data.id),
cereal::make_nvp("name", data.name)
);
template <typename Archive> void serialize(Archive &archive, player_data const &data) {
archive(cereal::make_nvp("id", data.id), cereal::make_nvp("name", data.name));
}
int main(int argc, char const *argv[])
{
int main(int argc, char const *argv[]) {
player_data player{3, "Gamer One"};
std::ostringstream oss;
cereal::JSONOutputArchive output(oss);

View File

@@ -10,9 +10,7 @@ CPMAddPackage(
NAME cxxopts
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 2.2.0
OPTIONS
"CXXOPTS_BUILD_EXAMPLES Off"
"CXXOPTS_BUILD_TESTS Off"
OPTIONS "CXXOPTS_BUILD_EXAMPLES Off" "CXXOPTS_BUILD_TESTS Off"
)
# ---- Create binary ----

View File

@@ -1,21 +1,19 @@
#include <cxxopts.hpp>
#include <iostream>
int main(int argc, char ** argv) {
int main(int argc, char** argv) {
cxxopts::Options options("MyProgram", "One line description of MyProgram");
options.add_options()
("h,help", "Show help")
("d,debug", "Enable debugging")
("f,file", "File name", cxxopts::value<std::string>());
options.add_options()("h,help", "Show help")("d,debug", "Enable debugging")(
"f,file", "File name", cxxopts::value<std::string>());
auto result = options.parse(argc, argv);
if (result["help"].as<bool>()) {
std::cout << options.help() << std::endl;
return 0;
}
for (auto arg: result.arguments()) {
for (auto arg : result.arguments()) {
std::cout << "option: " << arg.key() << ": " << arg.value() << std::endl;
}

View File

@@ -26,5 +26,5 @@ set_target_properties(CPMExampleDoctest PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ----
ENABLE_TESTING()
ADD_TEST(CPMExampleDoctest CPMExampleDoctest)
enable_testing()
add_test(CPMExampleDoctest CPMExampleDoctest)

View File

@@ -3,7 +3,7 @@
#include <doctest/doctest.h>
#include <fibonacci.h>
TEST_CASE("fibonacci"){
TEST_CASE("fibonacci") {
CHECK(fibonacci(0) == 0);
CHECK(fibonacci(1) == 1);
CHECK(fibonacci(2) == 1);
@@ -13,8 +13,8 @@ TEST_CASE("fibonacci"){
CHECK(fibonacci(13) == 233);
}
TEST_CASE("fastfibonacci"){
for (unsigned i=0; i<25; ++i){
TEST_CASE("fastfibonacci") {
for (unsigned i = 0; i < 25; ++i) {
CHECK(fibonacci(i) == fastFibonacci(i));
}
}

View File

@@ -14,7 +14,7 @@ CPMAddPackage(
DOWNLOAD_ONLY True
)
if (EnTT_ADDED)
if(EnTT_ADDED)
add_library(EnTT INTERFACE)
target_include_directories(EnTT INTERFACE ${EnTT_SOURCE_DIR}/src)
endif()
@@ -22,5 +22,5 @@ endif()
# ---- Executable ----
add_executable(CPMEnTTExample "main.cpp")
set_target_properties(CPMEnTTExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMEnTTExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMEnTTExample EnTT)

View File

@@ -1,54 +1,56 @@
#include <entt/entt.hpp>
#include <cstdint>
#include <entt/entt.hpp>
struct position {
float x;
float y;
float x;
float y;
};
struct velocity {
float dx;
float dy;
float dx;
float dy;
};
void update(entt::registry &registry) {
auto view = registry.view<position, velocity>();
auto view = registry.view<position, velocity>();
for(auto entity: view) {
// gets only the components that are going to be used ...
for (auto entity : view) {
// gets only the components that are going to be used ...
auto &vel = view.get<velocity>(entity);
auto &vel = view.get<velocity>(entity);
vel.dx = 0.;
vel.dy = 0.;
vel.dx = 0.;
vel.dy = 0.;
// ...
}
// ...
}
}
void update(std::uint64_t dt, entt::registry &registry) {
registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
// gets all the components of the view at once ...
registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
// gets all the components of the view at once ...
pos.x += vel.dx * dt;
pos.y += vel.dy * dt;
pos.x += vel.dx * dt;
pos.y += vel.dy * dt;
// ...
});
// ...
});
}
int main() {
entt::registry registry;
std::uint64_t dt = 16;
entt::registry registry;
std::uint64_t dt = 16;
for(auto i = 0; i < 10; ++i) {
auto entity = registry.create();
registry.assign<position>(entity, i * 1.f, i * 1.f);
if(i % 2 == 0) { registry.assign<velocity>(entity, i * .1f, i * .1f); }
for (auto i = 0; i < 10; ++i) {
auto entity = registry.create();
registry.assign<position>(entity, i * 1.f, i * 1.f);
if (i % 2 == 0) {
registry.assign<velocity>(entity, i * .1f, i * .1f);
}
}
update(dt, registry);
update(registry);
update(dt, registry);
update(registry);
// ...
// ...
}

View File

@@ -15,5 +15,5 @@ CPMAddPackage(
# ---- Executable ----
add_executable(CPMFmtExample "main.cpp")
set_target_properties(CPMFmtExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMFmtExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMFmtExample fmt)

View File

@@ -1,6 +1,6 @@
#include <fmt/format.h>
int main(){
int main() {
fmt::print("Hello, {}!\n", "world");
return 0;
}

View File

@@ -17,9 +17,7 @@ CPMAddPackage(
GITHUB_REPOSITORY google/googletest
GIT_TAG release-1.8.1
VERSION 1.8.1
OPTIONS
"INSTALL_GTEST OFF"
"gtest_force_shared_crt"
OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
)
# ---- Create binary ----
@@ -30,5 +28,5 @@ set_target_properties(CPMExampleGtest PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ----
enable_testing()
enable_testing()
add_test(CPMExampleGtest CPMExampleGtest)

View File

@@ -1,8 +1,7 @@
#include <gtest/gtest.h>
#include <fibonacci.h>
#include <gtest/gtest.h>
TEST(FibonacciTests, BasicChecks)
{
TEST(FibonacciTests, BasicChecks) {
ASSERT_TRUE(fibonacci(0) == 0);
ASSERT_TRUE(fibonacci(1) == 1);
ASSERT_TRUE(fibonacci(2) == 1);

View File

@@ -8,7 +8,7 @@ include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME nlohmann_json
VERSION 3.6.1
VERSION 3.6.1
# not using the repo as it takes forever to clone
URL https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip
URL_HASH SHA256=69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf
@@ -22,5 +22,5 @@ endif()
# ---- Executable ----
add_executable(CPMJSONExample "main.cpp")
set_target_properties(CPMJSONExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMJSONExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMJSONExample nlohmann_json)

View File

@@ -1,23 +1,15 @@
#include <nlohmann/json.hpp>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
int main(){
nlohmann::json json = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
int main() {
nlohmann::json json = {{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42}}},
{"list", {1, 0, 2}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
std::cout << "declared JSON object: " << std::setw(2) << json << std::endl;

View File

@@ -20,5 +20,5 @@ endif()
# ---- Executable ----
add_executable(CPMlinenoiseExample "main.cpp")
set_target_properties(CPMlinenoiseExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMlinenoiseExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMlinenoiseExample linenoise)

View File

@@ -1,64 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linenoise.h"
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
if (buf[0] == 'h') {
linenoiseAddCompletion(lc, "hello");
linenoiseAddCompletion(lc, "hello there");
}
}
int main(int argc, char **argv) {
char *line;
char *prgname = argv[0];
char *line;
char *prgname = argv[0];
/* Parse options, with --multiline we enable multi line editing. */
while(argc > 1) {
argc--;
argv++;
if (!strcmp(*argv,"--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv,"--keycodes")) {
linenoisePrintKeyCodes();
exit(0);
} else {
fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname);
exit(1);
}
/* Parse options, with --multiline we enable multi line editing. */
while (argc > 1) {
argc--;
argv++;
if (!strcmp(*argv, "--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv, "--keycodes")) {
linenoisePrintKeyCodes();
exit(0);
} else {
fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname);
exit(1);
}
}
/* Set the completion callback. This will be called every time the
* user uses the <tab> key. */
linenoiseSetCompletionCallback(completion);
/* Set the completion callback. This will be called every time the
* user uses the <tab> key. */
linenoiseSetCompletionCallback(completion);
/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while((line = linenoise("hello> ")) != NULL) {
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
linenoiseHistoryAdd(line); /* Add to the history. */
linenoiseHistorySave("history.txt"); /* Save the history on disk. */
} else if (!strncmp(line,"/historylen",11)) {
/* The "/historylen" command will change the history len. */
int len = atoi(line+11);
linenoiseHistorySetMaxLen(len);
} else if (line[0] == '/') {
printf("Unreconized command: %s\n", line);
}
free(line);
/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while ((line = linenoise("hello> ")) != NULL) {
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
linenoiseHistoryAdd(line); /* Add to the history. */
linenoiseHistorySave("history.txt"); /* Save the history on disk. */
} else if (!strncmp(line, "/historylen", 11)) {
/* The "/historylen" command will change the history len. */
int len = atoi(line + 11);
linenoiseHistorySetMaxLen(len);
} else if (line[0] == '/') {
printf("Unreconized command: %s\n", line);
}
return 0;
free(line);
}
return 0;
}

View File

@@ -14,7 +14,7 @@ CPMAddPackage(
DOWNLOAD_ONLY True
)
if(range-v3_ADDED)
if(range-v3_ADDED)
add_library(range-v3 INTERFACE IMPORTED)
target_include_directories(range-v3 INTERFACE "${range-v3_SOURCE_DIR}/include")
endif()
@@ -22,5 +22,5 @@ endif()
# ---- Executable ----
add_executable(CPMRangev3Example "main.cpp")
set_target_properties(CPMRangev3Example PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMRangev3Example PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMRangev3Example range-v3)

View File

@@ -18,27 +18,24 @@
// vector all_of is_six: false
// vector none_of is_six: false
#include <iostream>
#include <range/v3/algorithm/all_of.hpp>
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/algorithm/for_each.hpp>
#include <range/v3/algorithm/none_of.hpp>
#include <range/v3/view/all.hpp>
#include <iostream>
#include <vector>
using std::cout;
auto is_six = [](int i) { return i == 6; };
int
main()
{
std::vector<int> v{6, 2, 3, 4, 5, 6};
cout << std::boolalpha;
cout << "vector: " << ranges::view::all(v) << '\n';
int main() {
std::vector<int> v{6, 2, 3, 4, 5, 6};
cout << std::boolalpha;
cout << "vector: " << ranges::view::all(v) << '\n';
cout << "vector any_of is_six: " << ranges::any_of(v, is_six) << '\n';
cout << "vector all_of is_six: " << ranges::all_of(v, is_six) << '\n';
cout << "vector none_of is_six: " << ranges::none_of(v, is_six) << '\n';
cout << "vector any_of is_six: " << ranges::any_of(v, is_six) << '\n';
cout << "vector all_of is_six: " << ranges::all_of(v, is_six) << '\n';
cout << "vector none_of is_six: " << ranges::none_of(v, is_six) << '\n';
}
//[any_all_none_of]]

View File

@@ -21,6 +21,5 @@ endif()
# ---- Executable ----
add_executable(CPMSimpleMatchExample "main.cpp")
set_target_properties(CPMSimpleMatchExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMSimpleMatchExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMSimpleMatchExample simple_match)

View File

@@ -1,8 +1,7 @@
#include <iostream>
#include <simple_match/simple_match.hpp>
#include <iostream>
int main(int argc, char ** argv){
int main(int argc, char** argv) {
using namespace simple_match;
using namespace simple_match::placeholders;
@@ -12,22 +11,23 @@ int main(int argc, char ** argv){
while (true) {
std::cout << "> ";
std::getline(std::cin, input);
if (input == "quit") { break; }
if (input == "quit") {
break;
}
int x;
try {
x = std::stoi(input);
} catch(std::invalid_argument &) {
} catch (std::invalid_argument&) {
std::cout << "invalid input" << std::endl;
continue;
}
match(x,
1, []() {std::cout << "The answer is one\n"; },
2, []() {std::cout << "The answer is two\n"; },
_x < 10, [](auto&& a) {std::cout << "The answer " << a << " is less than 10\n"; },
10 < _x < 20, [](auto&& a) {std::cout << "The answer " << a << " is between 10 and 20 exclusive\n"; },
_, []() {std::cout << "Did not match\n"; }
);
match(
x, 1, []() { std::cout << "The answer is one\n"; }, 2,
[]() { std::cout << "The answer is two\n"; }, _x < 10,
[](auto&& a) { std::cout << "The answer " << a << " is less than 10\n"; }, 10 < _x < 20,
[](auto&& a) { std::cout << "The answer " << a << " is between 10 and 20 exclusive\n"; }, _,
[]() { std::cout << "Did not match\n"; });
}
return 0;

View File

@@ -13,20 +13,16 @@ CPMAddPackage(
DOWNLOAD_ONLY YES
)
if (lua_ADDED)
if(lua_ADDED)
# lua has no CMakeLists, so we create our own target
FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
file(GLOB lua_sources ${lua_SOURCE_DIR}/*.c)
list(REMOVE_ITEM lua_sources "${lua_SOURCE_DIR}/lua.c" "${lua_SOURCE_DIR}/luac.c")
add_library(lua STATIC ${lua_sources})
target_include_directories(lua
PUBLIC
$<BUILD_INTERFACE:${lua_SOURCE_DIR}>
)
target_include_directories(lua PUBLIC $<BUILD_INTERFACE:${lua_SOURCE_DIR}>)
endif()
CPMAddPackage(
NAME sol2
URL https://github.com/ThePhD/sol2/archive/v3.0.2.zip
@@ -34,7 +30,7 @@ CPMAddPackage(
DOWNLOAD_ONLY YES
)
if (sol2_ADDED)
if(sol2_ADDED)
add_library(sol2 INTERFACE IMPORTED)
target_include_directories(sol2 INTERFACE ${sol2_SOURCE_DIR}/include)
target_link_libraries(sol2 INTERFACE lua)
@@ -43,6 +39,5 @@ endif()
# ---- Executable ----
add_executable(CPMSol2Example "main.cpp")
set_target_properties(CPMSol2Example PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMSol2Example PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMSol2Example sol2)

View File

@@ -1,16 +1,17 @@
#include <sol/sol.hpp>
#include <cassert>
#include <sol/sol.hpp>
struct vars {
int boop = 0;
int boop = 0;
};
int main() {
sol::state lua;
lua.open_libraries( sol::lib::base );
lua.new_usertype<vars>("vars", "boop", &vars::boop);
lua.script("beep = vars.new()\n"
"beep.boop = 1\n"
"print('beep boop')");
assert(lua.get<vars>("beep").boop == 1);
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.new_usertype<vars>("vars", "boop", &vars::boop);
lua.script(
"beep = vars.new()\n"
"beep.boop = 1\n"
"print('beep boop')");
assert(lua.get<vars>("beep").boop == 1);
}

View File

@@ -7,9 +7,9 @@ project(CPMSpdlogExample)
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME spdlog
GITHUB_REPOSITORY gabime/spdlog
VERSION 1.7.0
NAME spdlog
GITHUB_REPOSITORY gabime/spdlog
VERSION 1.7.0
)
# ---- Executable ----

View File

@@ -10,18 +10,14 @@ CPMAddPackage(
NAME yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
# 0.6.2 uses deprecated CMake syntax
VERSION 0.6.3
VERSION 0.6.3
# 0.6.3 is not released yet, so use the most recent commit
GIT_TAG 012269756149ae99745b6dafefd415843d7420bb
OPTIONS
"YAML_CPP_BUILD_TESTS Off"
"YAML_CPP_BUILD_CONTRIB Off"
"YAML_CPP_BUILD_TOOLS Off"
GIT_TAG 012269756149ae99745b6dafefd415843d7420bb
OPTIONS "YAML_CPP_BUILD_TESTS Off" "YAML_CPP_BUILD_CONTRIB Off" "YAML_CPP_BUILD_TOOLS Off"
)
# ---- Executable ----
add_executable(CPMYamlExample "main.cpp")
set_target_properties(CPMYamlExample PROPERTIES CXX_STANDARD 17)
set_target_properties(CPMYamlExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMYamlExample yaml-cpp)

View File

@@ -1,7 +1,8 @@
#include <yaml-cpp/yaml.h>
#include <iostream>
int main(int argc, char ** argv){
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "usage: " << argv[0] << " <path to yaml file>" << std::endl;
return 1;