OpenLexocad  27.1
Tools.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
3  * *
4  * This file is part of the FreeCAD CAx development system. *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this library; see the file COPYING.LIB. If not, *
18  * write to the Free Software Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307, USA *
20  * *
21  ***************************************************************************/
22 
23 #pragma once
24 
25 #include <functional>
26 #include <algorithm>
27 #include <cmath>
28 #include <iostream>
29 #include <vector>
30 #include <string>
31 #include <boost/signals2.hpp>
32 #include <QString>
33 #include <QObject>
34 
35 #if (QT_VERSION < 0x050300)
37 {
38 public:
39  QSignalBlocker(QObject *object)
40  : object(object)
41  , blocked(object && object->blockSignals(true))
42  , inhibited(false)
43  {
44  }
46  {
47  if (object && !inhibited)
48  object->blockSignals(blocked);
49  }
50  void reblock()
51  {
52  if (object)
53  object->blockSignals(true);
54  inhibited = false;
55  }
56  void unblock()
57  {
58  if (object)
59  object->blockSignals(blocked);
60  inhibited = true;
61  }
62 
63 private:
64  QObject *object;
65  bool blocked;
66  bool inhibited;
67 };
68 #endif
69 
70 // ----------------------------------------------------------------------------
71 
72 namespace Base
73 {
74 
75 template <class T>
76 struct iotaGen
77 {
78 public:
79  T operator()() { return n++; }
80  iotaGen(T v) : n(v) {}
81 
82 private:
83  T n;
84 };
85 
86 // ----------------------------------------------------------------------------
87 
88 template <class T>
90 {
91  T i_;
92  std::ostream& (*f_)(std::ostream&, T);
93 
94 public:
95  manipulator(std::ostream& (*f)(std::ostream&, T), T i) : i_(i), f_(f)
96  {
97  }
98  friend std::ostream& operator<<( std::ostream& os, manipulator m)
99  {
100  return m.f_(os, m.i_);
101  }
102 };
103 
104 inline std::ostream& tabsN(std::ostream& os, int n)
105 {
106  for (int i=0;i<n;i++)
107  os << "\t";
108  return os;
109 }
110 
111 inline std::ostream& blanksN(std::ostream& os, int n)
112 {
113  for (int i=0;i<n;i++)
114  os << " ";
115  return os;
116 }
117 
118 inline manipulator<int> tabs(int n)
119 {
120  return manipulator<int>(&tabsN, n);
121 }
122 
124 {
125  return manipulator<int>(&blanksN, n);
126 }
127 
128 // ----------------------------------------------------------------------------
129 
130 template<class T>
131 inline T clamp (T num, T lower, T upper)
132 {
133  return std::max<T>(std::min<T>(upper,num),lower);
134 }
135 
136 template<class T>
137 inline T sgn (T t)
138 {
139  if (t == 0)
140  return T(0);
141  else
142  return (t > 0) ? T(1) : T(-1);
143 }
144 
145 #ifndef M_PI
146 #define M_PI 3.14159265358979323846
147 #endif
148 
149 template<class T>
150 inline T toRadians(T d)
151 {
152  return static_cast<T>((d*M_PI)/180.0);
153 }
154 
155 template<class T>
156 inline T toDegrees(T r)
157 {
158  return static_cast<T>((r/M_PI)*180.0);
159 }
160 
161 template<class T>
162 inline T fmod(T numerator, T denominator)
163 {
164  T modulo = std::fmod(numerator, denominator);
165  return (modulo >= T(0)) ? modulo : modulo + denominator;
166 }
167 
168 // ----------------------------------------------------------------------------
169 
170 class LX_BASE_EXPORT StopWatch
171 {
172 public:
173  StopWatch();
174  ~StopWatch();
175 
176  void start();
177  int restart();
178  int elapsed();
179  std::string toString(int ms) const;
180 
181 private:
182  struct Private;
183  Private* d;
184 };
185 
186 // ----------------------------------------------------------------------------
187 
188 template<typename Flag=bool>
189 struct FlagToggler {
190 
191  Flag &flag;
192  bool toggled;
193 
194  FlagToggler(Flag &_flag)
195  :flag(_flag),toggled(true)
196  {
197  flag = !flag;
198  }
199 
200  FlagToggler(Flag &_flag, Flag check)
201  :flag(_flag),toggled(check==_flag)
202  {
203  if (toggled)
204  flag = !flag;
205  }
206 
208  if (toggled)
209  flag = !flag;
210  }
211 };
212 
213 // ----------------------------------------------------------------------------
214 
215 template<typename Status, class Object>
217 {
218 public:
219  ObjectStatusLocker(Status s, Object* o, bool value = true) : status(s), obj(o)
220  { old_value = obj->testStatus(status); obj->setStatus(status, value); }
222  { obj->setStatus(status, old_value); }
223 private:
224  Status status;
225  Object* obj;
226  bool old_value;
227 };
228 
229 // ----------------------------------------------------------------------------
230 
232 {
233 public:
234  StateLocker(bool& flag, bool value = true) : lock(flag)
235  { old_value = lock; lock = value; }
237  { lock = old_value; }
238 private:
239  bool& lock;
240  bool old_value;
241 };
242 
243 // ----------------------------------------------------------------------------
244 
245 template<typename T>
247 {
248 public:
249  BitsetLocker(T& flags, std::size_t flag, bool value = true)
250  : flags(flags), flag(flag)
251  { oldValue = flags.test(flag); flags.set(flag,value); }
253  { flags.set(flag,oldValue); }
254 private:
255  T &flags;
256  std::size_t flag;
257  bool oldValue;
258 };
259 
260 // ----------------------------------------------------------------------------
261 
263  typedef boost::signals2::connection Connection;
264  typedef boost::signals2::shared_connection_block ConnectionBlock;
265  ConnectionBlock blocker;
266 
267 public:
268  ConnectionBlocker(Connection& c) : blocker(c) {
269  }
271  }
272 };
273 
274 // ----------------------------------------------------------------------------
275 
276 struct LX_BASE_EXPORT Tools
277 {
278  static std::string getUniqueName(const std::string&, const std::vector<std::string>&,int d=0);
279  static std::string addNumber(const std::string&, unsigned int, int d=0);
280  static std::string getIdentifier(const std::string&);
281  static std::wstring widen(const std::string& str);
282  static std::string narrow(const std::wstring& str);
283  static QString escapeEncodeString(const QString& s);
284  static std::string escapeEncodeString(const std::string& s);
285  static QString escapeEncodeFilename(const QString& s);
286  static std::string escapeEncodeFilename(const std::string& s);
287 
293  static inline std::string toStdString(const QString& s) { QByteArray tmp = s.toUtf8(); return std::string(tmp.constData(), tmp.size()); }
294 
300  static inline QString fromStdString(const std::string & s) { return QString::fromUtf8(s.c_str(), (int)s.size()); }
301 };
302 
303 
304 } // namespace Base
305 
306 
manipulator< int > blanks(int n)
Definition: Tools.h:123
~FlagToggler()
Definition: Tools.h:207
~BitsetLocker()
Definition: Tools.h:252
QSignalBlocker(QObject *object)
Definition: Tools.h:39
FlagToggler(Flag &_flag)
Definition: Tools.h:194
T operator()()
Definition: Tools.h:79
void unblock()
Definition: Tools.h:56
manipulator< int > tabs(int n)
Definition: Tools.h:118
~ObjectStatusLocker()
Definition: Tools.h:221
Definition: Tools.h:189
BitsetLocker(T &flags, std::size_t flag, bool value=true)
Definition: Tools.h:249
friend std::ostream & operator<<(std::ostream &os, manipulator m)
Definition: Tools.h:98
void reblock()
Definition: Tools.h:50
FlagToggler(Flag &_flag, Flag check)
Definition: Tools.h:200
Definition: Tools.h:246
Definition: Tools.h:170
Definition: Tools.h:36
ObjectStatusLocker(Status s, Object *o, bool value=true)
Definition: Tools.h:219
Definition: Tools.h:262
Definition: Tools.h:76
T toRadians(T d)
Definition: Tools.h:150
std::ostream & blanksN(std::ostream &os, int n)
Definition: Tools.h:111
StateLocker(bool &flag, bool value=true)
Definition: Tools.h:234
std::ostream & tabsN(std::ostream &os, int n)
Definition: Tools.h:104
static QString fromStdString(const std::string &s)
fromStdString Convert a std::string encoded as UTF-8 into a QString.
Definition: Tools.h:300
~StateLocker()
Definition: Tools.h:236
Flag & flag
Definition: Tools.h:191
ConnectionBlocker(Connection &c)
Definition: Tools.h:268
T clamp(T num, T lower, T upper)
Definition: Tools.h:131
static std::string toStdString(const QString &s)
toStdString Convert a QString into a UTF-8 encoded std::string.
Definition: Tools.h:293
Definition: Tools.h:276
Definition: Tools.h:216
T sgn(T t)
Definition: Tools.h:137
~QSignalBlocker()
Definition: Tools.h:45
T toDegrees(T r)
Definition: Tools.h:156
Definition: AbstractXMLReader.h:5
Definition: Tools.h:89
#define M_PI
Definition: Tools.h:146
T fmod(T numerator, T denominator)
Definition: Tools.h:162
~ConnectionBlocker()
Definition: Tools.h:270
bool toggled
Definition: Tools.h:192
Status
Definition: CoreDocument.h:152
Definition: Tools.h:231
manipulator(std::ostream &(*f)(std::ostream &, T), T i)
Definition: Tools.h:95
iotaGen(T v)
Definition: Tools.h:80