PDI 1.13.0

the PDI data interface

context.h
1/*******************************************************************************
2 * Copyright (C) 2015-2024 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * Copyright (C) 2021 Institute of Bioorganic Chemistry Polish Academy of Science (PSNC)
4 * Copyright (C) 2026 Julien Bigot <julien@julien-bigot.fr>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of CEA nor the names of its contributors may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 ******************************************************************************/
26
27#ifndef PDI_CONTEXT_H_
28#define PDI_CONTEXT_H_
29
30#include <functional>
31#include <memory>
32#include <string>
33#include <unordered_map>
34
35#include <pdi/pdi_fwd.h>
36#include <pdi/data_descriptor.h>
37#include <pdi/datatype_template.h>
38#include <pdi/ref_any.h>
39
40namespace PDI {
41
42class PDI_EXPORT Context
43{
44public:
47 class Iterator
48 {
49 friend class Context;
51 std::unordered_map<std::string, std::unique_ptr<Data_descriptor>>::iterator m_data;
52 Iterator(const std::unordered_map<std::string, std::unique_ptr<Data_descriptor>>::iterator& data);
53 Iterator(std::unordered_map<std::string, std::unique_ptr<Data_descriptor>>::iterator&& data);
54
55 public:
57 Data_descriptor& operator* ();
58 Iterator& operator++ ();
59 bool operator!= (const Iterator&);
60 bool operator== (const Iterator&);
61 };
62
65 typedef std::function<Datatype_template_sptr(Context&, PC_tree_t)> Datatype_template_parser;
66
67protected:
68 Iterator get_iterator(const std::unordered_map<std::string, std::unique_ptr<Data_descriptor>>::iterator& data);
69
70 Iterator get_iterator(std::unordered_map<std::string, std::unique_ptr<Data_descriptor>>::iterator&& data);
71
72public:
73 virtual ~Context();
74
77 virtual Data_descriptor& desc(const std::string& name) = 0;
78
81 virtual Data_descriptor& desc(const char* name) = 0;
82
85 virtual Data_descriptor& operator[] (const std::string& name) = 0;
86
89 virtual Data_descriptor& operator[] (const char* name) = 0;
90
93 virtual Iterator begin() = 0;
94
97 virtual Iterator end() = 0;
98
104 virtual Iterator find(const std::string& name) = 0;
105
109 virtual void event(const char* name) = 0;
110
116 virtual Datatype_template_sptr datatype(PC_tree_t node) = 0;
117
123 virtual void add_datatype(const std::string& name, Datatype_template_parser parser) = 0;
124
131 virtual std::function<void()> on_init(const std::function<void()>& callback) = 0;
132
140 virtual std::function<void()> on_data(const std::function<void(const std::string&, Ref)>& callback, const std::string& name = {}) = 0;
141
149 virtual std::function<void()> on_data_remove(const std::function<void(const std::string&, Ref)>& callback, const std::string& name = {}) = 0;
150
158 virtual std::function<void()> on_event(const std::function<void(const std::string&)>& callback, const std::string& name = {}) = 0;
159
167 virtual std::function<void()> on_missing_data(const std::function<void(const std::string&)>& callback, const std::string& name = {}) = 0;
168};
169
176class PDI_EXPORT Timer_event_handler
177{
178private:
179 Context& m_ctx;
180
181 const std::string m_plugin_name;
182
183public:
189 Timer_event_handler(Context& ctx, const std::string& plugin_name);
190
194};
195
197using TimerEventHandler [[deprecated("use Timer_event_handler instead")]] = Timer_event_handler;
198
199} // namespace PDI
200
201#endif // PDI_CONTEXT_H_
An iterator used to go through the descriptor store.
Definition context.h:48
friend class Context
Definition context.h:49
Data_descriptor * operator->()
Definition context.h:43
virtual Data_descriptor & desc(const char *name)=0
Accesses the descriptor for a specific name.
Iterator get_iterator(std::unordered_map< std::string, std::unique_ptr< Data_descriptor > >::iterator &&data)
virtual Iterator find(const std::string &name)=0
Find the Data_descriptor corresponding to a given name.
virtual Data_descriptor & desc(const std::string &name)=0
Accesses the descriptor for a specific name.
virtual Datatype_template_sptr datatype(PC_tree_t node)=0
Creates a new datatype template from a paraconf-style config.
Iterator get_iterator(const std::unordered_map< std::string, std::unique_ptr< Data_descriptor > >::iterator &data)
virtual void add_datatype(const std::string &name, Datatype_template_parser parser)=0
Adds new datatype parser to the context.
virtual std::function< void()> on_data_remove(const std::function< void(const std::string &, Ref)> &callback, const std::string &name={})=0
Adds new data remove callback to context.
std::function< Datatype_template_sptr(Context &, PC_tree_t)> Datatype_template_parser
A function that parses a PC_tree_t to create a datatype_template.
Definition context.h:65
virtual std::function< void()> on_init(const std::function< void()> &callback)=0
Adds new init callback to context.
virtual ~Context()
virtual std::function< void()> on_data(const std::function< void(const std::string &, Ref)> &callback, const std::string &name={})=0
Adds new data callback to context.
virtual Iterator end()=0
Returns an iterator past the last descriptor.
virtual std::function< void()> on_missing_data(const std::function< void(const std::string &)> &callback, const std::string &name={})=0
Adds new empty desc access callback to context.
virtual std::function< void()> on_event(const std::function< void(const std::string &)> &callback, const std::string &name={})=0
Adds new event callback to context.
virtual Iterator begin()=0
Returns an iterator on the first descriptor.
virtual void event(const char *name)=0
Triggers a PDI "event".
Definition data_descriptor.h:39
A scoped timer for a plugin operation.
Definition context.h:177
~Timer_event_handler()
Triggers the stop event of the timer.
Timer_event_handler(Context &ctx, const std::string &plugin_name)
Triggers the start event of the timer.
Definition array_datatype.h:38
std::shared_ptr< const Datatype_template > Datatype_template_sptr
Definition pdi_fwd.h:79
Ref_any< false, false > Ref
Definition pdi_fwd.h:99