forked from steemit/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_api_plugin.cpp
117 lines (90 loc) · 3.14 KB
/
hello_api_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <steemit/app/application.hpp>
#include <steemit/app/plugin.hpp>
#include <sstream>
#include <string>
namespace steemit { namespace example_plugin {
class hello_api_plugin : public steemit::app::plugin
{
public:
/**
* The plugin requires a constructor which takes app. This is called regardless of whether the plugin is loaded.
* The app parameter should be passed up to the superclass constructor.
*/
hello_api_plugin( steemit::app::application* app );
/**
* Plugin is destroyed via base class pointer, so a virtual destructor must be provided.
*/
virtual ~hello_api_plugin();
/**
* Every plugin needs a name.
*/
virtual std::string plugin_name()const override;
/**
* Called when the plugin is enabled, but before the database has been created.
*/
virtual void plugin_initialize( const boost::program_options::variables_map& options ) override;
/**
* Called when the plugin is enabled.
*/
virtual void plugin_startup() override;
std::string get_message();
private:
steemit::app::application* _app;
std::string _message;
uint32_t _plugin_call_count = 0;
};
class hello_api_api
{
public:
hello_api_api( const steemit::app::api_context& ctx );
/**
* Called immediately after the constructor. If the API class uses enable_shared_from_this,
* shared_from_this() is available in this method, which allows signal handlers to be registered
* with app::connect_signal()
*/
void on_api_startup();
std::string get_message();
private:
steemit::app::application& _app;
uint32_t _api_call_count = 0;
};
} }
FC_API( steemit::example_plugin::hello_api_api,
(get_message)
)
namespace steemit { namespace example_plugin {
hello_api_plugin::hello_api_plugin( steemit::app::application* app ) : steemit::app::plugin(app) {}
hello_api_plugin::~hello_api_plugin() {}
std::string hello_api_plugin::plugin_name()const
{
return "hello_api";
}
void hello_api_plugin::plugin_initialize( const boost::program_options::variables_map& options )
{
_message = "hello world";
}
void hello_api_plugin::plugin_startup()
{
app().register_api_factory< hello_api_api >( "hello_api_api" );
}
std::string hello_api_plugin::get_message()
{
std::stringstream result;
result << _message << " -- users have viewed this message " << (_plugin_call_count++) << " times";
return result.str();
}
hello_api_api::hello_api_api( const steemit::app::api_context& ctx ) : _app(ctx.app) {}
void hello_api_api::on_api_startup() {}
std::string hello_api_api::get_message()
{
std::stringstream result;
std::shared_ptr< hello_api_plugin > plug = _app.get_plugin< hello_api_plugin >( "hello_api" );
result << plug->get_message() << " -- you've viewed this message " << (_api_call_count++) << " times";
return result.str();
}
} }
/**
* The STEEMIT_DEFINE_PLUGIN() macro will define a steemit::plugin::create_hello_api_plugin()
* factory method which is expected by the manifest.
*/
STEEMIT_DEFINE_PLUGIN( hello_api, steemit::example_plugin::hello_api_plugin )