]> git.stg.codes - stg.git/blobdiff - projects/sgconf/api_action.h
Simplified module interfaces.
[stg.git] / projects / sgconf / api_action.h
diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h
new file mode 100644 (file)
index 0000000..55b3844
--- /dev/null
@@ -0,0 +1,119 @@
+#ifndef __STG_SGCONF_API_ACTION_H__
+#define __STG_SGCONF_API_ACTION_H__
+
+#include "action.h"
+
+#include "options.h"
+
+#include <string>
+#include <map>
+#include <vector>
+
+namespace SGCONF
+{
+
+typedef bool (* API_FUNCTION) (const CONFIG &,
+                               const std::string &,
+                               const std::map<std::string, std::string> &);
+
+class COMMAND
+{
+    public:
+        COMMAND(API_FUNCTION funPtr,
+                const std::string & arg,
+                const std::map<std::string, std::string> & options)
+            : m_funPtr(funPtr),
+              m_arg(arg),
+              m_options(options)
+        {}
+        bool Execute(const SGCONF::CONFIG & config) const
+        {
+            return m_funPtr(config, m_arg, m_options);
+        }
+
+    private:
+        API_FUNCTION m_funPtr;
+        std::string m_arg;
+        std::map<std::string, std::string> m_options;
+};
+
+class COMMANDS
+{
+    public:
+        void Add(API_FUNCTION funPtr,
+                 const std::string & arg,
+                 const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
+        bool Execute(const SGCONF::CONFIG & config) const
+        {
+            std::vector<COMMAND>::const_iterator it(m_commands.begin());
+            bool res = true;
+            while (it != m_commands.end() && res)
+            {
+                res = res && it->Execute(config);
+                ++it;
+            }
+            return res;
+        }
+    private:
+        std::vector<COMMAND> m_commands;
+};
+
+class API_ACTION : public ACTION
+{
+    public:
+        API_ACTION(COMMANDS & commands,
+                   const std::string & paramDescription,
+                   bool needArgument,
+                   const OPTION_BLOCK& suboptions,
+                   API_FUNCTION funPtr)
+            : m_commands(commands),
+              m_description(paramDescription),
+              m_argument(needArgument ? "1" : ""), // Hack
+              m_suboptions(suboptions),
+              m_funPtr(funPtr)
+        {}
+        API_ACTION(COMMANDS & commands,
+                   const std::string & paramDescription,
+                   bool needArgument,
+                   API_FUNCTION funPtr)
+            : m_commands(commands),
+              m_description(paramDescription),
+              m_argument(needArgument ? "1" : ""), // Hack
+              m_funPtr(funPtr)
+        {}
+
+        virtual ACTION * Clone() const { return new API_ACTION(*this); }
+
+        virtual std::string ParamDescription() const { return m_description; }
+        virtual std::string DefaultDescription() const { return ""; }
+        virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
+        virtual PARSER_STATE Parse(int argc, char ** argv);
+
+    private:
+        COMMANDS & m_commands;
+        std::string m_description;
+        std::string m_argument;
+        OPTION_BLOCK m_suboptions;
+        std::map<std::string, std::string> m_params;
+        API_FUNCTION m_funPtr;
+};
+
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+                       const std::string & paramDescription,
+                       bool needArgument,
+                       API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, paramDescription, needArgument, funPtr);
+}
+
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+                       API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, "", false, funPtr);
+}
+
+}
+
+#endif