forked from eosspark/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
94 lines (75 loc) · 2.07 KB
/
plugin.go
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
package app
import (
"github.com/urfave/cli"
. "github.com/zhangsifeng92/geos/exception/try"
)
/** these notifications get called from the plugin when their state changes so that
* the application can call shutdown in the reverse order.
*/
//Register(abstract_plugin) error
type Plugin interface {
SetProgramOptions(options *[]cli.Flag)
PluginInitialize(*cli.Context)
PluginStartup()
PluginShutdown()
Initialize(options *cli.Context)
StartUp()
ShutDown()
GetName() PluginTypeName
setName(name PluginTypeName)
GetState() State
setState(state State)
bind(plugin Plugin)
}
type PluginTypeName = string
type State int
const (
Registered = State(iota + 1) ///< the plugin is constructed but doesn't do anything
Initialized ///< the plugin has initialized any state required but is idle
Started ///< the plugin is actively running
Stopped ///< the plugin is no longer running
)
type AbstractPlugin struct {
self Plugin //must be pointer
name PluginTypeName
state State
}
func (a *AbstractPlugin) ShutDown() {
if a.state == Started {
a.state = Stopped
a.self.PluginShutdown()
}
}
func (a *AbstractPlugin) Initialize(options *cli.Context) {
if a.state == Registered {
a.state = Initialized
a.self.PluginInitialize(options)
App().PluginInitialized(a.self)
}
Assert(a.state == Initialized, "plugin initialize failed")
}
func (a *AbstractPlugin) StartUp() {
if a.state == Initialized {
a.state = Started
//static_cast<Impl*>(this)->plugin_requires([&](auto& plug){ plug.initialize(options); });
//static_cast<Impl*>(this)->plugin_initialize(options);
a.self.PluginStartup()
App().PluginStarted(a.self)
}
Assert(a.state == Started, "plugin startup failed")
}
func (a *AbstractPlugin) GetName() PluginTypeName {
return a.name
}
func (a *AbstractPlugin) setName(name PluginTypeName) {
a.name = name
}
func (a *AbstractPlugin) GetState() State {
return a.state
}
func (a *AbstractPlugin) setState(state State) {
a.state = state
}
func (a *AbstractPlugin) bind(plugin Plugin) {
a.self = plugin
}