Coverage Report

Created: 2026-09-14 06:52

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
  // Constructs a `DurationValue` from an `absl::Duration`.
55
  //
56
  // DCHECK-fails if the value is not in the supported range.
57
  //
58
  // Prefer using `SafeDurationValue` or `UnsafeDurationValue` if the caller
59
  // has already validated the value.
60
  explicit DurationValue(absl::Duration value) noexcept
61
49.0k
      : DurationValue(absl::in_place, value) {
62
49.0k
    ABSL_DCHECK_OK(internal::ValidateDuration(value));
63
49.0k
  }
64
65
  DurationValue() = default;
66
  DurationValue(const DurationValue&) = default;
67
  DurationValue(DurationValue&&) = default;
68
  DurationValue& operator=(const DurationValue&) = default;
69
  DurationValue& operator=(DurationValue&&) = default;
70
71
0
  ValueKind kind() const { return kKind; }
72
73
0
  absl::string_view GetTypeName() const { return DurationType::kName; }
74
75
  std::string DebugString() const;
76
77
  // See Value::SerializeTo().
78
  absl::Status SerializeTo(
79
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
80
      google::protobuf::MessageFactory* absl_nonnull message_factory,
81
      google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const;
82
83
  // See Value::ConvertToJson().
84
  absl::Status ConvertToJson(
85
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
86
      google::protobuf::MessageFactory* absl_nonnull message_factory,
87
      google::protobuf::Message* absl_nonnull json) const;
88
89
  absl::Status Equal(const Value& other,
90
                     const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
91
                     google::protobuf::MessageFactory* absl_nonnull message_factory,
92
                     google::protobuf::Arena* absl_nonnull arena,
93
                     Value* absl_nonnull result) const;
94
  using ValueMixin::Equal;
95
96
0
  bool IsZeroValue() const { return ToDuration() == absl::ZeroDuration(); }
97
98
  ABSL_DEPRECATED("Use ToDuration()")
99
46.2k
  absl::Duration NativeValue() const {
100
46.2k
    return static_cast<absl::Duration>(*this);
101
46.2k
  }
102
103
  ABSL_DEPRECATED("Use ToDuration()")
104
  // NOLINTNEXTLINE(google-explicit-constructor)
105
46.2k
  operator absl::Duration() const noexcept { return value_; }
106
107
12.0k
  absl::Duration ToDuration() const { return value_; }
108
109
0
  friend void swap(DurationValue& lhs, DurationValue& rhs) noexcept {
110
0
    using std::swap;
111
0
    swap(lhs.value_, rhs.value_);
112
0
  }
113
114
0
  friend bool operator==(DurationValue lhs, DurationValue rhs) {
115
0
    return lhs.value_ == rhs.value_;
116
0
  }
117
118
0
  friend bool operator<(const DurationValue& lhs, const DurationValue& rhs) {
119
0
    return lhs.value_ < rhs.value_;
120
0
  }
121
122
 private:
123
  friend class common_internal::ValueMixin<DurationValue>;
124
  friend DurationValue UnsafeDurationValue(absl::Duration value);
125
126
52.3k
  DurationValue(absl::in_place_t, absl::Duration value) : value_(value) {}
127
128
  absl::Duration value_ = absl::ZeroDuration();
129
};
130
131
3.31k
inline DurationValue UnsafeDurationValue(absl::Duration value) {
132
3.31k
  return DurationValue(absl::in_place, value);
133
3.31k
}
134
135
0
inline absl::StatusOr<DurationValue> SafeDurationValue(absl::Duration value) {
136
0
  absl::Status status = internal::ValidateDuration(value);
137
0
  if (!status.ok()) {
138
0
    return status;
139
0
  }
140
0
  return UnsafeDurationValue(value);
141
0
}
142
143
0
inline bool operator!=(DurationValue lhs, DurationValue rhs) {
144
0
  return !operator==(lhs, rhs);
145
0
}
146
147
0
inline std::ostream& operator<<(std::ostream& out, DurationValue value) {
148
0
  return out << value.DebugString();
149
0
}
150
151
}  // namespace cel
152
153
#endif  // THIRD_PARTY_CEL_CPP_COMMON_VALUES_DURATION_VALUE_H_