Scorum
plugin.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
27 
30 
31 #include <boost/program_options.hpp>
32 
33 #include <fc/io/json.hpp>
34 #include <fc/static_variant.hpp>
35 #include <fc/unique_ptr.hpp>
36 #include <fc/vector.hpp>
37 
38 #include <memory>
39 
40 namespace scorum {
41 namespace app {
42 
43 using fc::static_variant;
44 using fc::unique_ptr;
45 
47 {
48 public:
49  virtual ~abstract_plugin()
50  {
51  }
52  virtual std::string plugin_name() const = 0;
53 
66  virtual void plugin_initialize(const boost::program_options::variables_map& options) = 0;
67 
74  virtual void plugin_startup() = 0;
75 
81  virtual void plugin_shutdown() = 0;
82 
94  virtual void plugin_set_program_options(boost::program_options::options_description& command_line_options,
95  boost::program_options::options_description& config_file_options)
96  = 0;
97 };
98 
102 class plugin : public abstract_plugin
103 {
104 public:
106  virtual ~plugin() override;
107 
108  virtual std::string plugin_name() const override;
109  virtual void plugin_initialize(const boost::program_options::variables_map& options) override;
110  virtual void plugin_startup() override;
111  virtual void plugin_shutdown() override;
112  virtual void plugin_set_program_options(boost::program_options::options_description& command_line_options,
113  boost::program_options::options_description& config_file_options) override;
114 
116  {
117  return *app().chain_database();
118  }
119  application& app() const
120  {
121  assert(_app);
122  return *_app;
123  }
124 
125 protected:
126  void print_greeting();
127 
128  graphene::net::node& p2p_node()
129  {
130  return *app().p2p_node();
131  }
132 
133 private:
134  application* _app = nullptr;
135 };
136 
139 template <typename T> T dejsonify(const std::string& s)
140 {
141  return fc::json::from_string(s).as<T>();
142 }
143 
144 #define DEFAULT_VALUE_VECTOR(value) default_value({ fc::json::to_string(value) }, fc::json::to_string(value))
145 #define LOAD_VALUE_SET(options, name, container, type) \
146  if (options.count(name)) \
147  { \
148  const std::vector<std::string>& ops = options[name].as<std::vector<std::string>>(); \
149  std::transform(ops.begin(), ops.end(), std::inserter(container, container.end()), \
150  &scorum::app::dejsonify<type>); \
151  }
153 }
154 } // scorum::app
155 
156 #define SCORUM_DEFINE_PLUGIN(plugin_name, plugin_class) \
157  namespace scorum { \
158  namespace plugin { \
159  std::shared_ptr<scorum::app::abstract_plugin> create_##plugin_name##_plugin(app::application* app) \
160  { \
161  return std::make_shared<plugin_class>(app); \
162  } \
163  } \
164  }
165 
166 #define DEFINE_PLUGIN_EVALUATOR(PLUGIN, OPERATION, X) \
167  class X##_evaluator : public scorum::chain::evaluator_impl<scorum::chain::database, X##_evaluator, OPERATION> \
168  { \
169  public: \
170  typedef X##_operation operation_type; \
171  \
172  X##_evaluator(database& db, PLUGIN* plugin) \
173  : scorum::chain::evaluator_impl<scorum::chain::database, X##_evaluator, OPERATION>(db) \
174  , _plugin(plugin) \
175  { \
176  } \
177  \
178  void do_apply(const X##_operation& o); \
179  \
180  PLUGIN* _plugin; \
181  };
virtual void plugin_startup()=0
Begin normal runtime operations.
virtual void plugin_initialize(const boost::program_options::variables_map &options)=0
Perform early startup routines and register plugin indexes, callbacks, etc.
virtual void plugin_set_program_options(boost::program_options::options_description &command_line_options, boost::program_options::options_description &config_file_options)=0
Fill in command line parameters used by the plugin.
virtual void plugin_shutdown()=0
Cleanly shut down the plugin.
virtual std::string plugin_name() const =0
std::shared_ptr< chain::database > chain_database() const
graphene::net::node_ptr p2p_node()
application & app() const
Definition: plugin.hpp:119
virtual void plugin_initialize(const boost::program_options::variables_map &options) override
Perform early startup routines and register plugin indexes, callbacks, etc.
Definition: plugin.cpp:48
virtual void plugin_startup() override
Begin normal runtime operations.
Definition: plugin.cpp:53
plugin(application *app)
Definition: plugin.cpp:32
void print_greeting()
Definition: plugin.cpp:69
virtual ~plugin() override
Definition: plugin.cpp:38
chain::database & database()
Definition: plugin.hpp:115
virtual std::string plugin_name() const override
Definition: plugin.cpp:43
virtual void plugin_shutdown() override
Cleanly shut down the plugin.
Definition: plugin.cpp:58
virtual void plugin_set_program_options(boost::program_options::options_description &command_line_options, boost::program_options::options_description &config_file_options) override
Fill in command line parameters used by the plugin.
Definition: plugin.cpp:63
graphene::net::node & p2p_node()
Definition: plugin.hpp:128
tracks the blockchain state in an extensible manner
Definition: database.hpp:52
T dejsonify(const std::string &s)
Definition: plugin.hpp:139
Definition: asset.cpp:15