Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/custom_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_CUSTOM_VALUE_H_
19
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_CUSTOM_VALUE_H_
20
21
#include <cstddef>
22
#include <cstring>
23
#include <memory>
24
#include <type_traits>
25
26
namespace cel {
27
28
// CustomValueContent is an opaque 16-byte trivially copyable value. The format
29
// of the data stored within is unknown to everything except the the caller
30
// which creates it. Do not try to interpret it otherwise.
31
class CustomValueContent final {
32
 public:
33
3.66M
  static CustomValueContent Zero() {
34
3.66M
    CustomValueContent content;
35
3.66M
    std::memset(&content, 0, sizeof(content));
36
3.66M
    return content;
37
3.66M
  }
38
39
  template <typename T>
40
3.66M
  static CustomValueContent From(T value) {
41
3.66M
    static_assert(std::is_trivially_copyable_v<T>,
42
3.66M
                  "T must be trivially copyable");
43
3.66M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
3.66M
    CustomValueContent content;
46
3.66M
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
3.66M
    return content;
48
3.66M
  }
cel::CustomValueContent cel::CustomValueContent::From<cel::CustomListValueInterface::Content>(cel::CustomListValueInterface::Content)
Line
Count
Source
40
3.08M
  static CustomValueContent From(T value) {
41
3.08M
    static_assert(std::is_trivially_copyable_v<T>,
42
3.08M
                  "T must be trivially copyable");
43
3.08M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
3.08M
    CustomValueContent content;
46
3.08M
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
3.08M
    return content;
48
3.08M
  }
cel::CustomValueContent cel::CustomValueContent::From<cel::CustomMapValueInterface::Content>(cel::CustomMapValueInterface::Content)
Line
Count
Source
40
574k
  static CustomValueContent From(T value) {
41
574k
    static_assert(std::is_trivially_copyable_v<T>,
42
574k
                  "T must be trivially copyable");
43
574k
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
574k
    CustomValueContent content;
46
574k
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
574k
    return content;
48
574k
  }
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<cel::CustomStructValueInterface::Content>(cel::CustomStructValueInterface::Content)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<cel::OpaqueValueInterface::Content>(cel::OpaqueValueInterface::Content)
Unexecuted instantiation: optional_value.cc:cel::CustomValueContent cel::CustomValueContent::From<cel::(anonymous namespace)::OptionalValueContent>(cel::(anonymous namespace)::OptionalValueContent)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<bool>(bool)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<long>(long)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<unsigned long>(unsigned long)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<double>(double)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<absl::lts_20260526::Duration>(absl::lts_20260526::Duration)
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<absl::lts_20260526::Time>(absl::lts_20260526::Time)
49
50
  template <typename T, size_t N>
51
  static CustomValueContent From(const T (&array)[N]) {
52
    static_assert(std::is_trivially_copyable_v<T>,
53
                  "T must be trivially copyable");
54
    static_assert((sizeof(T) * N) <= 16,
55
                  "sizeof(T[N]) must be no greater than 16");
56
57
    CustomValueContent content;
58
    std::memcpy(content.raw_, array, sizeof(T) * N);
59
    return content;
60
  }
61
62
  template <typename T>
63
3.70M
  T To() const {
64
3.70M
    static_assert(std::is_trivially_copyable_v<T>,
65
3.70M
                  "T must be trivially copyable");
66
3.70M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
3.70M
    T value;
69
3.70M
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
3.70M
    return value;
71
3.70M
  }
cel::CustomListValueInterface::Content cel::CustomValueContent::To<cel::CustomListValueInterface::Content>() const
Line
Count
Source
63
3.03M
  T To() const {
64
3.03M
    static_assert(std::is_trivially_copyable_v<T>,
65
3.03M
                  "T must be trivially copyable");
66
3.03M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
3.03M
    T value;
69
3.03M
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
3.03M
    return value;
71
3.03M
  }
cel::CustomMapValueInterface::Content cel::CustomValueContent::To<cel::CustomMapValueInterface::Content>() const
Line
Count
Source
63
670k
  T To() const {
64
670k
    static_assert(std::is_trivially_copyable_v<T>,
65
670k
                  "T must be trivially copyable");
66
670k
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
670k
    T value;
69
670k
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
670k
    return value;
71
670k
  }
Unexecuted instantiation: cel::CustomStructValueInterface::Content cel::CustomValueContent::To<cel::CustomStructValueInterface::Content>() const
Unexecuted instantiation: cel::OpaqueValueInterface::Content cel::CustomValueContent::To<cel::OpaqueValueInterface::Content>() const
Unexecuted instantiation: bool cel::CustomValueContent::To<bool>() const
Unexecuted instantiation: long cel::CustomValueContent::To<long>() const
Unexecuted instantiation: unsigned long cel::CustomValueContent::To<unsigned long>() const
Unexecuted instantiation: double cel::CustomValueContent::To<double>() const
Unexecuted instantiation: absl::lts_20260526::Duration cel::CustomValueContent::To<absl::lts_20260526::Duration>() const
Unexecuted instantiation: absl::lts_20260526::Time cel::CustomValueContent::To<absl::lts_20260526::Time>() const
Unexecuted instantiation: optional_value.cc:cel::(anonymous namespace)::OptionalValueContent cel::CustomValueContent::To<cel::(anonymous namespace)::OptionalValueContent>() const
72
73
0
  bool IsZero() const {
74
0
    static const CustomValueContent kZero = Zero();
75
0
    return std::memcmp(raw_, kZero.raw_, sizeof(raw_)) == 0;
76
0
  }
77
78
 private:
79
  alignas(void*) std::byte raw_[16];
80
};
81
82
}  // namespace cel
83
84
#endif  // THIRD_PARTY_CEL_CPP_COMMON_VALUES_CUSTOM_VALUE_H_