Skip to main content

Notice

If you are using a Hotmail or Outlook email address, please change it now, as Microsoft is rejecting all email from our service outright.
Topic: cfg_ObjList and stream overload. (Read 3612 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

cfg_ObjList and stream overload.

I'm currently implementing some settings vars in my plugin and I've stumbled upon some annoying stuff with the stream read and write overloading.

The problem is here:
Code: [Select]
FB2K_STREAM_READER_OVERLOAD(MyClass*)
{
    stream >> value->some_string;
        stream >> value->some_int;
    return stream;
}
FB2K_STREAM_WRITER_OVERLOAD(MyClass*)
{
    stream << value->some_string;
        stream << value->some_int;
    return stream;
}

static const GUID MYCLASSLIST_VAR_GUID = { 0x9c7a49ce, 0xe698, 0x4069, { 0x80, 0x98, 0xa2, 0x4b, 0x6a, 0xb2, 0x21, 0x83 } };

cfg_objList<MyClass*> list(MYCLASSLIST_VAR_GUID);


The above code works when using cfg_ObjList<MyClass>, but keeping raw objects in a list doesn't feel right. At all.

What should I do so that a list of pointers gets written correctly?

I just like listening to music

cfg_ObjList and stream overload.

Reply #1
[quote author=$char(9836) link=msg=705154 date=1273747762]The above code works when using cfg_ObjList<MyClass>, but keeping raw objects in a list doesn't feel right. At all.[/quote]It's okay for value objects.

If you insist on using a list of pointers, maybe you should describe what the problem is, because people might not be inclined to take your code and recreate the problem, so that they can see the symptoms themselves. I am certainly not. ;)

It would also help to look at the definition of the FB2K_STREAM_*_OVERLOAD macros. Or realize that when your writer takes a pointer to an object and produces a stream of bytes, then your reader must take a stream of bytes and produce a pointer to an object. In other words, it would probably help to assign something to the value parameter in the reader function. Even without looking at the macro definition, that parameter must be declared as a reference to a pointer to make any sense.

cfg_ObjList and stream overload.

Reply #2
[quote author=$char(9836) link=msg=705154 date=1273747762]The above code works when using cfg_ObjList<MyClass>, but keeping raw objects in a list doesn't feel right. At all.
It's okay for value objects.
[/quote]

My instances will have pfc::string8 instances that very well could contain a few kB of text. Copying them around doesn't seem very smart.

If you insist on using a list of pointers, maybe you should describe what the problem is, because people might not be inclined to take your code and recreate the problem, so that they can see the symptoms themselves. I am certainly not. ;)


Oh right, sorry.

The overloads simply doesn't get called. Setting a break point in the reader doesn't get triggered and setting one in the writer shows that the function gets removed when linking. Using values instead of pointers make the code work just like I want.

My guess is that some other overload of '<<' and '>>' matches the pointers, and thus skips calling my overloads.  Is that close to the truth? I guess wrapping up MyClass* in a wrapper class would solve it then, although it doesn't seem very nice.

It would also help to look at the definition of the FB2K_STREAM_*_OVERLOAD macros. Or realize that when your writer takes a pointer to an object and produces a stream of bytes, then your reader must take a stream of bytes and produce a pointer to an object. In other words, it would probably help to assign something to the value parameter in the reader function. Even without looking at the macro definition, that parameter must be declared as a reference to a pointer to make any sense.


Yes, of course, the macro actually defines it as a reference. I certainly would have noticed this when I finally got the function to get called ;)



I just like listening to music

cfg_ObjList and stream overload.

Reply #3
Ok, I solved it be creating a light container class that wrapped up MyClass* and then added stream overloads for that class.

If anyone knows of a better way, please let me know!
I just like listening to music

 
SimplePortal 1.0.0 RC1 © 2008-2020