Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply the Beman Standard; more tree refactoring #6

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
cmake_minimum_required(VERSION 3.10)

project(stl-concepts VERSION 0.0.0 LANGUAGES CXX)
project(beman_optional26 VERSION 0.0.0 LANGUAGES CXX)

enable_testing()

set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)

add_subdirectory(extern)
add_subdirectory(src)
add_subdirectory(examples)

include(GNUInstallDirs)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Compiling the paper requires a working LaTeX installation. See instructions for

#### Basic Build

This project strives to be as normal and simple a CMake project as possible. This build workflow in particular will work, producing a static `example` library, ready to package:
This project strives to be as normal and simple a CMake project as possible. This build workflow in particular will work, producing a static `beman_optional26` library, ready to package:

```shell
cmake --workflow --preset gcc-14
Expand Down
32 changes: 32 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set(BEMAN_OPTIONAL26_LIBRARY "beman_optional26")

include(GNUInstallDirs)

# List of all buildable examples.
set(EXAMPLES
sample
range_loop
optional_ref
)

foreach(EXAMPLE ${EXAMPLES})
# Add example executable.
add_executable(${EXAMPLE} "")

# Add example source file.
target_sources(
${EXAMPLE}
PRIVATE
${EXAMPLE}.cpp
)

# Link example with the library.
target_link_libraries(${EXAMPLE} "${BEMAN_OPTIONAL26_LIBRARY}")

# Install .
install(
TARGETS ${EXAMPLE}
EXPORT ${TARGETS_EXPORT_NAME}
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endforeach()
42 changes: 42 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Beman.Optional26 Examples

List of usage examples for `Beman.Optional26`.

## Sample

Check [sample](sample.cpp) for basic `Beman.Optional26` library usage.

Build and run instructions:
```shell
# build
$ cmake --workflow --preset gcc-14

# run sample
$ .build/gcc-14/examples/RelWithDebInfo/sample
empty_opt is empty!
opt = 26
```

## Range Support (P3168R1)

Range support added in [*Give std::optional Range Support* (P3168R1)](https://wg21.link/P3168R1) examples:
* [./range_loop.cpp](./range_loop.cpp)


Build and run instructions:

```shell
# build
$ cmake --workflow --preset gcc-14

# run range_loop
$ .build/gcc-14/examples/RelWithDebInfo/range_loop
# note: 1st log (empty_opt) is missing from console!
"for each loop" over C++26 optional: opt = 26 # 2nd log (non empty opt)
```

## Optional Reference (P2988R5)

Reference support added in [*std::optional<T&>*(P2988R5)](https://wg21.link/P2988R5) examples:

* [./optional_ref.cpp](./optional_ref.cpp)
4 changes: 2 additions & 2 deletions src/examples/before_after.cpp → examples/optional_ref.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <beman/optional/optional.hpp>
#include <Beman/Optional26/optional.hpp>

#include <string>

Expand All @@ -12,7 +12,7 @@ auto find_cat_old(std::string) -> Cat* { return nullptr; }

auto find_cat_new(std::string) -> optional<Cat&> { return optional<Cat&>{}; }

Cat* doit_old(Cat& cat) { return &cat; }
Cat* doit_old(Cat& cat) { return &cat; }
optional<Cat&> doit(Cat& cat) { return optional<Cat&>{cat}; }

Cat* before1() {
Expand Down
17 changes: 17 additions & 0 deletions examples/range_loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Beman/Optional26/optional.hpp>
#include <iostream>

int main() {
beman::optional::optional<int> empty_opt{};
for ([[maybe_unused]] const auto& i : empty_opt) {
// Should not see this in console.
std::cout << "\"for each loop\" over C++26 optional: empty_opt\n";
}

beman::optional::optional<int> opt{26};
for (const auto& i : opt) {
// Should see this in console.
std::cout << "\"for each loop\" over C++26 optional: opt = " << i << "\n";
}
return 0;
}
16 changes: 16 additions & 0 deletions examples/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <Beman/Optional26/optional.hpp>
#include <iostream>

int main() {
beman::optional::optional<int> empty_opt{};
if (!empty_opt) {
std::cout << "empty_opt is empty!\n";
}

beman::optional::optional<int> opt{26};
if (opt) {
std::cout << "opt = " << *opt << "\n";
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// beman/optional/optional.h -*-C++-*-
#ifndef INCLUDED_BEMAN_OPTIONAL_OPTIONAL
#define INCLUDED_BEMAN_OPTIONAL_OPTIONAL
#ifndef BEMAN_OPTIONAL26_OPTIONAL
#define BEMAN_OPTIONAL26_OPTIONAL

/*
22.5.2 Header <optional> synopsis[optional.syn]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
add_library(optional STATIC "")
set(TARGET_LIBRARY "beman_optional26")

add_library("${TARGET_LIBRARY}" STATIC "")

target_sources(
optional
"${TARGET_LIBRARY}"
PRIVATE
optional.cpp
)

include(GNUInstallDirs)

target_include_directories(optional PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../>
target_include_directories("${TARGET_LIBRARY}" PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../../include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME}> # <prefix>/include/scratch
)
)

install(
TARGETS optional
TARGETS "${TARGET_LIBRARY}"
EXPORT ${TARGETS_EXPORT_NAME}1
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
)

string(TOLOWER ${CMAKE_PROJECT_NAME} CMAKE_LOWER_PROJECT_NAME)

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
)
)

target_link_libraries(optional)
target_link_libraries("${TARGET_LIBRARY}")

## Tests
add_executable(optional_test "")
Expand All @@ -41,7 +43,7 @@ target_sources(
optional_ref_monadic.t.cpp
)

target_link_libraries(optional_test optional)
target_link_libraries(optional_test "${TARGET_LIBRARY}")
target_link_libraries(optional_test gtest)
target_link_libraries(optional_test gtest_main)

Expand Down
1 change: 1 addition & 0 deletions src/Beman/optional/optional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <Beman/Optional26/optional.hpp>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <beman/optional/optional.hpp>
#include <Beman/Optional26/optional.hpp>

#include <beman/optional/optional.hpp> // test 2nd include OK
#include <Beman/Optional26/optional.hpp> // test 2nd include OK

#include <functional>
#include <ranges>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <beman/optional/optional.hpp>

#include <beman/optional/optional.hpp>
#include <Beman/Optional26/optional.hpp>

#include <gtest/gtest.h>

Expand All @@ -9,7 +7,7 @@ constexpr beman::optional::optional<int> get_opt_int(int) { return 42; }


TEST(OptionalMonadicTest, Transform) {
// lhs is empty
// lhs is empty
beman::optional::optional<int> o1;
auto o1r = o1.transform([](int i) { return i + 2; });
static_assert((std::is_same<decltype(o1r), beman::optional::optional<int>>::value));
Expand Down Expand Up @@ -85,27 +83,26 @@ TEST(OptionalMonadicTest, Transform) {

}

TEST(OptionalMonadicTest, TransformConstexpr) {

// test each overload in turn
constexpr beman::optional::optional<int> o16 = 42;
constexpr auto o16r = o16.transform(get_int);
static_assert(*o16r == 42);

constexpr beman::optional::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).transform(get_int);
static_assert(*o20r == 42);

constexpr beman::optional::optional<int> o32 = beman::optional::nullopt;
constexpr auto o32r = o32.transform(get_int);
static_assert(!o32r);
constexpr beman::optional::optional<int> o36 = beman::optional::nullopt;
constexpr auto o36r = std::move(o36).transform(get_int);
static_assert(!o36r);
TEST(OptionalMonadicTest, TransformConstexpr) {
// test each overload in turn
constexpr beman::optional::optional<int> o16 = 42;
constexpr auto o16r = o16.transform(get_int);
static_assert(*o16r == 42);

constexpr beman::optional::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).transform(get_int);
static_assert(*o20r == 42);

constexpr beman::optional::optional<int> o32 = beman::optional::nullopt;
constexpr auto o32r = o32.transform(get_int);
static_assert(!o32r);
constexpr beman::optional::optional<int> o36 = beman::optional::nullopt;
constexpr auto o36r = std::move(o36).transform(get_int);
static_assert(!o36r);
}

TEST(OptionalMonadicTest, Transform2) {
// lhs is empty
// lhs is empty
beman::optional::optional<int> o1;
auto o1r = o1.transform([](int i) { return i + 2; });
static_assert((std::is_same<decltype(o1r), beman::optional::optional<int>>::value));
Expand Down Expand Up @@ -173,23 +170,22 @@ TEST(OptionalMonadicTest, Transform2) {
EXPECT_TRUE(!o36r);
}

TEST(OptionalMonadicTest, TransformConstxpr)
{
// test each overload in turn
constexpr beman::optional::optional<int> o16 = 42;
constexpr auto o16r = o16.transform(get_int);
static_assert(*o16r == 42);

constexpr beman::optional::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).transform(get_int);
static_assert(*o20r == 42);

constexpr beman::optional::optional<int> o32 = beman::optional::nullopt;
constexpr auto o32r = o32.transform(get_int);
static_assert(!o32r);
constexpr beman::optional::optional<int> o36 = beman::optional::nullopt;
constexpr auto o36r = std::move(o36).transform(get_int);
static_assert(!o36r);
TEST(OptionalMonadicTest, TransformConstxpr) {
// test each overload in turn
constexpr beman::optional::optional<int> o16 = 42;
constexpr auto o16r = o16.transform(get_int);
static_assert(*o16r == 42);

constexpr beman::optional::optional<int> o20 = 42;
constexpr auto o20r = std::move(o20).transform(get_int);
static_assert(*o20r == 42);

constexpr beman::optional::optional<int> o32 = beman::optional::nullopt;
constexpr auto o32r = o32.transform(get_int);
static_assert(!o32r);
constexpr beman::optional::optional<int> o36 = beman::optional::nullopt;
constexpr auto o36r = std::move(o36).transform(get_int);
static_assert(!o36r);
}

TEST(OptionalMonadicTest, and_then)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <beman/optional/optional.hpp>

#include <beman/optional/optional.hpp>
#include <Beman/Optional26/optional.hpp>

#include <gtest/gtest.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <beman/optional/optional.hpp>

#include <beman/optional/optional.hpp>
#include <Beman/Optional26/optional.hpp>

#include <gtest/gtest.h>

Expand Down
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
add_subdirectory(beman)
add_subdirectory(examples)
add_subdirectory(Beman)
1 change: 0 additions & 1 deletion src/beman/optional/optional.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions src/examples/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions src/examples/main.cpp

This file was deleted.

Loading