source: cpp/frams/_demos/mutableparam_test.cpp @ 980

Last change on this file since 980 was 729, checked in by Maciej Komosinski, 7 years ago

mutableparam_test added: demonstrates usage of ParamTransaction? and dynamic properties and groups

File size: 2.3 KB
Line 
1#include <frams/util/sstringutils.h>
2#include <common/virtfile/stdiofile.h>
3#include <frams/param/mutableparam.h>
4#include <frams/param/mutparamlist.h>
5#include <frams/param/paramtrans.h>
6
7static void printParam(ParamInterface &pi)
8{
9        printf("groups:\n");
10        for (int g = 0; g < pi.getGroupCount(); g++)
11                printf("- %s\n", pi.grname(g));
12        printf("properties:\n");
13        for (int i = 0; i < pi.getPropCount(); i++)
14                if (!(pi.flags(i) & PARAM_USERHIDDEN))
15                        printf("- %s (i=%d)\n", pi.id(i), i);
16}
17
18int main()
19{
20        StdioFILE::setStdio(); //setup VirtFILE::Vstdin/out/err
21
22        //creating 3 objects
23        MutableParam p1("Object1", "Group 1"), p2("Object2", "Group 2"), p3("Object3", "Group 3");
24
25        p1.addGroup("Added Group");
26        p1.addGroup("Added Group 2");
27
28        p1.addProperty(NULL, "f1", "d 0 5", "Field 1", "Description 1", 0/*flags*/, 0/*group*/, -1);
29        p2.addProperty(NULL, "f2", "s", "Field 2", "Description 2", 0/*flags*/, 1/*group*/, -1);
30        p3.addProperty(NULL, "f3", "d 0 1", "Field 3", "Description 3", 0/*flags*/, 1/*group*/, -1);
31
32        MutableParamList combined;
33        combined += &p1;
34        combined += &p2;
35        combined += &p3;
36
37        ParamTransaction paramtrans(combined);
38
39        printf("\n=== Combined ===\n");
40        printParam(combined);
41
42        printf("\nNow adding new property in p1...\n");
43        p1.addProperty(NULL, "f4", "x", "Field 4", "Description 4", 0/*flags*/, 0/*group*/, -1);
44
45        printf("\n=== After adding f4 ===\n");
46        printParam(combined);
47
48        printf("\n=== Transaction view maintains the original index association ===\n");
49        //...so it can be safely iterated by index while param properties are being added or removed
50        //without knowing or detecing what was changed.
51        //Use case: simulator parameters in GUI - setting the "expdef" property
52        //affects the properties of the selected and set experiment definition.
53        printParam(paramtrans);
54
55        printf("\nProperties changed: %s   Groups changed: %s\n\n", paramtrans.propChanged() ? "YES" : "NO", paramtrans.groupChanged() ? "YES" : "NO");
56
57        printf("Now removing group in p1...\n");
58        p1.removeGroup(1);
59
60        printf("\nProperties changed: %s   Groups changed: %s\n\n", paramtrans.propChanged() ? "YES" : "NO", paramtrans.groupChanged() ? "YES" : "NO");
61
62        //in GUI, paramtrans.groupChanged() can be used to trigger rebuilding the tree of groups
63        //(see also: Framsticks GUI and sources of paramtree_* examples).
64
65        return 0;
66}
Note: See TracBrowser for help on using the repository browser.