Skip to content

Commit

Permalink
feat: c++ 20 compatibility (#166)
Browse files Browse the repository at this point in the history
* feat: c++ 20 compatibility

* Apply suggestions from code review

* Apply suggestions from code review

* feat: support for Real/Integer

* feat: add more concept and specializations
  • Loading branch information
vishwa2710 authored Jul 21, 2024
1 parent 06bdfd3 commit e64edaf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 36 deletions.
16 changes: 3 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ ENDIF ()

IF (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0)

MESSAGE (FATAL_ERROR "GCC version must be at least 4.8")
MESSAGE (FATAL_ERROR "GCC version must be at least 13.0")

ENDIF ()

Expand All @@ -133,7 +133,7 @@ ENDIF ()

### C++ 17 support

SET (CMAKE_CXX_STANDARD 17)
SET (CMAKE_CXX_STANDARD 20)
SET (CMAKE_CXX_STANDARD_REQUIRED ON)
SET (CMAKE_CXX_EXTENSIONS OFF)

Expand Down Expand Up @@ -266,16 +266,6 @@ ENDIF ()

FIND_PACKAGE ("yaml-cpp" REQUIRED)

### {fmt} [master]

FIND_PACKAGE ("fmt" REQUIRED)

IF (fmt_FOUND)

ELSE ()
MESSAGE (SEND_ERROR "[{fmt}] not found.")
ENDIF ()

## Versioning

IF (DEFINED PROJECT_VERSION_STRING AND EXISTS "${PROJECT_SOURCE_DIR}/src/${PROJECT_PATH}/Utilities/Version.cpp.in")
Expand Down
14 changes: 0 additions & 14 deletions docker/development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ RUN cd /tmp \
&& cp -r ./include/tsl /usr/local/include \
&& rm -rf /tmp/ordered-map

## {fmt}

ARG FMT_VERSION="5.2.0"

RUN cd /tmp \
&& git clone --branch ${FMT_VERSION} --depth 1 https://github.com/fmtlib/fmt.git \
&& cd fmt \
&& mkdir build \
&& cd build \
&& cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE .. \
&& make --silent -j $(nproc) \
&& make install \
&& rm -rf /tmp/fmt

## Rapidcsv

ARG RAPIDCSV_VERSION="6.11"
Expand Down
10 changes: 10 additions & 0 deletions include/OpenSpaceToolkit/Core/Type/Integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,14 @@ class Integer
} // namespace core
} // namespace ostk

template <>
struct std::formatter<ostk::core::type::Integer> : std::formatter<int>
{
template <typename FormatContext>
auto format(const ostk::core::type::Integer& anInteger, FormatContext& ctx) const
{
return std::formatter<int>::format(static_cast<int>(anInteger), ctx);
}
};

#endif
10 changes: 10 additions & 0 deletions include/OpenSpaceToolkit/Core/Type/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ class Real
} // namespace core
} // namespace ostk

template <>
struct std::formatter<ostk::core::type::Real> : std::formatter<double>
{
template <typename FormatContext>
auto format(const ostk::core::type::Real& aReal, FormatContext& ctx) const
{
return std::formatter<double>::format(static_cast<double>(aReal), ctx);
}
};

#endif
61 changes: 53 additions & 8 deletions include/OpenSpaceToolkit/Core/Type/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
#ifndef __OpenSpaceToolkit_Core_Type_String__
#define __OpenSpaceToolkit_Core_Type_String__

#include <OpenSpaceToolkit/Core/Type/Index.hpp>
#include <OpenSpaceToolkit/Core/Type/Size.hpp>

#define FMT_HEADER_ONLY
#include <format>
#include <ostream>
#include <regex>
#include <string>
#include <type_traits>

#include <fmt/format.h>
#include <OpenSpaceToolkit/Core/Type/Index.hpp>
#include <OpenSpaceToolkit/Core/Type/Size.hpp>

namespace ostk
{
Expand All @@ -29,9 +27,12 @@ class Array;
namespace type
{

using ostk::core::container::Array;
using ostk::core::type::Index;
using ostk::core::type::Size;
using ostk::core::container::Array;

class Real;
class Integer;

/// @brief A sequence of characters
/// @note The current implementation (derived for std::string) is temporary, as this type of
Expand Down Expand Up @@ -143,9 +144,9 @@ class String : public std::string
/// @return Formatted string

template <typename... Args>
static String Format(const char* aFormat, Args... anArgumentList)
static String Format(const std::string_view aFormat, Args&&... anArgumentList)
{
return fmt::format(aFormat, anArgumentList...);
return std::vformat(aFormat, std::make_format_args(std::forward<Args>(anArgumentList)...));
}
};

Expand Down Expand Up @@ -176,6 +177,12 @@ typename std::enable_if<HasToString<T>::value, std::string>::type CallToString(T
return t->toString();
}

// Concept to check if a type is convertible to std::string
template <typename T>
concept StringConvertible = requires(T t) {
{ std::string(t) } -> std::convertible_to<std::string>;
} && !std::is_array_v<T>; // Exclude array types to avoid ambiguity with const char*

} // namespace type
} // namespace core
} // namespace ostk
Expand All @@ -195,6 +202,44 @@ struct hash<ostk::core::type::String>
}
};

template <>
struct formatter<ostk::core::type::String> : formatter<string>
{
template <typename FormatContext>
auto format(const ostk::core::type::String& str, FormatContext& ctx) const
{
return formatter<string>::format(static_cast<const string&>(str), ctx);
}
};

// Generic formatter for StringConvertible types
template <typename T>
requires ostk::core::type::StringConvertible<T> && (!std::is_same_v<T, ostk::core::type::String>) &&
(!std::is_same_v<T, std::string>) && (!std::is_same_v<T, const char*>)
struct formatter<T> : formatter<string>
{
template <typename FormatContext>
auto format(const T& value, FormatContext& ctx) const
{
return formatter<string>::format(string(value), ctx);
}
};

// Concept to check if a type is an enum
template <typename T>
concept EnumType = is_enum_v<T>;

// Generic formatter for all enum types
template <EnumType T>
struct formatter<T> : formatter<underlying_type_t<T>>
{
template <typename FormatContext>
auto format(T value, FormatContext& ctx) const
{
return formatter<underlying_type_t<T>>::format(static_cast<underlying_type_t<T>>(value), ctx);
}
};

} // namespace std

#endif
2 changes: 1 addition & 1 deletion test/OpenSpaceToolkit/Core/Type/String.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ TEST(OpenSpaceToolkit_Core_Type_String, GetSubstring)

TEST(OpenSpaceToolkit_Core_Type_String, Split)
{
using ostk::core::type::String;
using ostk::core::container::Array;
using ostk::core::type::String;

{
EXPECT_EQ(Array<String>({"a", "b", "c", ""}), String("a,b,c,").split(","));
Expand Down

0 comments on commit e64edaf

Please sign in to comment.