Program listing for file kernel/src/utils/SiconosTools/Tools.hpp

Program listing for file kernel/src/utils/SiconosTools/Tools.hpp#

  1#ifndef TOOLS_H
  2#define TOOLS_H
  3
  4#include<string>
  5#include <sstream>
  6#include <vector>
  7#include <deque>
  8
  9#include "SiconosPointers.hpp"
 10
 11
 12TYPEDEF_TPL1_SPTR(UnsignedIntVector, std::vector, unsigned int)
 13
 14
 15typedef std::deque<bool> AllocationFlags;
 16
 17
 18
 19template <class T> std::string toString(const T& obj)
 20{
 21  static std::ostringstream o;
 22  o.str("");
 23  o << obj ;
 24  return o.str();
 25}
 26
 27
 28
 29template<class Seq> void purge(Seq& c)
 30{
 31  typename Seq::iterator i;
 32  for (i = c.begin(); i != c.end(); ++i)
 33  {
 34    delete *i;
 35    *i = nullptr;
 36  }
 37}
 38
 39
 40template<class InpIt> void purge(InpIt begin, InpIt end)
 41{
 42  while (begin != end)
 43  {
 44    delete *begin;
 45    *begin = nullptr;
 46    ++begin;
 47  }
 48}
 49
 50
 51template<class Seq> void purge(Seq& c, const std::vector<bool>& isAllocatedIn)
 52{
 53  typename Seq::iterator i;
 54  std::vector<bool>::const_iterator it = isAllocatedIn.begin();
 55  for (i = c.begin(); i != c.end(); ++i)
 56  {
 57    if (*it ++) delete *i;
 58    *i = nullptr;
 59  }
 60}
 61
 62
 63template<class InpIt> void purge(InpIt begin, InpIt end, const std::vector<bool>& isAllocatedIn)
 64{
 65  std::vector<bool>::const_iterator it = isAllocatedIn.begin();
 66  while (begin != end)
 67  {
 68    if (*it ++) delete *begin;
 69    *begin = nullptr;
 70    ++begin;
 71  }
 72}
 73
 74
 75template <class T, class SPT, class U> void setObject(SPT& obj, const U& val)
 76{
 77  if (!obj)
 78    obj.reset(new T(val));
 79  else
 80    *obj = val;
 81}
 82
 83#include "SiconosPointers.hpp"
 84
 85template <class S, class G>
 86std::shared_ptr<S> setOfGraph(std::shared_ptr<G> g)
 87{
 88  std::shared_ptr<S> r;
 89  r.reset(new S());
 90  for (typename G::VIterator vi = g->begin(); vi != g->end(); ++vi)
 91  {
 92    r->insert(g->bundle(*vi));
 93  }
 94  return(r);
 95}
 96
 97
 98
 99#endif
100
101#ifndef PRINTSEQUENCE_H
102#define PRINTSEQUENCE_H
103#include<algorithm>
104#include<iostream>
105#include<iterator>
106
107
108template<typename Iter>
109void print(Iter first, Iter last, const char* nm = "", const char * sep = "\n", std::ostream& os =  std::cout)
110{
111  if (nm != nullptr && *nm != '\0')
112    os << nm << ": " << sep;
113  typedef typename std::iterator_traits<Iter>::value_type T;
114  std::copy(first, last, std::ostream_iterator<T>(os, sep));
115  os << std::endl;
116}
117#endif