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