readutils.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 1999-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Tue Nov 16 14:25:40 1999
00011 // commit: $Id: readutils.h 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/io/readutils.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_IO_READUTILS_H_UTC20050626084021_DEFINED
00036 #define GROOVX_IO_READUTILS_H_UTC20050626084021_DEFINED
00037 
00038 #include "io/reader.h"
00039 
00040 #include "nub/ref.h"    // for dyn_cast
00041 
00042 #include "rutz/error.h"
00043 
00044 namespace io
00045 {
00047 namespace read_utils
00048 {
00049   rutz::fstring make_element_name(const rutz::fstring& seq_name,
00050                                   int element_num);
00051 
00052   rutz::fstring make_seq_length_name(const rutz::fstring& seq_name);
00053 
00055   inline int read_sequence_length(io::reader& reader,
00056                                   const rutz::fstring& seq_name)
00057     {
00058       int count = reader.read_int(make_seq_length_name(seq_name));
00059       if (0 > count)
00060         throw rutz::error("read negative value for sequence count",
00061                           SRC_POS);
00062       return count;
00063     }
00064 
00070   template <class T, class Inserter>
00071   inline void read_value_seq(io::reader& reader,
00072                              const rutz::fstring& seq_name,
00073                              Inserter inserter, int known_count = -1)
00074     {
00075       int count = (known_count == -1) ?
00076         read_sequence_length(reader, seq_name) : known_count;
00077 
00078       for (int i = 0; i < count; ++i)
00079         {
00080           T temp;
00081           reader.read_value(make_element_name(seq_name, i), temp);
00082           *inserter = temp;
00083           ++inserter;
00084         }
00085     }
00086 
00092   template <class T, class Inserter>
00093   inline void read_value_obj_seq(io::reader& reader,
00094                                  const rutz::fstring& seq_name,
00095                                  Inserter inserter, int known_count = -1)
00096     {
00097       int count = (known_count == -1) ?
00098         read_sequence_length(reader, seq_name) : known_count;
00099 
00100       for (int i = 0; i < count; ++i)
00101         {
00102           T temp;
00103           reader.read_value_obj(make_element_name(seq_name, i), temp);
00104           *inserter = temp;
00105           ++inserter;
00106         }
00107     }
00108 
00114   template <class C, class Inserter>
00115   inline void read_object_seq(io::reader& reader,
00116                               const rutz::fstring& seq_name,
00117                               Inserter inserter, int known_count = -1)
00118     {
00119       int count = (known_count == -1) ?
00120         read_sequence_length(reader, seq_name) : known_count;
00121 
00122       for (int i = 0; i < count; ++i)
00123         {
00124           *inserter = dyn_cast<C>(
00125                  reader.read_object(make_element_name(seq_name, i)));
00126 
00127           ++inserter;
00128         }
00129     }
00130 
00131 }} // end namespace io::read_utils
00132 
00133 static const char __attribute__((used)) vcid_groovx_io_readutils_h_utc20050626084021[] = "$Id: readutils.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00134 #endif // !GROOVX_IO_READUTILS_H_UTC20050626084021_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:39 2008 by Doxygen version 1.5.5.