32 #ifdef HAVE_SYS_TYPES_H 33 # include <sys/types.h> 41 #include <glib/gstdio.h> 45 #ifdef HAVE_SYS_TIMES_H 46 # include <sys/times.h> 54 #include <boost/uuid/uuid.hpp> 55 #include <boost/uuid/uuid_generators.hpp> 56 #include <boost/uuid/uuid_io.hpp> 62 static QofLogModule log_module = QOF_MOD_ENGINE;
74 if (!value)
return nullptr;
77 g_return_val_if_fail (value && G_IS_VALUE (value),
nullptr);
78 g_return_val_if_fail (GNC_VALUE_HOLDS_GUID (value),
nullptr);
80 val = (
GncGUID*) g_value_get_boxed (value);
87 static gnc::GUID s_null_guid {boost::uuids::uuid { {0}}};
88 static GncGUID * s_null_gncguid {guid_convert_create (s_null_guid)};
97 guid_convert_create (
gnc::GUID const & guid)
113 if (guid == s_null_gncguid)
122 if (!guid)
return nullptr;
132 return s_null_gncguid;
138 std::copy (source.begin(), source.end(), target.reserved);
146 gnc::GUID temp_random {gnc::GUID::create_random ()};
147 guid_assign (*guid, temp_random);
160 return gnc::GUID::create_random ();
166 if (!guid)
return nullptr;
168 auto temp_str = temp.to_string ();
169 return g_strdup (temp_str.c_str ());
175 if (!str || !guid)
return nullptr;
178 auto val = temp.to_string ();
182 std::copy (val.c_str (), val.c_str () + val.size () + 1, str);
183 return str + val.size ();
189 if (!guid || !str || !*str)
return false;
193 guid_assign (*guid, gnc::GUID::from_string (str));
197 PINFO(
"Failed to construct a GUID from %s", 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 nullptr guid pointer.");
235 GncGUID const & guid = * reinterpret_cast <
GncGUID const *> (ptr);
239 std::for_each (temp.begin (), temp.end (), [&hash] (
unsigned char a) {
249 return guid_equal (reinterpret_cast<const GncGUID*> (guid_a),
250 reinterpret_cast<const GncGUID*> (guid_b));
261 gnc_string_to_guid (
const GValue *src, GValue *dest)
265 const gchar *as_string;
267 g_return_if_fail (G_VALUE_HOLDS_STRING (src) &&
268 GNC_VALUE_HOLDS_GUID (dest));
270 as_string = g_value_get_string (src);
275 g_value_take_boxed (dest, guid);
279 gnc_guid_to_string (
const GValue *src, GValue *dest)
283 g_return_if_fail (G_VALUE_HOLDS_STRING (dest) &&
284 GNC_VALUE_HOLDS_GUID (src));
288 g_value_set_string (dest, str);
292 g_value_register_transform_func (G_TYPE_STRING,
296 g_value_register_transform_func (g_define_type_id,
305 GUID::create_random () noexcept
307 static boost::uuids::random_generator gen;
311 GUID::GUID (boost::uuids::uuid
const & other) noexcept
312 : implementation (other)
317 GUID::null_guid () noexcept
323 GUID::to_string () const noexcept
325 auto const & val = boost::uuids::to_string (implementation);
327 std::for_each (val.begin (), val.end (), [&ret] (
char a) {
328 if (a !=
'-') ret.push_back (a);
334 GUID::from_string (
const char* str)
337 throw guid_syntax_exception {};
340 static boost::uuids::string_generator strgen;
345 throw guid_syntax_exception {};
350 GUID::is_valid_guid (
const char* str)
354 static boost::uuids::string_generator strgen;
364 guid_syntax_exception::guid_syntax_exception () noexcept
365 : invalid_argument {
"Invalid syntax for guid."}
369 GUID::GUID (
GncGUID const & other) noexcept
370 : implementation {{other.reserved[0] , other.reserved[1]
371 , other.reserved[2], other.reserved[3]
372 , other.reserved[4], other.reserved[5]
373 , other.reserved[6], other.reserved[7]
374 , other.reserved[8], other.reserved[9]
375 , other.reserved[10], other.reserved[11]
376 , other.reserved[12], other.reserved[13]
377 , other.reserved[14], other.reserved[15]}
384 GUID::end () const noexcept -> decltype (implementation.end ())
386 return implementation.end ();
390 GUID::begin () const noexcept -> decltype (implementation.begin ())
392 return implementation.begin ();
396 GUID::operator < (GUID
const & other) noexcept
398 return implementation < other.implementation;
401 bool operator == (GUID
const & lhs,
GncGUID const & rhs) noexcept
403 return lhs.implementation == GUID(rhs).implementation;
407 operator != (GUID
const & one, GUID
const & two) noexcept
409 return one.implementation != two.implementation;
412 GUID & GUID::operator = (GUID && other) noexcept
414 boost::uuids::swap (other.implementation, implementation);
418 GUID::operator
GncGUID () const noexcept
421 guid_assign (ret, *
this);
void guid_replace(GncGUID *guid)
Generate a new guid.
#define PINFO(format, args...)
Print an informational note.
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.
globally unique ID User API
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.