ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.64k stars 399 forks source link

Sharing data across Tabs #88

Closed Daniel-Boll closed 3 years ago

Daniel-Boll commented 3 years ago

I'm having trouble trying to share a variable between different tabs, once they are different classes is there a fair enough way to accomplish this?

I've tried to create a class for the data and instantiate, then assign to each class that will need it. Yet still it doesn't work.

...
Routes routes;
Checkout checkout;

Tab () {
   auto data = std::make_shared<RoutesData>();
   Add(&mainContainer);
   ...
   routes.data = &data;
   checkout.data = &data;   
}
ArthurSonzogni commented 3 years ago

I am lacking informations here. Sorry.

So, you are sharning |data| among different component using a pointer. That's what I would have suggested and that's what you did. Is this working?

Daniel-Boll commented 3 years ago

Sorry for the lack of information, I'll try to provide more.

So basically, I have implemented a Doubly Linked List, then I'm trying to create a routes system. Something like this:

{
   "City a" : { "city a", "description of city a" } ⇌ {"city b", ... } ⇌ { "city  c", ... },
   "City b" : ...
} 

Then I created a class to this map<wstring, DLinkedList<vector>> which I called Routes Data

class RoutesData {
    public:
      std::unordered_map<
            std::wstring,
            DLinkedList<std::vector<std::wstring>>
       > routesData;
};

In it's constructor I just create a example so I can try to access in two different classes(Tabs).

RoutesData() {
      DLinkedList<std::vector<std::wstring>> temp;
      t.add(std::vector<std::wstring>{L"City a", L"City a description"});
      t.add(std::vector<std::wstring>{L"City b", L"City b description"});
      routesData.insert(std::make_pair(L"City a", temp));
}

Then in both components that I want to access it I created a shared_ptr of RoutesData.

class Checkout : public Component {
   ... 

   std::shared_ptr<RoutesData> routesDataReference;

   ...
}

And in the Tabs class I create the shared_ptr for both classes with

class Tabs : public Component {
    public:
       ...
       Routes routes;
       Checkout checkout;
       ...

    Tab() {
       auto t = std::make_shared<RoutesData>();

       routes.routesDataReference = t;
       checkout.routesDataReference = t;
    }
}

Edit:

I manage to get this working, the references above are already correct, the idiot here was passing the make_shared reference, I apologize for my lack of knowledge of c ++ and having made this issue.

routes.routesDataReference = &t;
checkout.routesDataReference = &t;

// And in the classes was receiving as

std::shared_ptr<RoutesData>* routesDataReference;
ArthurSonzogni commented 3 years ago

In version 0.5, I updated deeply the way to use Components. This is using a more functional style. https://arthursonzogni.com/FTXUI/doc/#autotoc_md52

The drawback is that is is probably breaking other's code a bit.