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

Copy a fmt::dynamic_format_arg_store<Context> object #2431

Closed
spyridon97 opened this issue Jul 21, 2021 · 7 comments
Closed

Copy a fmt::dynamic_format_arg_store<Context> object #2431

spyridon97 opened this issue Jul 21, 2021 · 7 comments
Labels

Comments

@spyridon97
Copy link

spyridon97 commented Jul 21, 2021

I have a fmt::dynamic_format_arg_store<fmt::format_context> args object which i populate with some variables.

I would like to be able to copy args into a new object args2 so that I can push back new variables while :

  1. maintaining all the variables of args in args2
  2. without touching args

Is that possible in any way?

  1. There is no copy constructor for fmt::dynamic_format_arg_store. Could one be implemented?
  2. Can I convert fmt::dynamic_format_arg_store to fmt::basic_format_args, and extract the variables from there and push them back to a fmt::dynamic_format_arg_store object?
@vitaut
Copy link
Contributor

vitaut commented Jul 21, 2021

There is no copy constructor for fmt::dynamic_format_arg_store. Could one be implemented?

It could be implemented but if you want to capture arguments you could use a std::tuple instead. Also note that dynamic_format_arg_store is moveable.

Can I convert fmt::dynamic_format_arg_store to fmt::basic_format_args, and extract the variables from there and push them back to a fmt::dynamic_format_arg_store object?

No

@spyridon97
Copy link
Author

spyridon97 commented Jul 21, 2021

I don't know beforehand the types of variables that I will push_back, therefore, how could I use a std::tuple?

I need to copy, not move, so being moveable is something I can't utilize.

As far as the copy- constructor, I quickly implemented something like below inside dynamic_format_arg_store:

    /**
     \rst
     Default constructor
     \endrst
     */
    constexpr FMT_INLINE dynamic_format_arg_store() = default;

    /**
     \rst
     Constructs a `dynamic_format_arg_store` object from
     dynamic_format_arg_store`.
     \endrst
     */
    constexpr FMT_INLINE dynamic_format_arg_store(
            const dynamic_format_arg_store<Context>& store)
    {
        this->data_ = store.data_;
        this->named_info_ = store.named_info_;
    }

Most probably that's not the best/correct way to do it, but it servers my purpose.

Do you think that something like that could be added? It would be highly appreciated.

Thanks in advance!

@vitaut
Copy link
Contributor

vitaut commented Jul 21, 2021

I don't know beforehand the types of variables that I will push_back, therefore, how could I use a std::tuple?

Yeah, tuple can only be used if types are known at compile-time.

Do you think that something like that could be added?

Maybe but why is move not sufficient?

@spyridon97
Copy link
Author

spyridon97 commented Jul 21, 2021

Maybe I am not understanding the move concept correctly.

Could you give me an example of how move could be used to achieve my purpose?

@vitaut
Copy link
Contributor

vitaut commented Jul 21, 2021

I don't know about your use case which is why I'm wondering why you need two identical copies. Another option is to pass a reference or a shared_ptr to dynamic_format_arg_store. Any of that will likely be more efficient than copying.

@spyridon97
Copy link
Author

spyridon97 commented Jul 21, 2021

I am trying to to something like below:

        fmt::dynamic_format_arg_store<fmt::format_context> dArgs;

        std::vector<int> v1 = {5, 1235, 1235123};
        dArgs.push_back(fmt::arg("global_username", std::ref("utkarsh1")));
        dArgs.push_back(fmt::arg("global_hostname", std::ref("miron")));
        dArgs.push_back(fmt::arg("global_vector", v1));

        fmt::dynamic_format_arg_store<fmt::format_context> dArgs2(dArgs);
        dArgs2.push_back(fmt::arg("extra", std::ref("extra-value")));

I want dArags to remain the same, and dargs2 to be instantiated with dArgs and extended in the future. I want to preserve dArags as is because later on, I will create dArags3, and also instantiate with dArags, and then add some other variables not related to dArags2.

In simple terms, I want to use dArags as the baseline for the creation of other dArags# to emulate something like a scope of arguments based on the class hierarchy of my code.

@vitaut
Copy link
Contributor

vitaut commented Jul 21, 2021

Makes sense. In that case my suggestions won't work and a PR to add a copy ctor would be welcome.

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

No branches or pull requests

2 participants