nullable_string16.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef BASE_STRINGS_NULLABLE_STRING16_H_
  5. #define BASE_STRINGS_NULLABLE_STRING16_H_
  6. #include <iosfwd>
  7. #include "base/base_export.h"
  8. #include "base/optional.h"
  9. #include "base/strings/string16.h"
  10. #include "base/strings/string_util.h"
  11. namespace base {
  12. // This class is a simple wrapper for string16 which also contains a null
  13. // state. This should be used only where the difference between null and
  14. // empty is meaningful.
  15. class BASE_EXPORT NullableString16 {
  16. public:
  17. NullableString16();
  18. NullableString16(const NullableString16& other);
  19. NullableString16(NullableString16&& other);
  20. NullableString16(const string16& string, bool is_null);
  21. explicit NullableString16(Optional<string16> optional_string16);
  22. ~NullableString16();
  23. NullableString16& operator=(const NullableString16& other);
  24. NullableString16& operator=(NullableString16&& other);
  25. const string16& string() const {
  26. return string_ ? *string_ : EmptyString16();
  27. }
  28. bool is_null() const { return !string_; }
  29. const Optional<string16>& as_optional_string16() const { return string_; }
  30. private:
  31. Optional<string16> string_;
  32. };
  33. inline bool operator==(const NullableString16& a, const NullableString16& b) {
  34. return a.as_optional_string16() == b.as_optional_string16();
  35. }
  36. inline bool operator!=(const NullableString16& a, const NullableString16& b) {
  37. return !(a == b);
  38. }
  39. BASE_EXPORT std::ostream& operator<<(std::ostream& out,
  40. const NullableString16& value);
  41. } // namespace base
  42. #endif // BASE_STRINGS_NULLABLE_STRING16_H_