aGrUM  0.16.0
DBInitializerFromCSV_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 namespace gum {
32 
33  namespace learning {
34 
35 
37  template < template < typename > class ALLOC >
39  const std::string filename,
40  bool fileContainsNames,
41  const std::string delimiter,
42  const char commentmarker,
43  const char quoteMarker,
44  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
45  IDBInitializer< ALLOC >(IDBInitializer< ALLOC >::InputType::STRING, alloc),
46  __filename(filename), __delimiter(delimiter),
47  __comment_marker(commentmarker), __quote_marker(quoteMarker),
48  __first_row_has_names(fileContainsNames),
49  __input_stream(__filename, std::ifstream::in),
50  __parser(__input_stream, delimiter, commentmarker, quoteMarker),
51  __var_names(alloc) {
52  // check that the input file was opened correctly
53  if ((__input_stream.rdstate() & std::ifstream::failbit) != 0) {
54  GUM_ERROR(IOError, "File " << filename << " not found");
55  }
56 
57  // if the first line contains names, store them into the intializer
58  if (fileContainsNames) {
59  __parser.next();
60  __var_names = __parser.current();
61  }
62 
63  GUM_CONSTRUCTOR(DBInitializerFromCSV);
64  }
65 
66 
68  template < template < typename > class ALLOC >
70  const DBInitializerFromCSV< ALLOC >& from,
71  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
72  DBInitializerFromCSV< ALLOC >(from.__filename,
73  from.__first_row_has_names,
74  from.__delimiter,
75  from.__quote_marker,
76  from.__comment_marker,
77  alloc) {}
78 
80  template < template < typename > class ALLOC >
82  const DBInitializerFromCSV< ALLOC >& from) :
83  DBInitializerFromCSV< ALLOC >(from, from.getAllocator()) {}
84 
86  template < template < typename > class ALLOC >
88  DBInitializerFromCSV< ALLOC >&& from,
89  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
90  DBInitializerFromCSV< ALLOC >(from.__filename,
91  from.__first_row_has_names,
92  from.__delimiter,
93  from.__quote_marker,
94  from.__comment_marker,
95  alloc) {}
96 
97 
99  template < template < typename > class ALLOC >
101  DBInitializerFromCSV< ALLOC >&& from) :
102  DBInitializerFromCSV< ALLOC >(std::move(from), from.getAllocator()) {}
103 
104 
106  template < template < typename > class ALLOC >
107  DBInitializerFromCSV< ALLOC >* DBInitializerFromCSV< ALLOC >::clone(
108  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) const {
109  ALLOC< DBInitializerFromCSV< ALLOC > > allocator(alloc);
110  DBInitializerFromCSV< ALLOC >* new_initializer = allocator.allocate(1);
111  try {
112  allocator.construct(new_initializer, *this, alloc);
113  } catch (...) {
114  allocator.deallocate(new_initializer, 1);
115  throw;
116  }
117 
118  return new_initializer;
119  }
120 
121 
123  template < template < typename > class ALLOC >
124  DBInitializerFromCSV< ALLOC >* DBInitializerFromCSV< ALLOC >::clone() const {
125  return clone(this->getAllocator());
126  }
127 
128 
130  template < template < typename > class ALLOC >
132  GUM_DESTRUCTOR(DBInitializerFromCSV);
133  }
134 
135 
137  template < template < typename > class ALLOC >
138  DBInitializerFromCSV< ALLOC >& DBInitializerFromCSV< ALLOC >::
139  operator=(const DBInitializerFromCSV< ALLOC >& from) {
140  if (this != &from) {
142  __filename = from.__filename;
143  __delimiter = from.__delimiter;
144  __comment_marker = from.__comment_marker;
145  __quote_marker = from.__quote_marker;
146  __first_row_has_names = from.__first_row_has_names;
147 
148  // open the CSV file
149  __input_stream.close();
150  __input_stream.open(__filename, std::ifstream::in);
151 
152  // check that the input file was opened correctly
153  if ((__input_stream.rdstate() & std::ifstream::failbit) != 0) {
154  GUM_ERROR(IOError, "File " << __filename << " not found");
155  }
156 
157  // make the parser use the new input stream
158  __parser.useNewStream(
159  __input_stream, __delimiter, __comment_marker, __quote_marker);
160 
161  // if the first line contains names, store them into the intializer
162  if (__first_row_has_names) {
163  __parser.next();
164  __var_names = __parser.current();
165  }
166  }
167 
168  return *this;
169  }
170 
171 
173  template < template < typename > class ALLOC >
174  DBInitializerFromCSV< ALLOC >& DBInitializerFromCSV< ALLOC >::
175  operator=(DBInitializerFromCSV< ALLOC >&& from) {
176  return operator=(from);
177  }
178 
179 
180  // ask the child class for the names of the variables
181  template < template < typename > class ALLOC >
182  INLINE std::vector< std::string, ALLOC< std::string > >
184  return __var_names;
185  }
186 
187 
188  // asks the child class for the content of the current row using strings
189  template < template < typename > class ALLOC >
190  INLINE const std::vector< std::string, ALLOC< std::string > >&
192  return __parser.current();
193  }
194 
195 
196  // indicates whether there is a next row to read (and point on it)
197  template < template < typename > class ALLOC >
199  return __parser.next();
200  }
201 
202  } /* namespace learning */
203 
204 } /* namespace gum */
205 
206 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual std::vector< std::string, ALLOC< std::string > > _variableNames() final
returns the names of the variables
virtual ~DBInitializerFromCSV()
destructor
DBInitializerFromCSV(const std::string filename, bool fileContainsNames=true, const std::string delimiter=",", const char commentmarker='#', const char quoteMarker='"', const allocator_type& alloc = allocator_type())
default constructor
DBInitializerFromCSV< ALLOC > & operator=(const DBInitializerFromCSV< ALLOC > &from)
copy operator
STL namespace.
allocator_type getAllocator() const
returns the allocator used
virtual DBInitializerFromCSV< ALLOC > * clone() const
virtual copy constructor
virtual bool _nextRow() final
indicates whether there is a next row to read (and point on it)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
IDBInitializer< ALLOC > & operator=(const IDBInitializer< ALLOC > &from)
copy operator
virtual const std::vector< std::string, ALLOC< std::string > > & _currentStringRow() final
returns the content of the current row using strings
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55