OpenLexocad  28.0
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 <ostream>
26 #include <vector>
27 #include <QString> // for QString
28 
29 
30 // ----------------------------------------------------------------------------
31 
32 namespace Base
33 {
34 
35 template <class T>
36 struct iotaGen
37 {
38 public:
39  T operator()() { return n++; }
40  iotaGen(T v) : n(v) {}
41 
42 private:
43  T n;
44 };
45 
46 // ----------------------------------------------------------------------------
47 
48 template <class T>
49 class manipulator
50 {
51  T i_;
52  std::ostream& (*f_)(std::ostream&, T);
53 
54 public:
55  manipulator(std::ostream& (*f)(std::ostream&, T), T i) : i_(i), f_(f)
56  {
57  }
58  friend std::ostream& operator<<( std::ostream& os, manipulator m)
59  {
60  return m.f_(os, m.i_);
61  }
62 };
63 
64 inline std::ostream& tabsN(std::ostream& os, int n)
65 {
66  for (int i=0;i<n;i++)
67  os << "\t";
68  return os;
69 }
70 
71 inline std::ostream& blanksN(std::ostream& os, int n)
72 {
73  for (int i=0;i<n;i++)
74  os << " ";
75  return os;
76 }
77 
78 inline manipulator<int> tabs(int n)
79 {
80  return manipulator<int>(&tabsN, n);
81 }
82 
83 inline manipulator<int> blanks(int n)
84 {
85  return manipulator<int>(&blanksN, n);
86 }
87 
88 // ----------------------------------------------------------------------------
89 
90 template<class T>
91 inline T clamp (T num, T lower, T upper)
92 {
93  return std::max<T>(std::min<T>(upper,num),lower);
94 }
95 
96 template<class T>
97 inline T sgn (T t)
98 {
99  if (t == 0)
100  return T(0);
101  else
102  return (t > 0) ? T(1) : T(-1);
103 }
104 
105 #ifndef M_PI
106 #define M_PI 3.14159265358979323846
107 #endif
108 
109 template<class T>
110 inline T toRadians(T d)
111 {
112  return static_cast<T>((d*M_PI)/180.0);
113 }
114 
115 template<class T>
116 inline T toDegrees(T r)
117 {
118  return static_cast<T>((r/M_PI)*180.0);
119 }
120 
121 template<class T>
122 inline T fmod(T numerator, T denominator)
123 {
124  T modulo = std::fmod(numerator, denominator);
125  return (modulo >= T(0)) ? modulo : modulo + denominator;
126 }
127 
128 // ----------------------------------------------------------------------------
129 
130 class LX_BASE_EXPORT StopWatch
131 {
132 public:
133  StopWatch();
134  ~StopWatch();
135 
136  void start();
137  int restart();
138  int elapsed();
139  std::string toString(int ms) const;
140 
141 private:
142  struct Private;
143  Private* d;
144 };
145 
146 // ----------------------------------------------------------------------------
147 
148 template<typename Flag=bool>
149 struct FlagToggler {
150 
151  Flag &flag;
152  bool toggled;
153 
154  FlagToggler(Flag &_flag)
155  :flag(_flag),toggled(true)
156  {
157  flag = !flag;
158  }
159 
160  FlagToggler(Flag &_flag, Flag check)
161  :flag(_flag),toggled(check==_flag)
162  {
163  if (toggled)
164  flag = !flag;
165  }
166 
167  ~FlagToggler() {
168  if (toggled)
169  flag = !flag;
170  }
171 };
172 
173 // ----------------------------------------------------------------------------
174 
175 template<typename Status, class Object>
176 class ObjectStatusLocker
177 {
178 public:
179  ObjectStatusLocker(Status s, Object* o, bool value = true) : status(s), obj(o)
180  { old_value = obj->testStatus(status); obj->setStatus(status, value); }
182  { obj->setStatus(status, old_value); }
183 private:
184  Status status;
185  Object* obj;
186  bool old_value;
187 };
188 
189 // ----------------------------------------------------------------------------
190 
191 class StateLocker
192 {
193 public:
194  StateLocker(bool& flag, bool value = true) : lock(flag)
195  { old_value = lock; lock = value; }
197  { lock = old_value; }
198 private:
199  bool& lock;
200  bool old_value;
201 };
202 
203 // ----------------------------------------------------------------------------
204 
205 template<typename T>
206 class BitsetLocker
207 {
208 public:
209  BitsetLocker(T& flags, std::size_t flag, bool value = true)
210  : flags(flags), flag(flag)
211  { oldValue = flags.test(flag); flags.set(flag,value); }
212  ~BitsetLocker()
213  { flags.set(flag,oldValue); }
214 private:
215  T &flags;
216  std::size_t flag;
217  bool oldValue;
218 };
219 
220 // ----------------------------------------------------------------------------
221 
222 //class ConnectionBlocker {
223 // typedef boost::signals2::connection Connection;
224 // typedef boost::signals2::shared_connection_block ConnectionBlock;
225 // ConnectionBlock blocker;
226 //
227 //public:
228 // ConnectionBlocker(Connection& c) : blocker(c) {
229 // }
230 // ~ConnectionBlocker() {
231 // }
232 //};
233 
234 // ----------------------------------------------------------------------------
235 
236 struct LX_BASE_EXPORT Tools
237 {
238  static std::string getUniqueName(const std::string&, const std::vector<std::string>&,int d=0);
239  static std::string addNumber(const std::string&, unsigned int, int d=0);
240  static std::string getIdentifier(const std::string&);
241  static std::wstring widen(const std::string& str);
242  static std::string narrow(const std::wstring& str);
243  static QString escapeEncodeString(const QString& s);
244  static std::string escapeEncodeString(const std::string& s);
245  static QString escapeEncodeFilename(const QString& s);
246  static std::string escapeEncodeFilename(const std::string& s);
247 
253  static inline std::string toStdString(const QString& s) { QByteArray tmp = s.toUtf8(); return std::string(tmp.constData(), tmp.size()); }
254 
260  static inline QString fromStdString(const std::string & s) { return QString::fromUtf8(s.c_str(), (int)s.size()); }
261 };
262 
263 
264 } // namespace Base
265 
266 
Base::sgn
T sgn(T t)
Definition: Tools.h:117
Base::StateLocker
Definition: Tools.h:212
Base::FlagToggler::FlagToggler
FlagToggler(Flag &_flag)
Definition: Tools.h:174
Base::clamp
T clamp(T num, T lower, T upper)
Definition: Tools.h:111
Base::tabsN
std::ostream & tabsN(std::ostream &os, int n)
Definition: Tools.h:84
Base::Tools
Definition: Tools.h:257
Base::blanksN
std::ostream & blanksN(std::ostream &os, int n)
Definition: Tools.h:91
Base::ObjectStatusLocker::ObjectStatusLocker
ObjectStatusLocker(Status s, Object *o, bool value=true)
Definition: Tools.h:199
Base::iotaGen::operator()
T operator()()
Definition: Tools.h:79
Base::tabs
manipulator< int > tabs(int n)
Definition: Tools.h:98
Base::manipulator
Definition: Tools.h:70
Base::ObjectStatusLocker::~ObjectStatusLocker
~ObjectStatusLocker()
Definition: Tools.h:201
Base::iotaGen::iotaGen
iotaGen(T v)
Definition: Tools.h:80
Base::toRadians
T toRadians(T d)
Definition: Tools.h:130
Base::BitsetLocker::~BitsetLocker
~BitsetLocker()
Definition: Tools.h:232
Base::FlagToggler::flag
Flag & flag
Definition: Tools.h:171
Base::BitsetLocker::BitsetLocker
BitsetLocker(T &flags, std::size_t flag, bool value=true)
Definition: Tools.h:229
Base::manipulator::manipulator
manipulator(std::ostream &(*f)(std::ostream &, T), T i)
Definition: Tools.h:75
Base::fmod
T fmod(T numerator, T denominator)
Definition: Tools.h:142
Base::BitsetLocker
Definition: Tools.h:227
M_PI
#define M_PI
Definition: Tools.h:126
Base::FlagToggler::toggled
bool toggled
Definition: Tools.h:172
Base::ObjectStatusLocker
Definition: Tools.h:197
Base::StopWatch
Definition: Tools.h:151
Base::toDegrees
T toDegrees(T r)
Definition: Tools.h:136
Base::StateLocker::StateLocker
StateLocker(bool &flag, bool value=true)
Definition: Tools.h:214
Base::blanks
manipulator< int > blanks(int n)
Definition: Tools.h:103
Base::manipulator::operator<<
friend std::ostream & operator<<(std::ostream &os, manipulator m)
Definition: Tools.h:78
Base
Definition: AbstractXMLReader.h:5
Base::FlagToggler::~FlagToggler
~FlagToggler()
Definition: Tools.h:187
Base::StateLocker::~StateLocker
~StateLocker()
Definition: Tools.h:216