source: trunk/FACT++/src/LocalControl.h @ 10870

Last change on this file since 10870 was 10870, checked in by tbretz, 8 years ago
Remove the necessity for a source file (of just three lines)
File size: 4.1 KB
Line 
1#ifndef FACT_LocalControl
2#define FACT_LocalControl
3
4#include <ostream>
5
6class StateMachineImp;
7
8// **************************************************************************
9/** @class LocalControl
10
11@brief Implements a local control for a StateMachine based on a Readline class
12
13This template implements all functions which overwrite any function from the
14Readline class needed for a local control of a state machien. Since
15several derivatives of the Readline class implement different kind of
16Readline access, this class can be derived by any of them due to its
17template argument. However, the normal case will be deriving it from
18either Console or Shell.
19
20@tparam T
21   The base class for RemoteControl. Either Readlien or a class
22   deriving from it. This is usually either Console or Shell.
23
24**/
25// **************************************************************************
26#include <boost/filesystem.hpp>
27
28#include "WindowLog.h"
29#include "StateMachineImp.h"
30
31using namespace std;
32
33template <class T>
34class LocalControl : public T
35{
36private:
37    char **Completion(const char *text, int pos, int)
38    {
39        return pos>0 ? 0 : T::Complete(fStateMachine->GetEventNames(), text);
40    }
41
42protected:
43    StateMachineImp *fStateMachine;
44
45    std::ostream &lout;
46
47    std::string fName;
48
49    LocalControl(const char *name) : T(name), 
50        fStateMachine(0), lout(T::GetStreamIn()),
51        fName(boost::filesystem::path(name).filename().c_str())
52    { }
53
54    bool PrintGeneralHelp()
55    {
56        T::PrintGeneralHelp();
57        lout << " " << kUnderline << "Specific commands:" << endl;
58        lout << kBold << "   st,states    " << kReset << "Display a list of the available states with description." << endl;
59        lout << endl;
60        return true;
61    }
62    bool PrintCommands()
63    {
64        lout << endl << kBold << "List of commands:" << endl;
65        fStateMachine->PrintListOfEvents(lout);
66        lout << endl;
67
68        return true;
69    }
70
71    bool Process(const std::string &str)
72    {
73        if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
74        {
75            lout << endl;
76            fStateMachine->PrintListOfEvents(lout, str.substr(str.find_first_of(' ')+1));
77            lout << endl;
78
79            return true;
80        }
81        if (str=="states" || str=="st")
82        {
83            if (fStateMachine)
84                fStateMachine->PrintListOfStates(lout);
85            return true;
86        }
87
88        if (T::Process(str))
89            return true;
90
91        return !fStateMachine->PostEvent(lout, str);
92    }
93
94public:
95
96    void SetReceiver(StateMachineImp &imp) { fStateMachine = &imp; }
97};
98
99// **************************************************************************
100/** @class LocalConsole
101
102@brief Derives the LocalControl from Control and adds prompt
103
104This is basically a LocalControl, which derives through the template
105argument from the Console class. It enhances the functionality of
106the local control with a proper updated prompt.
107
108 */
109// **************************************************************************
110#include "Console.h"
111#include "tools.h"
112
113class LocalConsole : public LocalControl<Console>
114{
115public:
116    LocalConsole(const char *name, bool continous=false)
117        : LocalControl<Console>(name)
118    {
119        SetContinous(continous);
120    }
121
122    string GetUpdatePrompt() const
123    {
124        return GetLinePrompt()+" "
125            "\033[34m\033[1m"+fName+"\033[0m:"
126            "\033[32m\033[1m"+fStateMachine->GetStateName()+"\033[0m> ";
127    }
128};
129
130// **************************************************************************
131/** @class LocalShell
132
133@brief Derives the LocalControl from Shell and adds a colored prompt
134
135This is basically a LocalControl, which derives through the template
136argument from the Shell class. It enhances the functionality of
137the local control with a proper updated prompt.
138
139 */
140// **************************************************************************
141#include "Shell.h"
142
143class LocalShell : public LocalControl<Shell>
144{
145public:
146    LocalShell(const char *name, bool = false)
147        : LocalControl<Shell>(name) { }
148
149    string GetUpdatePrompt() const
150    {
151        return GetLinePrompt()+' '+fName+':'+fStateMachine->GetStateName()+"> ";
152    }
153};
154
155#endif
Note: See TracBrowser for help on using the repository browser.