Ice 3.9
C++ API Reference
Loading...
Searching...
No Matches
ObserverHelper.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_OBSERVER_HELPER_H
4#define ICE_OBSERVER_HELPER_H
5
6#include "InstanceF.h"
7#include "Instrumentation.h"
8
9#include <exception>
10#include <string_view>
11
12#if defined(_MSC_VER)
13# pragma warning(push)
14# pragma warning(disable : 4251) // class ... needs to have dll-interface to be used by clients of class ...
15#endif
16
17namespace Ice
18{
19 class ObjectPrx;
20}
21
22namespace IceInternal
23{
24 std::string getExceptionId(std::exception_ptr);
25
26 template<typename T = Ice::Instrumentation::Observer> class ObserverHelperT
27 {
28 public:
29 using TPtr = std::shared_ptr<T>;
30
31 ObserverHelperT() = default;
32
33 ~ObserverHelperT()
34 {
35 if (_observer)
36 {
37 _observer->detach();
38 }
39 }
40
41 operator bool() const { return _observer != nullptr; }
42
43 T* operator->() const { return _observer.get(); }
44
45 void attach(const TPtr& o)
46 {
47 //
48 // Don't detach the existing observer. The observer is being
49 // replaced and the observed object is still being observed!
50 //
51 // if(_observer)
52 // {
53 // _observer->detach();
54 // }
55 _observer = o;
56 if (_observer)
57 {
58 _observer->attach();
59 }
60 }
61 [[nodiscard]] TPtr get() const { return _observer; }
62
63 void adopt(ObserverHelperT& other)
64 {
65 _observer = other._observer;
66 other._observer = 0;
67 }
68
69 void detach()
70 {
71 if (_observer)
72 {
73 _observer->detach();
74 _observer = nullptr;
75 }
76 }
77
78 void failed(const std::string& reason)
79 {
80 if (_observer)
81 {
82 _observer->failed(reason);
83 }
84 }
85
86 protected:
87 TPtr _observer;
88 };
89
90 class ICE_API DispatchObserver : public ObserverHelperT<Ice::Instrumentation::DispatchObserver>
91 {
92 public:
93 void userException()
94 {
95 if (_observer)
96 {
97 _observer->userException();
98 }
99 }
100
101 void reply(std::int32_t size)
102 {
103 if (_observer)
104 {
105 _observer->reply(size);
106 }
107 }
108 };
109
110 class ICE_API InvocationObserver : public ObserverHelperT<Ice::Instrumentation::InvocationObserver>
111 {
112 public:
113 InvocationObserver(const Ice::ObjectPrx& proxy, std::string_view operation, const Ice::Context& context);
114 InvocationObserver(Instance* instance, std::string_view operation);
115 InvocationObserver() = default;
116
117 void attach(const Ice::ObjectPrx&, std::string_view operation, const Ice::Context&);
118 void attach(Instance*, std::string_view operation);
119
120 void retried()
121 {
122 if (_observer)
123 {
124 _observer->retried();
125 }
126 }
127
129 getRemoteObserver(const Ice::ConnectionInfoPtr& con, const Ice::EndpointPtr& endpt, int requestId, int size)
130 {
131 if (_observer)
132 {
133 return _observer->getRemoteObserver(con, endpt, requestId, size);
134 }
135 return nullptr;
136 }
137
139 getCollocatedObserver(const Ice::ObjectAdapterPtr& adapter, int requestId, int size)
140 {
141 if (_observer)
142 {
143 return _observer->getCollocatedObserver(adapter, requestId, size);
144 }
145 return nullptr;
146 }
147
148 void userException()
149 {
150 if (_observer)
151 {
152 _observer->userException();
153 }
154 }
155
156 private:
157 using ObserverHelperT<Ice::Instrumentation::InvocationObserver>::attach;
158 };
159}
160
161#if defined(_MSC_VER)
162# pragma warning(pop)
163#endif
164
165#endif
The base class for all Ice proxies.
Definition Proxy.h:232
std::shared_ptr< ChildInvocationObserver > ChildInvocationObserverPtr
A shared pointer to a ChildInvocationObserver.
std::shared_ptr< ConnectionInfo > ConnectionInfoPtr
A shared pointer to a ConnectionInfo.
Definition ConnectionF.h:21
std::shared_ptr< ObjectAdapter > ObjectAdapterPtr
A shared pointer to an ObjectAdapter.
std::shared_ptr< Endpoint > EndpointPtr
A shared pointer to an Endpoint.
Definition EndpointF.h:20
std::map< std::string, std::string, std::less<> > Context
Represents additional information carried by an Ice request.
Definition Context.h:28
The Ice RPC framework.
Definition SampleEvent.h:60