Action Engine
Loading...
Searching...
No Matches
sinks.h
1// Copyright 2025 Google LLC
2//
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// http://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#ifndef ACTIONENGINE_DISTRIBUTED_SINKS_H_
16#define ACTIONENGINE_DISTRIBUTED_SINKS_H_
17
18#include <string>
19#include <string_view>
20#include <utility>
21
22#include "actionengine/data/conversion.h"
23
24namespace act::distributed {
25
26class Sink {
27 public:
28 virtual ~Sink() = default;
29 virtual absl::Status Append(std::string_view s) = 0;
30
31 template <typename T>
32 absl::Status Append(T&& value) {
33 auto value_str = act::ConvertTo<std::string>(std::forward<T>(value));
34 if (!value_str.ok()) {
35 return value_str.status();
36 }
37 return this->Append(*std::move(value_str));
38 }
39
40 [[nodiscard]] virtual std::string_view View() const = 0;
41};
42
43class StringSink final : public Sink {
44 public:
45 StringSink() = default;
46
47 explicit StringSink(std::string_view s);
48
49 absl::Status Append(std::string_view s) override;
50
51 [[nodiscard]] std::string_view View() const override;
52
53 private:
54 std::string str_;
55};
56
57} // namespace act::distributed
58
59#endif // ACTIONENGINE_DISTRIBUTED_SINKS_H_