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

Small issues in docs #1

Closed
c42f opened this issue Dec 8, 2012 · 2 comments
Closed

Small issues in docs #1

c42f opened this issue Dec 8, 2012 · 2 comments

Comments

@c42f
Copy link

c42f commented Dec 8, 2012

Hi, interesting to set yet another take on this problem :)

I had a look at the docs out of curiosity and have a few comments (maybe I am
just expecting too much, since this library seems to be only a few hours old!)

Support for user-defined types

I wanted to see how you achieved this, but I can't see that it works as
expected. For instance, the following is a compile error:

format::Print("a string: {0}\n") << std::string("asdf");

I thought about implementing positional arguments in tinyformat, and the best
solution I came up with involved a type of variant class as follows:

class Arg
{
    public:
        virtual void print(std::ostream& out) = 0;
};

template<typename T>
class ArgT : public Arg
{
    const T& m_value;
    public:
        virtual void print(std::ostream& out)
        {
            out << m_value;
        }
};

You'd then construct a std::vector<Arg*> as you sweep through the arguments,
creating an ArgT for the Nth argument. (The could be stack-allocated
in the tinyformat case, though unfortunately not with the << based interface.)
Reordering can then be achived simply using indexing.

In the end I didn't do it because I didn't need the functionality and it would
introduce some extra complexity to tinyformat, which is alreay a lot less tiny
than I would like ;-) It also means a virtual function call per argument which
is less than ideal...

i18n in printf
posix printf does have a notation to support positional arguments for i18n,
and this is implemented in glibc even though it's not in C99. For example,
try out:

printf("%2$s  %1$s\n", "first_arg", "second_arg");

Good luck with your implementation!
~Chris

@vitaut
Copy link
Contributor

vitaut commented Dec 9, 2012

Thank you for looking at my library and for your comments.

The documentation was a bit ahead of implementation.
I've just added support both for user-defined types (UDT) and for std::string.
For supporting UDTs I implemented a simple idea very similar to yours, the only
difference is that I use a pointer to a (member) function instead of an object with
a virtual function:

template <typename T>
void Formatter::FormatCustomArg(const void *arg) {
    const T &value = *static_cast<const T*>(arg);
    // format value using iostreams and append to the buffer.
}

I plan to add support for user-provided formatting functions which could be more
efficient than the current method by working directly with the output buffer and
could also take format specifications. Another way to improve the current method
is to implement an output stream that writes directly to the buffer and use it instead
of std::ostringstream.

I corrected a section about printf mentioning the POSIX extension that adds
positional arguments.

Thanks,
Victor

@vitaut vitaut closed this as completed Dec 9, 2012
@c42f
Copy link
Author

c42f commented Dec 9, 2012

Right, I couldn't help thinking more about the reordering problem and came to
the same conclusion: a list of pointer to template functions and void*
arguments would work nicely. I'm very tempted to implement it in tinyformat and
measure the overhead vs not reordering.

The idea of wrapping your buffer_ up in a std::streambuf and using that
instead of the stringstream also sounds good.

@cppden cppden mentioned this issue Jul 17, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 27, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 27, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 29, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 29, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 30, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 30, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 30, 2018
DanielaE referenced this issue in DanielaE/fmt Sep 30, 2018
vitaut pushed a commit that referenced this issue Sep 30, 2018
rimathia pushed a commit to rimathia/fmt that referenced this issue Sep 20, 2020
MSVS IDE speed-test target to run tests
GerHobbelt pushed a commit to GerHobbelt/fmt that referenced this issue Mar 3, 2024
GerHobbelt pushed a commit to GerHobbelt/fmt that referenced this issue May 23, 2024
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