Templated structs / variant values

JS js.mdnq at gmail.com
Wed Aug 14 19:26:38 PDT 2013


On Wednesday, 14 August 2013 at 22:28:13 UTC, Marek Janukowicz 
wrote:
> I need to have a generalized "settings" functionality that 
> should work like
> this:
> * I need to be able to add this to a class
> * each setting has its name, description, type and default value
> * I need to iterate over all settings for given object
>
> API would more or less look like:
> class A {
>  ... here I somehow define a setting called "maxTime"
> }
>
> A a = new A();
>
> auto b = a.settings["maxTime"].value; // b would contain 
> default value,
> because it wasn't set explicitly yet
> string s = a.settings["maxTime"].description; // s would 
> contain setting
> description
> a.settings["maxTime"].value = 10.seconds; // This would only 
> work if maxTime
> setting is of type duration
> foreach( name, value; a.settings ) ...
>
> Now the problem that I have is with storing those settings. 
> What I've come
> up with so far are either Variants or templated structs (either 
> really
> templated and type parametrized structs returned from template 
> functions).
> The problem with variants is that I don't know how to force the 
> value to be
> of certain type (variant accepts anything). As for templates 
> and structs I
> tried something like:
>
> auto setting (string name, string description, T, T deflt)() {
>   struct Setting {
>     string name = name;
>     T value;
>   }
>   return Setting(name, deflt);
> }
>
> but then I don't know if there is any way to build an array of 
> those to be
> able to iterate over them.
>
> I know generally there should be some kind of "setting 
> definition" defined
> on class level that would keep shared information (like names 
> and defaults)
> and then per-instance data containing just actual values, but I 
> don't really
> care and any solution where this shared information is copied 
> for every
> instance would also be fine.
>
> I know the description above is a bit messy (it's quite late 
> now), but
> hopefully you can get the picture. Any ideas how to approach 
> this problem?

I don't really follow what exactly you are trying to do. You 
first example is written using an associate array format which D 
already can handle. If you are wanting more "member" like 
behavior then there are several ways to go about it.

e.g.,

1. a.maxTime can be used by using opDispatch and redirecting it 
to settings["maxTime"].

2. You can use string mixins to create your own DSL like behavior 
to code how you want. This is somewhat complex and there is no 
syntax highlighting or property error analysis but it works.


If you are wanting to have default settings and specific settings 
for objects then it can be done. I did this once in C#. You can 
write a whole framework to do this. I used an AA that would 
lookup the value for the object(using the objects hash) and if it 
didn't exist it would get the parent value, if that didn't exist 
it would work itself up the tree until it found one.



e.g.,

A a = new A;

writeln(a.settings.maxTime); // uses opDispatch. maxTime is 
looked up in the global settings. If settings[a.hash~"maxTime"] 
exists then it's value is the maxTime setting for a. If it 
doesn't exist, try settings[a.parent.hash~"maxTime"], etc...



More information about the Digitalmars-d-learn mailing list