aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/apps.h
blob: d56fab5ad41c5a2ec1eba2dada7e0036c1f82970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <iostream>
#include <functional>
#include <string>
#include <set>
#include <botan/build.h>
#include <botan/hex.h>
#include <botan/auto_rng.h>
#include "getopt.h"

using namespace Botan;

typedef std::function<int (int, char*[])> main_fn;

class AppRegistrations
   {
   public:
      void add(const std::string& name, main_fn fn)
         {
         m_cmds[name] = fn;
         }

      bool has(const std::string& cmd) const
         {
         return m_cmds.count(cmd) > 0;
         }

      std::set<std::string> all_apps() const
         {
         std::set<std::string> apps;
         for(auto i : m_cmds)
            apps.insert(i.first);
         return apps;
         }

      int run(const std::string& cmd, int argc, char* argv[]) const
         {
         auto i = m_cmds.find(cmd);
         if(i != m_cmds.end())
            return i->second(argc, argv);
         return -1;
         }

      static AppRegistrations& instance()
         {
         static AppRegistrations s_apps;
         return s_apps;
         }

      class AppRegistration
         {
         public:
            AppRegistration(const std::string& name, main_fn fn)
               {
               AppRegistrations::instance().add(name, fn);
               }
         };

   private:
      AppRegistrations() {}

      std::map<std::string, main_fn> m_cmds;
   };

#define REGISTER_APP(nm) AppRegistrations::AppRegistration g_ ## nm ## _registration(#nm, nm)

#if defined(BOTAN_TARGET_OS_IS_WINDOWS) || defined(BOTAN_TARGET_OS_IS_MINGW)
  #undef BOTAN_TARGET_OS_HAS_SOCKETS
#else
  #define BOTAN_TARGET_OS_HAS_SOCKETS
#endif