/proc/self/cwd/common/values/timestamp_value.h
Line | Count | Source |
1 | | // Copyright 2023 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 | | // 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 | | // IWYU pragma: private, include "common/value.h" |
16 | | // IWYU pragma: friend "common/value.h" |
17 | | |
18 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_ |
19 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_ |
20 | | |
21 | | #include <ostream> |
22 | | #include <string> |
23 | | |
24 | | #include "absl/base/attributes.h" |
25 | | #include "absl/base/nullability.h" |
26 | | #include "absl/log/absl_check.h" |
27 | | #include "absl/status/status.h" |
28 | | #include "absl/status/statusor.h" |
29 | | #include "absl/strings/string_view.h" |
30 | | #include "absl/time/time.h" |
31 | | #include "absl/utility/utility.h" |
32 | | #include "common/type.h" |
33 | | #include "common/value_kind.h" |
34 | | #include "common/values/values.h" |
35 | | #include "internal/time.h" |
36 | | #include "google/protobuf/arena.h" |
37 | | #include "google/protobuf/descriptor.h" |
38 | | #include "google/protobuf/io/zero_copy_stream.h" |
39 | | #include "google/protobuf/message.h" |
40 | | |
41 | | namespace cel { |
42 | | |
43 | | class Value; |
44 | | class TimestampValue; |
45 | | |
46 | | TimestampValue UnsafeTimestampValue(absl::Time value); |
47 | | absl::StatusOr<TimestampValue> SafeTimestampValue(absl::Time value); |
48 | | |
49 | | // `TimestampValue` represents values of the primitive `timestamp` type. |
50 | | class TimestampValue final |
51 | | : private common_internal::ValueMixin<TimestampValue> { |
52 | | public: |
53 | | static constexpr ValueKind kKind = ValueKind::kTimestamp; |
54 | | |
55 | | explicit TimestampValue(absl::Time value) noexcept |
56 | 0 | : TimestampValue(absl::in_place, value) { |
57 | 0 | ABSL_DCHECK_OK(internal::ValidateTimestamp(value)); |
58 | 0 | } |
59 | | |
60 | | TimestampValue() = default; |
61 | | TimestampValue(const TimestampValue&) = default; |
62 | | TimestampValue(TimestampValue&&) = default; |
63 | | TimestampValue& operator=(const TimestampValue&) = default; |
64 | | TimestampValue& operator=(TimestampValue&&) = default; |
65 | | |
66 | 0 | ValueKind kind() const { return kKind; } |
67 | | |
68 | 0 | absl::string_view GetTypeName() const { return TimestampType::kName; } |
69 | | |
70 | | std::string DebugString() const; |
71 | | |
72 | | // See Value::SerializeTo(). |
73 | | absl::Status SerializeTo( |
74 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
75 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
76 | | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const; |
77 | | |
78 | | // See Value::ConvertToJson(). |
79 | | absl::Status ConvertToJson( |
80 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
81 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
82 | | google::protobuf::Message* absl_nonnull json) const; |
83 | | |
84 | | absl::Status Equal(const Value& other, |
85 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
86 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
87 | | google::protobuf::Arena* absl_nonnull arena, |
88 | | Value* absl_nonnull result) const; |
89 | | using ValueMixin::Equal; |
90 | | |
91 | 0 | bool IsZeroValue() const { return ToTime() == absl::UnixEpoch(); } |
92 | | |
93 | | ABSL_DEPRECATED("Use ToTime()") |
94 | 1 | absl::Time NativeValue() const { return static_cast<absl::Time>(*this); } |
95 | | |
96 | | ABSL_DEPRECATED("Use ToTime()") |
97 | | // NOLINTNEXTLINE(google-explicit-constructor) |
98 | 1 | operator absl::Time() const noexcept { return value_; } |
99 | | |
100 | 1 | absl::Time ToTime() const { return value_; } |
101 | | |
102 | 0 | friend void swap(TimestampValue& lhs, TimestampValue& rhs) noexcept { |
103 | 0 | using std::swap; |
104 | 0 | swap(lhs.value_, rhs.value_); |
105 | 0 | } |
106 | | |
107 | 0 | friend bool operator==(TimestampValue lhs, TimestampValue rhs) { |
108 | 0 | return lhs.value_ == rhs.value_; |
109 | 0 | } |
110 | | |
111 | 0 | friend bool operator<(const TimestampValue& lhs, const TimestampValue& rhs) { |
112 | 0 | return lhs.value_ < rhs.value_; |
113 | 0 | } |
114 | | |
115 | | private: |
116 | | friend class common_internal::ValueMixin<TimestampValue>; |
117 | | friend TimestampValue UnsafeTimestampValue(absl::Time value); |
118 | | |
119 | 49 | TimestampValue(absl::in_place_t, absl::Time value) : value_(value) {} |
120 | | |
121 | | absl::Time value_ = absl::UnixEpoch(); |
122 | | }; |
123 | | |
124 | 49 | inline TimestampValue UnsafeTimestampValue(absl::Time value) { |
125 | 49 | return TimestampValue(absl::in_place, value); |
126 | 49 | } |
127 | | |
128 | 0 | inline absl::StatusOr<TimestampValue> SafeTimestampValue(absl::Time value) { |
129 | 0 | absl::Status status = internal::ValidateTimestamp(value); |
130 | 0 | if (!status.ok()) { |
131 | 0 | return status; |
132 | 0 | } |
133 | 0 | return UnsafeTimestampValue(value); |
134 | 0 | } |
135 | | |
136 | 0 | inline bool operator!=(TimestampValue lhs, TimestampValue rhs) { |
137 | 0 | return !operator==(lhs, rhs); |
138 | 0 | } |
139 | | |
140 | 0 | inline std::ostream& operator<<(std::ostream& out, TimestampValue value) { |
141 | 0 | return out << value.DebugString(); |
142 | 0 | } |
143 | | |
144 | | } // namespace cel |
145 | | |
146 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_ |