HydrogenAudio

Hosted Forums => foobar2000 => Development - (fb2k) => Topic started by: vroomvroom on 2012-01-17 07:51:14

Title: how to access the instantiation of my component?
Post by: vroomvroom on 2012-01-17 07:51:14
Hi all,

Please bear with me while I ask a possibly dumb question. How do I access the instantiated object of my component? Let's say I'm creating two ui_elements, A and B. I would like A to be able to call B's public functions, without making B's public functions static. In order to do this, A would need access to B's instantiation but I'm unable to figure out how.

For example, taking the SDK's foo_sample's ui_element.cpp:
Code: [Select]
class CMyElemWindow : public ui_element_instance, public CWindowImpl<CMyElemWindow> {
...
}

// ui_element_impl_withpopup autogenerates standalone version of our component and proper menu commands. Use ui_element_impl instead if you don't want that.
class ui_element_myimpl : public ui_element_impl_withpopup<CMyElemWindow> {};

static service_factory_single_t<ui_element_myimpl> g_ui_element_myimpl_factory;

It seems that the last two lines have to do with the instantiation of my class, but I'm unable to figure out how to get access to this instantiation.

I realize I could (or should) just create a single ui_element instead of two, but having two separate ui_elements allows me to arrange them in different spots within foobar.
Any ideas?

Many thanks in advance..
Title: how to access the instantiation of my component?
Post by: foosion on 2012-02-20 12:33:42
I realize I could (or should) just create a single ui_element instead of two, but having two separate ui_elements allows me to arrange them in different spots within foobar.
I'm a bit confused by that statement. Can you describe your use case? Do you want communication between two different types of UI elements (i.e. different classes) or between different instances of the same class?

I realise your post is already a month old, so I have no idea if you have solved the problem in the meantime.
Title: how to access the instantiation of my component?
Post by: vroomvroom on 2012-02-29 07:49:04
Thanks for your response. Does it matter whether I want communication between different classes or different instances of the same class? They're still separate instances, and I do not have handles for any of them. By the way, my use case involved two different classes.

In any case, I got around the problem by doing two things: (I had multiple, not just two, classes, so I used two different solutions)
1) merge two classes into one
2) create a global variable that is a pointer to a class A, make class B a friend of class A, and use that pointer from class B (to access members in class A)

I realize 2) is kinda ugly, but it was the best I could do to keep things orderly (i.e. separate classes) while allowing "inter-class" communication.



Title: how to access the instantiation of my component?
Post by: foosion on 2012-02-29 10:17:04
It matters whether you want to achieve coupling between two instances from different classes (one-to-one communication) or broadcast to all instances of a given class (one-to-all communication). Note that UI elements should be able to handle multiple instances and not just zero or one. If you want to track your current instances you should use per-class instance lists instead of a single pointer.

I cannot give you more specific advice because your use case is still unclear to me.