Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: cfg_ObjList and stream overload. (Read 4505 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
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.

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
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.


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