BitShares-Core  4.0.0
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  virtual ~abstract_plugin(){}
37  virtual std::string plugin_name()const = 0;
38  virtual std::string plugin_description()const = 0;
39 
52  virtual void plugin_initialize( const boost::program_options::variables_map& options ) = 0;
53 
60  virtual void plugin_startup() = 0;
61 
67  virtual void plugin_shutdown() = 0;
68 
74  virtual void plugin_set_app( application* a ) = 0;
75 
87  virtual void plugin_set_program_options(
88  boost::program_options::options_description& command_line_options,
89  boost::program_options::options_description& config_file_options
90  ) = 0;
91 };
92 
97 class plugin : public abstract_plugin
98 {
99  public:
100  plugin();
101  virtual ~plugin() override;
102 
103  virtual std::string plugin_name()const override;
104  virtual std::string plugin_description()const override;
105  virtual void plugin_initialize( const boost::program_options::variables_map& options ) override;
106  virtual void plugin_startup() override;
107  virtual void plugin_shutdown() override;
108  virtual void plugin_set_app( application* app ) override;
109  virtual void plugin_set_program_options(
110  boost::program_options::options_description& command_line_options,
111  boost::program_options::options_description& config_file_options
112  ) override;
113 
114  chain::database& database() { return *app().chain_database(); }
115  application& app()const { assert(_app); return *_app; }
116  protected:
117  net::node& p2p_node() { return *app().p2p_node(); }
118 
119  private:
120  application* _app = nullptr;
121 };
122 
125 template<typename T>
126 T dejsonify(const string& s, uint32_t max_depth)
127 {
128  return fc::json::from_string(s).as<T>(max_depth);
129 }
130 
131 namespace impl {
132  template<typename T>
133  T dejsonify( const string& s )
134  {
135  return graphene::app::dejsonify<T>( s, GRAPHENE_MAX_NESTED_OBJECTS );
136  }
137 }
138 
139 #define DEFAULT_VALUE_VECTOR(value) default_value({fc::json::to_string(value)}, fc::json::to_string(value))
140 #define LOAD_VALUE_SET(options, name, container, type) \
141 if( options.count(name) ) { \
142  const std::vector<std::string>& ops = options[name].as<std::vector<std::string>>(); \
143  std::transform(ops.begin(), ops.end(), std::inserter(container, container.end()), &graphene::app::impl::dejsonify<type>); \
144 }
145 
147 } } //graphene::app
T dejsonify(const string &s, uint32_t max_depth)
Definition: plugin.hpp:126
T as(uint32_t max_depth) const
Definition: variant.hpp:336
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
virtual void plugin_set_app(application *a)=0
Register the application instance with the plugin.
virtual std::string plugin_name() const =0
provides application independent P2P broadcast and data synchronization
Definition: node.hpp:192
chain::database & database()
Definition: plugin.hpp:114
virtual void plugin_startup()=0
Begin normal runtime operations.
virtual void plugin_shutdown()=0
Cleanly shut down the plugin.
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
Definition: plugin.hpp:115
std::shared_ptr< chain::database > chain_database() const
net::node & p2p_node()
Definition: plugin.hpp:117
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