diff options
author | Shawn Willden <swillden@google.com> | 2020-12-01 09:17:27 -0700 |
---|---|---|
committer | Shawn Willden <swillden@google.com> | 2020-12-14 09:39:00 -0700 |
commit | f79067125cdf2977226a264180fa193895df0ef6 (patch) | |
tree | 8d84cfe31231ffaf35432df1b7313674b7ef5aa0 | |
parent | 03990c2489864216132c319372ae209a1d6e6766 (diff) | |
download | libcppbor-f79067125cdf2977226a264180fa193895df0ef6.tar.gz |
Remove downcastItem<>
This safe downcast template was never used and is hard to implement
with the coming SemanticTag changes, so this CL removes it.
Test: cppbor_test_external
Change-Id: I0d4dd8b7323653c3924080f8fb328282dbca4566
-rw-r--r-- | include/cppbor/cppbor.h | 16 | ||||
-rw-r--r-- | tests/cppbor_test.cpp | 162 |
2 files changed, 0 insertions, 178 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h index c33960b..f9d371e 100644 --- a/include/cppbor/cppbor.h +++ b/include/cppbor/cppbor.h @@ -779,22 +779,6 @@ std::string prettyPrint(const Item* item, size_t maxBStrSize = 32, std::string prettyPrint(const std::vector<uint8_t>& encodedCbor, size_t maxBStrSize = 32, const std::vector<std::string>& mapKeysNotToPrint = {}); -template <typename T> -std::unique_ptr<T> downcastItem(std::unique_ptr<Item>&& v) { - static_assert(std::is_base_of_v<Item, T> && !std::is_abstract_v<T>, - "returned type is not an Item or is an abstract class"); - if (v && T::kMajorType == v->type()) { - if constexpr (std::is_base_of_v<Simple, T>) { - if (T::kSimpleType != v->asSimple()->simpleType()) { - return nullptr; - } - } - return std::unique_ptr<T>(static_cast<T*>(v.release())); - } else { - return nullptr; - } -} - /** * Details. Mostly you shouldn't have to look below, except perhaps at the docstring for makeItem. */ diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp index ed4b94e..b45d804 100644 --- a/tests/cppbor_test.cpp +++ b/tests/cppbor_test.cpp @@ -1424,168 +1424,6 @@ TEST(FullParserTest, MapWithTruncatedEntry) { EXPECT_EQ("Need 4 byte(s) for length field, have 3.", message); } -TEST(ItemDowncastingTest, Uint) { - auto item = std::unique_ptr<Item>(new Uint(1)); - EXPECT_NE(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Nint) { - auto item = std::unique_ptr<Item>(new Nint(-211)); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Tstr) { - auto item = std::unique_ptr<Item>(new Tstr("string")); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Bstr) { - auto item = std::unique_ptr<Item>(new Bstr(std::vector<uint8_t>{1, 2, 3, 4, 5, 6})); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Array) { - auto item = std::unique_ptr<Item>(new Array(1, 2, "3", "4", Array(5, "6"))); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Map) { - auto item = std::unique_ptr<Item>(new Map(1, 2, "key", "value")); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Bool) { - auto item = std::unique_ptr<Item>(new Bool(false)); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Null) { - auto item = std::unique_ptr<Item>(new Null()); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Semantic) { - auto item = std::unique_ptr<Item>(new Semantic(11, Map("key", Array(1, 2, 3)))); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - TEST(MapGetValueByKeyTest, Map) { Array compoundItem(1, 2, 3, 4, 5, Map(4, 5, "a", "b")); auto clone = compoundItem.clone(); |