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

Separate out opts #1598

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
65 changes: 45 additions & 20 deletions include/glaze/core/opts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,29 @@ namespace glz
#define GLZ_NULL_TERMINATED true
#endif

struct opts
// Options that Glaze internally manipulates
struct opts_internal
{
enum struct internal : uint32_t {
none = 0,
opening_handled = 1 << 0, // the opening character has been handled
closing_handled = 1 << 1, // the closing character has been handled
ws_handled = 1 << 2, // whitespace has already been parsed
no_header = 1 << 3, // whether or not a binary header is needed
disable_write_unknown =
1 << 4, // whether to turn off writing unknown fields for a glz::meta specialized for unknown writing
is_padded = 1 << 5, // whether or not the read buffer is padded
disable_padding = 1 << 6, // to explicitly disable padding for contexts like includers
write_unchecked = 1 << 7 // the write buffer has sufficient space and does not need to be checked
};
// Sufficient space is only applicable to writing certain types and based on the write_padding_bytes

uint32_t internal{}; // default should be 0

[[nodiscard]] constexpr bool operator==(const opts_internal&) const noexcept = default;
};

struct opts : opts_internal
{
// USER CONFIGURABLE
uint32_t format = JSON;
Expand Down Expand Up @@ -96,31 +118,34 @@ namespace glz
// glaze_object_t concepts
bool_t concatenate = true; // Concatenates ranges of std::pair into single objects when writing

[[nodiscard]] constexpr bool operator==(const opts&) const noexcept = default;
};

// Special options for command line interface
struct opts_cli : opts
{
bool_t hide_non_invocable =
true; // Hides non-invocable members from the cli_menu (may be applied elsewhere in the future)

enum struct internal : uint32_t {
none = 0,
opening_handled = 1 << 0, // the opening character has been handled
closing_handled = 1 << 1, // the closing character has been handled
ws_handled = 1 << 2, // whitespace has already been parsed
no_header = 1 << 3, // whether or not a binary header is needed
disable_write_unknown =
1 << 4, // whether to turn off writing unknown fields for a glz::meta specialized for unknown writing
is_padded = 1 << 5, // whether or not the read buffer is padded
disable_padding = 1 << 6, // to explicitly disable padding for contexts like includers
write_unchecked = 1 << 7 // the write buffer has sufficient space and does not need to be checked
};
// Sufficient space is only applicable to writing certain types and based on the write_padding_bytes

// INTERNAL USE
uint32_t internal{}; // default should be 0

[[nodiscard]] constexpr bool operator==(const opts&) const noexcept = default;

[[nodiscard]] constexpr bool operator==(const opts_cli&) const noexcept = default;
};

#undef bool_t

consteval bool has_comments(auto&& Opts) {
if constexpr (requires { Opts.comments; })
return Opts.comments;
else
return false;
}

consteval bool hide_non_invocable(auto&& Opts) {
if constexpr (requires { Opts.hide_non_invocable; })
return Opts.hide_non_invocable;
else
return true; // defaults to true
}

consteval bool has_opening_handled(opts o) { return o.internal & uint32_t(opts::internal::opening_handled); }

consteval bool has_closing_handled(opts o) { return o.internal & uint32_t(opts::internal::closing_handled); }
Expand Down
8 changes: 4 additions & 4 deletions include/glaze/ext/cli_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace glz
}
}

template <opts Opts = opts{.prettify = true}, class T>
template <auto Opts = opts{.prettify = true}, class T>
requires(detail::glaze_object_t<T> || detail::reflectable<T>)
inline void run_cli_menu(T& value, cli_menu_boolean auto& show_menu)
{
Expand Down Expand Up @@ -154,7 +154,7 @@ namespace glz
run_cli_menu<Opts>(v, menu_boolean);
}
}
else if constexpr (Opts.hide_non_invocable) {
else if constexpr (hide_non_invocable(Opts)) {
}
else {
static_assert(false_v<Func>, "Your function is not invocable or not concrete");
Expand Down Expand Up @@ -204,7 +204,7 @@ namespace glz
else if constexpr (is_invocable_concrete<std::remove_cvref_t<Func>>) {
std::printf(" %d %.*s\n", uint32_t(I + 1), int(key.size()), key.data());
}
else if constexpr (Opts.hide_non_invocable) {
else if constexpr (hide_non_invocable(Opts)) {
// do not print non-invocable member
}
else {
Expand Down Expand Up @@ -251,7 +251,7 @@ namespace glz
}
}

template <opts Opts = opts{.prettify = true}, class T>
template <auto Opts = opts{.prettify = true}, class T>
requires(detail::glaze_object_t<T> || detail::reflectable<T>)
inline void run_cli_menu(T& value)
{
Expand Down
2 changes: 1 addition & 1 deletion include/glaze/json/minify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace glz
break;
}
case Comment: {
if constexpr (Opts.comments) {
if constexpr (has_comments(Opts)) {
const auto value = read_jsonc_comment(it, end);
if (value.size()) [[likely]] {
dump<false>(value, b, ix);
Expand Down
2 changes: 1 addition & 1 deletion include/glaze/json/prettify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace glz
break;
}
case Comment: {
if constexpr (Opts.comments) {
if constexpr (has_comments(Opts)) {
const auto value = read_jsonc_comment(it, end);
dump_not_empty(value, b, ix);
break;
Expand Down
8 changes: 4 additions & 4 deletions include/glaze/json/skip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace glz::detail
struct skip_value<JSON>
{
template <opts Opts>
requires(not Opts.comments)
requires(not has_comments(Opts))
GLZ_ALWAYS_INLINE static void op(is_context auto&& ctx, auto&& it, auto&& end) noexcept;

template <opts Opts>
requires(bool(Opts.comments))
requires(has_comments(Opts))
GLZ_ALWAYS_INLINE static void op(is_context auto&& ctx, auto&& it, auto&& end) noexcept;
};

Expand Down Expand Up @@ -101,7 +101,7 @@ namespace glz::detail
}

template <opts Opts>
requires(not Opts.comments)
requires(not has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_value<JSON>::op(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (not Opts.validate_skipped) {
Expand Down Expand Up @@ -190,7 +190,7 @@ namespace glz::detail
}

template <opts Opts>
requires(bool(Opts.comments))
requires(has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_value<JSON>::op(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (not has_ws_handled(Opts)) {
Expand Down
16 changes: 8 additions & 8 deletions include/glaze/util/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace glz::detail
#define GLZ_SKIP_WS(RETURN) \
if constexpr (!Opts.minified) { \
if constexpr (Opts.null_terminated) { \
if constexpr (Opts.comments) { \
if constexpr (has_comments(Opts)) { \
while (whitespace_comment_table[uint8_t(*it)]) { \
if (*it == '/') [[unlikely]] { \
skip_comment(ctx, it, end); \
Expand All @@ -558,7 +558,7 @@ namespace glz::detail
} \
} \
else { \
if constexpr (Opts.comments) { \
if constexpr (has_comments(Opts)) { \
while (it < end && whitespace_comment_table[uint8_t(*it)]) { \
if (*it == '/') [[unlikely]] { \
skip_comment(ctx, it, end); \
Expand Down Expand Up @@ -593,7 +593,7 @@ namespace glz::detail
{
if constexpr (!Opts.minified) {
if constexpr (Opts.null_terminated) {
if constexpr (Opts.comments) {
if constexpr (has_comments(Opts)) {
while (whitespace_comment_table[uint8_t(*it)]) {
if (*it == '/') [[unlikely]] {
skip_comment(ctx, it, end);
Expand All @@ -613,7 +613,7 @@ namespace glz::detail
}
}
else {
if constexpr (Opts.comments) {
if constexpr (has_comments(Opts)) {
while (it < end && whitespace_comment_table[uint8_t(*it)]) {
if (*it == '/') [[unlikely]] {
skip_comment(ctx, it, end);
Expand Down Expand Up @@ -922,7 +922,7 @@ namespace glz::detail
}

template <opts Opts, char open, char close, size_t Depth = 1>
requires(has_is_padded(Opts) && not bool(Opts.comments))
requires(has_is_padded(Opts) && not has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_until_closed(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
size_t depth = Depth;
Expand Down Expand Up @@ -970,7 +970,7 @@ namespace glz::detail
}

template <opts Opts, char open, char close, size_t Depth = 1>
requires(has_is_padded(Opts) && bool(Opts.comments))
requires(has_is_padded(Opts) && has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_until_closed(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
size_t depth = Depth;
Expand Down Expand Up @@ -1025,7 +1025,7 @@ namespace glz::detail
}

template <opts Opts, char open, char close, size_t Depth = 1>
requires(!has_is_padded(Opts) && not bool(Opts.comments))
requires(!has_is_padded(Opts) && not has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_until_closed(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
size_t depth = Depth;
Expand Down Expand Up @@ -1109,7 +1109,7 @@ namespace glz::detail
}

template <opts Opts, char open, char close, size_t Depth = 1>
requires(!has_is_padded(Opts) && bool(Opts.comments))
requires(!has_is_padded(Opts) && has_comments(Opts))
GLZ_ALWAYS_INLINE void skip_until_closed(is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
size_t depth = Depth;
Expand Down
Loading