Skip to content

Commit

Permalink
Move shrink_to_fit out of default opts
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Feb 27, 2025
1 parent 8c3b03b commit 734040c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
3 changes: 3 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ bool concatenate = true;
bool allow_conversions = true;
// Whether conversions between convertible types are allowed in binary, e.g. double -> float

bool shrink_to_fit = false;
// Shrinks dynamic containers to new size to save memory

bool hide_non_invocable = true;
// Hides non-invocable members from the cli_menu (may be applied elsewhere in the future)
```
Expand Down
12 changes: 6 additions & 6 deletions include/glaze/beve/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ namespace glz
if constexpr (resizable<T>) {
value.resize(n);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down Expand Up @@ -772,7 +772,7 @@ namespace glz
if constexpr (resizable<T>) {
value.resize(n);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down Expand Up @@ -885,7 +885,7 @@ namespace glz
if constexpr (resizable<T>) {
value.resize(n);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand All @@ -902,7 +902,7 @@ namespace glz

x.resize(length);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}

Expand Down Expand Up @@ -948,7 +948,7 @@ namespace glz
if constexpr (resizable<T>) {
value.resize(n);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down Expand Up @@ -982,7 +982,7 @@ namespace glz
if constexpr (resizable<T>) {
value.resize(n);

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down
13 changes: 12 additions & 1 deletion include/glaze/core/opts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ namespace glz
uint8_t indentation_width = 3; // Prettified JSON indentation size
bool new_lines_in_arrays = true; // Whether prettified arrays should have new lines for each element
bool append_arrays = false; // When reading into an array the data will be appended if the type supports it
bool shrink_to_fit = false; // Shrinks dynamic containers to new size to save memory
bool write_type_info = true; // Write type info for meta objects in variants
bool error_on_missing_keys = false; // Require all non nullable keys to be present in the object. Use
// skip_null_members = false to require nullable members
Expand Down Expand Up @@ -127,6 +126,10 @@ namespace glz
// bool allow_conversions = true;
// Whether conversions between convertible types are allowed in binary, e.g. double -> float

// ---
// bool shrink_to_fit = false;
// Shrinks dynamic containers to new size to save memory

// ---
// bool hide_non_invocable = true;
// Hides non-invocable members from the cli_menu (may be applied elsewhere in the future)
Expand Down Expand Up @@ -171,6 +174,14 @@ namespace glz
}
}

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

consteval bool check_hide_non_invocable(auto&& Opts) {
if constexpr (requires { Opts.hide_non_invocable; }) {
return Opts.hide_non_invocable;
Expand Down
4 changes: 2 additions & 2 deletions include/glaze/json/ndjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace glz
if constexpr (resizable<T>) {
value.clear();

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace glz
value.erase(value_it,
value.end()); // use erase rather than resize for non-default constructible elements

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/glaze/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ namespace glz
if constexpr (resizable<T> && not Opts.append_arrays) {
value.clear();

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down Expand Up @@ -1521,7 +1521,7 @@ namespace glz
value.erase(value_it,
value.end()); // use erase rather than resize for non-default constructible elements

if constexpr (Opts.shrink_to_fit) {
if constexpr (check_shrink_to_fit(Opts)) {
value.shrink_to_fit();
}
}
Expand Down

0 comments on commit 734040c

Please sign in to comment.