GNOME Bugzilla – Bug 318611
Data hiding in the code of EMsgComposer
Last modified: 2005-10-19 11:21:24 UTC
Please describe the problem: Hi there, I know it's unavoidable in projects like Evolution, yet it's always nice to go over code and fix the occurrences of unavoidable mistakes. One such mistake (well, it's not really a mistake but makes replacing the GtkHtml widget a lot more difficult) is violation of data hiding. For example: <mystruct.h> typedef struct { int hidden_a; } MyStruct; int mystruct_get_a (MyStruct *instance); <myprogram.c> #include <mystruct.h> int test (MyStruct *struc) { printf ("It has %d\n", struc->hidden_a); } Of course should the programmer here have used this: int test (MyStruct *struc) { printf ("It has %d\n", mystruct_get_a (struc)); } What can be done about this (I know most Evolution developers know this, but I'm repeating it to illustrate what I did in this patch): <mystruct.h> typedef struct _MyStructPrivate MyStructPrivate; typedef struct { MyStructPrivate *priv; } MyStruct; int mystruct_get_a (MyStruct *instance); <mystruct.c> struct _MyStructPrivate { int hidden_a; }; int mystruct_get_a (MyStruct *instance) { return instance->priv->hidden_a; } That is basically what I did with e-msg-composer.c and e-msg-composer.h The reason why I also like this to get accepted (and tested) is because this will be the basis of replacing the GtkHtml widget with a GtkMozEmbed widget. You see, if I'm sure that NOBODY used anything else but the agreed EMsgComposer API as defined in e-msg-composer.h, then I can securely replace the internals of e-msg-composer.c. I can then for example replace the Bonobo/CORBA stuff with normal method calls on a GObject/GtkWidget. And then also replace the GtkHtml widget with a GtkMozEmbed that has been set editable (check our go-evolution wiki for more information about that). Steps to reproduce: Actual results: Expected results: Does this happen every time? Other information:
Created attachment 53342 [details] [review] The patch that fixes this
If you're changing the public struct, you could include a pointer to the priv data there and use that in all the functions since doing object->priv is pretty much zero cost, while _PRIVATE(object) is a hash table lookup.
Right .. I might indeed change that (I have been thinking about it too). I tried reusing what was already in place. And the prev. evolution developer decided to use the GObject stuff for private data. I didn't want to break stuff when I began moving the struct-members. But found out that, indeed, it might be simpler and better to just replace it with a *priv and a typedef for EMsgComposerPrivate in e-msg-composer.h.
yes, please do.
Created attachment 53405 [details] [review] Same patch but now without _PRIVATE() This patch uses a normal *priv and forward declares the structs in the .c files.
Created attachment 53406 [details] [review] Little fix in the patch 53405 had a few small diff-problems (some lines weren't correctly being replaced). I created a new diff that doesn't have these problems. (So use this one, not the previous one).
The patch looks fine. I think its ok to commit to HEAD. Thanks
Committed