Skip to content

Commit

Permalink
IntType and IntTypeView
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 583407170
  • Loading branch information
jcking authored and copybara-github committed Nov 17, 2023
1 parent a26740e commit 21355d0
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 4 deletions.
1 change: 1 addition & 0 deletions common/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "common/types/duration_type.h" // IWYU pragma: export
#include "common/types/dyn_type.h" // IWYU pragma: export
#include "common/types/error_type.h" // IWYU pragma: export
#include "common/types/int_type.h" // IWYU pragma: export
#include "common/types/types.h"

namespace cel {
Expand Down
129 changes: 129 additions & 0 deletions common/types/int_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// IWYU pragma: private, include "common/type.h"
// IWYU pragma: friend "common/type.h"

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_INT_TYPE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_INT_TYPE_H_

#include <ostream>
#include <string>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "common/type_kind.h"

namespace cel {

class IntType;
class IntTypeView;

// `IntType` represents the primitive `int` type.
class IntType final {
public:
using view_alternative_type = IntTypeView;

static constexpr TypeKind kKind = TypeKind::kInt;
static constexpr absl::string_view kName = "int";

explicit IntType(IntTypeView);

IntType() = default;
IntType(const IntType&) = default;
IntType(IntType&&) = default;
IntType& operator=(const IntType&) = default;
IntType& operator=(IntType&&) = default;

constexpr TypeKind kind() const { return kKind; }

constexpr absl::string_view name() const { return kName; }

std::string DebugString() const { return std::string(name()); }

constexpr void swap(IntType&) noexcept {}
};

inline constexpr void swap(IntType& lhs, IntType& rhs) noexcept {
lhs.swap(rhs);
}

inline constexpr bool operator==(const IntType&, const IntType&) {
return true;
}

inline constexpr bool operator!=(const IntType& lhs, const IntType& rhs) {
return !operator==(lhs, rhs);
}

template <typename H>
H AbslHashValue(H state, const IntType& type) {
return H::combine(std::move(state), type.kind());
}

inline std::ostream& operator<<(std::ostream& out, const IntType& type) {
return out << type.DebugString();
}

class IntTypeView final {
public:
using alternative_type = IntType;

static constexpr TypeKind kKind = IntType::kKind;
static constexpr absl::string_view kName = IntType::kName;

// NOLINTNEXTLINE(google-explicit-constructor)
IntTypeView(const IntType& type ABSL_ATTRIBUTE_LIFETIME_BOUND
ABSL_ATTRIBUTE_UNUSED) noexcept {}

IntTypeView() = default;
IntTypeView(const IntTypeView&) = default;
IntTypeView(IntTypeView&&) = default;
IntTypeView& operator=(const IntTypeView&) = default;
IntTypeView& operator=(IntTypeView&&) = default;

constexpr TypeKind kind() const { return kKind; }

constexpr absl::string_view name() const { return kName; }

std::string DebugString() const { return std::string(name()); }

constexpr void swap(IntTypeView&) noexcept {}
};

inline constexpr void swap(IntTypeView& lhs, IntTypeView& rhs) noexcept {
lhs.swap(rhs);
}

inline constexpr bool operator==(IntTypeView, IntTypeView) { return true; }

inline constexpr bool operator!=(IntTypeView lhs, IntTypeView rhs) {
return !operator==(lhs, rhs);
}

template <typename H>
H AbslHashValue(H state, IntTypeView type) {
return H::combine(std::move(state), type.kind());
}

inline std::ostream& operator<<(std::ostream& out, IntTypeView type) {
return out << type.DebugString();
}

inline IntType::IntType(IntTypeView) {}

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPES_INT_TYPE_H_
152 changes: 152 additions & 0 deletions common/types/int_type_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sstream>

#include "absl/hash/hash.h"
#include "absl/types/optional.h"
#include "common/casting.h"
#include "common/native_type.h"
#include "common/type.h"
#include "internal/testing.h"

namespace cel {
namespace {

using testing::An;
using testing::Ne;

TEST(IntType, Kind) {
EXPECT_EQ(IntType().kind(), IntType::kKind);
EXPECT_EQ(Type(IntType()).kind(), IntType::kKind);
}

TEST(IntType, Name) {
EXPECT_EQ(IntType().name(), IntType::kName);
EXPECT_EQ(Type(IntType()).name(), IntType::kName);
}

TEST(IntType, DebugString) {
{
std::ostringstream out;
out << IntType();
EXPECT_EQ(out.str(), IntType::kName);
}
{
std::ostringstream out;
out << Type(IntType());
EXPECT_EQ(out.str(), IntType::kName);
}
}

TEST(IntType, Hash) {
EXPECT_EQ(absl::HashOf(IntType()), absl::HashOf(IntType()));
EXPECT_EQ(absl::HashOf(Type(IntType())), absl::HashOf(IntType()));
}

TEST(IntType, Equal) {
EXPECT_EQ(IntType(), IntType());
EXPECT_EQ(Type(IntType()), IntType());
EXPECT_EQ(IntType(), Type(IntType()));
EXPECT_EQ(Type(IntType()), Type(IntType()));
}

TEST(IntType, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(IntType()), NativeTypeId::For<IntType>());
EXPECT_EQ(NativeTypeId::Of(Type(IntType())), NativeTypeId::For<IntType>());
}

TEST(IntType, InstanceOf) {
EXPECT_TRUE(InstanceOf<IntType>(IntType()));
EXPECT_TRUE(InstanceOf<IntType>(Type(IntType())));
}

TEST(IntType, Cast) {
EXPECT_THAT(Cast<IntType>(IntType()), An<IntType>());
EXPECT_THAT(Cast<IntType>(Type(IntType())), An<IntType>());
}

TEST(IntType, As) {
EXPECT_THAT(As<IntType>(IntType()), Ne(absl::nullopt));
EXPECT_THAT(As<IntType>(Type(IntType())), Ne(absl::nullopt));
}

TEST(IntTypeView, Kind) {
EXPECT_EQ(IntTypeView().kind(), IntTypeView::kKind);
EXPECT_EQ(TypeView(IntTypeView()).kind(), IntTypeView::kKind);
}

TEST(IntTypeView, Name) {
EXPECT_EQ(IntTypeView().name(), IntTypeView::kName);
EXPECT_EQ(TypeView(IntTypeView()).name(), IntTypeView::kName);
}

TEST(IntTypeView, DebugString) {
{
std::ostringstream out;
out << IntTypeView();
EXPECT_EQ(out.str(), IntTypeView::kName);
}
{
std::ostringstream out;
out << TypeView(IntTypeView());
EXPECT_EQ(out.str(), IntTypeView::kName);
}
}

TEST(IntTypeView, Hash) {
EXPECT_EQ(absl::HashOf(IntTypeView()), absl::HashOf(IntTypeView()));
EXPECT_EQ(absl::HashOf(TypeView(IntTypeView())), absl::HashOf(IntTypeView()));
EXPECT_EQ(absl::HashOf(IntTypeView()), absl::HashOf(IntType()));
EXPECT_EQ(absl::HashOf(TypeView(IntTypeView())), absl::HashOf(IntType()));
}

TEST(IntTypeView, Equal) {
EXPECT_EQ(IntTypeView(), IntTypeView());
EXPECT_EQ(TypeView(IntTypeView()), IntTypeView());
EXPECT_EQ(IntTypeView(), TypeView(IntTypeView()));
EXPECT_EQ(TypeView(IntTypeView()), TypeView(IntTypeView()));
EXPECT_EQ(IntTypeView(), IntType());
EXPECT_EQ(TypeView(IntTypeView()), IntType());
EXPECT_EQ(TypeView(IntTypeView()), Type(IntType()));
EXPECT_EQ(IntType(), IntTypeView());
EXPECT_EQ(IntType(), IntTypeView());
EXPECT_EQ(IntType(), TypeView(IntTypeView()));
EXPECT_EQ(Type(IntType()), TypeView(IntTypeView()));
EXPECT_EQ(IntTypeView(), IntType());
}

TEST(IntTypeView, NativeTypeId) {
EXPECT_EQ(NativeTypeId::Of(IntTypeView()), NativeTypeId::For<IntTypeView>());
EXPECT_EQ(NativeTypeId::Of(TypeView(IntTypeView())),
NativeTypeId::For<IntTypeView>());
}

TEST(IntTypeView, InstanceOf) {
EXPECT_TRUE(InstanceOf<IntTypeView>(IntTypeView()));
EXPECT_TRUE(InstanceOf<IntTypeView>(TypeView(IntTypeView())));
}

TEST(IntTypeView, Cast) {
EXPECT_THAT(Cast<IntTypeView>(IntTypeView()), An<IntTypeView>());
EXPECT_THAT(Cast<IntTypeView>(TypeView(IntTypeView())), An<IntTypeView>());
}

TEST(IntTypeView, As) {
EXPECT_THAT(As<IntTypeView>(IntTypeView()), Ne(absl::nullopt));
EXPECT_THAT(As<IntTypeView>(TypeView(IntTypeView())), Ne(absl::nullopt));
}

} // namespace
} // namespace cel
11 changes: 7 additions & 4 deletions common/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DoubleWrapperType;
class DurationType;
class DynType;
class ErrorType;
class IntType;

class TypeView;
class AnyTypeView;
Expand All @@ -48,6 +49,7 @@ class DoubleWrapperTypeView;
class DurationTypeView;
class DynTypeView;
class ErrorTypeView;
class IntTypeView;

namespace common_internal {

Expand All @@ -58,7 +60,8 @@ struct IsTypeAlternative
std::is_same<BoolWrapperType, T>, std::is_same<BytesType, T>,
std::is_same<BytesWrapperType, T>, std::is_same<DoubleType, T>,
std::is_same<DoubleWrapperType, T>, std::is_same<DurationType, T>,
std::is_same<DynType, T>, std::is_same<ErrorType, T>>> {};
std::is_same<DynType, T>, std::is_same<ErrorType, T>,
std::is_same<IntType, T>>> {};

template <typename T>
inline constexpr bool IsTypeAlternativeV = IsTypeAlternative<T>::value;
Expand All @@ -71,7 +74,7 @@ using TypeVariant = absl::variant<
absl::monostate,
#endif
AnyType, BoolType, BoolWrapperType, BytesType, BytesWrapperType, DoubleType,
DoubleWrapperType, DurationType, DynType, ErrorType>;
DoubleWrapperType, DurationType, DynType, ErrorType, IntType>;

template <typename T>
struct IsTypeViewAlternative
Expand All @@ -82,7 +85,7 @@ struct IsTypeViewAlternative
std::is_same<DoubleTypeView, T>,
std::is_same<DoubleWrapperTypeView, T>,
std::is_same<DurationTypeView, T>, std::is_same<DynTypeView, T>,
std::is_same<ErrorTypeView, T>>> {};
std::is_same<ErrorTypeView, T>, std::is_same<IntTypeView, T>>> {};

template <typename T>
inline constexpr bool IsTypeViewAlternativeV = IsTypeViewAlternative<T>::value;
Expand All @@ -96,7 +99,7 @@ using TypeViewVariant = absl::variant<
#endif
AnyTypeView, BoolTypeView, BoolWrapperTypeView, BytesTypeView,
BytesWrapperTypeView, DoubleTypeView, DoubleWrapperTypeView,
DurationTypeView, DynTypeView, ErrorTypeView>;
DurationTypeView, DynTypeView, ErrorTypeView, IntTypeView>;

} // namespace common_internal

Expand Down

0 comments on commit 21355d0

Please sign in to comment.