iCub-main
FileReaderT.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #ifndef ICUB_FILEREADERT__
20 #define ICUB_FILEREADERT__
21 
22 #include <string>
23 #include <fstream>
24 #include <stdexcept>
25 
26 namespace iCub {
27 namespace learningmachine {
28 
29 
36 template<class T>
37 class FileReaderT {
38 private:
42  std::istream* stream;
43 
47  std::ifstream* f_stream;
48 
52  void clearStream() {
53  // close file stream if present
54  if(this->f_stream != (std::ifstream*) 0) {
55  this->f_stream->close();
56  }
57  // do not need to clear normal stream, as we already clear f_stream and
58  // don't want to delete std::cin
59  delete this->f_stream;
60  this->f_stream = (std::ifstream*) 0;
61  }
62 
68  void checkStream() {
69  if(this->stream == (std::istream*) 0) {
70  throw std::runtime_error("no stream opened.");
71  }
72  }
73 
74 public:
79  stream = (std::istream*) 0;
80  f_stream = (std::ifstream*) 0;
81  }
82 
87  this->clearStream();
88  }
89 
95  virtual void open(std::istream& s) {
96  // destroy previous stream if applicable
97  this->stream = &s;
98  }
99 
105  virtual void open(const std::string& f) {
106  // clear previous filestream
107  this->clearStream();
108  // put stream on the heap!
109  this->f_stream = new std::ifstream(f.c_str());
110  if(!this->f_stream->is_open()) {
111  this->clearStream();
112  throw std::runtime_error(std::string("could not open file '" + f + "'"));
113  }
114  return this->open(*this->f_stream);
115  }
116 
123  virtual bool hasNext() {
124  this->checkStream();
125  // peek so that eof bit is set
126  this->stream->peek();
127  return !this->stream->eof();
128  }
129 
134  virtual void reset() {
135  this->stream->clear();
136  this->stream->seekg(0, std::ios::beg);
137  }
138 
148  T* getNext() {
149  // check if there are actually more items
150  if(!hasNext()) {
151  throw std::runtime_error("end of stream.");
152  }
153 
154  // read next line
155  std::string line;
156  getline(*this->stream, line);
157 
158  // construct and fill object
159  T* obj = new T();
160  obj->fromString(line.c_str());
161 
162  return obj;
163  }
164 };
165 
166 } // learningmachine
167 } // iCub
168 
169 #endif
Template class that supports reading lines of a file to object instances using a fromString(char* lin...
Definition: FileReaderT.h:37
virtual bool hasNext()
Checks whether there are more items left in the stream.
Definition: FileReaderT.h:123
virtual void reset()
Resets the stream to the beginning.
Definition: FileReaderT.h:134
~FileReaderT()
Default destructor.
Definition: FileReaderT.h:86
virtual void open(std::istream &s)
Opens the given stream for reading.
Definition: FileReaderT.h:95
T * getNext()
Returns a pointer to the next item in the file.
Definition: FileReaderT.h:148
virtual void open(const std::string &f)
Opens a stream to the given filename for reading.
Definition: FileReaderT.h:105
FileReaderT()
Default constructor.
Definition: FileReaderT.h:78
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.