|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef STATEMACHINE_H_ |
|
19 #define STATEMACHINE_H_ |
|
20 |
|
21 #include <stdint.h> |
|
22 |
|
23 namespace analysis { |
|
24 |
|
25 typedef uint8_t byte_t; |
|
26 |
|
27 /** |
|
28 * Points to the beginning of the state in the memory |
|
29 */ |
|
30 typedef byte_t* StatePointer; |
|
31 |
|
32 /** |
|
33 * Points to the begining of the state as calculated |
|
34 * from the beginning of the state machine blob. Can be |
|
35 * turned into a StatePointer by combining it with the |
|
36 * state machine blob pointer. |
|
37 */ |
|
38 typedef unsigned int StateOffset; |
|
39 |
|
40 /** |
|
41 * Can be used for navigating throught the state machine |
|
42 * staets. In a sense 'a pointer' to some state. Can be used |
|
43 * to read information of the state or to jump to another location |
|
44 * Must be reset before usage. |
|
45 * |
|
46 * @tparam Encoding describes encoding of the state machine binary |
|
47 */ |
|
48 template <class Encoding> |
|
49 class StateCursor { |
|
50 |
|
51 public: |
|
52 /** Constructs the cursor. */ |
|
53 StateCursor(); |
|
54 |
|
55 /** |
|
56 * Resets the state cursor to point to a new location. After |
|
57 * first reset other methods can be used. |
|
58 * @param blob State machine binary. Ownership NOT transferred |
|
59 * @param offset Points to a state within the state machine binary |
|
60 */ |
|
61 void reset(byte_t* blob, StateOffset offset); |
|
62 |
|
63 /** |
|
64 * Describes, if this state is a terminal state. |
|
65 * @note Must not be used before reset() is once called |
|
66 */ |
|
67 bool isFinal(); |
|
68 |
|
69 /** |
|
70 * Moves to next state based on the input. Returns false, |
|
71 * if no transition for input was found. |
|
72 * @note Must not be used before reset() is once called |
|
73 * @param c An input character. |
|
74 * @returns True, if state did change, false otherwise |
|
75 */ |
|
76 bool next(wchar_t c); |
|
77 |
|
78 private: |
|
79 /** State machine binary. Does NOT hold ownership */ |
|
80 byte_t* blob_; |
|
81 |
|
82 /** Raw memory pointer to the state. State is within state machine binary */ |
|
83 StatePointer pointer_; |
|
84 }; |
|
85 |
|
86 /** |
|
87 * Represents the state machine. Mainly wraps the state machine binary. |
|
88 * Can be used to reset StateCursors |
|
89 * |
|
90 * @tparam Encoding describes the encoding of the state machine binary |
|
91 */ |
|
92 template<class Encoding> |
|
93 class StateMachine { |
|
94 public: |
|
95 |
|
96 static const StateOffset ROOT_STATE_OFFSET; |
|
97 |
|
98 public: |
|
99 |
|
100 /** |
|
101 * Constructs state. State machine is not functional, before |
|
102 * reset is called |
|
103 */ |
|
104 StateMachine(); |
|
105 |
|
106 /** |
|
107 * Loads the state machine with the state machine binary. |
|
108 * @param blob The state machine binary. Ownership is NOT transferred. |
|
109 */ |
|
110 void reset(byte_t* blob); |
|
111 |
|
112 /** |
|
113 * Resets the cursor to point out to this state machine's |
|
114 * root state. |
|
115 * @param cursor this cursor is set to point to the state machine's root state. |
|
116 * @note Must not be called before StateMachine is properly reset |
|
117 */ |
|
118 void rootState(StateCursor<Encoding>& cursor); |
|
119 |
|
120 private: |
|
121 |
|
122 /** State machine binary. Does NOT hold ownership */ |
|
123 byte_t* blob_; |
|
124 }; |
|
125 } |
|
126 |
|
127 #include "statemachine.inl" |
|
128 |
|
129 #endif /* STATEMACHINE_H_ */ |