33 #ifdef HAVE_SYS_TYPES_H 34 # include <sys/types.h> 42 #include <glib/gstdio.h> 46 #ifdef HAVE_SYS_TIMES_H 47 # include <sys/times.h> 56 #include <boost/uuid/uuid.hpp> 57 #include <boost/uuid/uuid_generators.hpp> 58 #include <boost/uuid/uuid_io.hpp> 63 static QofLogModule log_module = QOF_MOD_ENGINE;
75 if (!value)
return nullptr;
78 g_return_val_if_fail (value && G_IS_VALUE (value), NULL);
79 g_return_val_if_fail (GNC_VALUE_HOLDS_GUID (value), NULL);
81 val = (
GncGUID*) g_value_get_boxed (value);
88 static gnc::GUID s_null_guid {boost::uuids::uuid { {0}}};
89 static GncGUID * s_null_gncguid {guid_convert_create (s_null_guid)};
98 guid_convert_create (
gnc::GUID const & guid)
114 if (guid == s_null_gncguid)
123 if (!guid)
return nullptr;
125 memcpy (ret, guid,
sizeof (
GncGUID));
133 return s_null_gncguid;
139 memcpy (&target, &source,
sizeof (
GncGUID));
147 gnc::GUID temp_random {gnc::GUID::create_random ()};
148 guid_assign (*guid, temp_random);
161 return gnc::GUID::create_random ();
167 if (!guid)
return nullptr;
169 auto temp_str = temp.to_string ();
170 return g_strdup (temp_str.c_str ());
176 if (!str || !guid)
return NULL;
179 auto val = temp.to_string ();
183 std::copy (val.c_str (), val.c_str () + val.size () + 1, str);
184 return str + val.size ();
190 if (!guid || !str)
return false;
194 guid_assign (*guid, gnc::GUID::from_string (str));
206 if (!guid_1 || !guid_2)
207 return !guid_1 && !guid_2;
210 return temp1 == temp2;
216 if (!guid_1 || !guid_2)
217 return !guid_1 && !guid_2;
232 PERR (
"received NULL guid pointer.");
235 GncGUID const & guid = * reinterpret_cast <
GncGUID const *> (ptr);
239 unsigned retspot {0};
240 std::for_each (temp.begin (), temp.end (), [&hash] (
unsigned char a) {
250 return guid_equal (reinterpret_cast<const GncGUID*> (guid_a),
251 reinterpret_cast<const GncGUID*> (guid_b));
262 gnc_string_to_guid (
const GValue *src, GValue *dest)
266 const gchar *as_string;
268 g_return_if_fail (G_VALUE_HOLDS_STRING (src) &&
269 GNC_VALUE_HOLDS_GUID (dest));
271 as_string = g_value_get_string (src);
276 g_value_take_boxed (dest, guid);
280 gnc_guid_to_string (
const GValue *src, GValue *dest)
284 g_return_if_fail (G_VALUE_HOLDS_STRING (dest) &&
285 GNC_VALUE_HOLDS_GUID (src));
289 g_value_set_string (dest, str);
293 gnc_guid_get_type (
void)
295 static GType type = 0;
297 if (G_UNLIKELY (type == 0))
299 type = g_boxed_type_register_static (
"GncGUID",
301 (GBoxedFreeFunc)guid_free);
303 g_value_register_transform_func (G_TYPE_STRING,
307 g_value_register_transform_func (type,
319 GUID::create_random () noexcept
321 static boost::uuids::random_generator gen;
325 GUID::GUID (boost::uuids::uuid
const & other) noexcept
326 : implementation (other)
331 GUID::null_guid () noexcept
337 GUID::to_string () const noexcept
339 auto const & val = boost::uuids::to_string (implementation);
341 std::for_each (val.begin (), val.end (), [&ret] (
char a) {
342 if (a !=
'-') ret.push_back (a);
348 GUID::from_string (std::string
const & str)
352 static boost::uuids::string_generator strgen;
357 throw guid_syntax_exception {};
362 GUID::is_valid_guid (std::string
const & str)
366 static boost::uuids::string_generator strgen;
367 auto a = strgen (str);
376 guid_syntax_exception::guid_syntax_exception () noexcept
377 : invalid_argument {
"Invalid syntax for guid."}
381 GUID::GUID (
GncGUID const & other) noexcept
382 : implementation {{other.reserved[0] , other.reserved[1]
383 , other.reserved[2], other.reserved[3]
384 , other.reserved[4], other.reserved[5]
385 , other.reserved[6], other.reserved[7]
386 , other.reserved[8], other.reserved[9]
387 , other.reserved[10], other.reserved[11]
388 , other.reserved[12], other.reserved[13]
389 , other.reserved[14], other.reserved[15]}
396 GUID::end () const noexcept -> decltype (implementation.end ())
398 return implementation.end ();
402 GUID::begin () const noexcept -> decltype (implementation.begin ())
404 return implementation.begin ();
408 GUID::operator < (GUID
const & other) noexcept
410 return implementation < other.implementation;
413 bool operator == (GUID
const & lhs,
GncGUID const & rhs) noexcept
415 return lhs.implementation == GUID(rhs).implementation;
419 operator != (GUID
const & one, GUID
const & two) noexcept
421 return one.implementation != two.implementation;
424 GUID & GUID::operator = (GUID && other) noexcept
426 boost::uuids::swap (other.implementation, implementation);
430 GUID::operator
GncGUID () const noexcept
433 guid_assign (ret, *
this);
void guid_replace(GncGUID *guid)
Generate a new guid.
GncGUID guid_new_return(void)
Generate a new id.
GncGUID * guid_copy(const GncGUID *guid)
Returns a newly allocated GncGUID that matches the passed-in GUID.
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Given a string, replace the given guid with the parsed one unless the given value is null...
GncGUID * guid_new(void)
Allocate and construct a new GUID.
GHashTable * guid_hash_table_new(void)
Returns a GHashTable with <GUID*> as key and a <gpointer> as value and no destructor functions for ke...
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
guint guid_hash_to_guint(gconstpointer ptr)
Hash function for a GUID.
const GncGUID * gnc_value_get_guid(const GValue *value)
gnc_value_get_guid
#define PERR(format, args...)
Log a serious error.
GncGUID * guid_malloc(void)
Allocate memory for a GUID.
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
Given two GUIDs, return TRUE if they are non-NULL and equal.
gchar * guid_to_string(const GncGUID *guid)
The guid_to_string() routine returns a null-terminated string encoding of the id. ...
const GncGUID * guid_null(void)
Returns a GncGUID which is guaranteed to never reference any entity.
The type used to store guids in C.
gint guid_g_hash_table_equal(gconstpointer guid_a, gconstpointer guid_b)
Equality function for two GUIDs in a GHashTable.