delimited_iarchive.hpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, LABUST, UNIZG-FER
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the LABUST nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  *  Created on: 15.11.2013.
00035  *  Author: Dula Nad
00036  *********************************************************************/
00037 #ifndef DELIMITED_ISERIALIZATOR_HPP_
00038 #define DELIMITED_ISERIALIZATOR_HPP_
00039 #include <cstddef>
00040 #include <sstream>
00041 #include <boost/archive/detail/common_iarchive.hpp>
00042 
00043 namespace labust
00044 {
00045         namespace archive
00046         {
00048                 // class complete_iarchive
00049                 class delimited_iarchive :
00050                                 public boost::archive::detail::common_iarchive<delimited_iarchive>
00051                 {
00052                         // permit serialization system privileged access to permit
00053                         // implementation of inline templates for maximum speed.
00054                         friend class boost::archive::load_access;
00055 
00056                         // member template for saving primitive types.
00057                         // Specialize for any types/templates that special treatment
00058                         template<class T>
00059                         void load(T & t)
00060                         {
00061                                 //                              if (isspace(delimiter))
00062                                 //                              {
00063                                 //                                      in>>t;
00064                                 //                              }
00065                                 //                              else
00066                                 //                              {
00067 
00068                                 //This is probably more robust if wrong characters occur in the stream that directly "in>>t"
00069                                 //i.e. having 1234 ?1234 1234 will fail reading the rest correctly due to the '?' artifact.
00070                                 std::stringstream temp;
00071                                 while(in.peek() != delimiter && !in.fail())
00072                                 {
00073                                         temp.put(static_cast<char>(in.get()));
00074                                 }
00075 
00076                                 if (in.fail() && !in.eof())
00077                                 {
00078                                         boost::serialization::throw_exception(
00079                                                         boost::archive::archive_exception(
00080                                                                         boost::archive::archive_exception::input_stream_error
00081                                                         )
00082                                         );
00083                                 }
00084                                 in.get();
00085                                 temp>>t;
00086                                 //                              }
00087                 };
00088 
00089                         void load(boost::archive::class_name_type & t){}
00090 
00091                         std::istream& in;
00092                         char delimiter;
00093 
00094                 public:
00096                         // public interface used by programs that use the
00097                         // serialization library
00098 
00099                         // archives are expected to support this function
00100                         void load_binary(void *address, std::size_t count);
00101 
00102                         delimited_iarchive(std::istream& in, char delimiter = ' '):
00103                                 in(in),
00104                                 delimiter(delimiter){};
00105                 };
00106         }
00107 }
00108 
00109 /* DELIMITED_IARCHIVE_HPP_ */
00110 #endif


snippets
Author(s): Gyula Nagy
autogenerated on Mon Oct 6 2014 01:39:41