• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This file is generated by Maybe_h.template.
2
3// Copyright 2016 The Chromium Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7#ifndef {{"_".join(config.protocol.namespace)}}_Maybe_h
8#define {{"_".join(config.protocol.namespace)}}_Maybe_h
9
10// This macro allows to test for the version of the GNU C++ compiler.
11// Note that this also applies to compilers that masquerade as GCC,
12// for example clang and the Intel C++ compiler for Linux.
13// Use like:
14//  #if IP_GNUC_PREREQ(4, 3, 1)
15//   ...
16//  #endif
17#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
18#define IP_GNUC_PREREQ(major, minor, patchlevel)                      \
19  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= \
20   ((major)*10000 + (minor)*100 + (patchlevel)))
21#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
22#define IP_GNUC_PREREQ(major, minor, patchlevel) \
23  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >=  \
24   ((major)*10000 + (minor)*100 + (patchlevel)))
25#else
26#define IP_GNUC_PREREQ(major, minor, patchlevel) 0
27#endif
28
29#if defined(__mips64)
30#define IP_TARGET_ARCH_MIPS64 1
31#elif defined(__MIPSEB__) || defined(__MIPSEL__)
32#define IP_TARGET_ARCH_MIPS 1
33#endif
34
35// Allowing the use of noexcept by removing the keyword on older compilers that
36// do not support adding noexcept to default members.
37#if ((IP_GNUC_PREREQ(4, 9, 0) && !defined(IP_TARGET_ARCH_MIPS) && \
38      !defined(IP_TARGET_ARCH_MIPS64)) ||                         \
39     (defined(__clang__) && __cplusplus > 201300L))
40#define IP_NOEXCEPT noexcept
41#else
42#define IP_NOEXCEPT
43#endif
44
45//#include "Forward.h"
46
47{% for namespace in config.protocol.namespace %}
48namespace {{namespace}} {
49{% endfor %}
50
51template<typename T>
52class Maybe {
53public:
54    Maybe() : m_value() { }
55    Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
56    Maybe(Maybe&& other) IP_NOEXCEPT : m_value(std::move(other.m_value)) {}
57    void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
58    T* fromJust() const { DCHECK(m_value); return m_value.get(); }
59    T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
60    bool isJust() const { return !!m_value; }
61    std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
62private:
63    std::unique_ptr<T> m_value;
64};
65
66template<typename T>
67class MaybeBase {
68public:
69    MaybeBase() : m_isJust(false) { }
70    MaybeBase(T value) : m_isJust(true), m_value(value) { }
71    MaybeBase(MaybeBase&& other) IP_NOEXCEPT
72        : m_isJust(other.m_isJust),
73          m_value(std::move(other.m_value)) {}
74    void operator=(T value) { m_value = value; m_isJust = true; }
75    T fromJust() const { DCHECK(m_isJust); return m_value; }
76    T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
77    bool isJust() const { return m_isJust; }
78    T takeJust() { DCHECK(m_isJust); return m_value; }
79
80protected:
81    bool m_isJust;
82    T m_value;
83};
84
85template<>
86class Maybe<bool> : public MaybeBase<bool> {
87public:
88    Maybe() { m_value = false; }
89    Maybe(bool value) : MaybeBase(value) { }
90    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
91    using MaybeBase::operator=;
92};
93
94template<>
95class Maybe<int> : public MaybeBase<int> {
96public:
97    Maybe() { m_value = 0; }
98    Maybe(int value) : MaybeBase(value) { }
99    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
100    using MaybeBase::operator=;
101};
102
103template<>
104class Maybe<double> : public MaybeBase<double> {
105public:
106    Maybe() { m_value = 0; }
107    Maybe(double value) : MaybeBase(value) { }
108    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
109    using MaybeBase::operator=;
110};
111
112template<>
113class Maybe<String> : public MaybeBase<String> {
114public:
115    Maybe() { }
116    Maybe(const String& value) : MaybeBase(value) { }
117    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
118    using MaybeBase::operator=;
119};
120
121template<>
122class Maybe<Binary> : public MaybeBase<Binary> {
123public:
124    Maybe() { }
125    Maybe(Binary value) : MaybeBase(value) { }
126    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
127    using MaybeBase::operator=;
128};
129
130{% for namespace in config.protocol.namespace %}
131} // namespace {{namespace}}
132{% endfor %}
133
134#undef IP_GNUC_PREREQ
135#undef IP_TARGET_ARCH_MIPS64
136#undef IP_TARGET_ARCH_MIPS
137#undef IP_NOEXCEPT
138
139#endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h)
140