Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
InterceptorChain.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
7
8#include "InterceptorChain.h"
9
10using namespace Stroika::Foundation;
12
13using namespace Stroika::Frameworks;
14using namespace Stroika::Frameworks::WebServer;
15
16// This class MUST be re-entrant (internally synchronized)- and is STATELESS - so no syncro needed
17struct InterceptorChain::Rep_ : InterceptorChain::_IRep {
18 Rep_ (const Sequence<Interceptor>& interceptors)
19 : fInterceptors_{interceptors}
20 {
21 }
22 virtual Sequence<Interceptor> GetInterceptors () const override
23 {
24 return fInterceptors_;
25 }
26 virtual shared_ptr<_IRep> SetInterceptors (const Sequence<Interceptor>& interceptors) const override
27 {
28 return make_shared<Rep_> (interceptors);
29 }
30 virtual void HandleMessage (Message& m) const override
31 {
32 size_t sz = fInterceptors_.size ();
33 size_t i = 0;
34 for (; i < sz; ++i) {
35 try {
36 fInterceptors_[i].HandleMessage (m);
37 }
38 catch (...) {
39 exception_ptr e = current_exception ();
40 do {
41 fInterceptors_[i].HandleFault (m, e);
42 } while (i-- != 0);
44 }
45 }
46 for (; i > 0; --i) {
47 fInterceptors_[i - 1].CompleteNormally (m);
48 }
49 }
50 const Sequence<Interceptor> fInterceptors_; // no synchro needed because always readonly
51};
52
53/*
54 ********************************************************************************
55 *********************** WebServer::InterceptorChain ****************************
56 ********************************************************************************
57 */
58InterceptorChain::InterceptorChain (const Sequence<Interceptor>& interceptors)
59 : InterceptorChain{make_shared<Rep_> (interceptors)}
60{
61}
62
63InterceptorChain::InterceptorChain (const shared_ptr<_IRep>& rep)
64 : interceptors{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] const auto* property) -> Sequence<Interceptor> {
65 const InterceptorChain* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &InterceptorChain::interceptors);
66 return thisObj->fRep_.cget ().load ()->GetInterceptors ();
67 },
68 [qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] auto* property, const Sequence<Interceptor>& interceptors) {
69 InterceptorChain* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &InterceptorChain::interceptors);
70 auto rwLock = thisObj->fRep_.rwget ();
71 rwLock.store (rwLock->get ()->SetInterceptors (interceptors));
72 }}
73 , fRep_{rep}
74{
75}
76
77void InterceptorChain::AddBefore (const Interceptor& interceptor2Add, const Interceptor& before)
78{
79 auto rwLock = fRep_.rwget ();
80 [[maybe_unused]] bool found{false};
81 Sequence<Interceptor> newInterceptors = rwLock->get ()->GetInterceptors ();
82 for (size_t i = 0; i < newInterceptors.size (); ++i) {
83 if (newInterceptors[i] == before) {
84 newInterceptors.Insert (i, interceptor2Add);
85 found = true;
86 break;
87 }
88 }
89 Require (found);
90 rwLock.store (rwLock->get ()->SetInterceptors (newInterceptors));
91}
92
93void InterceptorChain::AddAfter (const Interceptor& interceptor2Add, const Interceptor& after)
94{
95 auto rwLock = fRep_.rwget ();
96
97 [[maybe_unused]] bool found{false};
98 Sequence<Interceptor> newInterceptors = rwLock->get ()->GetInterceptors ();
99 for (size_t i = 0; i < newInterceptors.size (); ++i) {
100 if (newInterceptors[i] == after) {
101 newInterceptors.Insert (i + 1, interceptor2Add);
102 found = true;
103 break;
104 }
105 }
106 Require (found);
107 rwLock.store (rwLock->get ()->SetInterceptors (newInterceptors));
108}
109
110Characters::String InterceptorChain::ToString () const
111{
112 StringBuilder sb;
113 sb << "["sv;
114 for (auto i : fRep_.load ()->GetInterceptors ()) {
115 sb << i << ", "sv;
116 }
117 sb << "]"sv;
118 return sb;
119}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
nonvirtual void Insert(size_t i, ArgByValueType< value_type > item)
Definition Sequence.inl:281
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:300
nonvirtual void AddBefore(const Interceptor &interceptor2Add, const Interceptor &before)
nonvirtual void AddAfter(const Interceptor &interceptor2Add, const Interceptor &after)