Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / Util.h
blob010cac8faf6e726232883b249a13fdedead8f165
1 #ifndef UTIL_H
2 #define UTIL_H
4 #include <vector>
5 #include <string>
6 #include <algorithm>
8 namespace {
10 template<class T> class Delete {
11 public:
12 inline void operator ()(T* & obj) {
13 delete obj;
14 obj = 0;
18 template<class T> void trimVector(std::vector<T> & v) {
19 std::vector<T>(v).swap(v);
22 template<typename InputIter, typename Type> const Type & setEach(
23 InputIter first, InputIter last, const Type & t) {
24 for (; first != last; ++first)
25 *first = t;
27 return t;
30 inline bool startsWith(const std::string & str, const std::string & prefix,
31 std::string::size_type pos = 0) {
32 return str.find(prefix, pos) == 0;
35 inline bool endsWith(const std::string & str, const std::string & postfix) {
36 size_t n = postfix.length();
37 return str.find(postfix, str.length()-n-1) != std::string::npos;
40 // C++ memset */
41 template<class T> inline void fill(T *ar, const T & val, unsigned long n) {
42 for (unsigned long i = 0; i < n; ++i)
43 ar[i] = val;
46 #define SPACES " \t\r\n"
48 inline const std::string & trim_right(std::string & s,
49 const std::string & t = SPACES) {
50 std::string::size_type i(s.find_last_not_of(t) );
51 if (i != std::string::npos )
52 s.erase(s.find_last_not_of(t) + 1);
54 return s;
57 inline const std::string & trim_left(std::string & s,
58 const std::string & t = SPACES) {
59 return s.erase(0, s.find_first_not_of(t) );
62 inline const std::string & trim(std::string & s,
63 const std::string & t = SPACES) {
64 trim_left(s, t);
65 trim_right(s, t);
66 return s;
69 inline void stolower(std::string & s) {
70 std::transform(s.begin(), s.end(), s.begin(), tolower);
73 inline void stoupper(std::string & s) {
74 std::transform(s.begin(), s.end(), s.begin(), toupper);
77 template<class Class>
78 class MethodCall {
79 public:
80 typedef void (Class::*Method)(void);
82 public:
83 MethodCall(Method _method) {
84 method = _method;
87 public:
88 void operator()(Class *c) {
89 if( c != 0 )
90 (c->*method)();
93 void operator()(Class & c) {
94 ((&c)->*method)();
97 private:
98 Method method;
101 template<class Class, class T>
102 class MethodCall1 {
103 public:
104 typedef void (Class::*Method)(T);
106 public:
107 MethodCall1(Method _method, T _x) {
108 method = _method;
109 x = _x;
112 public:
113 void operator()(Class *c) {
114 if( c != 0 )
115 (c->*method)(x);
118 private:
119 Method method;
120 T x;
123 template<class Class, class T1, class T2>
124 class MethodCall2 {
125 public:
126 typedef void (Class::*Method)(T1,T2);
128 public:
129 MethodCall2(Method _method, T1 _x, T2 _y) {
130 method = _method;
131 x = _x;
132 y = _y;
135 public:
136 void operator()(Class *c) {
137 if( c != 0 )
138 (c->*method)(x, y);
141 private:
142 Method method;
143 T1 x;
144 T2 y;
149 #endif /*UTIL_H*/