-
-
Notifications
You must be signed in to change notification settings - Fork 542
Expand file tree
/
Copy pathstandard_streams.cpp
More file actions
157 lines (136 loc) · 5.11 KB
/
standard_streams.cpp
File metadata and controls
157 lines (136 loc) · 5.11 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2011 Bryce Lelbach
// Copyright (c) 2011-2021 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <hpx/config.hpp>
#include <hpx/actions_base/plain_action.hpp>
#include <hpx/async_distributed/base_lco_with_value.hpp>
#include <hpx/components_base/agas_interface.hpp>
#include <hpx/components_base/server/component.hpp>
#include <hpx/components_base/server/create_component.hpp>
#include <hpx/modules/async_base.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/execution.hpp>
#include <hpx/modules/functional.hpp>
#include <hpx/runtime_distributed/runtime_fwd.hpp>
#include <hpx/components/iostreams/ostream.hpp>
#include <hpx/components/iostreams/standard_streams.hpp>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <type_traits>
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace iostreams { namespace detail {
std::ostream& get_coutstream() noexcept
{
return std::cout;
}
std::ostream& get_cerrstream() noexcept
{
return std::cerr;
}
std::stringstream& get_consolestream()
{
static std::stringstream console_stream;
return console_stream;
}
///////////////////////////////////////////////////////////////////////////
hpx::id_type return_id_type(future<bool> f, hpx::id_type id)
{
f.get(); //re-throw any errors
return id;
}
///////////////////////////////////////////////////////////////////////////
hpx::future<hpx::id_type> create_ostream(
char const* cout_name, std::ostream& strm)
{
LRT_(info).format(
"detail::create_ostream: creating '{}' stream object", cout_name);
if (agas::is_console())
{
typedef components::component<server::output_stream> ostream_type;
hpx::id_type cout_id(
components::server::construct<ostream_type>(std::ref(strm)),
hpx::id_type::management_type::managed);
return agas::register_name(cout_name, cout_id)
.then(hpx::launch::sync,
hpx::bind_back(&return_id_type, cout_id));
}
// the console locality will create the ostream during startup
return agas::on_symbol_namespace_event(cout_name, true);
}
///////////////////////////////////////////////////////////////////////////
void release_ostream(char const* name, hpx::id_type const& /* id */)
{
LRT_(info).format(
"detail::release_ostream: destroying '{}' stream object", name);
if (agas::is_console())
{
// now unregister the object from AGAS
agas::unregister_name(launch::sync, name);
}
}
}}} // namespace hpx::iostreams::detail
namespace hpx { namespace iostreams {
// force the creation of the singleton stream objects
void create_cout()
{
if (!agas::is_console())
{
HPX_THROW_EXCEPTION(hpx::error::service_unavailable,
"hpx::iostreams::create_cout",
"this function should be called on the console only");
}
detail::create_ostream(detail::cout_tag());
}
void create_cerr()
{
if (!agas::is_console())
{
HPX_THROW_EXCEPTION(hpx::error::service_unavailable,
"hpx::iostreams::create_cerr",
"this function should be called on the console only");
}
detail::create_ostream(detail::cerr_tag());
}
void create_consolestream()
{
if (!agas::is_console())
{
HPX_THROW_EXCEPTION(hpx::error::service_unavailable,
"hpx::iostreams::create_consolestream",
"this function should be called on the console only");
}
detail::create_ostream(detail::consolestream_tag());
}
std::stringstream const& get_consolestream()
{
if (get_runtime_ptr() != nullptr && !agas::is_console())
{
HPX_THROW_EXCEPTION(hpx::error::service_unavailable,
"hpx::iostreams::get_consolestream",
"this function should be called on the console only");
}
return detail::get_consolestream();
}
}} // namespace hpx::iostreams
///////////////////////////////////////////////////////////////////////////////
HPX_PLAIN_ACTION(hpx::iostreams::create_cout, create_cout_action)
HPX_PLAIN_ACTION(hpx::iostreams::create_cerr, create_cerr_action)
HPX_PLAIN_ACTION(
hpx::iostreams::create_consolestream, create_consolestream_action)
///////////////////////////////////////////////////////////////////////////////
namespace hpx {
// global standard ostream objects
iostreams::ostream<> cout;
iostreams::ostream<> cerr;
// extension: singleton stringstream on console
iostreams::ostream<> consolestream;
std::stringstream const& get_consolestream()
{
return iostreams::get_consolestream();
}
} // namespace hpx