]> ruin.nu Git - popboot.git/blob - action.h
Spelling fix in inittab
[popboot.git] / action.h
1 #ifndef __ACTION_H__
2 #define __ACTION_H__
3
4 #include <vector>
5 #include <string>
6 #include <ext/hash_map>
7 #include <locale>
8
9 //! A list of strings, each string representing an effect.
10 typedef std::vector<std::string> Literals;
11 //! A map, but really a list of pairs, the precondition as a string, and a bool, true if it's a hard precondition, otherwise false.
12 typedef __gnu_cxx::hash_map<std::string,bool> Preconditions;
13 //! A map from returnvalue of the execution to the effects achieved.
14 typedef __gnu_cxx::hash_map<int,Literals> EffectsMap;
15
16 // These are needed to be able to use std::string as key in a hash_map.
17 namespace __gnu_cxx {
18         template< typename CharT, typename Traits, typename Alloc >
19                 struct hash< std::basic_string<CharT, Traits, Alloc> > {
20                         size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
21
22                                 const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
23
24                                 return c.hash(s.c_str(), s.c_str() + s.size());
25
26                         }
27
28                 };
29
30         template< typename CharT, typename Traits, typename Alloc >
31                 struct hash< const std::basic_string<CharT, Traits, Alloc> > { //yes you need this version aswell!
32
33                         size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
34
35                                 const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
36
37                                 return c.hash(s.c_str(), s.c_str() + s.size());
38                         }
39
40                 };
41 };
42
43 /**
44  * This class holds the information associated with an action.
45  * This is the name, the preconditions, the executable and the effects.
46  */
47 class Action {
48         public:
49                 /**
50                  * Creates an action.
51                  * @param name The name given to this action.
52                  * @param preconditions The preconditions needed before execution of this action.
53                  * @param executable The executable which will be executed.
54                  * @param effects The effects which will be achieved after execution.
55                  */
56                 Action(std::string name, const Preconditions& preconditions, std::string executable, const EffectsMap& effects);
57
58                 /**
59                  * Creates a copy of the given action.
60                  */
61                 Action(const Action& action);
62                 /**
63                  * Creates an empty, non-functional, action.
64                  */
65                 Action(){};
66
67                 /**
68                  * Returns the effects which will be achived if the action returns
69                  * the input value.
70                  */
71                 const Literals& effects(int value) const;
72
73                 /**
74                  * Returns the preconditions needed before execution of this action.
75                  */
76                 const Preconditions& preconditions() const;
77                 /**
78                  * Returns the name of this action.
79                  */
80                 const std::string& name() const;
81                 /**
82                  * Executes this action.
83                  * @return the returnvalue of the execution. Can be used to find out the effects achieved.
84                  */
85                 int execute() const;
86
87         protected:
88                 //! The name of the action.
89                 std::string _name;
90                 //! The executable which can be executed.
91                 std::string _executable;
92                 //! The preconditions needed before execution.
93                 Preconditions _preconditions;
94                 //! The effects which can be achieved.
95                 EffectsMap _effects;
96                 //! An empty list of effects which will be returned if the returnvalue was not found in _effects.
97                 static const Literals _empty;
98 };
99
100
101 #endif