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