1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bindings/Nullable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ 1.5 +/* vim: set ts=2 sw=2 et tw=79: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_Nullable_h 1.11 +#define mozilla_dom_Nullable_h 1.12 + 1.13 +#include "mozilla/Assertions.h" 1.14 +#include "nsTArrayForwardDeclare.h" 1.15 +#include "mozilla/Move.h" 1.16 + 1.17 +namespace mozilla { 1.18 +namespace dom { 1.19 + 1.20 +// Support for nullable types 1.21 +template <typename T> 1.22 +struct Nullable 1.23 +{ 1.24 +private: 1.25 + // mIsNull MUST COME FIRST because otherwise the casting in our array 1.26 + // conversion operators would shift where it is found in the struct. 1.27 + bool mIsNull; 1.28 + T mValue; 1.29 + 1.30 +public: 1.31 + Nullable() 1.32 + : mIsNull(true) 1.33 + {} 1.34 + 1.35 + explicit Nullable(T aValue) 1.36 + : mIsNull(false) 1.37 + , mValue(aValue) 1.38 + {} 1.39 + 1.40 + explicit Nullable(Nullable<T>&& aOther) 1.41 + : mIsNull(aOther.mIsNull) 1.42 + , mValue(mozilla::Move(aOther.mValue)) 1.43 + {} 1.44 + 1.45 + Nullable(const Nullable<T>& aOther) 1.46 + : mIsNull(aOther.mIsNull) 1.47 + , mValue(aOther.mValue) 1.48 + {} 1.49 + 1.50 + void operator=(const Nullable<T>& aOther) 1.51 + { 1.52 + mIsNull = aOther.mIsNull; 1.53 + mValue = aOther.mValue; 1.54 + } 1.55 + 1.56 + void SetValue(T aValue) { 1.57 + mValue = aValue; 1.58 + mIsNull = false; 1.59 + } 1.60 + 1.61 + // For cases when |T| is some type with nontrivial copy behavior, we may want 1.62 + // to get a reference to our internal copy of T and work with it directly 1.63 + // instead of relying on the copying version of SetValue(). 1.64 + T& SetValue() { 1.65 + mIsNull = false; 1.66 + return mValue; 1.67 + } 1.68 + 1.69 + void SetNull() { 1.70 + mIsNull = true; 1.71 + } 1.72 + 1.73 + const T& Value() const { 1.74 + MOZ_ASSERT(!mIsNull); 1.75 + return mValue; 1.76 + } 1.77 + 1.78 + T& Value() { 1.79 + MOZ_ASSERT(!mIsNull); 1.80 + return mValue; 1.81 + } 1.82 + 1.83 + bool IsNull() const { 1.84 + return mIsNull; 1.85 + } 1.86 + 1.87 + bool Equals(const Nullable<T>& aOtherNullable) const 1.88 + { 1.89 + return (mIsNull && aOtherNullable.mIsNull) || 1.90 + (!mIsNull && !aOtherNullable.mIsNull && 1.91 + mValue == aOtherNullable.mValue); 1.92 + } 1.93 + 1.94 + bool operator==(const Nullable<T>& aOtherNullable) const 1.95 + { 1.96 + return Equals(aOtherNullable); 1.97 + } 1.98 + 1.99 + bool operator!=(const Nullable<T>& aOtherNullable) const 1.100 + { 1.101 + return !Equals(aOtherNullable); 1.102 + } 1.103 + 1.104 + // Make it possible to use a const Nullable of an array type with other 1.105 + // array types. 1.106 + template<typename U> 1.107 + operator const Nullable< nsTArray<U> >&() const { 1.108 + // Make sure that T is ok to reinterpret to nsTArray<U> 1.109 + const nsTArray<U>& arr = mValue; 1.110 + (void)arr; 1.111 + return *reinterpret_cast<const Nullable< nsTArray<U> >*>(this); 1.112 + } 1.113 + template<typename U> 1.114 + operator const Nullable< FallibleTArray<U> >&() const { 1.115 + // Make sure that T is ok to reinterpret to FallibleTArray<U> 1.116 + const FallibleTArray<U>& arr = mValue; 1.117 + (void)arr; 1.118 + return *reinterpret_cast<const Nullable< FallibleTArray<U> >*>(this); 1.119 + } 1.120 +}; 1.121 + 1.122 +} // namespace dom 1.123 +} // namespace mozilla 1.124 + 1.125 +#endif /* mozilla_dom_Nullable_h */