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