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

« back to all changes in this revision

Viewing changes to library/grt/src/grtpp_util.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
 
 
20
#ifndef _GRTPP_UTIL_H_
 
21
#define _GRTPP_UTIL_H_
 
22
//#ifdef max
 
23
//#undef max
 
24
//#endif
 
25
#include <boost/function.hpp>
 
26
#include "grtpp.h"
 
27
 
 
28
#include <set>
 
29
#include <boost/bind.hpp>
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
#define GRTLIST_FOREACH(type, list, iter) \
 
34
  for (grt::ListRef<type>::const_iterator iter##end= list.end(), iter= list.begin(); iter != iter##end; ++iter)
 
35
 
 
36
#define GRTLIST_REVERSE_FOREACH(type, list, iter) \
 
37
  for (grt::ListRef<type>::const_reverse_iterator iter##end= list.rend(), iter= list.rbegin(); iter != iter##end; ++iter)
 
38
 
 
39
 
 
40
namespace grt
 
41
{
 
42
  std::string MYSQLGRT_PUBLIC type_to_str(Type type);
 
43
  Type MYSQLGRT_PUBLIC str_to_type(const std::string &str);
 
44
 
 
45
  std::string MYSQLGRT_PUBLIC fmt_simple_type_spec(const SimpleTypeSpec &type);
 
46
  std::string MYSQLGRT_PUBLIC fmt_type_spec(const TypeSpec &type);
 
47
  std::string MYSQLGRT_PUBLIC fmt_arg_spec_list(const ArgSpecList &args);
 
48
  
 
49
  ValueRef MYSQLGRT_PUBLIC get_value_by_path(const ValueRef &root, const std::string &path);
 
50
  bool MYSQLGRT_PUBLIC set_value_by_path(const ValueRef &value, const std::string &path, const ValueRef &new_value);
 
51
 
 
52
  inline bool is_container_type(Type type)
 
53
  {
 
54
    if (type == ListType
 
55
        || type == DictType
 
56
        || type == ObjectType)
 
57
      return true;
 
58
    return false;
 
59
  }
 
60
    
 
61
  inline bool is_simple_type(Type type)
 
62
  {
 
63
    if (type == IntegerType
 
64
        || type == DoubleType
 
65
        || type == StringType)
 
66
      return true;
 
67
    return false;
 
68
  }
 
69
 
 
70
  std::string MYSQLGRT_PUBLIC get_guid();
 
71
  
 
72
  inline std::string path_base(const std::string &path)
 
73
  {
 
74
    std::string::size_type p= path.rfind('/');
 
75
    if (p != std::string::npos)
 
76
      return path.substr(0, p);
 
77
    return "";
 
78
  }
 
79
 
 
80
 
 
81
  inline std::string path_last(const std::string &path)
 
82
  {
 
83
    std::string::size_type p= path.rfind('/');
 
84
    if (p != std::string::npos)
 
85
      return path.substr(p);
 
86
    return "";
 
87
  }
 
88
 
 
89
/*
 
90
  inline std::string format_message(const MYX_GRT_MSG &msg, bool withtype= false)
 
91
  {
 
92
    std::string text;
 
93
    
 
94
    if (withtype)
 
95
    {
 
96
      switch (msg.msg_type)
 
97
      {
 
98
      case MYX_MSG_INFO:  text= "Info: "; break;
 
99
      case MYX_MSG_WARNING: text= "Warning: "; break;
 
100
      case MYX_MSG_ERROR: text= "Error: "; break;
 
101
      }
 
102
    }
 
103
    
 
104
    text+= msg.msg;
 
105
    
 
106
    if (msg.msg_detail && msg.msg_detail->strings_num > 0 && *msg.msg_detail->strings[0])
 
107
    {
 
108
      text+= " ("+std::string(msg.msg_detail->strings[0])+")";
 
109
    }
 
110
    
 
111
    return text;
 
112
  }
 
113
*/
 
114
 
 
115
  template<class O>
 
116
    inline Ref<O> find_named_object_in_list(const ListRef<O> &list, 
 
117
                                            const std::string &value,
 
118
                                            bool case_sensitive= true,
 
119
                                            const std::string &name= "name")
 
120
    {
 
121
      size_t i, c= list.count();
 
122
      
 
123
      if (case_sensitive)
 
124
      {
 
125
        for (i= 0; i < c; i++)
 
126
        {
 
127
          Ref<O> tmp= list[i];
 
128
          
 
129
          if (tmp.is_valid() && tmp->get_string_member(name) == value)
 
130
            return tmp;
 
131
        }
 
132
      }
 
133
      else
 
134
      {
 
135
        for (i= 0; i < c; i++)
 
136
        {
 
137
          Ref<O> tmp= list[i];
 
138
          
 
139
          if (tmp.is_valid() && g_strcasecmp(tmp->get_string_member(name).c_str(), value.c_str())==0)
 
140
            return tmp;
 
141
        }
 
142
      }
 
143
      return Ref<O>();
 
144
    }
 
145
  
 
146
 
 
147
  template<class O>
 
148
    inline Ref<O> find_object_in_list(const ListRef<O> &list, 
 
149
                                 const std::string &id)
 
150
    {
 
151
      size_t i, c= list.count();
 
152
      for (i= 0; i < c; i++)
 
153
      {
 
154
        Ref<O> value= list[i];
 
155
        
 
156
        if (value.is_valid() && value->id() == id)
 
157
          return value;
 
158
      }
 
159
      return Ref<O>();
 
160
    }
 
161
   
 
162
   
 
163
    template<class O>
 
164
    inline size_t find_object_index_in_list(ListRef<O> list,
 
165
      const std::string &id)
 
166
    {
 
167
      size_t i, c= list.count();
 
168
      for (i= 0; i < c; i++)
 
169
      {
 
170
        Ref<O> value= list.get(i);
 
171
 
 
172
        if (value.is_valid() && value.id() == id)
 
173
          return i;
 
174
      }
 
175
      return -1;
 
176
    }
 
177
 
 
178
    template<typename TPredicate>
 
179
    std::string get_name_suggestion(TPredicate duplicate_found_pred,const std::string &prefix, const bool serial)
 
180
    {
 
181
      char buffer[30] = "";
 
182
      int x= 1;
 
183
      std::string name;
 
184
 
 
185
      if (serial)
 
186
        sprintf(buffer, "%i", x);
 
187
      name= prefix+buffer;
 
188
      while (duplicate_found_pred(name))
 
189
      {
 
190
        sprintf(buffer, "%i", x++);
 
191
        name= prefix+buffer;
 
192
      } 
 
193
      return name;
 
194
    }
 
195
 
 
196
  MYSQLGRT_PUBLIC std::string get_name_suggestion_for_list_object(const BaseListRef &objlist, const std::string &prefix, bool serial= true);
 
197
  
 
198
  
 
199
  MYSQLGRT_PUBLIC ObjectRef find_child_object(const DictRef &dict, const std::string &id, bool recursive= true);
 
200
  MYSQLGRT_PUBLIC ObjectRef find_child_object(const BaseListRef &list, const std::string &id, bool recursive= true);
 
201
  MYSQLGRT_PUBLIC ObjectRef find_child_object(const ObjectRef &object, const std::string &id, bool recursive= true);
 
202
 
 
203
  MYSQLGRT_PUBLIC void update_ids(ObjectRef object, const std::set<std::string>& skip_members= std::set<std::string>());
 
204
  // the following merge functions are not recursive
 
205
  MYSQLGRT_PUBLIC void append_contents(BaseListRef target, BaseListRef source);
 
206
  MYSQLGRT_PUBLIC void replace_contents(BaseListRef target, BaseListRef source);
 
207
  MYSQLGRT_PUBLIC void merge_contents_by_name(ObjectListRef target,
 
208
                                              ObjectListRef source,
 
209
                                              bool replace_matching);
 
210
  MYSQLGRT_PUBLIC void merge_contents_by_id(ObjectListRef target,
 
211
                                            ObjectListRef source,
 
212
                                            bool replace_matching);
 
213
 
 
214
  MYSQLGRT_PUBLIC void replace_contents(DictRef target, DictRef source);
 
215
  MYSQLGRT_PUBLIC void merge_contents(DictRef target, DictRef source, bool overwrite);
 
216
  MYSQLGRT_PUBLIC void merge_contents(ObjectRef target, ObjectRef source);
 
217
  
 
218
  MYSQLGRT_PUBLIC bool compare_list_contents(const ObjectListRef &list1, const ObjectListRef &list2);
 
219
  
 
220
  MYSQLGRT_PUBLIC void remove_list_items_matching(ObjectListRef list, const boost::function<bool (grt::ObjectRef)> &matcher);
 
221
  
 
222
  MYSQLGRT_PUBLIC ObjectRef copy_object(GRT *grt, ObjectRef object, std::set<std::string> skip_members= std::set<std::string>());
 
223
 
 
224
  //XXX don't use this for objects, use CopyContext::copy() instead
 
225
  MYSQLGRT_PUBLIC ValueRef copy_value(ValueRef value, bool deep);
 
226
 
 
227
  struct MYSQLGRT_PUBLIC CopyContext
 
228
  {
 
229
    GRT *grt;
 
230
    std::map<internal::Value*, ValueRef> object_copies;
 
231
    std::list<ObjectRef> copies;
 
232
    
 
233
    CopyContext(GRT *agrt) : grt(agrt) {}
 
234
        
 
235
    ObjectRef copy(const ObjectRef &object, std::set<std::string> skip_members= std::set<std::string>());
 
236
    void finish() { update_references(); }
 
237
    void update_references();
 
238
    
 
239
  private:
 
240
    ObjectRef duplicate_object(ObjectRef object, std::set<std::string> skip_members);
 
241
    void copy_list(BaseListRef &list, const BaseListRef &source, bool dontfollow);
 
242
    void copy_dict(DictRef &dict, const DictRef &source, bool dontfollow);
 
243
  };
 
244
  
 
245
  
 
246
  // temporary code
 
247
  MYSQLGRT_PUBLIC bool init_python_support(grt::GRT *grt, const std::string &python_module_path);
 
248
  MYSQLGRT_PUBLIC void add_python_module_dir(grt::GRT *grt, const std::string &python_module_path);
 
249
  
 
250
  // diffing
 
251
  
 
252
  class DiffChange;
 
253
  typedef boost::function<bool (ValueRef, ValueRef,std::string)> TSlotNormalizerSlot;
 
254
 
 
255
  struct MYSQLGRT_PUBLIC Omf
 
256
  {
 
257
    TSlotNormalizerSlot normalizer;
 
258
    bool case_sensitive;
 
259
    //some structure members needs to be skiepped only when diffing model vs db but not when diffing model
 
260
    //_dontdiff_mask will hold mask to allow selective bypass of ceratin fields
 
261
    //1 always diff, 2 diff only vs db, 4 diff only vs live object
 
262
    unsigned int dontdiff_mask;
 
263
    Omf(): dontdiff_mask(1){};
 
264
    virtual ~Omf() {};
 
265
    virtual bool less(const ValueRef& , const ValueRef&) const= 0;
 
266
    virtual bool equal(const ValueRef& , const ValueRef&) const= 0;
 
267
  };
 
268
 
 
269
  struct default_omf : public Omf
 
270
{
 
271
    bool peq(const ValueRef &l, const ValueRef &r)const
 
272
    {
 
273
        if ((l.type() == r.type() && l.type() == ObjectType)
 
274
            && ObjectRef::can_wrap(l) && ObjectRef::can_wrap(r))
 
275
        {
 
276
            ObjectRef left = ObjectRef::cast_from(l);
 
277
            ObjectRef right = ObjectRef::cast_from(r);
 
278
            if(left->has_member("name"))
 
279
                return left->get_string_member("name") == right->get_string_member("name");
 
280
        }
 
281
        return l == r;
 
282
    }
 
283
 
 
284
    bool pless(const ValueRef &l, const ValueRef &r)const
 
285
    {
 
286
        if ((l.type() == r.type() && l.type() == ObjectType)
 
287
            && ObjectRef::can_wrap(l) && ObjectRef::can_wrap(r))
 
288
        {
 
289
            ObjectRef left = ObjectRef::cast_from(l);
 
290
            ObjectRef right = ObjectRef::cast_from(r);
 
291
            if(left->has_member("name"))
 
292
                return left->get_string_member("name") < right->get_string_member("name");
 
293
        }
 
294
        return l < r;
 
295
    }
 
296
 
 
297
 
 
298
  virtual bool less(const ValueRef& l, const ValueRef& r)const {return pless(l,r);};
 
299
  virtual bool equal(const ValueRef& l, const ValueRef& r)const  {return peq(l,r);};
 
300
};
 
301
 
 
302
 
 
303
  MYSQLGRT_PUBLIC
 
304
    boost::shared_ptr<DiffChange> diff_make(const ValueRef &source, const ValueRef &target, const Omf* omf);
 
305
  
 
306
  MYSQLGRT_PUBLIC 
 
307
    void diff_dump(const DiffChange &change);
 
308
 
 
309
};
 
310
 
 
311
#endif