@@ -91,20 +91,22 @@ class Action {
9191 }
9292};
9393
94- using ActionConstSharedPtr = std::shared_ptr<const Action>;
94+ using ActionPtr = std::unique_ptr<Action>;
95+ using ActionFactoryCb = std::function<ActionPtr()>;
9596
9697template <class ActionFactoryContext > class ActionFactory : public Config ::TypedFactory {
9798public:
98- virtual ActionConstSharedPtr
99- createAction (const Protobuf::Message& config, ActionFactoryContext& action_factory_context,
100- ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
99+ virtual ActionFactoryCb
100+ createActionFactoryCb (const Protobuf::Message& config,
101+ ActionFactoryContext& action_factory_context,
102+ ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
101103
102104 std::string category () const override { return " envoy.matching.action" ; }
103105};
104106
105107// On match, we either return the action to perform or another match tree to match against.
106108template <class DataType > struct OnMatch {
107- const ActionConstSharedPtr action_ ;
109+ const ActionFactoryCb action_cb_ ;
108110 const MatchTreeSharedPtr<DataType> matcher_;
109111 bool keep_matching_{};
110112};
@@ -128,39 +130,30 @@ template <class DataType> class OnMatchFactory {
128130// - The match could not be completed due to lack of data (isInsufficientData() will return true.)
129131// - The match was completed, no match found (isNoMatch() will return true.)
130132// - The match was completed, match found (isMatch() will return true, action() will return the
131- // ActionConstSharedPtr .)
133+ // ActionFactoryCb .)
132134struct MatchResult {
133135public:
134- MatchResult (ActionConstSharedPtr cb) : result_(std::move(cb)) {}
136+ MatchResult (ActionFactoryCb cb) : result_(std::move(cb)) {}
135137 static MatchResult noMatch () { return MatchResult (NoMatch{}); }
136138 static MatchResult insufficientData () { return MatchResult (InsufficientData{}); }
137139 bool isInsufficientData () const { return absl::holds_alternative<InsufficientData>(result_); }
138140 bool isComplete () const { return !isInsufficientData (); }
139141 bool isNoMatch () const { return absl::holds_alternative<NoMatch>(result_); }
140- bool isMatch () const { return absl::holds_alternative<ActionConstSharedPtr>(result_); }
141- const ActionConstSharedPtr& action () const {
142- ASSERT (isMatch ());
143- return absl::get<ActionConstSharedPtr>(result_);
144- }
145- // Returns the action by move. The caller must ensure that the MatchResult is not used after
146- // this call.
147- ActionConstSharedPtr actionByMove () {
148- ASSERT (isMatch ());
149- return absl::get<ActionConstSharedPtr>(std::move (result_));
150- }
142+ bool isMatch () const { return absl::holds_alternative<ActionFactoryCb>(result_); }
143+ ActionFactoryCb actionFactory () const { return absl::get<ActionFactoryCb>(result_); }
144+ ActionPtr action () const { return actionFactory ()(); }
151145
152146private:
153147 struct InsufficientData {};
154148 struct NoMatch {};
155- using Result = absl::variant<ActionConstSharedPtr , NoMatch, InsufficientData>;
149+ using Result = absl::variant<ActionFactoryCb , NoMatch, InsufficientData>;
156150 Result result_;
157151 MatchResult (NoMatch) : result_(NoMatch{}) {}
158152 MatchResult (InsufficientData) : result_(InsufficientData{}) {}
159153};
160154
161155// Callback to execute against skipped matches' actions.
162- using SkippedMatchCb = std::function<void (const ActionConstSharedPtr&)>;
163-
156+ using SkippedMatchCb = std::function<void (ActionFactoryCb)>;
164157/* *
165158 * MatchTree provides the interface for performing matches against the data provided by DataType.
166159 */
@@ -194,19 +187,19 @@ template <class DataType> class MatchTree {
194187 // Parent result's keep_matching skips the nested result.
195188 if (on_match->keep_matching_ && nested_result.isMatch ()) {
196189 if (skipped_match_cb) {
197- skipped_match_cb (nested_result.action ());
190+ skipped_match_cb (nested_result.actionFactory ());
198191 }
199192 return MatchResult::noMatch ();
200193 }
201194 return nested_result;
202195 }
203- if (on_match->action_ && on_match->keep_matching_ ) {
196+ if (on_match->action_cb_ && on_match->keep_matching_ ) {
204197 if (skipped_match_cb) {
205- skipped_match_cb (on_match->action_ );
198+ skipped_match_cb (on_match->action_cb_ );
206199 }
207200 return MatchResult::noMatch ();
208201 }
209- return MatchResult{on_match->action_ };
202+ return MatchResult{on_match->action_cb_ };
210203 }
211204};
212205
0 commit comments