Coverage Report

Created: 2026-09-03 06:30

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