parameters.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PARAMETERS_H
21 #define LIBMESH_PARAMETERS_H
22 
23 // C++ includes
24 #include <typeinfo>
25 #include <string>
26 #include <map>
27 
28 // Local includes
29 #include "libmesh/libmesh_common.h"
31 #include "libmesh/print_trace.h"
32 
33 // C++ includes
34 #include <cstddef>
35 #include <map>
36 #include <string>
37 #include <typeinfo>
38 
39 namespace libMesh
40 {
44  template<typename P>
45  void print_helper(std::ostream& os, const P* param);
46 
47  template<typename P>
48  void print_helper(std::ostream& os, const std::vector<P>* param);
49 
60 // ------------------------------------------------------------
61 // Parameters class definition
63 {
64 public:
65 
69  Parameters () {}
70 
74  Parameters (const Parameters&);
75 
79  virtual ~Parameters ();
80 
85  virtual Parameters& operator= (const Parameters& source);
86 
92  virtual Parameters& operator+= (const Parameters& source);
93 
101  template <typename T>
102  bool have_parameter (const std::string&) const;
103 
108  template <typename T>
109  const T& get (const std::string&) const;
110 
116  template <typename T>
117  void insert (const std::string&);
118 
125  template <typename T>
126  T& set (const std::string&);
127 
132  virtual void set_attributes(const std::string&, bool /*inserted_only*/) {}
133 
137  void remove (const std::string&);
138 
142  std::size_t n_parameters () const { return _values.size(); }
143 
144 #ifdef LIBMESH_HAVE_RTTI
145 
148  template <typename T>
149  unsigned int n_parameters () const;
150 #endif // LIBMESH_HAVE_RTTI
151 
155  virtual void clear ();
156 
160  void print (std::ostream& os=libMesh::out) const;
161 
162 private:
163 
167  class Value : public ReferenceCountedObject<Value>
168  {
169  public:
170 
174  virtual ~Value() {}
175 
176 #ifdef LIBMESH_HAVE_RTTI
177 
181  virtual std::string type () const = 0;
182 #endif // LIBMESH_HAVE_RTTI
183 
188  virtual void print(std::ostream&) const = 0;
189 
194  virtual Value* clone () const = 0;
195  };
196 
197 public:
198 
203  template <typename T>
204  class Parameter : public Value
205  {
206  public:
207 
211  const T& get () const { return _value; }
212 
216  T& set () { return _value; }
217 
218 #ifdef LIBMESH_HAVE_RTTI
219 
222  virtual std::string type () const;
223 #endif // LIBMESH_HAVE_RTTI
224 
228  virtual void print(std::ostream&) const;
229 
233  virtual Value* clone () const;
234 
235  private:
240  };
241 
245  typedef std::map<std::string, Value*>::iterator iterator;
246 
250  typedef std::map<std::string, Value*>::const_iterator const_iterator;
251 
255  iterator begin();
256 
260  const_iterator begin() const;
261 
265  iterator end();
266 
270  const_iterator end() const;
271 
272 protected:
273 
277  std::map<std::string, Value*> _values;
278 
279 };
280 
281 // ------------------------------------------------------------
282 // Parameters::Parameter<> class inline methods
283 
284 // This only works with Run-Time Type Information, even though
285 // typeid(T) *should* be determinable at compile time regardless...
286 #ifdef LIBMESH_HAVE_RTTI
287 template <typename T>
288 inline
289 std::string Parameters::Parameter<T>::type () const
290 {
291  return demangle(typeid(T).name());
292 }
293 #endif
294 
295 template <typename T>
296 inline
297 void Parameters::Parameter<T>::print (std::ostream& os) const
298 {
299  // Call helper function overloaded for basic scalar and vector types
300  print_helper(os, static_cast<const T*>(&_value));
301 }
302 
303 template <typename T>
304 inline
306 {
307  // No good for Solaris C++! - BSK
308 // Parameters::Parameter<T>
309 // *copy = new Parameters::Parameter<T>;
311  *copy = new Parameter<T>;
312 
313  libmesh_assert(copy);
314 
315  copy->_value = _value;
316 
317  return copy;
318 }
319 
320 
321 // ------------------------------------------------------------
322 // Parameters class inline methods
323 inline
324 void Parameters::clear () // since this is inline we must define it
325 { // before its first use (for some compilers)
326  while (!_values.empty())
327  {
328  Parameters::iterator it = _values.begin();
329 
330  delete it->second;
331  it->second = NULL;
332 
333  _values.erase(it);
334  }
335 }
336 
337 
338 
339 inline
341 {
342  this->clear();
343 
344  return (*this += source);
345 }
346 
347 inline
349 {
350  for (Parameters::const_iterator it = source._values.begin();
351  it != source._values.end(); ++it)
352  {
353  if (_values.find(it->first) != _values.end())
354  delete _values[it->first];
355  _values[it->first] = it->second->clone();
356  }
357 
358  return *this;
359 }
360 
361 inline
363 {
364  *this = p;
365 }
366 
367 
368 
369 inline
371 {
372  this->clear ();
373 }
374 
375 
376 
377 inline
378 void Parameters::print (std::ostream& os) const
379 {
380  Parameters::const_iterator it = _values.begin();
381 
382  os << "Name\t Type\t Value\n"
383  << "---------------------\n";
384  while (it != _values.end())
385  {
386  os << " " << it->first
387 #ifdef LIBMESH_HAVE_RTTI
388  << "\t " << it->second->type()
389 #endif // LIBMESH_HAVE_RTTI
390  << "\t "; it->second->print(os);
391  os << '\n';
392 
393  ++it;
394  }
395 }
396 
397 
398 
399 // Declare this now that Paramers::print() is defined.
400 // By declaring this early we can use it in subsequent
401 // methods. Required for gcc-4.0.2 -- 11/30/2005, BSK
402 inline
403 std::ostream& operator << (std::ostream& os, const Parameters& p)
404 {
405  p.print(os);
406  return os;
407 }
408 
409 
410 
411 template <typename T>
412 inline
413 bool Parameters::have_parameter (const std::string& name) const
414 {
415  Parameters::const_iterator it = _values.find(name);
416 
417  if (it != _values.end())
418 #ifdef LIBMESH_HAVE_RTTI
419  if (dynamic_cast<const Parameter<T>*>(it->second) != NULL)
420 #else // LIBMESH_HAVE_RTTI
421  if (libmesh_cast_ptr<const Parameter<T>*>(it->second) != NULL)
422 #endif // LIBMESH_HAVE_RTTI
423  return true;
424 
425  return false;
426 }
427 
428 
429 
430 template <typename T>
431 inline
432 const T& Parameters::get (const std::string& name) const
433 {
434  if (!this->have_parameter<T>(name))
435  {
436  libMesh::err << "ERROR: no"
437 #ifdef LIBMESH_HAVE_RTTI
438  << ' ' << demangle(typeid(T).name())
439 #endif // LIBMESH_HAVE_RTTI
440  << " parameter named \""
441  << name << "\":" << std::endl
442  << *this;
443 
444  libmesh_error();
445  }
446 
447  Parameters::const_iterator it = _values.find(name);
448 
449  libmesh_assert(it != _values.end());
450  libmesh_assert(it->second);
451 
452  return libmesh_cast_ptr<Parameter<T>*>(it->second)->get();
453 }
454 
455 template <typename T>
456 inline
457 void Parameters::insert (const std::string &name)
458 {
459  if (!this->have_parameter<T>(name))
460  _values[name] = new Parameter<T>;
461 
462  set_attributes(name, true);
463 }
464 
465 
466 template <typename T>
467 inline
468 T& Parameters::set (const std::string& name)
469 {
470  if (!this->have_parameter<T>(name))
471  _values[name] = new Parameter<T>;
472 
473  set_attributes(name, false);
474 
475  return libmesh_cast_ptr<Parameter<T>*>(_values[name])->set();
476 }
477 
478 inline
479 void Parameters::remove (const std::string& name)
480 {
481  Parameters::iterator it = _values.find(name);
482 
483  if (it != _values.end())
484  {
485  delete it->second;
486  it->second = NULL;
487 
488  _values.erase(it);
489  }
490 }
491 
492 
493 
494 #ifdef LIBMESH_HAVE_RTTI
495 template <typename T>
496 inline
497 unsigned int Parameters::n_parameters () const
498 {
499  unsigned int cnt = 0;
500 
501  Parameters::const_iterator it = _values.begin();
502  const Parameters::const_iterator vals_end = _values.end();
503 
504  for (; it != vals_end; ++it)
505  if (dynamic_cast<Parameter<T>*>(it->second) != NULL)
506  cnt++;
507 
508  return cnt;
509 }
510 #endif
511 
512 inline
514 {
515  return _values.begin();
516 }
517 
518 inline
520 {
521  return _values.begin();
522 }
523 
524 inline
526 {
527  return _values.end();
528 }
529 
530 inline
532 {
533  return _values.end();
534 }
535 
536 //non-member scalar print function
537 template<typename P>
538 void print_helper(std::ostream& os, const P* param)
539 {
540  os << *param;
541 }
542 
543 //non-member vector print function
544 template<typename P>
545 void print_helper(std::ostream& os, const std::vector<P>* param)
546 {
547  for (unsigned int i=0; i<param->size(); ++i)
548  os << (*param)[i] << " ";
549 }
550 
551 } // namespace libMesh
552 
553 #endif // LIBMESH_PARAMETERS_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo