GithubHelp home page GithubHelp logo

ezprint's Introduction

ezprint

ezprint is what printing should've always looked like.

struct Point { int x, y; };
std::vector<Point> v{{1, 2}, {3, 4}};
ez::println(v);  // prints {{1 2} {3 4}}

Plug and play, concise, cross-platform.

Installation

ezprint is header only, just #include "ezprint.hpp". Requires C++17.

Examples

ez::println(1);  // 1

ez::println(1, "2", "3"s, "4"sv, 5.0);  // 1 2 3 4 5
ez::println(Point{1, 2}, Point{3, 4});  // {1 2} {3 4}
ez::println(std::array{1, 2, 3, 4});    // {1 2 3 4}
ez::println(std::vector{1, 2, 3});      // {1 2 3}

ez::println(std::tuple{Point{1, 2}, "3"s, 4.0});  // {{1 2} 3 4}

ez::println(std::map<std::string, Point>{{"a", {1, 2}}, {"b", {3, 4}}});  // {a: {1 2}  b: {3 4}}

struct Rec { Point a, b; };
ez::println(Rec{{1, 2}, {3, 4}});  // {{1 2} {3 4}}

struct NamedRec { Rec rec; std::string name; };
ez::println(NamedRec{{{1, 2}, {3, 4}}, "5"});  // {{{1 2} {3 4}} 5}

ez::println(
  std::tuple{Rec{1, 2, 3, 4}, "5", 6.0, "7"s, std::pair{std::tuple{8}, std::tuple{Point{10, 11}, "12"sv}}}
);  // {{{1 2} {3 4}} 5 6 7 {{8} {{10 11} 12}}}

It just works.

Usage

ezprint prints streamable types, types that can be streamed through std::ostream&, and their composition.

ez::fprint(os, args...)
ez::fprintln(os, args...)

Prints args to os. The suffix ln adds a newline at the end.

ez::print(args...)
ez::println(args...)

Shorthand for fprint(std::cout, args...).

ez::sprint(args...)

Prints to a std::string and returns it.

Streamable Types

A streamable type is one of the following.

  1. A type that can be streamed to std::ostream&.
  2. A range-like type, ie can be looped over.
  3. A tuple-like type, ie has tuple_size.
  4. An aggregate.

An unstreamable type is printed as an error string. This makes it possible to print types composed of both streamable and unstreamable types.

Remarks

  • An aggregate can have at most 64 data members. This is controlled by the implementation by DEFINE_AS_TUPLE.

  • ezprint is standard compliant and has no undefined behaviour.

  • ezprint has no dependencies beyond the standard library.

Acknowledgements

Inspiration behind the reflection is by Antony Polukhin.

ezprint's People

Contributors

sinacam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.