io: move view conversion operator to named function

This commit is contained in:
Danny Robson 2016-06-28 15:58:41 +10:00
parent fef1dc8300
commit bf5cab6156
7 changed files with 57 additions and 25 deletions

View File

@ -21,6 +21,8 @@
#include "view.hpp"
#include <type_traits>
#include <sys/mman.h>
#include <fcntl.h>
@ -50,8 +52,13 @@ namespace util {
const uint8_t* cbegin (void) const &;
const uint8_t* cend (void) const &;
template <typename T> operator util::view<const T *restrict> () const &;
template <typename T> operator util::view< T *restrict> () &;
template <typename T>
util::view<std::add_const_t<T>*>
as_view () const &;
template <typename T>
util::view<T*>
as_view () &;
private:
fd m_fd;

View File

@ -20,24 +20,28 @@
#define __UTIL_IO_POSIX_IPP
#include "./pointer.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename T>
util::detail::posix::mapped_file::operator util::view<const T *restrict> () const &
util::view<std::add_const_t<T>*>
util::detail::posix::mapped_file::as_view (void) const&
{
return {
reinterpret_cast<const T*restrict> (cbegin ()),
reinterpret_cast<const T*restrict> (cend ())
reinterpret_cast<const T*> (cbegin ()),
reinterpret_cast<const T*> (align (cend (), alignof (T)))
};
}
//-----------------------------------------------------------------------------
template <typename T>
util::detail::posix::mapped_file::operator util::view<T *restrict> () &
util::view<T*>
util::detail::posix::mapped_file::as_view (void) &
{
return {
reinterpret_cast<T* restrict> (begin ()),
reinterpret_cast<T* restrict> (end ())
reinterpret_cast<T *> (begin ()),
reinterpret_cast<T *> (align (end (), alignof(T)))
};
}

View File

@ -60,8 +60,13 @@ namespace util {
const uint8_t* cbegin (void) const &;
const uint8_t* cend (void) const &;
template <typename T> operator util::view<const T *restrict> () const &;
template <typename T> operator util::view< T *restrict> () &;
template <typename T>
util::view<std::add_const_t<T>*>
as_view () const &;
template <typename T>
util::view<T*>
as_view () &;
private:
::util::win32::handle m_file;

View File

@ -21,24 +21,28 @@
#define __UTIL_IO_WIN32_IPP
#include "pointer.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename T>
util::detail::win32::mapped_file::operator util::view<const T *restrict> () const &
util::view<std::add_const_t<T>*>
util::detail::win32::mapped_file::as_view (void) const&
{
return {
reinterpret_cast<const T*restrict> (cbegin ()),
reinterpret_cast<const T*restrict> (cend ())
reinterpret_cast<const T*> (cbegin ()),
reinterpret_cast<const T*> (align (cend (), alignof (T)))
};
}
//-----------------------------------------------------------------------------
template <typename T>
util::detail::win32::mapped_file::operator util::view<T *restrict> () &
util::view<T*>
util::detail::win32::mapped_file::as_view (void) &
{
return {
reinterpret_cast<T* restrict> (begin ()),
reinterpret_cast<T* restrict> (end ())
reinterpret_cast<T *> (begin ()),
reinterpret_cast<T *> (align (end (), alignof(T)))
};
}

View File

@ -11,9 +11,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "io.hpp"
#include "json/except.hpp"
#include "json/tree.hpp"
@ -22,6 +23,8 @@
#include <boost/filesystem/path.hpp>
///////////////////////////////////////////////////////////////////////////////
enum {
ARG_COMMAND,
ARG_INPUT,
@ -30,23 +33,27 @@ enum {
};
//-----------------------------------------------------------------------------
void
print_usage (int argc, char **argv) {
print_usage (int argc, char **argv)
{
(void)argc;
std::cerr << "usage: " << argv[0] << " <input>\n";
}
//-----------------------------------------------------------------------------
int
main (int argc, char **argv) {
main (int argc, char **argv)
{
if (argc != NUM_ARGS) {
print_usage (argc, argv);
return EXIT_FAILURE;
}
try {
boost::filesystem::path input (argv[ARG_INPUT]);
std::cout << *json::tree::parse (input) << "\n";
const util::mapped_file src (argv[ARG_INPUT]);
std::cout << *json::tree::parse (src.as_view<const char> ()) << '\n';
} catch (const json::error& err) {
std::cerr << err.what () << "\n";
return EXIT_FAILURE;

View File

@ -20,6 +20,8 @@
#include "json/schema.hpp"
#include "json/except.hpp"
#include "io.hpp"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
@ -41,8 +43,11 @@ main (int argc, char **argv) {
}
try {
auto schema = json::tree::parse (fs::path (argv[ARG_SCHEMA]));
auto input = json::tree::parse (fs::path (argv[ARG_INPUT]));
const util::mapped_file schema_src (argv[ARG_SCHEMA]);
const util::mapped_file input_src (argv[ARG_INPUT]);
auto schema = json::tree::parse (schema_src.as_view<const char> ());
auto input = json::tree::parse (input_src.as_view<const char> ());
json::schema::validate (*input, schema->as_object ());
} catch (const json::error &e) {

View File

@ -42,8 +42,8 @@ main (int argc, char ** argv) {
}
try {
util::mapped_file data (argv[ARG_PATH]);
json::flat::parse (data.operator util::view<const char*restrict> ());
const util::mapped_file data (argv[ARG_PATH]);
json::flat::parse (data.as_view<const char> ());
} catch (const json::error &x) {
std::cerr << "error: " << x.what () << '\n';
return EXIT_FAILURE;