1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_STRINGS_NULLABLE_STRING16_H_
6 #define BASE_STRINGS_NULLABLE_STRING16_H_
7 
8 #include <iosfwd>
9 
10 #include "base/base_export.h"
11 #include "base/optional.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_util.h"
14 
15 namespace base {
16 
17 // This class is a simple wrapper for string16 which also contains a null
18 // state.  This should be used only where the difference between null and
19 // empty is meaningful.
20 class BASE_EXPORT NullableString16 {
21  public:
22   NullableString16();
23   NullableString16(const NullableString16& other);
24   NullableString16(NullableString16&& other);
25   NullableString16(const string16& string, bool is_null);
26   explicit NullableString16(Optional<string16> optional_string16);
27   ~NullableString16();
28 
29   NullableString16& operator=(const NullableString16& other);
30   NullableString16& operator=(NullableString16&& other);
31 
string()32   const string16& string() const {
33     return string_ ? *string_ : EmptyString16();
34   }
is_null()35   bool is_null() const { return !string_; }
as_optional_string16()36   const Optional<string16>& as_optional_string16() const { return string_; }
37 
38  private:
39   Optional<string16> string_;
40 };
41 
42 inline bool operator==(const NullableString16& a, const NullableString16& b) {
43   return a.as_optional_string16() == b.as_optional_string16();
44 }
45 
46 inline bool operator!=(const NullableString16& a, const NullableString16& b) {
47   return !(a == b);
48 }
49 
50 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
51                                      const NullableString16& value);
52 
53 }  // namespace base
54 
55 #endif  // BASE_STRINGS_NULLABLE_STRING16_H_
56