Skip to content

Commit

Permalink
Merge pull request #168 from google/cpp-sync
Browse files Browse the repository at this point in the history
Use ABSL_LOG macros over LOG macros
  • Loading branch information
kyessenov authored May 18, 2023
2 parents 51bbf36 + c1a562a commit c5b9ae1
Show file tree
Hide file tree
Showing 31 changed files with 838 additions and 396 deletions.
1 change: 1 addition & 0 deletions base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ cc_library(
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_absl//absl/types:variant",
"@com_google_absl//absl/utility",
"@com_googlesource_code_re2//:re2",
],
)
Expand Down
2 changes: 2 additions & 0 deletions base/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ absl::Span<const absl::string_view> Type::aliases() const {
return static_cast<const ListType*>(this)->aliases();
case Kind::kMap:
return static_cast<const MapType*>(this)->aliases();
case Kind::kWrapper:
return static_cast<const WrapperType*>(this)->aliases();
default:
// Everything else does not support aliases.
return absl::Span<const absl::string_view>();
Expand Down
34 changes: 34 additions & 0 deletions base/types/wrapper_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "base/types/wrapper_type.h"

#include "absl/base/optimization.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"

namespace cel {

Expand Down Expand Up @@ -46,6 +48,20 @@ absl::string_view WrapperType::name() const {
}
}

absl::Span<const absl::string_view> WrapperType::aliases() const {
switch (base_internal::Metadata::GetInlineVariant<Kind>(*this)) {
case Kind::kDouble:
return static_cast<const DoubleWrapperType*>(this)->aliases();
case Kind::kInt:
return static_cast<const IntWrapperType*>(this)->aliases();
case Kind::kUint:
return static_cast<const UintWrapperType*>(this)->aliases();
default:
// The other wrappers do not have aliases.
return absl::Span<const absl::string_view>();
}
}

const Handle<Type>& WrapperType::wrapped() const {
switch (base_internal::Metadata::GetInlineVariant<Kind>(*this)) {
case Kind::kBool:
Expand All @@ -66,4 +82,22 @@ const Handle<Type>& WrapperType::wrapped() const {
}
}

absl::Span<const absl::string_view> DoubleWrapperType::aliases() const {
static constexpr absl::string_view kAliases[] = {
"google.protobuf.FloatValue"};
return absl::MakeConstSpan(kAliases);
}

absl::Span<const absl::string_view> IntWrapperType::aliases() const {
static constexpr absl::string_view kAliases[] = {
"google.protobuf.Int32Value"};
return absl::MakeConstSpan(kAliases);
}

absl::Span<const absl::string_view> UintWrapperType::aliases() const {
static constexpr absl::string_view kAliases[] = {
"google.protobuf.UInt32Value"};
return absl::MakeConstSpan(kAliases);
}

} // namespace cel
17 changes: 17 additions & 0 deletions base/types/wrapper_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "absl/base/attributes.h"
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/internal/data.h"
#include "base/kind.h"
#include "base/type.h"
Expand Down Expand Up @@ -69,13 +70,17 @@ class WrapperType : public Type, base_internal::InlineData {
const Handle<Type>& wrapped() const;

private:
friend class Type;
friend class BoolWrapperType;
friend class BytesWrapperType;
friend class DoubleWrapperType;
friend class IntWrapperType;
friend class StringWrapperType;
friend class UintWrapperType;

// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;

using Base::Base;
};

Expand Down Expand Up @@ -189,6 +194,7 @@ class DoubleWrapperType final : public WrapperType {
const Handle<DoubleType>& wrapped() const { return DoubleType::Get(); }

private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
Expand All @@ -202,6 +208,9 @@ class DoubleWrapperType final : public WrapperType {
<< base_internal::kInlineVariantShift);

constexpr DoubleWrapperType() : WrapperType(kMetadata) {}

// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};

class IntWrapperType final : public WrapperType {
Expand All @@ -226,6 +235,7 @@ class IntWrapperType final : public WrapperType {
const Handle<IntType>& wrapped() const { return IntType::Get(); }

private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
Expand All @@ -239,6 +249,9 @@ class IntWrapperType final : public WrapperType {
<< base_internal::kInlineVariantShift);

constexpr IntWrapperType() : WrapperType(kMetadata) {}

// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};

class StringWrapperType final : public WrapperType {
Expand Down Expand Up @@ -300,6 +313,7 @@ class UintWrapperType final : public WrapperType {
const Handle<UintType>& wrapped() const { return UintType::Get(); }

private:
friend class WrapperType;
friend class TypeFactory;
template <size_t Size, size_t Align>
friend struct base_internal::AnyData;
Expand All @@ -313,6 +327,9 @@ class UintWrapperType final : public WrapperType {
<< base_internal::kInlineVariantShift);

constexpr UintWrapperType() : WrapperType(kMetadata) {}

// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
};

extern template class Handle<WrapperType>;
Expand Down
142 changes: 96 additions & 46 deletions base/values/list_value_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
#include "absl/base/attributes.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/variant.h"
#include "absl/utility/utility.h"
#include "base/memory.h"
#include "base/value_factory.h"
#include "base/values/list_value.h"
#include "internal/overloaded.h"

namespace cel {

Expand Down Expand Up @@ -75,6 +78,53 @@ class ListValueBuilder;

namespace base_internal {

// ComposableListType is a variant which represents either the ListType or the
// element Type for creating a ListType.
template <typename T>
using ComposableListType = absl::variant<Handle<T>, Handle<ListType>>;

// Create a ListType from ComposableListType.
template <typename T>
absl::StatusOr<Handle<ListType>> ComposeListType(
ValueFactory& value_factory, ComposableListType<T>&& composable) {
return absl::visit(
internal::Overloaded{
[&value_factory](
Handle<T>&& element) -> absl::StatusOr<Handle<ListType>> {
return value_factory.type_factory().CreateListType(
std::move(element));
},
[](Handle<ListType>&& list) -> absl::StatusOr<Handle<ListType>> {
return std::move(list);
},
},
std::move(composable));
}

template <typename List, typename DebugStringer>
std::string ComposeListValueDebugString(const List& list,
const DebugStringer& debug_stringer) {
std::string out;
out.push_back('[');
auto current = list.begin();
if (current != list.end()) {
out.append(debug_stringer(*current));
++current;
for (; current != list.end(); ++current) {
out.append(", ");
out.append(debug_stringer(*current));
}
}
out.push_back(']');
return out;
}

struct ComposedListType {
explicit ComposedListType() = default;
};

inline constexpr ComposedListType kComposedListType{};

// Implementation of ListValueBuilder. Specialized to store some value types as
// C++ primitives, avoiding Handle overhead. Anything that does not have a C++
// primitive is stored as Handle<Value>.
Expand All @@ -92,22 +142,22 @@ class ListValueBuilderImpl<T, void> : public ListValueBuilderInterface {
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<typename ValueTraits<T>::type_type> type)
: ListValueBuilderInterface(value_factory),
type_(std::move(type)),
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}

ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}

std::string DebugString() const override {
size_t count = size();
std::string out;
out.push_back('[');
if (count != 0) {
out.append(storage_[0]->DebugString());
for (size_t index = 1; index < count; index++) {
out.append(", ");
out.append(storage_[index]->DebugString());
}
}
out.push_back(']');
return out;
return ComposeListValueDebugString(
storage_,
[](const Handle<Value>& value) { return value->DebugString(); });
}

absl::Status Add(Handle<Value> value) override {
Expand All @@ -127,14 +177,14 @@ class ListValueBuilderImpl<T, void> : public ListValueBuilderInterface {

absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
value_factory().type_factory().CreateListType(type_));
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::DynamicListValue>(
std::move(type), std::move(storage_));
}

private:
Handle<typename ValueTraits<T>::type_type> type_;
ComposableListType<typename ValueTraits<T>::type_type> type_;
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
};

Expand All @@ -147,22 +197,21 @@ class ListValueBuilderImpl<Value, void> : public ListValueBuilderInterface {
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<Type> type)
: ListValueBuilderInterface(value_factory),
type_(std::move(type)),
type_(absl::in_place_type<Handle<Type>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}

ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}

std::string DebugString() const override {
size_t count = size();
std::string out;
out.push_back('[');
if (count != 0) {
out.append(storage_[0]->DebugString());
for (size_t index = 1; index < count; index++) {
out.append(", ");
out.append(storage_[index]->DebugString());
}
}
out.push_back(']');
return out;
return ComposeListValueDebugString(
storage_,
[](const Handle<Value>& value) { return value->DebugString(); });
}

absl::Status Add(Handle<Value> value) override {
Expand All @@ -178,14 +227,14 @@ class ListValueBuilderImpl<Value, void> : public ListValueBuilderInterface {

absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
value_factory().type_factory().CreateListType(type_));
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::DynamicListValue>(
std::move(type), std::move(storage_));
}

private:
Handle<Type> type_;
ComposableListType<Type> type_;
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
};

Expand All @@ -198,23 +247,22 @@ class ListValueBuilderImpl : public ListValueBuilderInterface {
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<typename ValueTraits<T>::type_type> type)
: ListValueBuilderInterface(value_factory),
type_(std::move(type)),
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
std::move(type)),
storage_(Allocator<U>{value_factory.memory_manager()}) {}

ListValueBuilderImpl(
ComposedListType,
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
Handle<ListType> type)
: ListValueBuilderInterface(value_factory),
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
storage_(Allocator<U>{value_factory.memory_manager()}) {}

std::string DebugString() const override {
using value_traits = ValueTraits<T>;
size_t count = size();
std::string out;
out.push_back('[');
if (count != 0) {
out.append(value_traits::DebugString(storage_[0]));
for (size_t index = 1; index < count; index++) {
out.append(", ");
out.append(value_traits::DebugString(storage_[index]));
}
}
out.push_back(']');
return out;
return ComposeListValueDebugString(storage_, [](const U& value) {
return ValueTraits<T>::DebugString(value);
});
}

absl::Status Add(Handle<Value> value) override {
Expand All @@ -236,14 +284,14 @@ class ListValueBuilderImpl : public ListValueBuilderInterface {

absl::StatusOr<Handle<ListValue>> Build() && override {
CEL_ASSIGN_OR_RETURN(auto type,
value_factory().type_factory().CreateListType(type_));
ComposeListType(value_factory(), std::move(type_)));
return value_factory()
.template CreateListValue<base_internal::StaticListValue<T>>(
std::move(type), std::move(storage_));
}

private:
Handle<typename ValueTraits<T>::type_type> type_;
ComposableListType<typename ValueTraits<T>::type_type> type_;
std::vector<U, Allocator<U>> storage_;
};

Expand All @@ -257,6 +305,8 @@ class ListValueBuilder final
using Impl = base_internal::ListValueBuilderImpl<
T, typename base_internal::ValueTraits<T>::underlying_type>;

static_assert(!std::is_same_v<T, ListValue>);

public:
using Impl::Impl;
};
Expand Down
Loading

0 comments on commit c5b9ae1

Please sign in to comment.