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 void
util::write (const fd &out, util::write (const fd &out,
const T *restrict first, const void *restrict data,
const T *restrict last) size_t bytes)
{ {
CHECK (first); const char *restrict cursor = reinterpret_cast<const char*> (data);
CHECK (last); size_t remaining = bytes;
CHECK_LE (first, last);
const char *restrict cursor = reinterpret_cast<const char*> (first);
size_t remaining = sizeof (T) * (last - first);
while (remaining) { while (remaining) {
ssize_t consumed = ::write (out, cursor, 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&); slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&);
template <typename T> void write (const fd&, const void *restrict data, size_t bytes);
void write (const fd&, const T *restrict data, size_t count);
template <typename T> template <typename T>
void write (const fd&, const T &data); void write (const fd&, const T &data);
@ -83,10 +82,6 @@ namespace util {
template <typename T> template <typename T>
void write (const fd&, const T *restrict first, const T *restrict last); 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 { class indenter : public std::streambuf {
protected: protected:

18
io.ipp
View File

@ -9,26 +9,18 @@ namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename T> template <typename T>
void 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, &data, sizeof (T));
write (_fd, first, first + sizeof (T) * count);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename T> template <typename T>
void void
write (const fd &_fd, const T &data) write (const fd &_fd, const T *restrict first, const T *restrict last)
{ {
write (_fd, &data, 1); write (_fd, first, (last - first) * sizeof (T));
}
//-------------------------------------------------------------------------
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);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------

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); CHECK (m_data != NULL);
munmap (m_data, m_size); 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> std::vector<json::flat::item>
json::flat::parse (const boost::filesystem::path &path) 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 ()); 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> std::unique_ptr<json::tree::node>
json::tree::parse (const boost::filesystem::path &path) 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 ()); return parse ((const char*)f.cbegin (), (const char*)f.cend ());
} }

View File

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