~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to backend/wbpublic/grt/common.h

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
#ifndef _BE_COMMON_H_
 
20
#define _BE_COMMON_H_
 
21
 
 
22
#include <cstring>
 
23
#include <vector>
 
24
#include <string>
 
25
#include <glib.h>
 
26
#include <algorithm>
 
27
 
 
28
#ifndef _WIN32
 
29
#include <stdexcept>
 
30
#include <stdarg.h>
 
31
#endif
 
32
 
 
33
#include <string.h>
 
34
#include "grtpp.h"
 
35
 
 
36
#include "wbpublic_public_interface.h"
 
37
 
 
38
#ifdef __GNUC__
 
39
#define UNUSED __attribute__((unused))
 
40
#else
 
41
#define UNUSED
 
42
#endif
 
43
 
 
44
namespace bec {
 
45
  
 
46
  enum MatchType
 
47
  {
 
48
    MatchAny,
 
49
    MatchBefore,
 
50
    MatchAfter,
 
51
    MatchLast
 
52
  };
 
53
  
 
54
  enum MoveType
 
55
  {
 
56
    MoveUp,
 
57
    MoveDown
 
58
  };
 
59
 
 
60
  enum FindType
 
61
  {
 
62
    FindPrefix,
 
63
    FindFull
 
64
  };
 
65
 
 
66
#define DATETIME_FMT "%Y-%m-%d %H:%M"
 
67
  std::string WBPUBLICBACKEND_PUBLIC_FUNC fmttime(time_t t= 0, const char *fmt= "%b %d, %Y");
 
68
 
 
69
  std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_string(const std::string &s,
 
70
                            const std::string &from,
 
71
                            const std::string &to);
 
72
  void WBPUBLICBACKEND_PUBLIC_FUNC replace_string_inplace(std::string &text,
 
73
                            const std::string &from,
 
74
                            const std::string &to);
 
75
  std::string WBPUBLICBACKEND_PUBLIC_FUNC rtrim(const std::string &value);
 
76
 
 
77
  
 
78
  // replaces a variable from a string in format %variable%
 
79
  // a filter can be passed to the variable as in %variable|filter%
 
80
  // supported filters are upper, lower and capitalize
 
81
  std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_variable(const std::string &format, const std::string &variable, const std::string &value);
 
82
  
 
83
  std::string WBPUBLICBACKEND_PUBLIC_FUNC append_extension_if_needed(const std::string &path,
 
84
                                                              const std::string &ext);
 
85
 
 
86
  inline bool has_prefix(const std::string &str, const std::string &pref)
 
87
  {
 
88
    if (strncmp(str.c_str(), pref.c_str(), pref.length())==0)
 
89
      return true;
 
90
    return false;
 
91
  }
 
92
 
 
93
 
 
94
  inline bool has_suffix(const std::string &str, const std::string &suf)
 
95
  {
 
96
    if (suf.length() < str.length() && strncmp(str.c_str()+str.length()-suf.length(), suf.c_str(), suf.length())==0)
 
97
      return true;
 
98
    return false;
 
99
  }
 
100
 
 
101
 
 
102
  std::string WBPUBLICBACKEND_PUBLIC_FUNC make_path(const std::string &prefix, const std::string &file);
 
103
 
 
104
  inline std::string pathlist_append(const std::string &l, const std::string &s)
 
105
  {
 
106
    if (l.empty())
 
107
      return s;
 
108
    return l+G_SEARCHPATH_SEPARATOR+s;
 
109
  }
 
110
 
 
111
  inline std::string pathlist_prepend(const std::string &l, const std::string &s)
 
112
  {
 
113
    if (l.empty())
 
114
      return s;
 
115
    return s+G_SEARCHPATH_SEPARATOR+l;
 
116
  }
 
117
  
 
118
  template <class T>
 
119
  size_t find_list_ref_item_position(grt::ListRef<T> &item_data, std::string& name, MatchType match = MatchAny, grt::Ref<T>* reference = NULL, FindType find_mode = FindPrefix)
 
120
  {
 
121
 
 
122
    if ((match == MatchBefore || match == MatchAfter) && !reference)
 
123
        throw std::invalid_argument("A reference must be specified for MatchBefore and MatchAfter");
 
124
 
 
125
    bool search_enabled = match != MatchAfter;
 
126
    bool exit = false;
 
127
 
 
128
    size_t index = grt::BaseListRef::npos;
 
129
    
 
130
    for ( grt::TypedListConstIterator<T> end = item_data.end(),
 
131
         inst = item_data.begin(); inst != end && !exit; ++inst)
 
132
    {
 
133
      // If skip is defined will omit the entries until the 'skip' element is found
 
134
      if (search_enabled)
 
135
      {
 
136
        // For MatchBefore the search ends when the reference item is found
 
137
        if (match == MatchBefore && (*reference) == (*inst))
 
138
          exit = true;
 
139
        else
 
140
        {
 
141
          std::string item_name = (*inst)->name();
 
142
          
 
143
          int compare_result = (find_mode == FindPrefix) ? item_name.compare(0, name.length(), name) : item_name.compare(name);
 
144
 
 
145
          // index will contain always the position of the last matched entry
 
146
          if (compare_result == 0)
 
147
          {
 
148
            index = item_data.get_index(*inst);
 
149
            
 
150
            // MatchBefore needs to search until the reference is found
 
151
            // MatchLast needs to search until the whole list has been searched to get the last match
 
152
            // MatchAfter and MatchAny are done as soon as a match is found
 
153
            if (match != MatchBefore && match != MatchLast)
 
154
              exit = true;
 
155
          }
 
156
        }
 
157
      }
 
158
      
 
159
      // For MatchAfter the search starts once the reference item has been found
 
160
      else if ((*reference) == (*inst))
 
161
        search_enabled = true;
 
162
    }  
 
163
    
 
164
    return index;
 
165
  }
 
166
  
 
167
  template <class T>
 
168
  void move_list_ref_item(MoveType move_type, grt::ListRef<T> items, const grt::ValueRef &object)
 
169
  {
 
170
    grt::Type object_type = object.type();
 
171
    
 
172
    std::string group_name;
 
173
    std::string item_name;
 
174
    std::string search_name = "";
 
175
    size_t group_indicator_position = std::string::npos;
 
176
    
 
177
    MatchType match = (move_type == MoveUp) ? MatchBefore : MatchAfter;
 
178
    
 
179
    grt::Ref<T> item;
 
180
    
 
181
    // Gets the relevant index for the selected object
 
182
    size_t item_index = grt::BaseListRef::npos;
 
183
    if (object_type == grt::ObjectType)
 
184
    {
 
185
      item = grt::Ref<T>::cast_from(object);
 
186
      
 
187
      item_index = items.get_index( item );
 
188
      item_name = item->name();
 
189
      group_indicator_position = item_name.find("/");
 
190
      
 
191
      // When a grouped item is selected, the movement will be done across same
 
192
      // group items
 
193
      if (group_indicator_position != std::string::npos)
 
194
        search_name = item_name.substr(0, group_indicator_position + 1);
 
195
    }
 
196
    else
 
197
    {
 
198
      group_name = object.repr();
 
199
      group_name+="/";
 
200
      
 
201
      // Searchses the index of the initial element of the group position
 
202
      item_index = find_list_ref_item_position<T>(items, group_name);
 
203
      item = items[item_index];
 
204
      item_name = group_name;
 
205
    }
 
206
    
 
207
    
 
208
    // This is executed whenever the target position depends on the main list
 
209
    // The only case where this is excluded is when the selected item belongs to a group
 
210
    if (group_indicator_position == std::string::npos)
 
211
    {
 
212
      std::vector<std::string> items_list;
 
213
      
 
214
      // Gets the main list items ( groups and non grouped items )
 
215
      for ( grt::TypedListConstIterator<T> end = items.end(),
 
216
           inst = items.begin(); inst != end; ++inst)
 
217
      {
 
218
        std::string item_name = (*inst)->name();
 
219
        size_t position = item_name.find("/");
 
220
        
 
221
        if (position != std::string::npos)
 
222
        {
 
223
          std::string group_name = item_name.substr(0, position + 1);
 
224
          if (std::find(items_list.begin(), items_list.end(), group_name) == items_list.end())
 
225
            items_list.push_back(group_name);
 
226
        }
 
227
        else
 
228
          items_list.push_back(item_name);
 
229
      }
 
230
        
 
231
      // Searches the item inside the list of non grouped items/groups, will find it only if it's a non grouped item
 
232
      size_t item_list_position = std::find(items_list.begin(), items_list.end(), std::string(item_name)) - items_list.begin();
 
233
      
 
234
      size_t offset = ( move_type == MoveUp ) ? -1 : 1;
 
235
      
 
236
      item_name = items_list.at(item_list_position + offset);
 
237
      
 
238
      // If the next item is a group
 
239
      group_indicator_position = item_name.find("/");
 
240
      
 
241
      if(group_indicator_position != std::string::npos)
 
242
      {
 
243
        search_name = item_name.substr(0, group_indicator_position + 1);
 
244
        if (move_type == MoveUp)
 
245
          match = MatchAny;
 
246
      }
 
247
      else
 
248
        search_name = item_name;
 
249
    }
 
250
    
 
251
    // Searchses the index of the target position
 
252
    size_t target_index = grt::BaseListRef::npos;
 
253
    target_index = find_list_ref_item_position<T>(items, search_name, match, &item);    
 
254
    
 
255
    if (move_type == MoveDown)
 
256
      items.reorder(target_index, item_index);
 
257
    else
 
258
      items.reorder(item_index, target_index);
 
259
    
 
260
  }  
 
261
 
 
262
  //! \brief RAII style GMutex locker/unlocker  
 
263
  class WBPUBLICBACKEND_PUBLIC_FUNC GMutexLock
 
264
  {
 
265
  protected:
 
266
    GMutex *mutex;
 
267
  public:
 
268
    GMutexLock(GMutex *mtx) : mutex(mtx)
 
269
    {
 
270
      if (mutex)
 
271
        g_mutex_lock(mutex);
 
272
    }
 
273
    GMutexLock(const GMutexLock &lock)
 
274
    {
 
275
      GMutexLock *lock_= const_cast<GMutexLock*>(&lock);
 
276
      operator=(*lock_);
 
277
    }
 
278
    ~GMutexLock()
 
279
    {
 
280
      reset();
 
281
    }
 
282
    GMutex * release()
 
283
    {
 
284
      GMutex *res= mutex;
 
285
      mutex= NULL;
 
286
      return res;
 
287
    }
 
288
    void reset()
 
289
    {
 
290
      if (mutex)
 
291
      {
 
292
        g_mutex_unlock(mutex);
 
293
        mutex= NULL;
 
294
      }
 
295
    }
 
296
    GMutexLock & operator=(GMutexLock& lock)
 
297
    {
 
298
      mutex= lock.release();
 
299
      return *this;
 
300
    }
 
301
  };
 
302
  
 
303
  class WBPUBLICBACKEND_PUBLIC_FUNC GMutexTryLock : public GMutexLock
 
304
  {
 
305
  public:
 
306
    GMutexTryLock(GMutex *mtx) : GMutexLock(0)
 
307
    {
 
308
      if (!g_mutex_trylock(mtx))
 
309
        mutex = NULL;
 
310
      else
 
311
        mutex = mtx;
 
312
    }
 
313
    
 
314
    bool locked() const
 
315
    {
 
316
      return mutex != NULL;
 
317
    }
 
318
  };
 
319
 
 
320
  //! \brief RAII style GStaticMutex locker/unlocker  
 
321
  class WBPUBLICBACKEND_PUBLIC_FUNC GStaticMutexLock // Note: must compile with strict-aliasing disabled (-fno-strict-aliasing in gcc)
 
322
  {
 
323
      GStaticMutex    &mutex;
 
324
    public:
 
325
    GStaticMutexLock(GStaticMutex& mtx);
 
326
    ~GStaticMutexLock();
 
327
  };
 
328
 
 
329
  class WBPUBLICBACKEND_PUBLIC_FUNC GStaticRecMutexLock // Note: must compile with strict-aliasing disabled (-fno-strict-aliasing in gcc)
 
330
  {
 
331
    GStaticRecMutex    &mutex;
 
332
  public:
 
333
    GStaticRecMutexLock(GStaticRecMutex& mtx);
 
334
    ~GStaticRecMutexLock();
 
335
  };
 
336
 
 
337
  class WBPUBLICBACKEND_PUBLIC_FUNC TimerActionThread
 
338
  {
 
339
  public:
 
340
    typedef boost::function<void ()> Action;
 
341
    static TimerActionThread * create(const Action &action, gulong milliseconds);
 
342
    ~TimerActionThread();
 
343
    void stop(bool clear_exit_signal);
 
344
    boost::signals2::signal<void ()> on_exit;
 
345
  private:
 
346
    GMutex *_action_mutex;
 
347
    Action _action;
 
348
    gulong _microseconds;
 
349
    GThread *_thread;
 
350
    TimerActionThread(const Action &action, gulong milliseconds);
 
351
    static gpointer start(gpointer data);
 
352
    void main_loop();
 
353
  };
 
354
 
 
355
  class WBPUBLICBACKEND_PUBLIC_FUNC ScopeExitTrigger
 
356
  {
 
357
  public:
 
358
    typedef boost::function<void ()> Slot;
 
359
    ScopeExitTrigger() {}
 
360
    ScopeExitTrigger(const Slot &cb) : slot(cb) {}
 
361
    ~ScopeExitTrigger() { slot(); }
 
362
    ScopeExitTrigger & operator=(const Slot &cb) { slot= cb; return *this; }
 
363
    Slot slot;
 
364
  };
 
365
  
 
366
};
 
367
 
 
368
#endif /* _BE_COMMON_H_ */