2
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
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
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.
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
20
#ifndef _GRTPP_UTIL_H_
21
#define _GRTPP_UTIL_H_
25
#include <boost/function.hpp>
29
#include <boost/bind.hpp>
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)
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)
42
std::string MYSQLGRT_PUBLIC type_to_str(Type type);
43
Type MYSQLGRT_PUBLIC str_to_type(const std::string &str);
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);
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);
52
inline bool is_container_type(Type type)
56
|| type == ObjectType)
61
inline bool is_simple_type(Type type)
63
if (type == IntegerType
65
|| type == StringType)
70
std::string MYSQLGRT_PUBLIC get_guid();
72
inline std::string path_base(const std::string &path)
74
std::string::size_type p= path.rfind('/');
75
if (p != std::string::npos)
76
return path.substr(0, p);
81
inline std::string path_last(const std::string &path)
83
std::string::size_type p= path.rfind('/');
84
if (p != std::string::npos)
85
return path.substr(p);
90
inline std::string format_message(const MYX_GRT_MSG &msg, bool withtype= false)
98
case MYX_MSG_INFO: text= "Info: "; break;
99
case MYX_MSG_WARNING: text= "Warning: "; break;
100
case MYX_MSG_ERROR: text= "Error: "; break;
106
if (msg.msg_detail && msg.msg_detail->strings_num > 0 && *msg.msg_detail->strings[0])
108
text+= " ("+std::string(msg.msg_detail->strings[0])+")";
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")
121
size_t i, c= list.count();
125
for (i= 0; i < c; i++)
129
if (tmp.is_valid() && tmp->get_string_member(name) == value)
135
for (i= 0; i < c; i++)
139
if (tmp.is_valid() && g_strcasecmp(tmp->get_string_member(name).c_str(), value.c_str())==0)
148
inline Ref<O> find_object_in_list(const ListRef<O> &list,
149
const std::string &id)
151
size_t i, c= list.count();
152
for (i= 0; i < c; i++)
154
Ref<O> value= list[i];
156
if (value.is_valid() && value->id() == id)
164
inline size_t find_object_index_in_list(ListRef<O> list,
165
const std::string &id)
167
size_t i, c= list.count();
168
for (i= 0; i < c; i++)
170
Ref<O> value= list.get(i);
172
if (value.is_valid() && value.id() == id)
178
template<typename TPredicate>
179
std::string get_name_suggestion(TPredicate duplicate_found_pred,const std::string &prefix, const bool serial)
181
char buffer[30] = "";
186
sprintf(buffer, "%i", x);
188
while (duplicate_found_pred(name))
190
sprintf(buffer, "%i", x++);
196
MYSQLGRT_PUBLIC std::string get_name_suggestion_for_list_object(const BaseListRef &objlist, const std::string &prefix, bool serial= true);
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);
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);
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);
218
MYSQLGRT_PUBLIC bool compare_list_contents(const ObjectListRef &list1, const ObjectListRef &list2);
220
MYSQLGRT_PUBLIC void remove_list_items_matching(ObjectListRef list, const boost::function<bool (grt::ObjectRef)> &matcher);
222
MYSQLGRT_PUBLIC ObjectRef copy_object(GRT *grt, ObjectRef object, std::set<std::string> skip_members= std::set<std::string>());
224
//XXX don't use this for objects, use CopyContext::copy() instead
225
MYSQLGRT_PUBLIC ValueRef copy_value(ValueRef value, bool deep);
227
struct MYSQLGRT_PUBLIC CopyContext
230
std::map<internal::Value*, ValueRef> object_copies;
231
std::list<ObjectRef> copies;
233
CopyContext(GRT *agrt) : grt(agrt) {}
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();
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);
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);
253
typedef boost::function<bool (ValueRef, ValueRef,std::string)> TSlotNormalizerSlot;
255
struct MYSQLGRT_PUBLIC Omf
257
TSlotNormalizerSlot normalizer;
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){};
265
virtual bool less(const ValueRef& , const ValueRef&) const= 0;
266
virtual bool equal(const ValueRef& , const ValueRef&) const= 0;
269
struct default_omf : public Omf
271
bool peq(const ValueRef &l, const ValueRef &r)const
273
if ((l.type() == r.type() && l.type() == ObjectType)
274
&& ObjectRef::can_wrap(l) && ObjectRef::can_wrap(r))
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");
284
bool pless(const ValueRef &l, const ValueRef &r)const
286
if ((l.type() == r.type() && l.type() == ObjectType)
287
&& ObjectRef::can_wrap(l) && ObjectRef::can_wrap(r))
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");
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);};
304
boost::shared_ptr<DiffChange> diff_make(const ValueRef &source, const ValueRef &target, const Omf* omf);
307
void diff_dump(const DiffChange &change);