]> ruin.nu Git - popboot.git/blobdiff - action.h
Spelling fix in inittab
[popboot.git] / action.h
index 9b3c9276a105ab8366324c63839ffde92d34d70f..40e2db3137e4481e7adcad2be73f3ec75ed8a0f2 100644 (file)
--- a/action.h
+++ b/action.h
@@ -3,23 +3,99 @@
 
 #include <vector>
 #include <string>
+#include <ext/hash_map>
+#include <locale>
 
-typedef std::vector<std::string> literals;
+//! A list of strings, each string representing an effect.
+typedef std::vector<std::string> Literals;
+//! 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.
+typedef __gnu_cxx::hash_map<std::string,bool> Preconditions;
+//! A map from returnvalue of the execution to the effects achieved.
+typedef __gnu_cxx::hash_map<int,Literals> EffectsMap;
 
+// These are needed to be able to use std::string as key in a hash_map.
+namespace __gnu_cxx {
+       template< typename CharT, typename Traits, typename Alloc >
+               struct hash< std::basic_string<CharT, Traits, Alloc> > {
+                       size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+                               const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
+
+                               return c.hash(s.c_str(), s.c_str() + s.size());
+
+                       }
+
+               };
+
+       template< typename CharT, typename Traits, typename Alloc >
+               struct hash< const std::basic_string<CharT, Traits, Alloc> > { //yes you need this version aswell!
+
+                       size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+                               const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
+
+                               return c.hash(s.c_str(), s.c_str() + s.size());
+                       }
+
+               };
+};
+
+/**
+ * This class holds the information associated with an action.
+ * This is the name, the preconditions, the executable and the effects.
+ */
 class Action {
        public:
-               Action(std::string executable, literals preconditions, literals effects);
+               /**
+                * Creates an action.
+                * @param name The name given to this action.
+                * @param preconditions The preconditions needed before execution of this action.
+                * @param executable The executable which will be executed.
+                * @param effects The effects which will be achieved after execution.
+                */
+               Action(std::string name, const Preconditions& preconditions, std::string executable, const EffectsMap& effects);
+
+               /**
+                * Creates a copy of the given action.
+                */
                Action(const Action& action);
+               /**
+                * Creates an empty, non-functional, action.
+                */
                Action(){};
-               const literals& effects() const;
-               const literals& preconditions() const;
-               const std::string& executable() const;
+
+               /**
+                * Returns the effects which will be achived if the action returns
+                * the input value.
+                */
+               const Literals& effects(int value) const;
+
+               /**
+                * Returns the preconditions needed before execution of this action.
+                */
+               const Preconditions& preconditions() const;
+               /**
+                * Returns the name of this action.
+                */
+               const std::string& name() const;
+               /**
+                * Executes this action.
+                * @return the returnvalue of the execution. Can be used to find out the effects achieved.
+                */
                int execute() const;
 
        protected:
+               //! The name of the action.
+               std::string _name;
+               //! The executable which can be executed.
                std::string _executable;
-               literals _preconditions;
-               literals _effects;
+               //! The preconditions needed before execution.
+               Preconditions _preconditions;
+               //! The effects which can be achieved.
+               EffectsMap _effects;
+               //! An empty list of effects which will be returned if the returnvalue was not found in _effects.
+               static const Literals _empty;
 };
 
+
 #endif