Having trouble with objects.
Jarrod
qwerty at ytre.wq
Tue Mar 18 05:17:36 PDT 2008
Hey all.
As I might have mentioned a while ago, I have a config library that I
wrapped in D classes a while ago.
While it works perfectly fine, it's currently running somewhat
inefficiently and I wish to fix this.
The problem: The library currently contains two classes. A root class and
a node class. To load/create a config, one would create a root class and
tie it to a file. Each key=value pair in the config can be accessed by
passing the value's "path" to the root object, which will return a node.
Herein lies the issue.
Nodes are very short lived objects, and are often created and discarded
very quickly. It would make far more sense if a node was a struct, as
classes being uses like this tend to needlessly waste memory. The problem
is I need to create the node in the root object (or in a parent node) and
pass it back to the caller which would be more efficient to do with
classes. In C++, what I would do is pass the back argument to the
struct's constructor, which would create the struct and pass it back
rather efficiently, and the node would be destroyed when scope is left. I
doubt this is possible in D since structs don't have constructors.
So what should I do?
Another issue: Nodes can contain different data types. As such, I need to
access their contents using an overloaded function 'getValue' which will
use the passed parameter to determine what type of data the node holds
and then pass it back. Yes, this sucks. Since DMD doesn't have implicit,
multiple opCast overloads it appears that the only other solution is to
have a different node type for each type of data. I guess this can be
done with templates but nodes are created at runtime, not compile time.
Different node types also mean I need to know what type of node the root
config will return and I can't always do that since configs are user
editable.
Is there anything I can do about this?
After writing this post out and looking at my situation I kind of get the
feeling I'm just going to have to grit my teeth and bear it for now
because it looks like the only workarounds are probably going to be
elaborate hacks. But hey, if you can think of something I'm all ears.
Thanks,
Jarrod.
PS pleaseeee add multiple implicit opCasts and struct constructors to D
soon, Walter. It would be really helpful, especially for making and using
abstract data types.
PSS It's kind of late so I'm sorry if this post has any grammatical
errors.
More information about the Digitalmars-d-learn
mailing list