io: remove redundant write overloads

This commit is contained in:
Danny Robson 2015-10-29 10:48:54 +11:00
parent 73720feceb
commit cb12d4fd1f
7 changed files with 15 additions and 32 deletions

13
io.cpp
View File

@ -73,18 +73,13 @@ util::slurp (const boost::filesystem::path& path) {
//-----------------------------------------------------------------------------
template <typename T>
void
util::write (const fd &out,
const T *restrict first,
const T *restrict last)
const void *restrict data,
size_t bytes)
{
CHECK (first);
CHECK (last);
CHECK_LE (first, last);
const char *restrict cursor = reinterpret_cast<const char*> (first);
size_t remaining = sizeof (T) * (last - first);
const char *restrict cursor = reinterpret_cast<const char*> (data);
size_t remaining = bytes;
while (remaining) {
ssize_t consumed = ::write (out, cursor, remaining);

7
io.hpp
View File

@ -74,8 +74,7 @@ namespace util {
slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&);
template <typename T>
void write (const fd&, const T *restrict data, size_t count);
void write (const fd&, const void *restrict data, size_t bytes);
template <typename T>
void write (const fd&, const T &data);
@ -83,10 +82,6 @@ namespace util {
template <typename T>
void write (const fd&, const T *restrict first, const T *restrict last);
template <typename T>
void write (const boost::filesystem::path &, const T *restrict first, const T *restrict last);
//-------------------------------------------------------------------------
class indenter : public std::streambuf {
protected:

18
io.ipp
View File

@ -9,26 +9,18 @@ namespace util {
//-------------------------------------------------------------------------
template <typename T>
void
write (const fd &_fd, const T *restrict data, size_t count)
write (const fd &_fd, const T &data)
{
auto first = reinterpret_cast<const char*> (data);
write (_fd, first, first + sizeof (T) * count);
write (_fd, &data, sizeof (T));
}
//-------------------------------------------------------------------------
template <typename T>
void
write (const fd &_fd, const T &data)
write (const fd &_fd, const T *restrict first, const T *restrict last)
{
write (_fd, &data, 1);
}
//-------------------------------------------------------------------------
inline void
write (const fd &_fd, const void *_data, size_t _bytes)
{
auto data = reinterpret_cast<const uint8_t*> (_data);
write (_fd, data, data + _bytes);
write (_fd, first, (last - first) * sizeof (T));
}
//-------------------------------------------------------------------------

View File

@ -34,7 +34,8 @@ mapped_file::mapped_file (const char *_path, int fflags, int mflags):
//----------------------------------------------------------------------------
mapped_file::~mapped_file () {
mapped_file::~mapped_file ()
{
CHECK (m_data != NULL);
munmap (m_data, m_size);
}

View File

@ -169,7 +169,7 @@ json::flat::parse (const char *first, const char *last)
std::vector<json::flat::item>
json::flat::parse (const boost::filesystem::path &path)
{
util::mapped_file f (path);
util::mapped_file f (path.string ().c_str ());
return parse ((const char *)f.cbegin (), (const char*)f.cend ());
}

View File

@ -170,7 +170,7 @@ parse (std::vector<json::flat::item>::const_iterator first,
std::unique_ptr<json::tree::node>
json::tree::parse (const boost::filesystem::path &path)
{
util::mapped_file f (path);
util::mapped_file f (path.string ().c_str ());
return parse ((const char*)f.cbegin (), (const char*)f.cend ());
}

View File

@ -28,7 +28,7 @@
util::image::buffer<1,uint8_t>
util::pgm::read (const boost::filesystem::path &path)
{
util::mapped_file raw (path);
util::mapped_file raw (path.string ().c_str ());
std::ifstream cooked (path.string (), std::ios::binary);
char magic[2];