Merge "add missing packages requires" into tizen
[profile/ivi/ico-uxf-homescreen.git] / lib / misc / state-machine / CicoStateCore.h
1 /*
2  * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Contact: 
5  *
6  * Licensed under the Apache License, Version 2.0 (the License){}
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License. 
17  */
18 #ifndef CICOSTATECORE_H
19 #define CICOSTATECORE_H
20
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 #include "CicoEvent.h"
25
26 #ifndef CICOEVENTINFO_H
27 class CicoEventInfo;
28 #endif
29
30 #ifndef CICOSTATE_H
31 class CicoState;
32 #endif
33
34 #ifndef CICOSTATEACTION_H
35 class CicoStateAction;
36 #endif
37
38 #ifndef CICOSTATEMACHINE_H
39 class CicoStateMachine;
40 #endif
41
42 #ifndef CICOSTAEACTION_H
43 class CicoStateAction;
44 #endif
45
46 #ifndef CICOHISTORYSTATE_H
47 class CicoHistoryState;
48 #endif
49
50
51 /**
52  * It is the core definition of the state transition
53  * @author m.kamoshida
54  * @version 0.1
55  */
56 /**
57  * @brief The CicoStateCore class
58  */
59 class CicoStateCore {
60 public:
61     // Type of state object
62     enum E_TOSO {
63         EStateMachine = 0,              // kind of stateMachine object
64         EState,                         // kind of state object
65         EHistoryState,                  // kind of history state object
66         EFinalState                     // kind of final state 0bject
67     };
68
69     enum E_GetPolicy {                  // get current state policy
70         ELvlTop = 0,                    //  First-degree active state
71         ELvlAll,                        //  ALL active state
72         ELvlBttm                        //  Buttom active state
73     };
74     // Constructior
75                 CicoStateCore(E_TOSO ts, CicoStateCore* parent =0);
76                 CicoStateCore(E_TOSO ts, const std::string& name,
77                               CicoStateCore* parent =0);
78                 CicoStateCore(E_TOSO ts, const std::string& name, int value,
79                               CicoStateCore* parent =0);
80                 CicoStateCore(E_TOSO ts, int value, CicoStateCore* parent =0);
81
82     // destructor
83     virtual     ~CicoStateCore();
84
85     // Identification name get/set
86     const std::string& getName() const;
87     void        setName(const std::string& name);
88
89     // Identification number get/set
90     int         getValue() const;
91     void        setValue(int value);
92
93     // get parent
94     const CicoStateCore* getParent() const;
95
96     // active/inactive get
97     bool        isActive() const;             // active state
98
99     // get object kind
100     E_TOSO      getType() const;
101     bool        isStateMachine() const;
102     bool        isState() const;
103     bool        isFinalState() const;
104     bool        isHistoryState() const;
105
106     bool        isRegisteredInit() const;
107
108     // c-wrapper I/F
109     void        getObjects(std::vector<CicoStateCore*>& obj);
110     const CicoEventInfo* getEventInfo(const std::string& name,
111                                       unsigned short ev) const;
112 protected:
113     /**
114      * union kind type
115      */
116     enum E_SttUnionKind {
117         EPUnused = 0,                   // unused
118         EPDir,                          // pointer direct
119         EPVec                           // pointer vector
120     };
121
122     /**
123      * event infomation define
124      */
125     typedef struct {
126         E_SttUnionKind                  suk;
127         union {
128             CicoEventInfo*               d; // direct pointer
129             std::vector<CicoEventInfo*>* v; // vecter pointers
130         } u;
131     } stEventInfo;
132
133     /**
134      * start position define
135      */
136     typedef struct {
137         E_SttUnionKind                  suk; // union kind
138         union {
139             CicoStateCore*               d; // direct pointer
140             std::vector<CicoStateCore*>* v; // vector pointers
141         } u;
142     } stStartPosition;
143
144     /**
145      * StateAction define
146      */
147     typedef struct {
148         CicoStateAction*                dcsa; // direct pointer
149         int                             dav;  // additional value
150     } stDirSttAction;
151
152     typedef struct {
153         std::vector<CicoStateAction*>*  vcsa; // vector pointers
154         std::vector<int>*               vav;  // vector additional values
155     } stVecSttAction;
156
157     typedef struct {
158         E_SttUnionKind                  suk; // union kind
159         union {
160             stDirSttAction              d;   // direct data
161             stVecSttAction              v;   // vector datas
162         } u;
163     } stSttAction;
164
165     // my area operations
166     void        initTransition();
167     void        initStartPosition();
168     void        initSttAction();
169
170     void        clearTransition();
171     void        clearStartPosition();
172     void        clearSttAction();
173
174
175
176     void        setParent(CicoStateCore* parent);
177     friend void setParentF(CicoStateCore* stt, CicoStateCore* parent);
178
179
180     void        addState(CicoStateCore* state);
181     friend void addStateF(CicoStateCore* parent, CicoStateCore* state);
182
183
184     void        setFinishTransition(CicoStateCore* state);
185
186
187     void        addTransition(const CicoEventInfo& evInf,
188                               CicoStateCore* state);
189
190
191     void        addInitState(CicoStateCore* state);
192
193
194     void        addEntryAction(CicoStateAction* action, int addval);
195
196
197     void        addExitAction(CicoStateAction* action, int addval);
198
199
200     void        addDoAction(CicoStateAction* action, int addval);
201
202
203     void        addAction(stSttAction& ssa, CicoStateAction* action,
204                           int addval);
205
206
207     void        toActivate(const CicoEvent& ev);
208     friend void toActivateF(CicoStateCore* stt, const CicoEvent& ev);
209
210
211     void        toDeactivate(const CicoEvent& ev);
212     friend void toDeactivateF(CicoStateCore* stt, const CicoEvent& ev);
213
214
215     void        onEntry(const CicoEvent& ev);
216     void        onExit(const CicoEvent& ev);
217     void        onDo(const CicoEvent& ev);
218     void        onEntryRun(CicoStateAction* run, const CicoEvent& ev, int val);
219     void        onExitRun(CicoStateAction* run, const CicoEvent& ev, int val);
220     void        onDoRun(CicoStateAction* run, const CicoEvent& ev, int val);
221
222     enum E_ActiveImpact {
223         Single = 0,                     // is Active
224         Follower                        // is Follower active
225     };
226     // get active state-objects
227     bool        getActiveSonar(std::vector<CicoState*>& stt,
228                                std::vector<E_ActiveImpact>& impact);
229     //
230     bool        getActiveSonarChild(std::vector<CicoState*>& stt,
231                                     std::vector<E_ActiveImpact>& impact);
232
233     // 
234     const CicoEventInfo* eventTest(const CicoEvent& ev,
235                                    const CicoStateCore* sm) const;
236     // 
237     friend const CicoEventInfo* eventTestF(const CicoStateCore* stt,
238                                            const CicoEvent& ev,
239                                            const CicoStateCore* sm);
240
241     // start state-object   Activation
242     virtual bool start(const CicoEvent& ev, bool parent = true);
243     friend bool startF(CicoStateCore* s, const CicoEvent& ev, bool parent);
244
245     // stop state-object  Deactivation
246     virtual bool stop(const CicoEvent& ev);
247
248     //
249     virtual void holdHistory();         // Hold History
250     friend void holdHistoryF(CicoStateCore* s);
251
252     // 
253     bool        getCurrentState(std::vector<const CicoState*>& states,
254                                 E_GetPolicy policy);
255     const CicoState* getCurrentState(); //
256     friend bool getCurrentStateF(CicoStateCore* stt,
257                                 std::vector<const CicoState*>& states,
258                                 E_GetPolicy policy);
259
260     void        getRegisteredInit(std::vector<CicoStateCore*>& stt, int& cnt);
261
262     //
263     void        stateEnd(const CicoEvent& ev);
264     friend void stateEndF(CicoStateCore* stt, const CicoEvent& ev);
265
266     // The transition from s to d
267     void        onTransition(const CicoEvent& ev, CicoStateCore* s, CicoStateCore* d);
268
269     //
270     void        getRoots(const CicoStateCore* stt, std::vector<CicoStateCore*>& roots);
271
272     // do action system
273     bool        onDoExec(const CicoEvent& ev);
274     friend bool onDoExecF(CicoStateCore* s, const CicoEvent& ev);
275
276     //
277     const CicoStateCore* getState(const int value) const;
278     const CicoStateCore* getState(const std::string& name) const;
279     friend const CicoStateCore* getStateF(const CicoStateCore* s,
280                                           const int value);
281     friend const CicoStateCore* getStateF(const CicoStateCore* s,
282                                           const std::string& name);
283
284     // log
285     friend void getLogPartF(const CicoStateCore* s, std::string& l);
286
287 protected:
288     bool        m_activeState;
289     std::string m_name;                 // identification name
290     int         m_value;                // identification number
291     E_TOSO      m_eType;                // type my class
292     CicoStateCore* m_parent;            // parent state object
293     std::vector<CicoStateCore*> m_childs; // Child state-objects
294     bool        m_existHistoy;          // flag History holder
295     CicoStateCore* m_stateTermination;  //
296     // transition datas
297     stEventInfo m_eventInfo;
298     // start position
299     stStartPosition m_startPosition;
300     // entry action
301     stSttAction m_entry;
302     // exit action
303     stSttAction m_exit;
304     // do action
305     stSttAction m_do;
306
307 private:
308
309 };
310
311 /**
312  * @brief CicoStateCore::getName
313  * @      get state-object identification nemae
314  * @return state-object identification name
315  */
316 inline const std::string& CicoStateCore::getName() const
317 {
318     return m_name;
319 }
320
321 /**
322  * @brief CicoStateCore::getValue
323  * @     get state-object identification number
324  * @return state-object identification number
325  */
326 inline int CicoStateCore::getValue() const
327 {
328     return m_value;
329 }
330
331 /**
332  * @brief CicoStateCore::setValue
333  * @      set state-object identification number
334  * @paran value identification number
335  */
336 inline void CicoStateCore::setValue(int value)
337 {
338     m_value = value;
339 }
340
341 /**
342  * @brief CicoStateCore::getParent
343  * @return not 0:parent state-object pointer / 0:none parent
344  */
345 inline const CicoStateCore* CicoStateCore::getParent() const
346 {
347     return m_parent;                    // parent state object
348 }
349
350 /**
351  * @brief CicoStateCore::getType
352  * @return type of state-object class
353  */
354 inline CicoStateCore::E_TOSO CicoStateCore::getType() const
355 {
356     return m_eType;
357 }
358
359 /**
360  * @brief CicoStateCore::isActive
361  * get active/notactive status
362  * @return active state true:active / false:not active
363  */
364 inline bool CicoStateCore::isActive() const
365 {
366     return m_activeState;
367 }
368
369 /**
370  * @brief CicoStateCore::isStateMachine
371  * @return true:stateMachine-object
372  * @       false:none stateMachine-object
373  */
374 inline bool CicoStateCore::isStateMachine() const
375 {
376     if (EStateMachine == m_eType) {
377         return true;
378     }
379     return false;
380 }
381
382 /**
383  * @brief CicoStateCore::isState
384  * @return true:state-object
385  * @       false:none state-object
386  */
387 inline bool CicoStateCore::isState() const
388 {
389     if (EState == m_eType) {
390         return true;
391     }
392     return false;
393 }
394
395 /**
396  * @brief CicoStateCore::isFinalState
397  * @return true:finalState-object
398  * @       false:none finaltate-object
399  */
400 inline bool CicoStateCore::isFinalState() const
401 {
402     if (EFinalState == m_eType) {
403         return true;
404     }
405     return false;
406 }
407
408 /**
409  * @brief CicoStateCore::isHistoryState
410  * @return true:historyState-object
411  * @       false:none historystate-object
412  */
413 inline bool CicoStateCore::isHistoryState() const
414 {
415     if (EHistoryState == m_eType) {
416         return true;
417     }
418     return false;
419 }
420 /**
421  * @brief getLogPartF
422  * @param e
423  * @param l
424  */
425 void getLogPartF(const CicoEvent& e, std::string& l);
426
427 /**
428  * @brief getLogPartF
429  * @param e
430  * @param l
431  */
432 void getLogPartF(const CicoEventInfo& e, std::string& l);
433
434 #endif // CICOSTATECORE_H