0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-14 22:11:06 +01:00
construct/include/ircd/util/pubsetbuf.h

113 lines
2.1 KiB
C
Raw Normal View History

2018-04-25 01:47:19 +02:00
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_UTIL_PUBSETBUF_H
//
// stringstream buffer set macros
//
namespace ircd {
inline namespace util {
2018-04-25 01:47:19 +02:00
inline std::stringbuf &
pubsetbuf(std::stringbuf &sb,
const mutable_buffer &buf)
{
sb.pubsetbuf(data(buf), size(buf));
return sb;
}
2018-04-25 01:47:19 +02:00
template<class stringstream>
inline stringstream &
2018-04-25 01:47:19 +02:00
pubsetbuf(stringstream &ss,
const mutable_buffer &buf)
{
ss.rdbuf()->pubsetbuf(data(buf), size(buf));
return ss;
}
template<class stringstream>
inline stringstream &
2018-04-25 01:47:19 +02:00
pubsetbuf(stringstream &ss,
std::string &s)
{
auto *const &data
{
mutable_cast(s.data())
2018-04-25 01:47:19 +02:00
};
ss.rdbuf()->pubsetbuf(data, s.size());
return ss;
}
template<class stringstream>
inline stringstream &
2018-04-25 01:47:19 +02:00
pubsetbuf(stringstream &ss,
std::string &s,
const size_t &size)
{
s.resize(size, char{});
return pubsetbuf(ss, s);
}
template<class stringstream>
inline stringstream &
2018-04-25 01:47:19 +02:00
resizebuf(stringstream &ss,
std::string &s)
{
const auto pos(ss.tellp());
assert(pos >= 0);
s.resize(pos);
2018-04-25 01:47:19 +02:00
return ss;
}
#if RB_CXX_EPOCH >= 11 || defined(__clang__)
/// since c++20
inline string_view
view(std::stringbuf &sb,
const const_buffer &buf = {})
{
const string_view ret
{
sb.view()
};
assert(!buf || data(ret) == data(buf));
return ret;
}
#endif
2018-04-25 01:47:19 +02:00
/// buf has to match the rdbuf you gave the stringstream
template<class stringstream>
inline string_view
2018-04-25 01:47:19 +02:00
view(stringstream &ss,
const const_buffer &buf)
{
const auto tell
{
std::min(size_t(ss.tellp()), size(buf))
};
2018-04-25 01:47:19 +02:00
ss.flush();
ss.rdbuf()->pubsync();
const string_view ret
{
data(buf), tell
2018-04-25 01:47:19 +02:00
};
assert(size(ret) <= size(buf));
return ret;
}
} // namespace util
} // namespace ircd