-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from gjbex/development
Reorganize source code
- Loading branch information
Showing
27 changed files
with
191 additions
and
25 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
|
||
project(exceptions LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 23) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
|
||
add_executable(fac.exe fac.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Expected | ||
|
||
Illustrates how to use exceptoin to do error handling. | ||
|
||
## What is it? | ||
|
||
1. `fac.cpp`: C++ application that reads an argument from the command line, | ||
validates the value, computes the factorial and displays the output or | ||
the error message. | ||
1. `CMakeLists.txt`: CMake file to build the applications. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
|
||
project(expected LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 23) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
|
||
add_executable(fac.exe fac.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Expected | ||
|
||
Illustrates how to use `std::expected` for error handling, showcasing the | ||
monadic interface (slightly over the top). | ||
|
||
## What is it? | ||
|
||
1. `fac.cpp`: C++ application that reads an argument from the command line, | ||
validates the value, computes the factorial and displays the output or | ||
the error message. | ||
1. `CMakeLists.txt`: CMake file to build the applications. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <expected> | ||
#include <iostream> | ||
#include <limits> | ||
#include <stdexcept> | ||
|
||
using ErrorResult = std::string; | ||
using ParseResult = std::string; | ||
|
||
std::expected<ParseResult, ErrorResult> parse_arguments(int argc, char* argv[]) { | ||
std::string app_name {argv[0]}; | ||
if (argc != 2) { | ||
return std::unexpected(app_name + " takes 1 argument"); | ||
} | ||
return ErrorResult(argv[1]); | ||
} | ||
|
||
using ValidateResult = int; | ||
|
||
std::expected<ValidateResult, ErrorResult> validate_arguments(const ParseResult& arg) { | ||
try { | ||
return ValidateResult(std::stoi(arg)); | ||
} catch (const std::invalid_argument& e) { | ||
return std::unexpected("can not convert '" + arg + "' to an integer"); | ||
} catch (const std::out_of_range& e) { | ||
return std::unexpected(arg + " is too large to convert to an integer"); | ||
} | ||
} | ||
|
||
using FunctionResult = int; | ||
|
||
std::expected<FunctionResult, ErrorResult> fac(ValidateResult n) { | ||
if (n < 0) { | ||
return std::unexpected("fac argument " + std::to_string(n) + ", must be positive"); | ||
} else { | ||
const int max_int {std::numeric_limits<int>::max()}; | ||
int result = 1; | ||
for (int i = 2; i <= n; i++) { | ||
if (result < max_int/i) { | ||
result *= i; | ||
} else { | ||
return std::unexpected("fac argument " + std::to_string(n) + " causes overflow"); | ||
} | ||
} | ||
return FunctionResult(result); | ||
} | ||
} | ||
|
||
std::expected<FunctionResult, ErrorResult> show_result(FunctionResult result) { | ||
std::cout << result << std::endl; | ||
return result; | ||
} | ||
|
||
std::expected<FunctionResult, ErrorResult> show_error(const ErrorResult& error) { | ||
std::cerr << "# error: " << error << std::endl; | ||
return std::unexpected(error); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
auto result = parse_arguments(argc, argv) | ||
.and_then(validate_arguments) | ||
.and_then(fac) | ||
.and_then(show_result) | ||
.or_else(show_error) ; | ||
if (result) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Memory leak | ||
|
||
Code illustrating a memory leak created by inappropriate exception | ||
handling. | ||
|
||
# What is it? | ||
1. `memory_leak.cpp`: illustration of a memory leak caused by bad exception | ||
handling. | ||
1. `memory_leak_fixed.cpp`: illustrates using RAII to ensure correct resource | ||
management. | ||
1. `Makefile`: make file for these examples. | ||
|
||
## How to use it? | ||
Run `memory_leak.exe` and `memory_leak_fixed.exe` through valgrind with -1 and 5 | ||
as arguments and note the difference. |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Error handling | ||
|
||
Code illustrations for chapter 3, Modularity in Stroustrup's | ||
"A tour of C++" on error handling using exceptions. | ||
|
||
# What is it? | ||
1. `Exceptions`: illustration of error handling with exceptions. | ||
1. `MemoryLeak`: illustrates how inappropriately handled exceptions can | ||
lead to memory leaks, and how to fix that. | ||
1. `Expected`: illustrates error handling using `std::expected`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
|
||
project(general_utilities LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 23) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
|
||
add_executable(optional.exe optional.cpp) | ||
add_executable(variant_visit.exe variant_visit.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# General utilities | ||
A few illustrations of features in the general utilities STL. | ||
|
||
## What is it? | ||
1. `optional.cpp`: illustrations the use of `std::optional`. | ||
1. `variant_visit.cpp`: illustration of how to use `std::variant` and `std::visit`. | ||
1. `CMakeLists.txt`: CMake file to build the applications. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
source-code/C++17/Makefile → source-code/GrammarFeatures/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Grammar features | ||
A few illustrations of grammar features added in recent C++ standards. | ||
|
||
## What is it? | ||
1. `if_init.cpp`: variable defition in `if`-statment (C++17). | ||
1. `structured_bindings.cpp`: illustrates structured binding of a | ||
`struct` to individual variables (C++17). | ||
1. `Makefile`: make file to build the applications. |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
project(grids LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 23) | ||
set(CMaKE_CXX_STANDARD_REQUIRED YES) | ||
set(CMAKE_CXX_EXTENSIONS NO) | ||
|
||
add_compile_options(-Wall -Wextra -Wpedantic) | ||
|
||
add_executable(iterable.exe main.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters