source: cpp/frams/_demos/loader_test_param.cpp @ 352

Last change on this file since 352 was 348, checked in by Maciej Komosinski, 10 years ago
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
File size: 4.6 KB
RevLine 
[307]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
[308]5#include <frams/param/multiparamload.h>
[307]6#include <frams/virtfile/stdiofile.h>
7#include <frams/errmgr/stdouterr.h>
8
9/**
10 @file
[308]11 Sample code: Loading Framsticks "objects" (structures).
[307]12
[308]13 After loading, objects are printed ("saved") to standard output.
14 Additional information and messages are printed to standard error.
[329]15 You can redirect one or both streams if needed.
16 
17 A sample input file for this program is "loader_test_param.in",
18 so you can run this program from the "cpp" directory as
19 ./loader_test_param  frams/_demos/loader_test_param.in
20 
[307]21 \include loader_test_param.cpp
22 */
23
24struct Data
25{
26public:
[311]27        SString text, longtext, deftext;
[329]28        paInt i1, i2, i3, i4;
29        double f1, f2, f3, f4;
30        ExtValue x1, x2;
31        int notchanged, notloaded, notsaved;
[307]32};
33
34#define FIELDSTRUCT Data
35ParamEntry data_paramtab[] =
36{
[329]37        { "Data", 1, 16, "data", },
[307]38        { "text", 0, 0, "Text", "s 0 10", FIELD(text), }, // 10 = length limit
39        { "longtext", 0, 0, "Long text", "s 1", FIELD(longtext), }, // 1 = multiline, 0 = unlimited
[311]40        { "deftext", 0, 0, "Text with default value", "s 0 0 some text", FIELD(deftext), },
[307]41        { "i1", 0, 0, "Integer 1", "d", FIELD(i1), },          // unrestricted integer
[308]42        { "i2", 0, 0, "Integer 2", "d -5 5 1", FIELD(i2), }, // [-5..5] integer, default=1
[311]43        { "i3", 0, 0, "Integer 3", "d -1 3 0 ~Minus~Zero~One~Two~Three", FIELD(i3), }, // [0..3] + text labels (Param::getText())
[329]44        { "i4", 0, 0, "Integer 4", "d 0 10 15", FIELD(i4), }, // invalid default
[307]45        { "f1", 0, 0, "Float 1", "d", FIELD(f1), },          // unrestricted float
46        { "f2", 0, 0, "Float 2", "f -100 100 -100", FIELD(f2), }, // [-100..100] float, default=-100
[329]47        { "f3", 0, 0, "Float 3", "f -10 10", FIELD(f3), }, // [-10..10] float
[330]48        { "f4", 0, 0, "Float 4", "f 1 -1 44", FIELD(f4), }, // unrestricted float (because min>max), default=44
[329]49        { "x1", 0, 0, "Untyped 1", "x", FIELD(x1), }, // any type (class ExtValue)
50        { "x2", 0, 0, "Untyped 2", "x", FIELD(x2), }, // any type (class ExtValue)
[330]51        { "notchanged", 0, PARAM_READONLY, "Read only field", "d", FIELD(notchanged), }, // neither load() nor setDefault() can change this field
52        { "notloaded", 0, PARAM_DONTLOAD, "Non-loadable field", "d", FIELD(notloaded), }, // load() does not change this field
53        { "notsaved", 0, PARAM_DONTSAVE, "Non-saveable field", "d", FIELD(notsaved), }, // save() skips this field
[307]54        { 0, 0, 0, },
55};
56#undef FIELDSTRUCT
57
58int main(int argc, char*argv[])
59{
60        if (argc < 2)
61        {
62                fprintf(stderr, "Arguments: filename\n");
63                return 1;
64        }
65
66        StdioFILEDontClose virt_stderr(stderr);
[308]67        StdioFILEDontClose virt_stdout(stdout);
[307]68        StdoutErrorHandler error_handler(0, &virt_stderr);
69        StdioFileSystem_autoselect stdiofilesys;
70        MultiParamLoader loader(argv[1]);
71
72        Data data;
73        Param param(data_paramtab, &data);
74
[329]75        data.notchanged = 100;
76        data.notloaded = 200;
[311]77
[307]78        loader.addObject(&param);
[323]79        loader.breakOn(MultiParamLoader::OnError + MultiParamLoader::BeforeObject + MultiParamLoader::AfterObject + MultiParamLoader::OnComment + MultiParamLoader::BeforeUnknown);
[307]80
81        while (int status = loader.go())
82        {
83                switch (status)
84                {
85                case MultiParamLoader::OnComment:
[348]86                        fprintf(stderr, "comment: '%s'\n", loader.getComment().c_str());
[307]87                        break;
88
[323]89                case MultiParamLoader::BeforeUnknown:
90                        // At this point we could change our mind and load the unknown object using MultiParamLoader::loadObjectNow() functions.
91                        // It is "unknown", so we would have to provide its ParamInterface.
92                        // In fact, this method is used not just for truly unknown objects but also for
93                        // dynamic objects that cannot be added using MultiParamLoader.addObject().
[348]94                        fprintf(stderr, "unknown object found: '%s' (will be skipped)\n", loader.getObjectName().c_str());
[323]95                        break;
96
[307]97                case MultiParamLoader::AfterObject:
[308]98                        fprintf(stderr, "loaded:\n");
[307]99                        for (int i = 0; i < param.getPropCount(); i++)
[348]100                                fprintf(stderr, "%s=%s\n", param.id(i), param.getText(i).c_str());
101                        fprintf(stderr, "type of 'x1' is: %s\n", data.x1.typeDescription().c_str());
102                        fprintf(stderr, "type of 'x2' is: %s\n", data.x2.typeDescription().c_str());
[308]103                        fprintf(stderr, "-----\n\n");
104                        param.save(&virt_stdout);
[307]105                        break;
106
107                case MultiParamLoader::BeforeObject:
[308]108                        fprintf(stderr, "----- object found, loading...\n");
[311]109                        data.notchanged++;
[307]110                        param.setDefault(); //reset (initialize) struct contents
111                        break;
112
[308]113                case MultiParamLoader::OnError:
[348]114                        fprintf(stderr, "Error: %s", loader.getError().c_str());
[307]115                }
116        }
[308]117        return 0;
[307]118}
Note: See TracBrowser for help on using the repository browser.