Skip to content

Commit

Permalink
Merge pull request #24 from gjbex/development
Browse files Browse the repository at this point in the history
Reorganize source code
  • Loading branch information
gjbex authored Mar 6, 2024
2 parents e2a5a7f + 349fc7b commit 99ab78a
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 25 deletions.
Binary file modified scientific_cpp.pptx
Binary file not shown.
10 changes: 0 additions & 10 deletions source-code/C++17/README.md

This file was deleted.

9 changes: 9 additions & 0 deletions source-code/ErrorHandling/Exceptions/CMakeLists.txt
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)
10 changes: 10 additions & 0 deletions source-code/ErrorHandling/Exceptions/README.md
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.
9 changes: 9 additions & 0 deletions source-code/ErrorHandling/Expected/CMakeLists.txt
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)
11 changes: 11 additions & 0 deletions source-code/ErrorHandling/Expected/README.md
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.
70 changes: 70 additions & 0 deletions source-code/ErrorHandling/Expected/fac.cpp
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CXX = g++
CXXFLAGS = -std=c++14 -g -O2 -Wall -Wextra
LDLIBS = -lm

all: fac.exe memory_leak.exe memory_leak_fixed.exe
all: memory_leak.exe memory_leak_fixed.exe

%.exe: %.o
$(CXX) $(CXXFLAGS) -o $@ $< $(LDLIBS)
Expand Down
15 changes: 15 additions & 0 deletions source-code/ErrorHandling/MemoryLeak/README.md
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.
10 changes: 10 additions & 0 deletions source-code/ErrorHandling/README.md
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`.
10 changes: 10 additions & 0 deletions source-code/GeneralUtilities/CMakeLists.txt
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)
7 changes: 7 additions & 0 deletions source-code/GeneralUtilities/README.md
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.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CXX = g++
CXXFLAGS = -std=c++17 -O2 -g -Wall -Wextra -Wpedantic

all: if_init.exe optional.exe structured_bindings.exe variant_visit.exe
all: if_init.exe structured_bindings.exe

%.exe: %.o
$(CXX) $(CXXFLAGS) -o $@ $^
Expand Down
8 changes: 8 additions & 0 deletions source-code/GrammarFeatures/README.md
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.
10 changes: 0 additions & 10 deletions source-code/Modularity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,3 @@ Code illustrations for chapter 3, Modularity in Stroustrup's
1. `Particles`: example of separate compilation.
1. `ParticlesCMake`: example of build using CMake.
1. `Stats`: example of separate compilation.
1. `fac.cpp`: illustrates exception handling.
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.
5 changes: 4 additions & 1 deletion source-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ programming concepts and idiosyncracies particular to C++.
1. `Boost`: some illustrations of using the Boost C++ library,
mostly in the context of numerical programming.
1. `UsingCLibraries`: illustration of how to use a C library from C++ code.
1. `C++17`: illustrations of features added in the C++17 standard
1. `GrammarFeatures`: illustrations of recent C++ grammar features added in
recent C++ standarsd
specification.
1. `Ranges`: ranges are an interesting concept for working more
conveniently with STL containers and composing algorithms.
Expand All @@ -47,3 +48,5 @@ programming concepts and idiosyncracies particular to C++.
1. `ParallelExecution`: illustration of parallelizing algorithms using
execution policies and TBB.
1. `Cling`: Xeus-cling kernel in Jupyter Lab for interactive C++ development.
1. `GeneralUtilities`: examples of using STL general utilities.
1. `ErrorHandling`: examples of error handling.
10 changes: 10 additions & 0 deletions source-code/Templates/Concepts/CMakeLists.txt
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)
1 change: 1 addition & 0 deletions source-code/Templates/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ types.
1. `iterable.h`: defintion of the iterable concept.
1. `main.cpp`: application that will fail to compile because the template
constraints are not satisfied.
1. `CMakeLists.txt`: CMake file to build the applications.
17 changes: 15 additions & 2 deletions source-code/Templates/Concepts/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ requires Iterable<T> void print(T t) {
std::cout << std::endl;
}

template <Iterable T>
void print2(T t) {
for (auto i : t) {
std::cout << i << " ";
}
std::cout << std::endl;
}

int main() {
static_assert(Iterable<std::vector<int>>, "vector<int> is iterable");
print(std::vector<int>{1, 2, 3, 4, 5});
print2(std::vector<int>{1, 2, 3, 4, 5});
static_assert(Iterable<std::span<float>>);
static_assert(Iterable<float[]>);
print({1, 2, 3, 4, 5});
float data[] = {1, 2, 3, 4, 5};
std::span<float> s{data};
print(s);
print2(s);
// static_assert(Iterable<float[]>); // error: float[] is not an iterable
// print({1, 2, 3, 4, 5}); // float[] is not an iterable
return 0;
}

0 comments on commit 99ab78a

Please sign in to comment.