DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
message.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_MESSAGE_H
13 #define DOCWIRE_MESSAGE_H
14 
15 #include <functional>
16 #include <memory>
17 #include <typeinfo>
18 
19 namespace docwire
20 {
21 
22 enum class continuation { proceed, skip, stop };
23 struct message_callbacks;
24 using message_sequence_streamer = std::function<continuation(const message_callbacks&)>;
25 
26 template <typename T>
27 struct message;
28 
30 {
31  virtual ~message_base() = default;
32  virtual std::type_info const& object_type() const noexcept = 0;
33 
34  template <typename T>
35  bool is() const noexcept
36  {
37  return object_type() == typeid(T);
38  }
39  template <typename T>
40  const T& get() const
41  {
42  return static_cast<const message<T>&>(*this).object;
43  }
44 
45  template <typename T>
46  T& get()
47  {
48  return static_cast<message<T>&>(*this).object;
49  }
50 };
51 
52 template <typename T>
54 {
55  T object;
56  message(T&& object) : object(std::move(object)) {}
57  std::type_info const& object_type() const noexcept override
58  {
59  return typeid(T);
60  }
61 };
62 
63 using message_ptr = std::shared_ptr<message_base>;
64 
66 {
67  std::function<continuation(message_ptr)> m_further;
68  std::function<continuation(message_ptr)> m_back;
69 
70  continuation further(message_ptr msg) const { return m_further(std::move(msg)); }
71 
72  template <typename T>
73  continuation further(T&& object) const { return m_further(std::make_shared<message<T>>(std::forward<T>(object))); }
74 
75  continuation back(message_ptr msg) const { return m_back(std::move(msg)); }
76 
77  template <typename T>
78  continuation back(T&& object) const { return m_back(std::make_shared<message<T>>(std::forward<T>(object))); }
79 
80  continuation operator()(message_ptr msg) const { return further(std::move(msg)); }
81 
82  template <typename T>
83  continuation operator()(T&& object) const { return further(std::forward<T>(object)); }
84 };
85 
86 } // namespace docwire
87 
88 #endif //DOCWIRE_MESSAGE_H
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19