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/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
5.34M
  static CustomValueContent Zero() {
34
5.34M
    CustomValueContent content;
35
5.34M
    std::memset(&content, 0, sizeof(content));
36
5.34M
    return content;
37
5.34M
  }
38
39
  template <typename T>
40
5.34M
  static CustomValueContent From(T value) {
41
5.34M
    static_assert(std::is_trivially_copyable_v<T>,
42
5.34M
                  "T must be trivially copyable");
43
5.34M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
5.34M
    CustomValueContent content;
46
5.34M
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
5.34M
    return content;
48
5.34M
  }
cel::CustomValueContent cel::CustomValueContent::From<cel::CustomListValueInterface::Content>(cel::CustomListValueInterface::Content)
Line
Count
Source
40
4.54M
  static CustomValueContent From(T value) {
41
4.54M
    static_assert(std::is_trivially_copyable_v<T>,
42
4.54M
                  "T must be trivially copyable");
43
4.54M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
4.54M
    CustomValueContent content;
46
4.54M
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
4.54M
    return content;
48
4.54M
  }
cel::CustomValueContent cel::CustomValueContent::From<cel::CustomMapValueInterface::Content>(cel::CustomMapValueInterface::Content)
Line
Count
Source
40
798k
  static CustomValueContent From(T value) {
41
798k
    static_assert(std::is_trivially_copyable_v<T>,
42
798k
                  "T must be trivially copyable");
43
798k
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
44
45
798k
    CustomValueContent content;
46
798k
    std::memcpy(content.raw_, std::addressof(value), sizeof(T));
47
798k
    return content;
48
798k
  }
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
8.61M
  T To() const {
64
8.61M
    static_assert(std::is_trivially_copyable_v<T>,
65
8.61M
                  "T must be trivially copyable");
66
8.61M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
8.61M
    T value;
69
8.61M
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
8.61M
    return value;
71
8.61M
  }
cel::CustomListValueInterface::Content cel::CustomValueContent::To<cel::CustomListValueInterface::Content>() const
Line
Count
Source
63
7.72M
  T To() const {
64
7.72M
    static_assert(std::is_trivially_copyable_v<T>,
65
7.72M
                  "T must be trivially copyable");
66
7.72M
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
7.72M
    T value;
69
7.72M
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
7.72M
    return value;
71
7.72M
  }
cel::CustomMapValueInterface::Content cel::CustomValueContent::To<cel::CustomMapValueInterface::Content>() const
Line
Count
Source
63
892k
  T To() const {
64
892k
    static_assert(std::is_trivially_copyable_v<T>,
65
892k
                  "T must be trivially copyable");
66
892k
    static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16");
67
68
892k
    T value;
69
892k
    std::memcpy(std::addressof(value), raw_, sizeof(T));
70
892k
    return value;
71
892k
  }
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_