Commit 86419ce0 authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

Change NullableString16 to wrap a base::Optional<base::string16>.

This allows the existing mojo traits for string16 to be reused for
NullableString16.

Bug: 577685,604860
Change-Id: I69a7be9e14c033ca2c7ab92e7cc0078eaa8a391a
Reviewed-on: https://chromium-review.googlesource.com/509669
Commit-Queue: Sam McNally <sammc@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#473800}
parent e65dd51e
......@@ -5,13 +5,29 @@
#include "base/strings/nullable_string16.h"
#include <ostream>
#include "base/strings/utf_string_conversions.h"
#include <utility>
namespace base {
NullableString16::NullableString16() = default;
NullableString16::NullableString16(const NullableString16& other) = default;
NullableString16::NullableString16(NullableString16&& other) = default;
NullableString16::NullableString16(const string16& string, bool is_null) {
if (!is_null)
string_.emplace(string);
}
NullableString16::NullableString16(Optional<string16> optional_string16)
: string_(std::move(optional_string16)) {}
NullableString16::~NullableString16() = default;
NullableString16& NullableString16::operator=(const NullableString16& other) =
default;
NullableString16& NullableString16::operator=(NullableString16&& other) =
default;
std::ostream& operator<<(std::ostream& out, const NullableString16& value) {
return value.is_null() ? out << "(null)" : out << UTF16ToUTF8(value.string());
return value.is_null() ? out << "(null)" : out << value.string();
}
} // namespace base
......@@ -8,30 +8,39 @@
#include <iosfwd>
#include "base/base_export.h"
#include "base/optional.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
namespace base {
// This class is a simple wrapper for string16 which also contains a null
// state. This should be used only where the difference between null and
// empty is meaningful.
class NullableString16 {
class BASE_EXPORT NullableString16 {
public:
NullableString16() : is_null_(true) { }
NullableString16(const string16& string, bool is_null)
: string_(string), is_null_(is_null) {
NullableString16();
NullableString16(const NullableString16& other);
NullableString16(NullableString16&& other);
NullableString16(const string16& string, bool is_null);
explicit NullableString16(Optional<string16> optional_string16);
~NullableString16();
NullableString16& operator=(const NullableString16& other);
NullableString16& operator=(NullableString16&& other);
const string16& string() const {
return string_ ? *string_ : EmptyString16();
}
const string16& string() const { return string_; }
bool is_null() const { return is_null_; }
bool is_null() const { return !string_; }
const Optional<string16>& as_optional_string16() const { return string_; }
private:
string16 string_;
bool is_null_;
Optional<string16> string_;
};
inline bool operator==(const NullableString16& a, const NullableString16& b) {
return a.is_null() == b.is_null() && a.string() == b.string();
return a.as_optional_string16() == b.as_optional_string16();
}
inline bool operator!=(const NullableString16& a, const NullableString16& b) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment