File: Maybe_h.template

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (77 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// This file is generated by Maybe_h.template.

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef {{"_".join(config.protocol.namespace)}}_Maybe_h
#define {{"_".join(config.protocol.namespace)}}_Maybe_h

//#include "Forward.h"

{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}

namespace detail {
template<typename T>
class PtrMaybe {
public:
    PtrMaybe() = default;
    PtrMaybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
    PtrMaybe(PtrMaybe&& other) noexcept : m_value(std::move(other.m_value)) {}
    void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
    T* fromJust() const { DCHECK(m_value); return m_value.get(); }
    T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
    bool isJust() const { return !!m_value; }
    std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
private:
    std::unique_ptr<T> m_value;
};

template<typename T>
class ValueMaybe {
public:
    ValueMaybe() : m_isJust(false), m_value() { }
    ValueMaybe(T value) : m_isJust(true), m_value(std::move(value)) { }
    ValueMaybe(ValueMaybe&& other) noexcept
        : m_isJust(other.m_isJust),
          m_value(std::move(other.m_value)) {}
    void operator=(T value) { m_value = value; m_isJust = true; }
    const T& fromJust() const { DCHECK(m_isJust); return m_value; }
    const T& fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
    bool isJust() const { return m_isJust; }
    T takeJust() { DCHECK(m_isJust); return std::move(m_value); }
private:
    bool m_isJust;
    T m_value;
};

template <typename T>
struct MaybeTypedef { typedef PtrMaybe<T> type; };

template <>
struct MaybeTypedef<bool> { typedef ValueMaybe<bool> type; };

template <>
struct MaybeTypedef<int> { typedef ValueMaybe<int> type; };

template <>
struct MaybeTypedef<double> { typedef ValueMaybe<double> type; };

template <>
struct MaybeTypedef<String> { typedef ValueMaybe<String> type; };

template <>
struct MaybeTypedef<Binary> { typedef ValueMaybe<Binary> type; };

}  // namespace detail

template <typename T>
using Maybe = typename detail::MaybeTypedef<T>::type;

{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}

#endif // !defined({{"_".join(config.protocol.namespace)}}_Maybe_h)