Add methods for creating boolean simple values
Add helper methods and static factory methods for creating/accessing
boolean CBOR simple values.
Bug: 803648
Change-Id: If0297d149ee1b58022fcf6b92e521b28e5f9faff
Reviewed-on: https://chromium-review.googlesource.com/875196
Commit-Queue: Jun Choi <hongjunchoi@chromium.org>
Reviewed-by: Balazs Engedy <engedy@chromium.org>
Reviewed-by: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Kim Paulhamus <kpaulhamus@chromium.org>
Cr-Commit-Position: refs/heads/master@{#530958}
diff --git a/components/cbor/cbor_values.cc b/components/cbor/cbor_values.cc
index b4517650..837b5c5 100644
--- a/components/cbor/cbor_values.cc
+++ b/components/cbor/cbor_values.cc
@@ -46,6 +46,16 @@
NOTREACHED();
}
+CBORValue::CBORValue(SimpleValue in_simple)
+ : type_(Type::SIMPLE_VALUE), simple_value_(in_simple) {
+ CHECK(static_cast<int>(in_simple) >= 20 && static_cast<int>(in_simple) <= 23);
+}
+
+CBORValue::CBORValue(bool boolean_value) : type_(Type::SIMPLE_VALUE) {
+ simple_value_ = boolean_value ? CBORValue::SimpleValue::TRUE_VALUE
+ : CBORValue::SimpleValue::FALSE_VALUE;
+}
+
CBORValue::CBORValue(int integer_value)
: CBORValue(base::checked_cast<int64_t>(integer_value)) {}
@@ -90,11 +100,6 @@
CBORValue::CBORValue(MapValue&& in_map) noexcept
: type_(Type::MAP), map_value_(std::move(in_map)) {}
-CBORValue::CBORValue(SimpleValue in_simple)
- : type_(Type::SIMPLE_VALUE), simple_value_(in_simple) {
- CHECK(static_cast<int>(in_simple) >= 20 && static_cast<int>(in_simple) <= 23);
-}
-
CBORValue& CBORValue::operator=(CBORValue&& that) noexcept {
InternalCleanup();
InternalMoveConstructFrom(std::move(that));
@@ -129,6 +134,16 @@
return CBORValue();
}
+CBORValue::SimpleValue CBORValue::GetSimpleValue() const {
+ CHECK(is_simple());
+ return simple_value_;
+}
+
+bool CBORValue::GetBool() const {
+ CHECK(is_bool());
+ return simple_value_ == SimpleValue::TRUE_VALUE;
+}
+
const int64_t& CBORValue::GetInteger() const {
CHECK(is_integer());
return integer_value_;
@@ -166,11 +181,6 @@
return map_value_;
}
-CBORValue::SimpleValue CBORValue::GetSimpleValue() const {
- CHECK(is_simple());
- return simple_value_;
-}
-
void CBORValue::InternalMoveConstructFrom(CBORValue&& that) {
type_ = that.type_;
diff --git a/components/cbor/cbor_values.h b/components/cbor/cbor_values.h
index 53eea21..af752f9 100644
--- a/components/cbor/cbor_values.h
+++ b/components/cbor/cbor_values.h
@@ -6,6 +6,7 @@
#define COMPONENTS_CBOR_CBOR_VALUES_H_
#include <stdint.h>
+
#include <string>
#include <tuple>
#include <vector>
@@ -96,6 +97,10 @@
CBORValue() noexcept; // A NONE value.
explicit CBORValue(Type type);
+
+ explicit CBORValue(SimpleValue in_simple);
+ explicit CBORValue(bool boolean_value);
+
explicit CBORValue(int integer_value);
explicit CBORValue(int64_t integer_value);
explicit CBORValue(uint64_t integer_value) = delete;
@@ -113,8 +118,6 @@
explicit CBORValue(const MapValue& in_map);
explicit CBORValue(MapValue&& in_map) noexcept;
- explicit CBORValue(SimpleValue in_simple);
-
CBORValue& operator=(CBORValue&& that) noexcept;
~CBORValue();
@@ -129,6 +132,11 @@
// Returns true if the current object represents a given type.
bool is_type(Type type) const { return type == type_; }
bool is_none() const { return type() == Type::NONE; }
+ bool is_simple() const { return type() == Type::SIMPLE_VALUE; }
+ bool is_bool() const {
+ return is_simple() && (simple_value_ == SimpleValue::TRUE_VALUE ||
+ simple_value_ == SimpleValue::FALSE_VALUE);
+ }
bool is_unsigned() const { return type() == Type::UNSIGNED; }
bool is_negative() const { return type() == Type::NEGATIVE; }
bool is_integer() const { return is_unsigned() || is_negative(); }
@@ -136,10 +144,10 @@
bool is_string() const { return type() == Type::STRING; }
bool is_array() const { return type() == Type::ARRAY; }
bool is_map() const { return type() == Type::MAP; }
- bool is_simple() const { return type() == Type::SIMPLE_VALUE; }
// These will all fatally assert if the type doesn't match.
SimpleValue GetSimpleValue() const;
+ bool GetBool() const;
const int64_t& GetInteger() const;
const int64_t& GetUnsigned() const;
const int64_t& GetNegative() const;
diff --git a/components/cbor/cbor_values_unittest.cc b/components/cbor/cbor_values_unittest.cc
index b003e83..fbeebc3 100644
--- a/components/cbor/cbor_values_unittest.cc
+++ b/components/cbor/cbor_values_unittest.cc
@@ -133,6 +133,16 @@
undefined_value.GetSimpleValue());
}
+TEST(CBORValuesTest, ConstructSimpleBooleanValue) {
+ CBORValue true_value(true);
+ ASSERT_EQ(CBORValue::Type::SIMPLE_VALUE, true_value.type());
+ EXPECT_TRUE(true_value.GetBool());
+
+ CBORValue false_value(false);
+ ASSERT_EQ(CBORValue::Type::SIMPLE_VALUE, false_value.type());
+ EXPECT_FALSE(false_value.GetBool());
+}
+
// Test copy constructors
TEST(CBORValuesTest, CopyUnsigned) {
CBORValue value(74);