Coverage Report

Created: 2026-05-27 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/duration_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_DURATION_VALUE_H_
19
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_DURATION_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 DurationValue;
45
46
DurationValue UnsafeDurationValue(absl::Duration value);
47
absl::StatusOr<DurationValue> SafeDurationValue(absl::Duration value);
48
49
// `DurationValue` represents values of the primitive `duration` type.
50
class DurationValue final : private common_internal::ValueMixin<DurationValue> {
51
 public:
52
  static constexpr ValueKind kKind = ValueKind::kDuration;
53
54
  explicit DurationValue(absl::Duration value) noexcept
55
166
      : DurationValue(absl::in_place, value) {
56
166
    ABSL_DCHECK_OK(internal::ValidateDuration(value));
57
166
  }
58
59
  DurationValue() = default;
60
  DurationValue(const DurationValue&) = default;
61
  DurationValue(DurationValue&&) = default;
62
  DurationValue& operator=(const DurationValue&) = default;
63
  DurationValue& operator=(DurationValue&&) = default;
64
65
0
  ValueKind kind() const { return kKind; }
66
67
0
  absl::string_view GetTypeName() const { return DurationType::kName; }
68
69
  std::string DebugString() const;
70
71
  // See Value::SerializeTo().
72
  absl::Status SerializeTo(
73
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
74
      google::protobuf::MessageFactory* absl_nonnull message_factory,
75
      google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const;
76
77
  // See Value::ConvertToJson().
78
  absl::Status ConvertToJson(
79
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
80
      google::protobuf::MessageFactory* absl_nonnull message_factory,
81
      google::protobuf::Message* absl_nonnull json) const;
82
83
  absl::Status Equal(const Value& other,
84
                     const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
85
                     google::protobuf::MessageFactory* absl_nonnull message_factory,
86
                     google::protobuf::Arena* absl_nonnull arena,
87
                     Value* absl_nonnull result) const;
88
  using ValueMixin::Equal;
89
90
0
  bool IsZeroValue() const { return ToDuration() == absl::ZeroDuration(); }
91
92
  ABSL_DEPRECATED("Use ToDuration()")
93
1
  absl::Duration NativeValue() const {
94
1
    return static_cast<absl::Duration>(*this);
95
1
  }
96
97
  ABSL_DEPRECATED("Use ToDuration()")
98
  // NOLINTNEXTLINE(google-explicit-constructor)
99
1
  operator absl::Duration() const noexcept { return value_; }
100
101
5
  absl::Duration ToDuration() const { return value_; }
102
103
0
  friend void swap(DurationValue& lhs, DurationValue& rhs) noexcept {
104
0
    using std::swap;
105
0
    swap(lhs.value_, rhs.value_);
106
0
  }
107
108
0
  friend bool operator==(DurationValue lhs, DurationValue rhs) {
109
0
    return lhs.value_ == rhs.value_;
110
0
  }
111
112
0
  friend bool operator<(const DurationValue& lhs, const DurationValue& rhs) {
113
0
    return lhs.value_ < rhs.value_;
114
0
  }
115
116
 private:
117
  friend class common_internal::ValueMixin<DurationValue>;
118
  friend DurationValue UnsafeDurationValue(absl::Duration value);
119
120
174
  DurationValue(absl::in_place_t, absl::Duration value) : value_(value) {}
121
122
  absl::Duration value_ = absl::ZeroDuration();
123
};
124
125
8
inline DurationValue UnsafeDurationValue(absl::Duration value) {
126
8
  return DurationValue(absl::in_place, value);
127
8
}
128
129
0
inline absl::StatusOr<DurationValue> SafeDurationValue(absl::Duration value) {
130
0
  absl::Status status = internal::ValidateDuration(value);
131
0
  if (!status.ok()) {
132
0
    return status;
133
0
  }
134
0
  return UnsafeDurationValue(value);
135
0
}
136
137
0
inline bool operator!=(DurationValue lhs, DurationValue rhs) {
138
0
  return !operator==(lhs, rhs);
139
0
}
140
141
0
inline std::ostream& operator<<(std::ostream& out, DurationValue value) {
142
0
  return out << value.DebugString();
143
0
}
144
145
}  // namespace cel
146
147
#endif  // THIRD_PARTY_CEL_CPP_COMMON_VALUES_DURATION_VALUE_H_