#How to pass data into a coroutine without storing the data into the said coroutine

1 messages · Page 1 of 1 (latest)

hearty crater
#

Soo i wanna desigin my own coroutine stuff for something along the lines of


T wait_for_packet_id(int id)
{

}

task<void> process()
{
  //if some more data is needed
  co_await wait_for_packet_id(123);

  co_return
}

void main_loop()
{

  while (true)
  {
    auto data = read_data_from_network();
  
    if(data.id)
    {
      //wakeup a coroutine (or all on at a time) that waits for it 
      continue;
    }
    process();
    //if process did not finish maybe add to queue or something
    // not yet at this part of the thing
  }
}

The issue that i am having is that everyway i look at it, it seems that i need to basically store std::any within task<T>/promise type which (i hope) is not the actual solution of this.
I am aware that wait_for_packet_id needs to "register" itself to a queue/vector of such waiting coroutines and basically map the ids to waiting routines or something that's not really the issue i am having.
The issue is getting the data in.

One other thing i was thinking about is having a custom awaitable that stores the event id and basically have a queue/vector of such ids (inheritance or something) and wake the coroutines that way. but then it seems kinda weird returning event_id& or somethign idk kinda lost

forest daggerBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

wispy sparrow
#

The best option would just be templated on the data you actually want to store, or use if templates aren't an option use dynamic dispatch and a shared_ptr

hearty crater
#

Its not about the type i want to store

#

Its about the type i want to input into the coroutine

wispy sparrow
#

Well it still sounds like your best bet is just to use dynamic dispatch, and not resort to std::any, unless I am missing something, I am not sure where in the example you would need an std::any

hearty crater
#

T from wait_for_packet_id should either be a coroutine or awaitable basically. What its supposed to do is give controll back to the main loop untill a packet of the expected ID appears then return the packet

#

So i need a way of puttin data into the routine