xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/message_sink.h (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PRIVATE_JOIN_AND_COMPUTE_MESSAGE_SINK_H_
17 #define PRIVATE_JOIN_AND_COMPUTE_MESSAGE_SINK_H_
18 
19 #include <memory>
20 
21 #include "private_join_and_compute/private_join_and_compute.pb.h"
22 #include "private_join_and_compute/util/status.inc"
23 
24 namespace private_join_and_compute {
25 
26 // An interface for message sinks.
27 template <typename T>
28 class MessageSink {
29  public:
30   virtual ~MessageSink() = default;
31 
32   // Subclasses should accept a message and process it appropriately.
33   virtual Status Send(const T& message) = 0;
34 
35  protected:
36   MessageSink() = default;
37 };
38 
39 // A dummy message sink, that simply stores the last message received, and
40 // allows retrieval. Intended for testing.
41 template <typename T>
42 class DummyMessageSink : public MessageSink<T> {
43  public:
44   ~DummyMessageSink() override = default;
45 
46   // Simply copies the message.
Send(const T & message)47   Status Send(const T& message) override {
48     last_message_ = std::make_unique<T>(message);
49     return OkStatus();
50   }
51 
52   // Will fail if no message was received.
last_message()53   const T& last_message() { return *last_message_; }
54 
55  private:
56   std::unique_ptr<T> last_message_;
57 };
58 
59 }  // namespace private_join_and_compute
60 
61 #endif  // PRIVATE_JOIN_AND_COMPUTE_MESSAGE_SINK_H_
62