Callbacks and pointers :)

hello :smiley:

iā€™m just having a think about the method iā€™ve used for handling data in my program and think that itā€™s a bit sloppy, and would just like to check for opinions.

my main component (sitting in a dialog window) holds an array of objects. this array is stored there as it is used thruout the program, but there are two ā€˜pageā€™ components which operate on the data in different ways.

at the moment, each of these subcomponents are created with a pointer to the array, which they use to access the data. however, iā€™m sure that this is a stupid and naive way of doing things and iā€™d like to get into the habit of doing things sensibly.

what is recommended? do i put all the code that manipulates the data within the main component, and have some kind of callback system? iā€™m not entirely sure how iā€™d implement that kind of thing.

if, for example, a page component wishes to set a value of an object in the array, what would be the best way? at the moment, it simply calls a class member function to perform some operation on the array from the pointer. this just feels like bad practise. should the function that manipulates the data exist within the main component?

actually, obviously itā€™s best that the data live in its own class to handle all manipulation. (thatā€™s very stupid of me to not do that here)ā€¦ but how does the page component best tell the data to operate?

iā€™d greatly appreciate any help on this topic. cheers

oooh, i think i have an idea-

do i inherit ActionBroadcaster in my page component, and have it send an action message with some defined name; then the main component would have an action for this message which would tell the data to operate?

is this right? :?:

In oo parlance, each ā€˜pageā€™ ā€œis aā€ thing, but has slightly different ā€˜behaviorsā€™. This implies inheritance. You can make ā€˜pageā€™ a base class, but declare the ā€˜data operationā€™ function virtual, then derive two classes from ā€˜pageā€™, and implement the ā€˜data operationā€™ differently in each. This assumes of course that the interface (calling convention) for the different data operations is the same.

[edit]
Thought Iā€™d also mention that calling functions with parameters is hundreds of times faster than passing messages between JUCE components.

if these page type classes were similarly derived then, how would they actually access the data which is stored in the parent class?

forgetting details, if the main structure looked like this:

class Component maincomponent
{
DataStore data
PageType page1
PageType page2
};

how would page1 and page2 actually access the data? at the moment they recieve a pointer to the DataStore which they use to operate, but something tells me that thatā€™s sloppy, and there should be a more ā€˜solidā€™ method of them accessing the data, perhaps getting the parent to ā€˜doā€™ stuff with it.

for example, if there was a ā€˜incrementAllDataValues()ā€™ function they could call - who would actually ā€˜doā€™ that? would they indicate to the maincomponent that they want to do it, and let the parent handle whether or not it should call the function? or should they just do what they do now and call it themselves?

and if they want to retrieve a data value? something tells me that itā€™s better to have a temporary storage in the page; it notifies the maincomponent that it needs a value, the value is retrieved by the maincomponent and stored in the pageā€™s ā€˜currentValueā€™ space, and then the page is updated? or is it an acceptable method to simply use a pointer within the page to just get it itself?

i feel that it may be ā€˜betterā€™ to keep each class contained, so that the actual ā€˜dataStoreā€™ can be independant. does that make sense? or am i needlessly complicating things? iā€™m just wary of developing bad practises when there may be a really tight acceptable way of doing such things.

Hmmm. Not sure if Iā€™m getting you completely. Butā€¦

Iā€™d make a singleton class to hold the array AND carry out manipulation itself.

e.g. ( with much omitted )


typedef OwnedArray<ObjectType> ObjectTypeArray;

class DataStore
{
public:
       DataStore();
       ~DataStore();

      bool doSomethingInterestingWith( int arrayIndex )
      {
              array[ arrayIndex ]->interestingProcedure();
      }

      juce_DeclareSingleton (DataStore, false)


private:
      ObjectTypeArray array;
}

Then your calling classes just doā€¦

This assumes you only have one datastore of course!

see juce_Singleton.h

klf

yes, having a class which contains the data and operations is what i have done (not sure why i didnā€™t do that in the first place) but actually getting the other components to be able to REFER to an instance of that class stored in a parent component is what i was troubling myself with.

i.e. the ā€˜pagesā€™ are stored in the same component as the data; should the pages have direct access to the classes stored in their parent? or just tell the parent that they want to do something with it and the parent tells the data to do it?

iā€™ll look at this singleton thing tho - iā€™ve no idea what that is! :o

am i being stupid? am i worrying about nothing?

possibly. Iā€™m no expert but I see nothing wrong with objects having member variable pointers to objects in the parent.

calling the parent to do something with the data would mean passing a ā€œthisā€ pointer to the child object. So thats passing a pointer anyway.
or
using juce getParentComponent() which is essentially the same thing but youā€™d have to somehow cast the result to get at any specialised methods ( extensions to base Component ) (which caused me all sorts of circular header file nightmares - I gave up on that and went with resending mouseevents to the parent instead)

so yeah. just give em a pointer unless you like the singleton thang.

klf

PS stoned

i would say go with the action listenerā€¦ thatā€™s what i usually do (not sure if it is the best approach though)

yeah, actionListeners. wasnā€™t thinking of them as my subcomponents donā€™t have buttons, just graphics so I was mouseListening.

:hihi: thatā€™s the spirit!

i was hoping to see at least one person say something about actionlisteners!

so i inherit an actionbroadcaster, and then send a string message to the parent to instruct it to ā€˜doā€™ something with the data class?

when it comes to actually getting info from the data class tho, what would the approach be?

lets say i want to retrieve a colour from the datastore. is the following approach a sensible one?

(from within page1)

page1::initiateColourRetrieval()
{
    sendActionMessage(T("getcolour"));
}


mainParent::actionListenerCallback(const String msg)
{
   if (msg == T("getcolour"))
      col = dataStore->getColour();
      myPage1->giveColour(col);
}

crumbs it all looks rather convoluted...
i imagine if i just wanted to take a number from the store to do a calculation with then this kind of approach is stupid...

am i missing something here? have i just demonstrated a complete misunderstanding of callback technique?

If youā€™re worrying about references, maybe ReferenceCountedObjectPtrā€™s are what youā€™re after?

[quote=ā€œhaydxnā€]:hihi: thatā€™s the spirit!

i was hoping to see at least one person say something about actionlisteners!

so i inherit an actionbroadcaster, and then send a string message to the parent to instruct it to ā€˜doā€™ something with the data class?

when it comes to actually getting info from the data class tho, what would the approach be?

lets say i want to retrieve a colour from the datastore. is the following approach a sensible one?

(from within page1)

page1::initiateColourRetrieval()
{
    sendActionMessage(T("getcolour"));
}


mainParent::actionListenerCallback(const String msg)
{
   if (msg == T("getcolour"))
      col = dataStore->getColour();
      myPage1->giveColour(col);
}

crumbs it all looks rather convoluted...
i imagine if i just wanted to take a number from the store to do a calculation with then this kind of approach is stupid...

am i missing something here? have i just demonstrated a complete misunderstanding of callback technique?[/quote]

hmmm. don't like that much meself. seems like far too much going on for a simple data retrieval. if mainParent, page1 and page2 all need to speak to the dataStore then just give the fuckers all a  pointer keeping mainParent as in charge of creation/deletion.

or if you can see your dataStore as a sort of "global registry" then I fancy the singleton as you can "summon" the bastard from anywhere. I like that way but it has to fit with your design. (you wouldn't want ALL your objects as singletons ALL summonable from ANYWHERE! LOL!)


[code]
page1::initiateColourRetrieval()
{
    _colour = DataStore::getInstance()->getColour();
}
[/code]

yes, indeed having callbacks and whatnot for data retrieval does create a lot of red-tape. i can imagine there being times when itā€™s useful, but thereā€™s no point in keeping the main object in charge of everything - after all the page object should dictate its own instructions over the data.

thanks a lot for discussing this guys. itā€™s much easier to see the best options when people help shine their opinions on things.