GuillaumeDua / CppShelf

Collection of powerfuls - C++ Single-Header Libraries Files
https://guillaumedua.github.io/CppShelf/
MIT License
9 stars 1 forks source link

[ag] better format, print + as opt-in #134

Open GuillaumeDua opened 1 year ago

GuillaumeDua commented 1 year ago

Dependencies


Pocs


๐Ÿ“˜๐Ÿ’กDesign

API:

csl::ag::concepts::aggregate auto value = /* some aggregate value */;

// 1: express intent: parse is "how", join is "what"
fmt::println("{indented}", csl::ag::io::join(value));
fmt::println("{indexed,indented}", csl::ag::io::join(value));
fmt::println("{indexed,typenamed,indented}", csl::ag::io::join(value));
fmt::println("{indexed,typenamed,compact}", csl::ag::io::join(value));

// 2: vs. join is "how" + "what"
fmt::println("{}",   csl::ag::io::join<opt_ins...>(value));
fmt::println("{:n}", csl::ag::io::join<opt_ins...>(value)); // none
fmt::println("{:c}", csl::ag::io::join<opt_ins...>(value)); // compact => clash with indented

// 3: vs. same as 2, with opt-in presentation/style
csl::ag::io::join<opt_ins...>(value, presentation);

// 4: vs. "how" + "what" as view composition
fmt::println("{}", csl::ag::io::join<presentation::indented>(value));
fmt::println("{}", value | join<presentation::indented>);
fmt::println("{}", value | indexed | typenamed | join<presentation>);

Call: split formatting style and join_view, see #262 , #263

๐Ÿ“˜๐Ÿ’ก Back to design:

๐Ÿ“˜๐Ÿ’กBack to design (2):

Implementation design:

// simple formatter
struct fmt::formatter<csl::ag::concepts::aggregate>{}; // parse {} or {:n}
struct fmt::formatter<join_view<csl::ag::concepts::aggregate>>{};

template <std::size_t depth> struct presentation_factory; // -> as tttp
fmt::println("value = {}", csl::ag::io::join<presentation::indented>(value))

// indexed<T> -> std::index_sequence<csl::ag::size_v | std::tuple_size_v>, or std::views::enumerate
// typenamed<T> - with detection not T::csl_ag_io_product

fmt::println("value = {}", csl::ag::io::join<presentation::indented>(
   indexed{ typenamed { value } }
))
fmt::println("{}", value | indexed | typenamed | join<presentation>)

Recursive lambda quick impl:

 const auto printer = overload{
      [](const auto & self, std::size_t depth, const csl::ag::concepts::aggregate auto & value){
          fmt::println("{:\t>{}}{{", "", depth);
          using type = std::remove_cvref_t<decltype(value)>;
          [&]<std::size_t ... indexes>(std::index_sequence<indexes...>){
              ((std::invoke(
                  self,
                  self, depth + 1, csl::ag::get<indexes>(value)
              ), ...));
          }(std::make_index_sequence<csl::ag::size_v<type>>{});
          fmt::println("{:\t>{}}}}", "", depth);
      },
      [](const auto & /* self */, std::size_t depth, const auto & value){
          fmt::println("{:\t>{}}{}", "", depth, value);
      }
  };

  printer(printer, 0, value);

๐Ÿ’ก Ideas

๐Ÿ’ก views composition join_view<strategies...>

see #262

struct A{ int i; };
struct B{ A a; char c; }

csl::ag::join_view<typenamed, indented /*etc...*/>(B{}, ",");

๐Ÿ’ก Idea : flatten,rewrap<T is aggregate ? view<aggregate> : T>

see #263

GuillaumeDua commented 5 days ago

Reopen, as not fully completed in #241 / #233