Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / valueiterator.h @ 81ad8b66

History | View | Annotate | Download (3.45 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  VALUEITERATOR.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#ifndef VALUEITERATOR_H_
19
#define VALUEITERATOR_H_
20
21
#include <string>
22
#include <vector>
23
#include "envirdefs.h"
24
25
NAMESPACE_BEGIN
26
27
/**
28
 * Support class for parsing simulation scenarios. Given a sequence of
29
 * values in string form, this class can enumerate those values.
30
 * Generating numeric sequences with a simple from-to-step syntax
31
 * is also supported.
32
 *
33
 * Syntax: items separated by comma. Quotation marks are respected
34
 * (that is, commas are ignored within string constants).
35
 * If an item has the format <tt><i>number..number</i></tt> or
36
 * <tt><i>number..number</i> step <i>number</i></tt>, it generates
37
 * a numeric sequence.
38
 *
39
 * Example:
40
 * <pre>
41
 * ValueIterator v;
42
 * v.parse("1, 2, foo, bar, 5..8, 10..100 step 10, 10..1 step -1, 1.2e8..1.6e8 step 1e+7");
43
 * v.dump();
44
 * </pre>
45
 */
46
class ENVIR_API ValueIterator
47
{
48
  private:
49
    // the parsed iteration spec
50
    struct Item {
51
        bool isNumeric; // discriminator between the following two
52
        std::string text;
53
        double from, to, step;
54
        int n; // number of steps, calculated from (from,to,step)
55
    };
56
    std::vector<Item> items;
57
58
    // iteration state
59
    int itemIndex;  // index into items[]
60
    int k;          // index within the item
61
62
    // counts how many times op++() was invoked on the iterator
63
    int pos;
64
65
  private:
66
    void parseAsNumericRegion(Item& item);
67
68
  public:
69
    /**
70
     * Constructor; if the optional string argument is specified,
71
     * it will get parsed.
72
     */
73
    ValueIterator(const char *s = NULL);
74
75
    /**
76
     * Destructor.
77
     */
78
    ~ValueIterator();
79
80
    /**
81
     * Parses the given string.
82
     */
83
    void parse(const char *s);
84
85
    /**
86
     * Stateless access: returns the length of the sequence, with numeric
87
     * ranges expanded. Does not change the state of the iterator.
88
     */
89
    int length() const;
90
91
    /**
92
     * Stateless access: returns the ith value in the sequence.
93
     * If i<0 or i>=length(), an error is thrown.
94
     * Does not change the state of the iterator.
95
     */
96
    std::string get(int k) const;
97
98
    /**
99
     * Restarts the iteration.
100
     */
101
    void restart();
102
103
    /**
104
     * Moves the iterator to the next element.
105
     */
106
    void operator++(int);
107
108
    /**
109
     * Returns the current element. Result is undefined when the iteration is
110
     * over (end() returns true).
111
     */
112
    std::string get() const;
113
114
    /**
115
     * Same as get().
116
     */
117
    std::string operator()() const  {return get();}
118
119
    /**
120
     * Returns how many times operator++ was invoked on the iterator
121
     * since construction or the last restart() call.
122
     */
123
    int getPosition() const  {return pos;}
124
125
    /**
126
     * Returns true when the iteration is over, that is, after invoking
127
     * operator++() after the last element.
128
     */
129
    bool end() const;
130
131
    /**
132
     * For debugging purposes
133
     */
134
    void dump() const;
135
};
136
137
NAMESPACE_END
138
139
140
#endif