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

multiple formats for a single type ? #862

Closed
jcelerier opened this issue Sep 15, 2018 · 5 comments
Closed

multiple formats for a single type ? #862

jcelerier opened this issue Sep 15, 2018 · 5 comments

Comments

@jcelerier
Copy link
Contributor

jcelerier commented Sep 15, 2018

So, this is more or less a follow-up of #859 : how can there be multiple distinct ways to format a given type with fmt 5 ?

e.g., I'd like to have in one file a formatter which generates a debug-like output, and in another file a formatter which generates some HTML representation, for a given C++ type foo.

With fmt 4 this is easy: just have a function format_html and another format_debug which both take a fmt::MemoryWriter and a foo but serialize the type according to the desired output.

@vitaut
Copy link
Contributor

vitaut commented Sep 17, 2018

With fmt 5 you can also have two functions that perform different formatting, e.g.

// Alternative you can format into memory_buffer instead of returning a string.
std::string format_xml(int n) {
  return fmt::format("<answer>{}</answer>", n);
}

std::string format_debug(int n) {
  return fmt::format("answer={}", n);
}

@vitaut vitaut closed this as completed Sep 17, 2018
@jcelerier
Copy link
Contributor Author

jcelerier commented Sep 17, 2018

but what if a user-defined formatter needs different ways to be formatted ?

how would you format, e.g. a std::vector<int> to [ 1, 2, 3 ] in one function, and <list><value>1</value><value>2</value><value>3</value></list> in another ? fmt::formatter can only be specialized once.

@jcelerier
Copy link
Contributor Author

jcelerier commented Sep 18, 2018

@vitaut this really doesn't work: for instance I cannot provide a custom serialization for std::vector :

template <typename T, typename Alloc>
struct formatter<std::vector<T, Alloc>>
{
  template <typename ParseContext>
  constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }

  template <typename FormatContext>
  auto format(const std::vector<T, Alloc>& v, FormatContext &ctx)
  {
    auto out = ctx.out();
    format_to(out, "[");
    const auto n = v.size();
    if (n > 0)
    {
      format_to(out, "{}", v[0]);

      for (std::size_t i = 1; i < n; i++)
      {
        format_to(out, ", {}", v[i]);
      }
    }
    format_to(out, "]");
    return ctx.out();
  }
};

because if at some point someone includes <fmt/ranges.h> it won't compile anymore

@jcelerier
Copy link
Contributor Author

(which is sensibly the same problem than #865)

@vitaut
Copy link
Contributor

vitaut commented Sep 19, 2018

but what if a user-defined formatter needs different ways to be formatted ?

fmt provides you with formatting primitives that you can use to build your own functions that write data in XML or any other format. You probably shouldn't plug the data-format specific logic into the fmt's formatters themselves in the same way as you wouldn't write a custom ostream class for XML output.

The problem with ranges.h is a separate issue that will be addressed in #865.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants