source: branches/FACT++_lidctrl_new_eth/src/State.cc@ 19128

Last change on this file since 19128 was 16282, checked in by tbretz, 12 years ago
Replaced push_back by emplac_back were reasonable.
File size: 2.2 KB
Line 
1// **************************************************************************
2/** @struct State
3
4@brief A struct which stores an index, a comment and a name of a State
5
6To have proper descriptions of states in the network, this struct provides
7a simple storage for the properties of a state.
8
9Assume you want to write a descriptive string for a state machine
10with two states, it could look like this:
11
12"1:Disconnected=Connection not established\n2:Connected=Connection established."
13
14Such a string can then be converted with SplitStates into a vector
15of State objects.
16
17*/
18// **************************************************************************
19#include "State.h"
20
21#include <sstream>
22#include <algorithm>
23
24#include "tools.h"
25
26using namespace std;
27using namespace Tools;
28
29// --------------------------------------------------------------------------
30//
31//! Construct a Description object
32//!
33//! @param i
34//! Index of the state, e.g. 1
35//!
36//! @param n
37//! Name of the state, e.g. 'Connected'
38//!
39//! @param c
40//! Descriptive text of the state, e.g. "Connection to hardware established."
41//
42State::State(int i, const std::string &n, const std::string &c, const Time &t)
43 : index(i), name(Trim(n)), comment(Trim(c)), time(t)
44{
45}
46
47// --------------------------------------------------------------------------
48//
49//! This function breaks down a descriptive string into its components.
50//! For details see class reference.
51//!
52//! @param buffer
53//! string which should be broekn into pieces
54//!
55//! @returns
56//! A vector<State> containing all the states found.
57//
58vector<State> State::SplitStates(const string &buffer)
59{
60 vector<State> vec;
61
62 string buf;
63 stringstream stream(buffer);
64 while (getline(stream, buf, '\n'))
65 {
66 if (buf.empty())
67 continue;
68
69 const size_t p1 = buf.find_first_of(':');
70 const size_t p2 = buf.find_first_of('=');
71
72 stringstream s(buf.substr(0, p1));
73
74 int index;
75 s >> index;
76
77 const string name = buf.substr(p1+1, p2-p1-1);
78 const string comment = p2==string::npos ? "" : buf.substr(p2+1);
79
80 vec.emplace_back(index, name, comment);
81 }
82
83 sort(vec.begin(), vec.end(), State::Compare);
84
85 return vec;
86}
Note: See TracBrowser for help on using the repository browser.