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

feat: support freestanding #92

Merged
merged 4 commits into from
Apr 21, 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
12 changes: 11 additions & 1 deletion include/emio/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#include <iterator>
#include <limits>
#include <span>
#include <string>

#if __STDC_HOSTED__
# include <string>
#endif

#include <string_view>
#include <type_traits>

Expand Down Expand Up @@ -155,13 +159,15 @@ class memory_buffer final : public buffer {
return {vec_.data(), used_ + this->get_used_count()};
}

#if __STDC_HOSTED__
/**
* Obtains a copy of the underlying string object.
* @return The string.
*/
[[nodiscard]] std::string str() const {
return std::string{view()};
}
#endif

/**
* Resets the buffer's read and write position to the beginning of the internal storage.
Expand Down Expand Up @@ -227,13 +233,15 @@ class span_buffer : public buffer {
return {span_.data(), this->get_used_count()};
}

#if __STDC_HOSTED__
/**
* Obtains a copy of the underlying string object.
* @return The string.
*/
[[nodiscard]] std::string str() const {
return std::string{view()};
}
#endif

/**
* Resets the buffer's read and write position to the beginning of the span.
Expand Down Expand Up @@ -303,10 +311,12 @@ struct get_value_type<std::back_insert_iterator<Container>> {
using type = typename Container::value_type;
};

#if __STDC_HOSTED__
template <typename Char, typename Traits>
struct get_value_type<std::ostreambuf_iterator<Char, Traits>> {
using type = Char;
};
#endif

template <typename T>
using get_value_type_t = typename get_value_type<T>::type;
Expand Down
1 change: 0 additions & 1 deletion include/emio/detail/ct_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <algorithm>
#include <array>
#include <cstddef>
#include <string>

#include "conversion.hpp"
#include "predef.hpp"
Expand Down
6 changes: 4 additions & 2 deletions include/emio/detail/validated_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ class runtime_string {
constexpr runtime_string() = default;

// Don't allow temporary strings or any nullptr.
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): as intended
constexpr runtime_string(std::string&&) = delete;
constexpr runtime_string(std::nullptr_t) = delete;
constexpr runtime_string(int) = delete;
#if __STDC_HOSTED__
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): as intended
constexpr runtime_string(std::string&&) = delete;
#endif

/**
* Constructs the runtime format/scan string from any suitable char sequence.
Expand Down
2 changes: 2 additions & 0 deletions include/emio/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ constexpr result<OutputIt> format_to(OutputIt out, emio::format_string<Args...>
return buf.out();
}

#if __STDC_HOSTED__
/**
* Formats arguments according to the format string, and returns the result as string.
* @param args The format args with the format string.
Expand Down Expand Up @@ -219,6 +220,7 @@ template <typename T, typename... Args>
result<std::string> format(T format_str, const Args&... args) noexcept {
return emio::vformat(emio::make_format_args(format_str, args...));
}
#endif

/**
* Return type of (v)format_to_n functions.
Expand Down
2 changes: 2 additions & 0 deletions include/emio/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ constexpr const void* ptr(const std::unique_ptr<T, Deleter>& p) {
return p.get();
}

#if __STDC_HOSTED__
template <typename T>
const void* ptr(const std::shared_ptr<T>& p) {
return p.get();
}
#endif

} // namespace emio
4 changes: 3 additions & 1 deletion include/emio/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ class reader {
constexpr reader() = default;

// Don't allow temporary strings or any nullptr.
constexpr reader(std::string&&) = delete; // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved): as intended
constexpr reader(std::nullptr_t) = delete;
constexpr reader(int) = delete;
#if __STDC_HOSTED__
constexpr reader(std::string&&) = delete; // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved): as intended
#endif

/**
* Constructs the reader from any suitable char sequence.
Expand Down
24 changes: 22 additions & 2 deletions include/emio/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <concepts>
#include <cstdint>
#include <optional>
#include <stdexcept>
#if __STDC_HOSTED__
# include <stdexcept>
#endif
#include <type_traits>

#include "detail/predef.hpp"
Expand Down Expand Up @@ -82,14 +84,32 @@ inline constexpr struct {
* - "no value" -> a value should be accessed but result<T> held an error
* - "no error" -> a error should be accessed but result<T> held an value
*/
#if __STDC_HOSTED__
class bad_result_access : public std::logic_error {
public:
/**
* Constructs the bad result access from a message.
* @param msg The exception message.
*/
explicit bad_result_access(const std::string_view& msg) : logic_error{msg.data()} {}
explicit bad_result_access(const std::string_view& msg) : logic_error{std::string{msg}} {}
};
#else
class bad_result_access : public std::exception {
public:
/**
* Constructs the bad result access from a message.
* @param msg The exception message.
*/
explicit bad_result_access(const std::string_view& msg) : msg_{msg} {}

[[nodiscard]] const char* what() const noexcept override {
return msg_.data();
}

private:
std::string_view msg_;
};
#endif

namespace detail {

Expand Down
2 changes: 2 additions & 0 deletions include/emio/scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class scanner<std::string_view> {
reader format_rdr_;
};

#if __STDC_HOSTED__
/**
* Scanner for std::string.
*/
Expand All @@ -160,5 +161,6 @@ class scanner<std::string> : public scanner<std::string_view> {
return success;
}
};
#endif

} // namespace emio
6 changes: 5 additions & 1 deletion include/emio/std.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#pragma once

#include <exception>
#include <filesystem>
#if __STDC_HOSTED__
# include <filesystem>
#endif
#include <optional>
#include <variant>

Expand Down Expand Up @@ -58,6 +60,7 @@ class formatter<T> : public formatter<std::string_view> {
}
};

#if __STDC_HOSTED__
/**
* Formatter for std::filesystem::path.
*/
Expand All @@ -68,6 +71,7 @@ class formatter<std::filesystem::path> : public formatter<std::string_view> {
return formatter<std::string_view>::format(out, arg.native());
}
};
#endif

/**
* Formatter for std::monostate.
Expand Down
11 changes: 10 additions & 1 deletion test/compile_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ if (PROJECT_IS_TOP_LEVEL)
enable_testing()
endif ()

add_executable(emio_compile ../compile_test/compile.cpp)
add_executable(emio_compile compile.cpp)

target_link_libraries(emio_compile
emio::emio
)

target_compile_features(emio_compile PRIVATE cxx_std_20)

add_executable(emio_compile_freestanding compile.cpp)

target_link_libraries(emio_compile_freestanding
emio::emio
)

target_compile_features(emio_compile_freestanding PRIVATE cxx_std_20)
target_compile_options(emio_compile_freestanding PRIVATE -ffreestanding)

add_folders(CompileTest)
17 changes: 8 additions & 9 deletions test/compile_test/compile.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//
// Created by neubertt on 09.03.2024.
//
#include "emio/emio.hpp"
#include <emio/emio.hpp>

consteval auto created_with_span_buffer() {
std::array<char, 5> storage{};
Expand Down Expand Up @@ -35,9 +32,11 @@ consteval auto created_with_iterator_buffer() {
return storage;
}

int main() {
created_with_span_buffer();
created_with_static_buffer();
created_with_memory_buffer();
created_with_iterator_buffer();
extern "C" int main() {
const auto a = created_with_span_buffer();
const auto b = created_with_static_buffer();
const auto c = created_with_memory_buffer();
const auto d = created_with_iterator_buffer();

return a.size() + b.size() + c.size() + d.size();
}
Loading