VLink  2.0.0
A high-performance communication middleware
runnable_plugin_interface.h
浏览该文件的文档.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file runnable_plugin_interface.h
26  * @brief Plugin contract for self-driving plugins that own a private @c MessageLoop thread.
27  *
28  * @details
29  * @c RunablePluginInterface (the spelling is intentional and preserved for backwards
30  * compatibility) blends @c MessageLoop with the VLink @c Plugin framework so a dynamic
31  * plugin can carry its own event loop and dispatch timers, subscribers and other
32  * asynchronous primitives without depending on the host's loop.
33  *
34  * Plugin contract:
35  *
36  * | Hook | When the host calls it | Mandatory action |
37  * | ----------------- | --------------------------------------- | --------------------------------------------------- |
38  * | constructor | At @c Plugin::load time | Cheap construction; no thread-local work yet |
39  * | @c async_run() | After load, on the host's thread | Inherited from @c MessageLoop; starts the loop |
40  * | @c on_init() | Right after @c async_run() succeeds | Set up subscribers, timers and other live resources |
41  * | @c on_deinit() | Just before unload | Tear down everything created in @c on_init() |
42  * | destructor | When the @c Plugin handle is released | Final cleanup; loop has already been stopped |
43  *
44  * Plugin lifecycle:
45  *
46  * @verbatim
47  * Plugin::load(...)
48  * |
49  * v
50  * constructor --> async_run() --> on_init()
51  * |
52  * v
53  * plugin work (loop running)
54  * |
55  * v
56  * on_deinit()
57  * |
58  * v
59  * stop loop / unload
60  * @endverbatim
61  *
62  * @par Example
63  * @code
64  * // Inside the plugin shared library:
65  * class MyPlugin : public vlink::RunablePluginInterface {
66  * public:
67  * void on_init() override { ... } // create subscribers / timers
68  * void on_deinit() override { ... } // release everything created above
69  * };
70  * VLINK_PLUGIN_DECLARE(MyPlugin, 1, 0)
71  *
72  * // Inside the host process:
73  * vlink::Plugin plugin;
74  * auto instance = plugin.load<vlink::RunablePluginInterface>("my_plugin.so", 1, 0);
75  * instance->async_run();
76  * instance->on_init();
77  * // ... let it run ...
78  * instance->on_deinit();
79  * @endcode
80  */
81 
82 #pragma once
83 
84 #include "../base/message_loop.h"
85 #include "../base/plugin.h"
86 
87 namespace vlink {
88 
89 /**
90  * @class RunablePluginInterface
91  * @brief Abstract plugin base that already owns a @c MessageLoop event thread.
92  *
93  * @details
94  * The plugin inherits both the @c Plugin registration machinery and a @c MessageLoop,
95  * so it can post tasks, run timers and consume VLink subscriptions on its own thread.
96  * Lifecycle is split between @c on_init() (called once after the loop starts) and
97  * @c on_deinit() (called once before the loop is torn down).
98  */
101 
102  protected:
104 
105  ~RunablePluginInterface() override = default;
106 
107  public:
108  /**
109  * @brief Called by the host once the plugin's @c MessageLoop is running.
110  *
111  * @details
112  * Override to create subscribers, timers and other primitives that require the loop
113  * to be alive. Runs on the caller's thread.
114  */
115  virtual void on_init() = 0;
116 
117  /**
118  * @brief Called by the host just before the plugin is unloaded.
119  *
120  * @details
121  * Override to release every resource created in @c on_init(). After this call the
122  * host stops the loop and detaches the shared library.
123  */
124  virtual void on_deinit() = 0;
125 
126  private:
128 };
129 
130 } // namespace vlink
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
#define VLINK_PLUGIN_REGISTER(InterfaceType)
Declares a plugin's identity from the demangled name of its abstract interface.
Definition: plugin.h:345