Coverage Report

Created: 2026-07-11 06:47

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