Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rework OpaqueType and OpaqueTypeView #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ cc_library(
"//internal:status_macros",
"//internal:strings",
"//internal:time",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:fixed_array",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/log:absl_check",
Expand All @@ -289,6 +291,7 @@ cc_library(
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_absl//absl/types:variant",
"@com_google_absl//absl/utility",
],
Expand Down
58 changes: 28 additions & 30 deletions common/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
#include <type_traits>
#include <utility>

#include "absl/algorithm/container.h"
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/hash/hash.h"
#include "absl/container/fixed_array.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "absl/types/variant.h"
#include "common/casting.h"
#include "common/memory.h"
#include "common/native_type.h"
#include "common/sized_input_view.h"
#include "common/type_interface.h" // IWYU pragma: export
#include "common/type_kind.h"
#include "common/types/any_type.h" // IWYU pragma: export
Expand Down Expand Up @@ -663,6 +664,16 @@ struct MapTypeData final {
const Type value;
};

struct OpaqueTypeData final {
using Parameters = absl::FixedArray<Type, 1>;

explicit OpaqueTypeData(std::string name, Parameters parameters)
: name(std::move(name)), parameters(std::move(parameters)) {}

const std::string name;
const Parameters parameters;
};

} // namespace common_internal

template <>
Expand Down Expand Up @@ -746,23 +757,17 @@ inline H AbslHashValue(H state, MapTypeView type) {
return H::combine(std::move(state), type.kind(), type.key(), type.value());
}

inline SizedInputView<TypeView> OpaqueType::parameters() const {
return interface_->parameters();
inline OpaqueType::OpaqueType(OpaqueTypeView other) : data_(other.data_) {}

inline absl::string_view OpaqueType::name() const { return data_->name; }

inline absl::Span<const Type> OpaqueType::parameters() const {
return data_->parameters;
}

inline bool operator==(const OpaqueType& lhs, const OpaqueType& rhs) {
if (lhs.interface_.operator->() == rhs.interface_.operator->()) {
return true;
}
auto lhs_parameters = lhs.parameters();
auto rhs_parameters = rhs.parameters();
if (lhs_parameters.size() != rhs_parameters.size()) {
return false;
}
return lhs.name() == rhs.name() &&
std::equal(lhs_parameters.begin(), lhs_parameters.end(),
rhs_parameters.begin()) &&
lhs.interface_->Equals(*rhs.interface_);
absl::c_equal(lhs.parameters(), rhs.parameters());
}

template <typename H>
Expand All @@ -772,27 +777,21 @@ inline H AbslHashValue(H state, const OpaqueType& type) {
for (const auto& parameter : parameters) {
state = H::combine(std::move(state), parameter);
}
type.interface_->HashValue(absl::HashState::Create(&state));
return std::move(state);
}

inline SizedInputView<TypeView> OpaqueTypeView::parameters() const {
return interface_->parameters();
inline OpaqueTypeView::OpaqueTypeView(const OpaqueType& type) noexcept
: data_(type.data_) {}

inline absl::string_view OpaqueTypeView::name() const { return data_->name; }

inline absl::Span<const Type> OpaqueTypeView::parameters() const {
return data_->parameters;
}

inline bool operator==(OpaqueTypeView lhs, OpaqueTypeView rhs) {
if (lhs.interface_.operator->() == rhs.interface_.operator->()) {
return true;
}
auto lhs_parameters = lhs.parameters();
auto rhs_parameters = rhs.parameters();
if (lhs_parameters.size() != rhs_parameters.size()) {
return false;
}
return lhs.name() == rhs.name() &&
std::equal(lhs_parameters.begin(), lhs_parameters.end(),
rhs_parameters.begin()) &&
lhs.interface_->Equals(*rhs.interface_);
absl::c_equal(lhs.parameters(), rhs.parameters());
}

template <typename H>
Expand All @@ -802,7 +801,6 @@ inline H AbslHashValue(H state, OpaqueTypeView type) {
for (const auto& parameter : parameters) {
state = H::combine(std::move(state), parameter);
}
type.interface_->HashValue(absl::HashState::Create(&state));
return std::move(state);
}

Expand Down
47 changes: 39 additions & 8 deletions common/types/opaque_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,58 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>
#include <string>
#include <utility>

#include "absl/container/fixed_array.h"
#include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "common/memory.h"
#include "common/sized_input_view.h"
#include "common/type.h"

namespace cel {

std::string OpaqueTypeInterface::DebugString() const {
auto parameters = this->parameters();
namespace {

std::string OpaqueDebugString(absl::string_view name,
absl::Span<const Type> parameters) {
if (parameters.empty()) {
return std::string(name());
return std::string(name);
}
return absl::StrCat(
name, "<", absl::StrJoin(parameters, ", ", absl::StreamFormatter()), ">");
}

absl::FixedArray<Type, 1> SizedInputViewToFixedArray(
SizedInputView<TypeView> parameters) {
absl::FixedArray<Type, 1> fixed_parameters(parameters.size(), DynType());
size_t index = 0;
for (const auto& parameter : parameters) {
fixed_parameters[index++] = Type(parameter);
}
return absl::StrCat(name(), "<",
absl::StrJoin(parameters, ", ", absl::StreamFormatter()),
">");
ABSL_DCHECK_EQ(index, parameters.size());
return fixed_parameters;
}

} // namespace

OpaqueType::OpaqueType(MemoryManagerRef memory_manager, absl::string_view name,
SizedInputView<TypeView> parameters)
: data_(memory_manager.MakeShared<common_internal::OpaqueTypeData>(
std::string(name),
SizedInputViewToFixedArray(std::move(parameters)))) {}

std::string OpaqueType::DebugString() const {
return OpaqueDebugString(name(), parameters());
}

SizedInputView<TypeView> OpaqueTypeInterface::parameters() const {
return SizedInputView<TypeView>();
std::string OpaqueTypeView::DebugString() const {
return OpaqueDebugString(name(), parameters());
}

} // namespace cel
Loading