LightWrap++
AC++wrapperfortheLightWave3DSDK
utility.h
Go to the documentation of this file.
1 #ifndef LWPP_UTILITY
2 #define LWPP_UTILITY
3 
4 #include <lwpp/debug.h>
5 
6 #include <lwenvel.h>
7 #include <lwxpanel.h>
8 #include <algorithm>
9 
10 namespace lwpp
11 {
12  #pragma warning(disable : 4996)
13 
14  class Nameable
15  {
16  public:
17  std::string name;
18  public:
19  /*
20  * @param _name Optional name to set the object to
21  */
22  explicit Nameable(const char *_name = 0) {SetName(_name);}
23  explicit Nameable(std::string _name) {SetName(_name);}
24  virtual ~Nameable() {;}
26  virtual Nameable &operator=(const Nameable &from)
27  {
28  if (this != &from)
29  {
30  name = from.name;
31  }
32  return *this;
33  }
35  virtual void SetName(const std::string _name)
36  {
37  name = _name;
38  }
39  virtual void SetName(const char *_name)
40  {
41  if (_name == 0)
42  {
43  name.clear(); // empty string
44  }
45  else
46  {
47  SetName (std::string(_name));
48  }
49  }
51  /*
52  virtual const char *GetName() const
53  {
54  return name.empty() ? 0 : name.c_str();
55  }
56  */
57 
58  virtual const std::string &GetName() const
59  {
60  return name;
61  }
62  };
63 
65  {
66  public:
69  private:
71  ReferenceCounted &operator=(const ReferenceCounted &);
72  };
73 
74  template <class T>
75  class Reference
76  {
77  private:
78  T *ptr;
79  public:
80  Reference(T *p = 0)
81  : ptr(p)
82  {
83  if (ptr) ++ptr->nReferences;
84  }
86  {
87  ptr = r.ptr;
88  if (ptr) ++ptr->nReferences;
89  }
91  {
92  if (r.ptr) r.ptr->nReferences++;
93  if (ptr && --ptr->nReferences == 0) delete ptr;
94  ptr = r.ptr;
95  return *this;
96  }
97 
99  {
100  if (p) p->nReferences++;
101  if (ptr && --ptr->nReferences == 0) delete ptr;
102  ptr = p;
103  return *this;
104  }
106  {
107  if (ptr && --ptr->nReferences == 0) delete ptr;
108  }
110  {
111  assert(ptr);
112  return ptr;
113  }
114  T *adress() const
115  {
116  return ptr;
117  }
118  const T *operator->() const
119  {
120  assert(ptr);
121  return ptr;
122  }
123  operator bool() const {return ptr != 0;}
124  bool operator<(const Reference<T> &r) const
125  {
126  return ptr < r.ptr;
127  }
128  };
129 
131  void OpenURL(const std::string urlString);
132  /*
133  {
134  // Thanks to Willard Myers, on the qt-interest mailing list.
135  ICInstance icInstance;
136  if (ICStart ( &icInstance, '????') == noErr)
137  {
138  ConstStr255Param hint (0x0);
139  long l = urlString.length();
140  long s = 0;
141  ICLaunchURL (icInstance, hint, urlString.c_str(), l, &s, &l);
142  ICStop (icInstance);
143  }
144  };
145  */
146 
148  bool isDoubleClick(unsigned int milliseconds);
149 
150  struct to_lower
151  {
152  int operator() ( int ch )
153  {
154  return ::tolower ( ch );
155  }
156  };
157 
159  inline std::string &toLowercase(std::string &str)
160  {
161  std::transform (str.begin (), str.end (), str.begin (), to_lower());
162  return str;
163  }
164 
166  inline std::string &toCapitalize(std::string &str)
167  {
168  bool caps = true;
169  for (size_t i = 0; i < str.length(); ++i)
170  {
171  if (caps)
172  {
173  str[i] = (char)::toupper(str[i]);
174  caps = false;
175  }
176  else
177  {
178  str[i] = (char)::tolower(str[i]);
179  }
180  caps = str[i] == ' ';
181  }
182  return str;
183  }
184 
185  bool inline nocase_compare (const char c1, const char c2)
186  {
187  return ::toupper(c1) == ::toupper(c2);
188  }
189 
191  bool inline cmpNoCase(const std::string &s1, const std::string &s2)
192  {
193  std::string::const_iterator pos;
194  pos = std::search (s1.begin(),s1.end(), // haystack
195  s2.begin(),s2.end(), // needle
196  nocase_compare); // comparison criterion
197  return (pos != s1.end());
198  }
199 
200 }
201 
202 #endif //LWPP_UTILITY