Bug fix: check if vm exists
[avr-sim.git] / Util.h
blob7ed4b47a6d5c17cdf8b92227c29b23b23f5d8016
1 #ifndef UTIL_H
2 #define UTIL_H
4 #include <vector>
5 #include <string>
7 namespace {
9 template<class T> class Delete {
10 public:
11 inline void operator ()(T* & obj) {
12 delete obj;
13 obj = 0;
17 template<class T> void trimVector(std::vector<T> & v) {
18 std::vector<T>(v).swap(v);
21 template<typename InputIter, typename Type> const Type & setEach(
22 InputIter first, InputIter last, const Type & t) {
23 for (; first != last; ++first)
24 *first = t;
26 return t;
29 inline bool startsWith(const std::string & str, const std::string & prefix) {
30 return str.find(prefix) == 0;
33 inline bool endsWith(const std::string & str, const std::string & postfix) {
34 size_t n = postfix.length();
35 return str.find(postfix, str.length()-n-1) != std::string::npos;
38 // C++ memset */
39 template<class T> inline void fill(T *ar, const T & val, unsigned long n) {
40 for (unsigned long i = 0; i < n; ++i)
41 ar[i] = val;
44 #define SPACES " \t\r\n"
46 inline const std::string & trim_right(std::string & s,
47 const std::string & t = SPACES) {
48 std::string::size_type i(s.find_last_not_of(t) );
49 if (i != std::string::npos )
50 s.erase(s.find_last_not_of(t) + 1);
52 return s;
55 inline const std::string & trim_left(std::string & s,
56 const std::string & t = SPACES) {
57 return s.erase(0, s.find_first_not_of(t) );
60 inline const std::string & trim(std::string & s,
61 const std::string & t = SPACES) {
62 trim_left(s, t);
63 trim_right(s, t);
64 return s;
67 inline void stolower(std::string & s) {
68 std::transform(s.begin(), s.end(), s.begin(), tolower);
71 inline void stoupper(std::string & s) {
72 std::transform(s.begin(), s.end(), s.begin(), toupper);
75 template<class Class>
76 class MethodCall {
77 public:
78 typedef void (Class::*Method)(void);
80 public:
81 MethodCall(Method _method) {
82 method = _method;
85 public:
86 void operator()(Class *c) {
87 if( c != 0 )
88 (c->*method)();
91 private:
92 Method method;
97 #endif /*UTIL_H*/