BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
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 
28 #include <boost/program_options.hpp>
29 #include <fc/io/json.hpp>
30 
31 namespace graphene { namespace app {
32 
34 {
35  public:
36  explicit abstract_plugin(application& a) : _app(a) {}
37  virtual ~abstract_plugin() = default;
38 
40  virtual std::string plugin_name()const = 0;
41 
43  virtual std::string plugin_description()const = 0;
44 
46  application& app()const { return _app; }
47 
60  virtual void plugin_initialize( const boost::program_options::variables_map& options ) = 0;
61 
68  virtual void plugin_startup() = 0;
69 
75  virtual void plugin_shutdown() = 0;
76 
88  virtual void plugin_set_program_options(
89  boost::program_options::options_description& command_line_options,
90  boost::program_options::options_description& config_file_options
91  ) = 0;
92  protected:
94 };
95 
100 class plugin : public abstract_plugin
101 {
102  public:
104 
105  std::string plugin_name()const override;
106  std::string plugin_description()const override;
107  void plugin_initialize( const boost::program_options::variables_map& options ) override;
108  void plugin_startup() override;
109  void plugin_shutdown() override;
111  boost::program_options::options_description& command_line_options,
112  boost::program_options::options_description& config_file_options
113  ) override;
114 
116  protected:
117  net::node_ptr p2p_node() const { return app().p2p_node(); }
118 };
119 
122 template<typename T>
123 T dejsonify(const string& s, uint32_t max_depth)
124 {
125  return fc::json::from_string(s).as<T>(max_depth);
126 }
127 
128 namespace impl {
129  template<typename T>
130  T dejsonify( const string& s )
131  {
132  return graphene::app::dejsonify<T>( s, GRAPHENE_MAX_NESTED_OBJECTS );
133  }
134 }
135 
136 #define DEFAULT_VALUE_VECTOR(value) default_value({fc::json::to_string(value)}, fc::json::to_string(value))
137 #define LOAD_VALUE_SET(options, name, container, type) \
138 do { \
139  if( options.count(name) > 0 ) { \
140  const std::vector<std::string>& ops = options[name].as<std::vector<std::string>>(); \
141  std::transform(ops.begin(), ops.end(), std::inserter(container, container.end()), \
142  &graphene::app::impl::dejsonify<type>); \
143  } \
144 } while (false)
145 
147 } } //graphene::app
graphene::app::abstract_plugin::plugin_initialize
virtual void plugin_initialize(const boost::program_options::variables_map &options)=0
Perform early startup routines and register plugin indexes, callbacks, etc.
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::app::plugin::database
chain::database & database()
Definition: plugin.hpp:115
graphene::app::impl::dejsonify
T dejsonify(const string &s)
Definition: plugin.hpp:130
graphene::app::abstract_plugin::_app
application & _app
Definition: plugin.hpp:93
fc::json::from_string
static variant from_string(const string &utf8_str, parse_type ptype=legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:458
graphene::net::node_ptr
std::shared_ptr< node > node_ptr
Definition: node.hpp:326
graphene::app::abstract_plugin
Definition: plugin.hpp:33
graphene::app::application::chain_database
std::shared_ptr< chain::database > chain_database() const
Definition: application.cpp:1404
graphene::app::plugin::plugin_description
std::string plugin_description() const override
Get the description of the plugin.
Definition: plugin.cpp:35
graphene::app::application
Definition: application.hpp:91
graphene::app::abstract_plugin::plugin_startup
virtual void plugin_startup()=0
Begin normal runtime operations.
graphene::app::plugin
Definition: plugin.hpp:100
graphene::app::abstract_plugin::plugin_name
virtual std::string plugin_name() const =0
Get the name of the plugin.
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
graphene::app::plugin::plugin_shutdown
void plugin_shutdown() override
Cleanly shut down the plugin.
Definition: plugin.cpp:50
graphene::app::plugin::plugin_initialize
void plugin_initialize(const boost::program_options::variables_map &options) override
Perform early startup routines and register plugin indexes, callbacks, etc.
Definition: plugin.cpp:40
graphene::app::abstract_plugin::abstract_plugin
abstract_plugin(application &a)
Definition: plugin.hpp:36
json.hpp
application.hpp
graphene::app::plugin::plugin_startup
void plugin_startup() override
Begin normal runtime operations.
Definition: plugin.cpp:45
graphene::app::plugin::plugin_name
std::string plugin_name() const override
Get the name of the plugin.
Definition: plugin.cpp:30
graphene::app::abstract_plugin::plugin_set_program_options
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.
GRAPHENE_MAX_NESTED_OBJECTS
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:33
graphene::app::dejsonify
T dejsonify(const string &s, uint32_t max_depth)
Definition: plugin.hpp:123
graphene::app::plugin::p2p_node
net::node_ptr p2p_node() const
Definition: plugin.hpp:117
graphene::app::abstract_plugin::app
application & app() const
Get a reference of the application bound to the plugin.
Definition: plugin.hpp:46
graphene::app::abstract_plugin::plugin_shutdown
virtual void plugin_shutdown()=0
Cleanly shut down the plugin.
graphene::app::plugin::plugin_set_program_options
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:55
graphene::app::abstract_plugin::plugin_description
virtual std::string plugin_description() const =0
Get the description of the plugin.
graphene
Definition: api.cpp:48
graphene::app::abstract_plugin::~abstract_plugin
virtual ~abstract_plugin()=default
graphene::app::application::p2p_node
net::node_ptr p2p_node()
Definition: application.cpp:1399