11cb0ef41Sopenharmony_ci// This file is generated by Maybe_h.template.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved.
41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci// found in the LICENSE file.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#ifndef {{"_".join(config.protocol.namespace)}}_Maybe_h
81cb0ef41Sopenharmony_ci#define {{"_".join(config.protocol.namespace)}}_Maybe_h
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// This macro allows to test for the version of the GNU C++ compiler.
111cb0ef41Sopenharmony_ci// Note that this also applies to compilers that masquerade as GCC,
121cb0ef41Sopenharmony_ci// for example clang and the Intel C++ compiler for Linux.
131cb0ef41Sopenharmony_ci// Use like:
141cb0ef41Sopenharmony_ci//  #if IP_GNUC_PREREQ(4, 3, 1)
151cb0ef41Sopenharmony_ci//   ...
161cb0ef41Sopenharmony_ci//  #endif
171cb0ef41Sopenharmony_ci#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
181cb0ef41Sopenharmony_ci#define IP_GNUC_PREREQ(major, minor, patchlevel)                      \
191cb0ef41Sopenharmony_ci  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= \
201cb0ef41Sopenharmony_ci   ((major)*10000 + (minor)*100 + (patchlevel)))
211cb0ef41Sopenharmony_ci#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
221cb0ef41Sopenharmony_ci#define IP_GNUC_PREREQ(major, minor, patchlevel) \
231cb0ef41Sopenharmony_ci  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >=  \
241cb0ef41Sopenharmony_ci   ((major)*10000 + (minor)*100 + (patchlevel)))
251cb0ef41Sopenharmony_ci#else
261cb0ef41Sopenharmony_ci#define IP_GNUC_PREREQ(major, minor, patchlevel) 0
271cb0ef41Sopenharmony_ci#endif
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci#if defined(__mips64)
301cb0ef41Sopenharmony_ci#define IP_TARGET_ARCH_MIPS64 1
311cb0ef41Sopenharmony_ci#elif defined(__MIPSEB__) || defined(__MIPSEL__)
321cb0ef41Sopenharmony_ci#define IP_TARGET_ARCH_MIPS 1
331cb0ef41Sopenharmony_ci#endif
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Allowing the use of noexcept by removing the keyword on older compilers that
361cb0ef41Sopenharmony_ci// do not support adding noexcept to default members.
371cb0ef41Sopenharmony_ci#if ((IP_GNUC_PREREQ(4, 9, 0) && !defined(IP_TARGET_ARCH_MIPS) && \
381cb0ef41Sopenharmony_ci      !defined(IP_TARGET_ARCH_MIPS64)) ||                         \
391cb0ef41Sopenharmony_ci     (defined(__clang__) && __cplusplus > 201300L))
401cb0ef41Sopenharmony_ci#define IP_NOEXCEPT noexcept
411cb0ef41Sopenharmony_ci#else
421cb0ef41Sopenharmony_ci#define IP_NOEXCEPT
431cb0ef41Sopenharmony_ci#endif
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci//#include "Forward.h"
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
481cb0ef41Sopenharmony_cinamespace {{namespace}} {
491cb0ef41Sopenharmony_ci{% endfor %}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_citemplate<typename T>
521cb0ef41Sopenharmony_ciclass Maybe {
531cb0ef41Sopenharmony_cipublic:
541cb0ef41Sopenharmony_ci    Maybe() : m_value() { }
551cb0ef41Sopenharmony_ci    Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
561cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : m_value(std::move(other.m_value)) {}
571cb0ef41Sopenharmony_ci    void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
581cb0ef41Sopenharmony_ci    T* fromJust() const { DCHECK(m_value); return m_value.get(); }
591cb0ef41Sopenharmony_ci    T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
601cb0ef41Sopenharmony_ci    bool isJust() const { return !!m_value; }
611cb0ef41Sopenharmony_ci    std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
621cb0ef41Sopenharmony_ciprivate:
631cb0ef41Sopenharmony_ci    std::unique_ptr<T> m_value;
641cb0ef41Sopenharmony_ci};
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_citemplate<typename T>
671cb0ef41Sopenharmony_ciclass MaybeBase {
681cb0ef41Sopenharmony_cipublic:
691cb0ef41Sopenharmony_ci    MaybeBase() : m_isJust(false) { }
701cb0ef41Sopenharmony_ci    MaybeBase(T value) : m_isJust(true), m_value(value) { }
711cb0ef41Sopenharmony_ci    MaybeBase(MaybeBase&& other) IP_NOEXCEPT
721cb0ef41Sopenharmony_ci        : m_isJust(other.m_isJust),
731cb0ef41Sopenharmony_ci          m_value(std::move(other.m_value)) {}
741cb0ef41Sopenharmony_ci    void operator=(T value) { m_value = value; m_isJust = true; }
751cb0ef41Sopenharmony_ci    T fromJust() const { DCHECK(m_isJust); return m_value; }
761cb0ef41Sopenharmony_ci    T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
771cb0ef41Sopenharmony_ci    bool isJust() const { return m_isJust; }
781cb0ef41Sopenharmony_ci    T takeJust() { DCHECK(m_isJust); return m_value; }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciprotected:
811cb0ef41Sopenharmony_ci    bool m_isJust;
821cb0ef41Sopenharmony_ci    T m_value;
831cb0ef41Sopenharmony_ci};
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_citemplate<>
861cb0ef41Sopenharmony_ciclass Maybe<bool> : public MaybeBase<bool> {
871cb0ef41Sopenharmony_cipublic:
881cb0ef41Sopenharmony_ci    Maybe() { m_value = false; }
891cb0ef41Sopenharmony_ci    Maybe(bool value) : MaybeBase(value) { }
901cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
911cb0ef41Sopenharmony_ci    using MaybeBase::operator=;
921cb0ef41Sopenharmony_ci};
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_citemplate<>
951cb0ef41Sopenharmony_ciclass Maybe<int> : public MaybeBase<int> {
961cb0ef41Sopenharmony_cipublic:
971cb0ef41Sopenharmony_ci    Maybe() { m_value = 0; }
981cb0ef41Sopenharmony_ci    Maybe(int value) : MaybeBase(value) { }
991cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
1001cb0ef41Sopenharmony_ci    using MaybeBase::operator=;
1011cb0ef41Sopenharmony_ci};
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_citemplate<>
1041cb0ef41Sopenharmony_ciclass Maybe<double> : public MaybeBase<double> {
1051cb0ef41Sopenharmony_cipublic:
1061cb0ef41Sopenharmony_ci    Maybe() { m_value = 0; }
1071cb0ef41Sopenharmony_ci    Maybe(double value) : MaybeBase(value) { }
1081cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
1091cb0ef41Sopenharmony_ci    using MaybeBase::operator=;
1101cb0ef41Sopenharmony_ci};
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_citemplate<>
1131cb0ef41Sopenharmony_ciclass Maybe<String> : public MaybeBase<String> {
1141cb0ef41Sopenharmony_cipublic:
1151cb0ef41Sopenharmony_ci    Maybe() { }
1161cb0ef41Sopenharmony_ci    Maybe(const String& value) : MaybeBase(value) { }
1171cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
1181cb0ef41Sopenharmony_ci    using MaybeBase::operator=;
1191cb0ef41Sopenharmony_ci};
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_citemplate<>
1221cb0ef41Sopenharmony_ciclass Maybe<Binary> : public MaybeBase<Binary> {
1231cb0ef41Sopenharmony_cipublic:
1241cb0ef41Sopenharmony_ci    Maybe() { }
1251cb0ef41Sopenharmony_ci    Maybe(Binary value) : MaybeBase(value) { }
1261cb0ef41Sopenharmony_ci    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
1271cb0ef41Sopenharmony_ci    using MaybeBase::operator=;
1281cb0ef41Sopenharmony_ci};
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
1311cb0ef41Sopenharmony_ci} // namespace {{namespace}}
1321cb0ef41Sopenharmony_ci{% endfor %}
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci#undef IP_GNUC_PREREQ
1351cb0ef41Sopenharmony_ci#undef IP_TARGET_ARCH_MIPS64
1361cb0ef41Sopenharmony_ci#undef IP_TARGET_ARCH_MIPS
1371cb0ef41Sopenharmony_ci#undef IP_NOEXCEPT
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci#endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h)
140