Skip to content

Commit

Permalink
Use std::optional and upgrade to C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-smidge committed Apr 26, 2024
1 parent f34a8fb commit 9d4bb75
Show file tree
Hide file tree
Showing 79 changed files with 1,266 additions and 1,380 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++17
Standard: c++20
TabWidth: 4
UseTab: Never
...
19 changes: 14 additions & 5 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,32 @@ on:

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
BUILD_TYPE: Debug

jobs:
build:
strategy:
matrix:
os: [ubuntu-20.04, ubuntu-22.04]

runs-on: ${{ matrix.os }}
config: [
{ os: ubuntu-20.04, c_compiler: gcc-10, cpp_compiler: g++-10 },
{ os: ubuntu-20.04, c_compiler: clang-12, cpp_compiler: clang-12 },
{ os: ubuntu-22.04, c_compiler: gcc-11, cpp_compiler: g++-11 },
{ os: ubuntu-22.04, c_compiler: gcc-12, cpp_compiler: g++-12 },
{ os: ubuntu-22.04, c_compiler: gcc-13, cpp_compiler: g++-13 },
{ os: ubuntu-20.04, c_compiler: clang-13, cpp_compiler: clang-13 },
{ os: ubuntu-20.04, c_compiler: clang-14, cpp_compiler: clang-14 },
{ os: ubuntu-20.04, c_compiler: clang-15, cpp_compiler: clang-15 },
]

runs-on: ${{ matrix.config.os }}

steps:
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_C_COMPILER=${{matrix.config.c_compiler}} -DCMAKE_CXX_COMPILER=${{matrix.config.cpp_compiler}}

- name: Build
# Build your program with the given configuration
Expand Down
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# The typical build output directory
/build/

# Sub-projects
/subprojects/

# Compilation database
compile_commands.json
.clangd/
Expand Down
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(libdjinterop
VERSION 0.21.0
VERSION 0.22.0
DESCRIPTION "C++ library providing access to DJ record libraries")
set(PROJECT_HOMEPAGE_URL "https://github.com/xsco/libdjinterop")

Expand All @@ -15,8 +15,8 @@ if(POLICY CMP0076)
cmake_policy(SET CMP0076 NEW)
endif()

# Require C++17
set(CMAKE_CXX_STANDARD 17)
# Require C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
if(MSVC)
add_compile_options("/W4" "/wd4251" "/wd4275" "$<$<CONFIG:RELEASE>:/O2>")
Expand All @@ -27,6 +27,13 @@ else()
add_compile_options("$<$<CONFIG:DEBUG>:-Wall>")
endif()

# Public headers rely on the __cplusplus macro being populated correctly.
# It can be useful to know exactly what it evaluates to.
try_run(CPP_MACRO_PRINT_COMPILE_RESULT CPP_MACRO_PRINT_RUN_RESULT
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_LIST_DIR}/cmake/cplusplus_macro_print.cpp"
RUN_OUTPUT_VARIABLE CPP_MACRO_PRINT_OUTPUT)
message(STATUS "${CPP_MACRO_PRINT_OUTPUT}")

# Build shared a library by default.
option(BUILD_SHARED_LIBS "Build shared library" ON)
Expand Down Expand Up @@ -101,10 +108,6 @@ if(DJINTEROP_LIBRARY_TYPE STREQUAL "STATIC_LIBRARY")
endif()

# Generate config.hpp based on build-time environment.
include(CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX(optional DJINTEROP_STD_OPTIONAL)
CHECK_INCLUDE_FILE_CXX(experimental/optional DJINTEROP_STD_EXPERIMENTAL_OPTIONAL)

configure_file(
include/djinterop/config.hpp.in
include/djinterop/config.hpp)
Expand Down Expand Up @@ -170,7 +173,6 @@ install(FILES
include/djinterop/djinterop.hpp
include/djinterop/exceptions.hpp
include/djinterop/musical_key.hpp
include/djinterop/optional.hpp
include/djinterop/pad_color.hpp
include/djinterop/performance_data.hpp
include/djinterop/semantic_version.hpp
Expand Down
7 changes: 7 additions & 0 deletions cmake/cplusplus_macro_print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main()
{
std::cout << "Value of __cplusplus macro: " << __cplusplus;
return 0;
}
2 changes: 1 addition & 1 deletion example/engine_prime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main()
td.year = 1970;
td.title = "Some Song"s;
td.artist = "Some Artist"s;
td.publisher = djinterop::stdx::nullopt; // indicates missing metadata
td.publisher = std::nullopt; // indicates missing metadata
td.key = djinterop::musical_key::a_minor;
td.bitrate = 320;
td.rating = 60; // note: rating is in the range 0-100
Expand Down
6 changes: 3 additions & 3 deletions include/djinterop/album_art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef DJINTEROP_ALBUM_ART_HPP
#define DJINTEROP_ALBUM_ART_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <cstdint>
Expand All @@ -33,7 +33,7 @@ namespace djinterop
class album_art
{
public:
typedef uint_least8_t image_data_type;
using image_data_type = uint_least8_t;

std::string hash_hex_str;
std::vector<image_data_type> image_data;
Expand Down
10 changes: 2 additions & 8 deletions include/djinterop/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef DJINTEROP_CONFIG_HPP
#define DJINTEROP_CONFIG_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

// Statement about whether the library was built/installed as static or shared.
Expand Down Expand Up @@ -55,12 +55,6 @@
#endif // DJINTEROP_SOURCE
#define DJINTEROP_LOCAL DJINTEROP_SYMBOL_LOCAL

// Symbols defined after this point represent the environment at the time
// that this library was built. The environment when this library is used must
// be compatible in order to use the library successfully.
#cmakedefine DJINTEROP_STD_OPTIONAL
#cmakedefine DJINTEROP_STD_EXPERIMENTAL_OPTIONAL

#include <cstddef>
static_assert(sizeof(std::byte) == 1,
"Only platforms where sizeof(std::byte) == 1 are supported");
Expand Down
32 changes: 17 additions & 15 deletions include/djinterop/crate.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
Tdd!his file is part of libdjinterop.
dd!
ldd!ibdjinterop is free software: you can redistribute it and/or modify
idd!t under the terms of the GNU Lesser General Public License as published
by tdd!he Free Software Foundation, either version 3 of the License, or (dd!at
your option) any later version. dd! ldd!ibdjinterop is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
This file is part of libdjinterop.
libdjinterop is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libdjinterop is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with libdjinterop. If not, see <http://www.gnu.org/licenses/>.
Expand All @@ -17,17 +19,17 @@ Lesser General Public License for more details.
#ifndef DJINTEROP_CRATE_HPP
#define DJINTEROP_CRATE_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <djinterop/config.hpp>
#include <djinterop/optional.hpp>

namespace djinterop
{
Expand Down Expand Up @@ -118,7 +120,7 @@ class DJINTEROP_PUBLIC crate
///
/// If the crate doesn't have a parent, then `djinterop::nullopt` is
/// returned.
stdx::optional<crate> parent() const;
std::optional<crate> parent() const;

/// Removes a track from the crate
///
Expand All @@ -133,15 +135,15 @@ class DJINTEROP_PUBLIC crate
///
/// If `djinterop::nullopt` is given, then this crate will have no parent.
/// That is, it becomes a root crate.
void set_parent(stdx::optional<crate> parent) const;
void set_parent(std::optional<crate> parent) const;

/// Gets the sub-crate of this one with a given name.
///
/// Note that descendants of sub-crates are not found by this method, i.e.
/// the search does not recurse into the immediate sub-crates of this crate.
///
/// If no such crate is found, then `djinterop::nullopt` is returned.
stdx::optional<crate> sub_crate_by_name(const std::string& name) const;
std::optional<crate> sub_crate_by_name(const std::string& name) const;

/// Returns the crate's contained tracks.
///
Expand Down
18 changes: 9 additions & 9 deletions include/djinterop/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
#ifndef DJINTEROP_DATABASE_HPP
#define DJINTEROP_DATABASE_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <cstdint>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

#include <djinterop/config.hpp>
#include <djinterop/optional.hpp>

namespace djinterop
{
Expand All @@ -55,9 +55,9 @@ class DJINTEROP_PUBLIC database

/// Returns the crate with the given ID
///
/// If no such crate exists in the database, then `djinterop::stdx::nullopt`
/// If no such crate exists in the database, then `std::nullopt`
/// is returned.
stdx::optional<crate> crate_by_id(int64_t id) const;
std::optional<crate> crate_by_id(int64_t id) const;

/// Returns all crates contained in the database
std::vector<crate> crates() const;
Expand Down Expand Up @@ -104,8 +104,8 @@ class DJINTEROP_PUBLIC database

/// Returns the root-level crate with the given name.
///
/// If no such crate exists, then `djinterop::stdx::nullopt` is returned.
stdx::optional<crate> root_crate_by_name(const std::string& name) const;
/// If no such crate exists, then `std::nullopt` is returned.
std::optional<crate> root_crate_by_name(const std::string& name) const;

/// Returns all root crates contained in the database
///
Expand All @@ -114,9 +114,9 @@ class DJINTEROP_PUBLIC database

/// Returns the track with the given id
///
/// If no such track exists in the database, then `djinterop::stdx::nullopt`
/// If no such track exists in the database, then `std::nullopt`
/// is returned.
stdx::optional<track> track_by_id(int64_t id) const;
std::optional<track> track_by_id(int64_t id) const;

/// Returns all tracks whose `relative_path` attribute in the database
/// matches the given string
Expand Down
4 changes: 2 additions & 2 deletions include/djinterop/djinterop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef DJINTEROP_DJINTEROP_HPP
#define DJINTEROP_DJINTEROP_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <djinterop/album_art.hpp>
Expand Down
4 changes: 2 additions & 2 deletions include/djinterop/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef DJINTEROP_ENGINE_ENGINE_HPP
#define DJINTEROP_ENGINE_ENGINE_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <array>
Expand Down
4 changes: 2 additions & 2 deletions include/djinterop/engine/engine_version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef DJINTEROP_ENGINE_ENGINE_VERSION_HPP
#define DJINTEROP_ENGINE_ENGINE_VERSION_HPP

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <ostream>
Expand Down
9 changes: 3 additions & 6 deletions include/djinterop/engine/v2/beat_data_blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#pragma once

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <cstddef>
Expand All @@ -28,9 +28,6 @@
#include <vector>

#include <djinterop/config.hpp>
#include <djinterop/optional.hpp>
#include <djinterop/pad_color.hpp>
#include <djinterop/performance_data.hpp>

namespace djinterop::engine::v2
{
Expand Down Expand Up @@ -90,7 +87,7 @@ struct DJINTEROP_PUBLIC beat_grid_marker_blob
struct DJINTEROP_PUBLIC beat_data_blob
{
/// Type of collection of beat grid markers.
typedef std::vector<beat_grid_marker_blob> beat_grid_marker_blobs_type;
using beat_grid_marker_blobs_type = std::vector<beat_grid_marker_blob>;

/// Sample rate, in hertz.
double sample_rate;
Expand Down
8 changes: 4 additions & 4 deletions include/djinterop/engine/v2/change_log_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

#pragma once

#if __cplusplus < 201703L
#error This library needs at least a C++17 compliant compiler
#if __cplusplus < 202002L
#error This library needs at least a C++20 compliant compiler
#endif

#include <cstdint>
#include <memory>
#include <optional>
#include <vector>

#include <djinterop/config.hpp>
#include <djinterop/optional.hpp>

namespace djinterop::engine::v2
{
Expand Down Expand Up @@ -86,7 +86,7 @@ class DJINTEROP_PUBLIC change_log_table
/// Get the most recent entry in the changelog.
///
/// \return Returns the last entry.
[[nodiscard]] djinterop::stdx::optional<change_log_row> last() const;
[[nodiscard]] std::optional<change_log_row> last() const;

private:
std::shared_ptr<engine_library_context> context_;
Expand Down
Loading

0 comments on commit 9d4bb75

Please sign in to comment.