Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tests/IostreamOptional.h
2723 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/DenseHash.h"
5
#include <ostream>
6
#include <optional>
7
8
namespace std
9
{
10
11
inline std::ostream& operator<<(std::ostream& lhs, const std::nullopt_t&)
12
{
13
return lhs << "none";
14
}
15
16
template<typename T>
17
auto operator<<(std::ostream& lhs, const std::optional<T>& t) -> decltype(lhs << *t) // SFINAE to only instantiate << for supported types
18
{
19
if (t)
20
return lhs << *t;
21
else
22
return lhs << "none";
23
}
24
25
template<typename T>
26
auto operator<<(std::ostream& lhs, const std::vector<T>& t) -> decltype(lhs << t[0])
27
{
28
lhs << "{ ";
29
bool first = true;
30
for (const T& element : t)
31
{
32
if (first)
33
first = false;
34
else
35
lhs << ", ";
36
37
lhs << element;
38
}
39
40
return lhs << " }";
41
}
42
43
template<typename K, typename H, typename E>
44
auto operator<<(std::ostream& lhs, const Luau::DenseHashSet<K, H, E>& set) -> decltype(lhs << *set.begin())
45
{
46
lhs << "{ ";
47
bool first = true;
48
for (const K& element : set)
49
{
50
if (first)
51
first = false;
52
else
53
lhs << ", ";
54
55
lhs << element;
56
}
57
58
return lhs << " }";
59
}
60
61
} // namespace std
62
63