OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
Module.hpp
Go to the documentation of this file.
1 
2 #ifndef FOCS_MODULE
3 #define FOCS_MODULE
4 
5 #include "DataProvider.hpp"
6 #include "FileReader.hpp"
7 #include "FileWriter.hpp"
8 #include "Log.hpp"
9 
10 #include <boost/filesystem.hpp>
11 
12 #include <iostream>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 
18 // This is everything needed to create a new module
19 
20 namespace focs {
33  static const std::string module_allocator_function = "focs_module_allocator";
44  static const std::string module_deleter_function = "focs_module_deleter";
45 
46  class ModuleManager;
50  ModuleConfiguration(ModuleManager& module_manager_, Log& log_) : module_manager{module_manager_}, log{log_} {}
54  Log& log;
55  };
56 
57  // TODO: Should this class get moved into its own header, like FileReader?
58  // If helper classes/functions are needed, it will be. (This will likely
59  // happen, as it would behoove me to tie together KvStore and
60  // getopt/boost::program_options/etc, or otherwise switch to JSON for
61  // configs with extra processing for including other files, etc, like
62  // KvStore has. Must ask someone smarter.)
63 
65  class Command {
66  public:
68  virtual ~Command() = default;
70  virtual const std::vector<std::string>& respond_to() const = 0;
72  virtual int call_command(int argc, const char* argv[]) const = 0;
74  virtual const std::string* brief_summary() const {return nullptr;}
76  virtual const std::string* group() const {return nullptr;}
77  };
78 
80  class Module {
81  public:
83  virtual ~Module() = default;
84 
86  virtual const std::string& name() const = 0;
88  virtual const std::vector<std::unique_ptr<Command>>& commands() const {return no_commands_;}
90  virtual const std::vector<std::unique_ptr<DataProvider>>& data_providers() const {return no_data_providers_;}
92  virtual const std::vector<std::unique_ptr<FileReader>>& file_readers() const {return no_file_readers_;}
94  virtual const std::vector<std::unique_ptr<FileWriter>>& file_writers() const {return no_file_writers_;}
95  private:
97  std::vector<std::unique_ptr<Command>> no_commands_{};
99  std::vector<std::unique_ptr<DataProvider>> no_data_providers_{};
101  std::vector<std::unique_ptr<FileReader>> no_file_readers_{};
103  std::vector<std::unique_ptr<FileWriter>> no_file_writers_{};
104  };
105 }
106 
107 // Nothing below here is for user consumption.
108 
109 namespace focs {
110  class ModuleLoader {
111  public:
112  static bool is_module(const boost::filesystem::path& path);
113 
114  ModuleLoader(const boost::filesystem::path& path, const ModuleConfiguration& configuration);
115  ~ModuleLoader();
116 
117  void load_module();
118  std::shared_ptr<Module> get_module() const;
119  void unload_module();
120  bool is_loaded() const;
121  const boost::filesystem::path& path() const;
122 
123  private: // these need to accommodate every implementation or this needs to be split out into a base class again
125  const ModuleConfiguration& configuration_;
126  void* handle_ { nullptr };
127  std::shared_ptr<Module> module_;
128  };
129 }
130 
131 #endif // FOCS_MODULE
132 
virtual ~Module()=default
ModuleManager & module_manager
Definition: Module.hpp:52
std::shared_ptr< Module > get_module() const
ModuleConfiguration(ModuleManager &module_manager_, Log &log_)
Definition: Module.hpp:50
virtual const std::vector< std::unique_ptr< Command > > & commands() const
Definition: Module.hpp:88
@ string
bool is_loaded() const
ModuleLoader(const boost::filesystem::path &path, const ModuleConfiguration &configuration)
virtual ~Command()=default
static bool is_module(const boost::filesystem::path &path)
const boost::filesystem::path & path() const
string path
Definition: color_dtdb.py:221
virtual const std::vector< std::string > & respond_to() const =0
virtual const std::string * brief_summary() const
Definition: Module.hpp:74
virtual const std::vector< std::unique_ptr< DataProvider > > & data_providers() const
Definition: Module.hpp:90
virtual const std::vector< std::unique_ptr< FileReader > > & file_readers() const
Definition: Module.hpp:92
virtual const std::string & name() const =0
virtual const std::string * group() const
Definition: Module.hpp:76
Multiple output, versatile logger.
Definition: Log.hpp:42
virtual int call_command(int argc, const char *argv[]) const =0
virtual const std::vector< std::unique_ptr< FileWriter > > & file_writers() const
Definition: Module.hpp:94