BitShares-Core  6.0.1
BitShares blockchain implementation and command-line interface 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 if( options.count(name) > 0 ) { \
139  const std::vector<std::string>& ops = options[name].as<std::vector<std::string>>(); \
140  std::transform(ops.begin(), ops.end(), std::inserter(container, container.end()), &graphene::app::impl::dejsonify<type>); \
141 }
142 
144 } } //graphene::app
virtual ~abstract_plugin()=default
T dejsonify(const string &s, uint32_t max_depth)
Definition: plugin.hpp:123
std::shared_ptr< chain::database > chain_database() const
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:33
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
std::shared_ptr< node > node_ptr
Definition: node.hpp:319
virtual std::string plugin_name() const =0
Get the name of the plugin.
net::node_ptr p2p_node() const
Definition: plugin.hpp:117
chain::database & database()
Definition: plugin.hpp:115
T as(uint32_t max_depth) const
Definition: variant.hpp:337
virtual void plugin_startup()=0
Begin normal runtime operations.
virtual void plugin_shutdown()=0
Cleanly shut down the plugin.
abstract_plugin(application &a)
Definition: plugin.hpp:36
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
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.
application & app() const
Get a reference of the application bound to the plugin.
Definition: plugin.hpp:46
virtual void plugin_initialize(const boost::program_options::variables_map &options)=0
Perform early startup routines and register plugin indexes, callbacks, etc.
virtual std::string plugin_description() const =0
Get the description of the plugin.