0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd::util: Deinline various utils; minor cleanup.

This commit is contained in:
Jason Volk 2018-10-25 13:39:41 -07:00
parent 9ad3a0720c
commit 13f38782f5
6 changed files with 194 additions and 166 deletions

View file

@ -15,24 +15,3 @@ namespace ircd::util
{
string_view getenv(const string_view &);
}
inline ircd::string_view
ircd::util::getenv(const string_view &key)
{
if(unlikely(size(key) > 127))
throw std::runtime_error
{
"getenv(): variable key is too long."
};
// Ensure the key is null terminated for the std:: call.
char keystr[128];
const size_t len{copy(keystr, key)};
keystr[std::min(len, sizeof(keystr) - 1)] = '\0';
const string_view var
{
std::getenv(keystr)
};
return var;
}

View file

@ -15,8 +15,15 @@
// String generating patterns
//
namespace ircd {
namespace util {
namespace ircd::util
{
std::string string(const char *const &buf, const size_t &size);
std::string string(const uint8_t *const &buf, const size_t &size);
std::string string(const size_t &size, const std::function<size_t (const mutable_buffer &)> &closure);
std::string string(const size_t &size, const std::function<string_view (const mutable_buffer &)> &closure);
template<class T> std::string string(const mutable_buffer &buf, const T &s);
template<class T> std::string string(const T &s);
}
/// This is the ubiquitous ircd::string() template serving as the "toString()"
/// for the project. Particpating types that want to have a string(T)
@ -24,8 +31,8 @@ namespace util {
/// this is primarily for debug strings, not meant for performance or service.
///
template<class T>
auto
string(const T &s)
std::string
ircd::util::string(const T &s)
{
std::stringstream ss;
ss << s;
@ -35,67 +42,12 @@ string(const T &s)
/// Alternative to ircd::string() using a provided buffer for the
/// std::stringstream to avoid allocating one.
template<class T>
auto
string(const mutable_buffer &buf,
const T &s)
std::string
ircd::util::string(const mutable_buffer &buf,
const T &s)
{
std::stringstream ss;
pubsetbuf(ss, buf);
ss << s;
return ss.str();
}
inline auto
string(const char *const &buf,
const size_t &size)
{
return std::string{buf, size};
}
inline auto
string(const uint8_t *const &buf,
const size_t &size)
{
return string(reinterpret_cast<const char *>(buf), size);
}
/// Close over the common pattern to write directly into a post-C++11 standard
/// string through the data() member requiring a const_cast. Closure returns
/// the final size of the data written into the buffer.
inline auto
string(const size_t &size,
const std::function<size_t (const mutable_buffer &)> &closure)
{
std::string ret(size, char{});
const mutable_buffer buf
{
const_cast<char *>(ret.data()), ret.size()
};
const size_t consumed
{
closure(buf)
};
assert(consumed <= buffer::size(buf));
data(buf)[consumed] = '\0';
ret.resize(consumed);
return ret;
}
/// Close over the common pattern to write directly into a post-C++11 standard
/// string through the data() member requiring a const_cast. Closure returns
/// a view of the data actually written to the buffer.
inline auto
string(const size_t &size,
const std::function<string_view (const mutable_buffer &)> &closure)
{
return string(size, [&closure]
(const mutable_buffer &buffer)
{
return ircd::size(closure(buffer));
});
}
} // namespace util
} // namespace ircd

View file

@ -21,8 +21,8 @@ struct ircd::util::timer
IRCD_OVERLOAD(nostart)
using clock = std::chrono::steady_clock;
nanoseconds accumulator;
clock::time_point start;
nanoseconds accumulator {0ns};
clock::time_point start {clock::now()};
bool stopped() const;
template<class duration = std::chrono::seconds> duration get() const;
@ -32,57 +32,15 @@ struct ircd::util::timer
timer(const std::function<void ()> &);
timer(nostart_t);
timer();
timer() = default;
};
/// Default construction will start the timer.
inline
ircd::util::timer::timer()
:accumulator{0ns}
,start{clock::now()}
{
}
/// timer(timer::nostart)
inline
ircd::util::timer::timer(nostart_t)
:accumulator{0ns}
,start{clock::time_point::min()}
:start{clock::time_point::min()}
{
}
inline
ircd::util::timer::timer(const std::function<void ()> &func)
:timer{}
{
func();
stop();
}
inline void
ircd::util::timer::stop()
{
if(stopped())
return;
const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
start = clock::time_point::min();
}
inline void
ircd::util::timer::cont()
{
if(!stopped())
{
const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
}
start = clock::now();
}
template<class duration>
duration
ircd::util::timer::at()
@ -99,10 +57,3 @@ const
{
return std::chrono::duration_cast<duration>(accumulator);
}
inline bool
ircd::util::timer::stopped()
const
{
return start == clock::time_point::min();
}

View file

@ -70,15 +70,7 @@ using custom_ptr = std::unique_ptr<T, std::function<void (T *)>>;
// Misc size() participants.
//
inline size_t
size(std::ostream &s)
{
const auto cur(s.tellp());
s.seekp(0, std::ios::end);
const auto ret(s.tellp());
s.seekp(cur, std::ios::beg);
return ret;
}
size_t size(std::ostream &s);
template<size_t SIZE>
constexpr size_t

View file

@ -15,23 +15,3 @@ namespace ircd::util
{
string_view what(const std::exception_ptr eptr = std::current_exception()) noexcept;
}
/// Get what() from exception_ptr
///
inline ircd::string_view
ircd::util::what(const std::exception_ptr eptr)
noexcept try
{
if(likely(eptr))
std::rethrow_exception(eptr);
return {};
}
catch(const std::exception &e)
{
return e.what();
}
catch(...)
{
return {};
}

View file

@ -8,6 +8,55 @@
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
///////////////////////////////////////////////////////////////////////////////
//
// util/util.h
//
size_t
ircd::util::size(std::ostream &s)
{
const auto cur(s.tellp());
s.seekp(0, std::ios::end);
const auto ret(s.tellp());
s.seekp(cur, std::ios::beg);
return ret;
}
///////////////////////////////////////////////////////////////////////////////
//
// util/env.h
//
ircd::string_view
ircd::util::getenv(const string_view &key)
{
thread_local char keystr[128];
if(unlikely(size(key) >= sizeof(keystr)))
throw error
{
"getenv(): variable key is too long."
};
// Ensure the key is null terminated for the std:: call.
const size_t len
{
strlcpy(keystr, key)
};
const string_view var
{
std::getenv(keystr)
};
return var;
}
///////////////////////////////////////////////////////////////////////////////
//
// util/pretty.h
//
//
// Human readable time suite
//
@ -188,8 +237,108 @@ ircd::util::si(const uint64_t &value)
};
}
///////////////////////////////////////////////////////////////////////////////
//
// binary <-> hex suite
// util/string.h
//
/// Close over the common pattern to write directly into a post-C++11 standard
/// string through the data() member requiring a const_cast. Closure returns
/// a view of the data actually written to the buffer.
std::string
ircd::util::string(const size_t &size,
const std::function<string_view (const mutable_buffer &)> &closure)
{
return string(size, [&closure]
(const mutable_buffer &buffer)
{
return ircd::size(closure(buffer));
});
}
/// Close over the common pattern to write directly into a post-C++11 standard
/// string through the data() member requiring a const_cast. Closure returns
/// the final size of the data written into the buffer.
std::string
ircd::util::string(const size_t &size,
const std::function<size_t (const mutable_buffer &)> &closure)
{
std::string ret(size, char{});
const mutable_buffer buf
{
const_cast<char *>(ret.data()), ret.size()
};
const size_t consumed
{
closure(buf)
};
assert(consumed <= buffer::size(buf));
data(buf)[consumed] = '\0';
ret.resize(consumed);
return ret;
}
std::string
ircd::util::string(const char *const &buf,
const size_t &size)
{
return std::string{buf, size};
}
std::string
ircd::util::string(const uint8_t *const &buf,
const size_t &size)
{
return string(reinterpret_cast<const char *>(buf), size);
}
///////////////////////////////////////////////////////////////////////////////
//
// util/timer.h
//
ircd::util::timer::timer(const std::function<void ()> &func)
:timer{}
{
func();
stop();
}
void
ircd::util::timer::stop()
{
if(stopped())
return;
const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
start = clock::time_point::min();
}
void
ircd::util::timer::cont()
{
if(!stopped())
{
const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
}
start = clock::now();
}
bool
ircd::util::timer::stopped()
const
{
return start == clock::time_point::min();
}
///////////////////////////////////////////////////////////////////////////////
//
// util/u2a.h
//
std::string
@ -237,3 +386,28 @@ ircd::util::a2u(const mutable_buffer &out,
return { data(out), len };
}
///////////////////////////////////////////////////////////////////////////////
//
// util/what.h
//
/// Get what() from exception_ptr
///
ircd::string_view
ircd::util::what(const std::exception_ptr eptr)
noexcept try
{
if(likely(eptr))
std::rethrow_exception(eptr);
return {};
}
catch(const std::exception &e)
{
return e.what();
}
catch(...)
{
return {};
}