Clean up guid usage.

Now that there is a subclass of boost::uuids::uuid, that subclass can be
used for GncGUID implementation. This removes the need for the untidy
casts to/from bost::uuids::uuid, and simplifies the logic in many areas.
This commit is contained in:
lmat 2016-06-07 18:06:02 -04:00 committed by John Ralls
parent 293a286693
commit 1c00937abe
2 changed files with 31 additions and 71 deletions

View File

@ -59,8 +59,6 @@ extern "C"
#include <sstream>
#include <string>
using namespace std;
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = QOF_MOD_ENGINE;
@ -74,6 +72,7 @@ static QofLogModule log_module = QOF_MOD_ENGINE;
const GncGUID*
gnc_value_get_guid (const GValue *value)
{
if (!value) return nullptr;
GncGUID *val;
g_return_val_if_fail (value && G_IS_VALUE (value), NULL);
@ -98,74 +97,52 @@ guid_null (void)
GncGUID *
guid_malloc (void)
{
/*Note, the Boost uuid is a POD, so its constructor is trivial*/
return reinterpret_cast<GncGUID*> (new boost::uuids::uuid);
return new GncGUID;
}
void
guid_free (GncGUID *guid)
{
if (!guid)
return;
if (!guid) return;
if (guid == &s_null_guid)
/*!!Don't delete that!!*/
return;
delete reinterpret_cast<boost::uuids::uuid*> (guid);
guid = nullptr;
delete guid;
}
GncGUID *
guid_copy (const GncGUID *guid)
{
const boost::uuids::uuid * old {reinterpret_cast<const boost::uuids::uuid*> (guid)};
boost::uuids::uuid * ret {new boost::uuids::uuid (*old)};
return reinterpret_cast<GncGUID*> (ret);
if (!guid) return nullptr;
return new GncGUID {*guid};
}
/*Takes an allocated guid pointer and constructs it in place*/
void
guid_replace (GncGUID *guid)
{
static boost::uuids::random_generator gen;
boost::uuids::uuid * val {reinterpret_cast<boost::uuids::uuid*> (guid)};
val->boost::uuids::uuid::~uuid ();
boost::uuids::uuid temp (gen ());
val->swap (temp);
if (!guid) return;
auto other = GncGUID::create_random();
guid->swap (other);
}
GncGUID *
guid_new (void)
{
GncGUID * ret {guid_malloc ()};
guid_replace (ret);
return ret;
return new GncGUID {GncGUID::create_random ()};
}
GncGUID
guid_new_return (void)
{
/*we need to construct our value as a boost guid so that
it can be deconstructed (in place) in guid_replace*/
boost::uuids::uuid guid;
GncGUID * ret {reinterpret_cast<GncGUID*> (&guid)};
guid_replace (ret);
/*return a copy*/
return *ret;
return GncGUID::create_random ();
}
gchar *
guid_to_string (const GncGUID * guid)
{
/* We need to malloc here, not 'new' because it will be freed
by the caller which will use free (not delete).*/
gchar * ret {reinterpret_cast<gchar*> (g_malloc (sizeof (gchar)*GUID_ENCODING_LENGTH+1))};
gchar * temp {guid_to_string_buff (guid, ret)};
if (!temp){
g_free (ret);
return nullptr;
}
return ret;
if (!guid) return nullptr;
return g_strdup (guid->to_string ().c_str ());
}
gchar *
@ -173,28 +150,21 @@ guid_to_string_buff (const GncGUID * guid, gchar *str)
{
if (!str || !guid) return NULL;
boost::uuids::uuid const & tempg = *reinterpret_cast<boost::uuids::uuid const *> (guid);
unsigned destspot {0};
string const & val {to_string (tempg)};
for (auto val_char : val)
if (val_char != '-')
str [destspot++] = val_char;
str[GUID_ENCODING_LENGTH] = '\0';
return &str[GUID_ENCODING_LENGTH];
auto val = guid->to_string ();
/*We need to be sure to copy the terminating null character.*/
std::copy (val.c_str (), val.c_str() + val.size () + 1, str);
return str + val.size();
}
gboolean
string_to_guid (const char * str, GncGUID * guid)
{
if (!guid || !str)
return false;
if (!guid || !str) return false;
try
{
static boost::uuids::string_generator strgen;
boost::uuids::uuid * converted {reinterpret_cast<boost::uuids::uuid*> (guid)};
new (converted) boost::uuids::uuid (strgen (str));
auto other = GncGUID::from_string (str);
guid->swap (other);
}
catch (...)
{
@ -207,20 +177,16 @@ gboolean
guid_equal (const GncGUID *guid_1, const GncGUID *guid_2)
{
if (!guid_1 || !guid_2)
return false;
boost::uuids::uuid const * g1 {reinterpret_cast<boost::uuids::uuid const *> (guid_1)};
boost::uuids::uuid const * g2 {reinterpret_cast<boost::uuids::uuid const *> (guid_2)};
return *g1 == *g2;
return !guid_1 && !guid_2;
return *guid_1 == *guid_2;
}
gint
guid_compare (const GncGUID *guid_1, const GncGUID *guid_2)
{
boost::uuids::uuid const * g1 {reinterpret_cast<boost::uuids::uuid const *> (guid_1)};
boost::uuids::uuid const * g2 {reinterpret_cast<boost::uuids::uuid const *> (guid_2)};
if (*g1 < *g2)
if (*guid_1 < *guid_2)
return -1;
if (*g1 == *g2)
if (*guid_1 == *guid_2)
return 0;
return 1;
}
@ -228,21 +194,19 @@ guid_compare (const GncGUID *guid_1, const GncGUID *guid_2)
guint
guid_hash_to_guint (gconstpointer ptr)
{
const boost::uuids::uuid * guid = reinterpret_cast<const boost::uuids::uuid*> (ptr);
if (!guid)
if (!ptr)
{
PERR ("received NULL guid pointer.");
return 0;
}
GncGUID const & guid = * reinterpret_cast <GncGUID const *> (ptr);
guint hash {0};
unsigned retspot {0};
for (auto guidspot : *guid)
{
hash <<= 4;
hash |= guidspot;
}
std::for_each (guid.begin (), guid.end (), [&hash] (unsigned char a) {
hash <<=4;
hash |= a;
});
return hash;
}
@ -321,10 +285,6 @@ GncGUID::create_random () noexcept
return {gen ()};
}
GncGUID::GncGUID () noexcept
{
}
GncGUID::GncGUID (boost::uuids::uuid const & other) noexcept
: boost::uuids::uuid (other)
{

View File

@ -33,7 +33,7 @@ struct guid_syntax_exception : public std::invalid_argument
struct GncGUID : public boost::uuids::uuid
{
GncGUID (boost::uuids::uuid const &) noexcept;
GncGUID () noexcept;
GncGUID () noexcept = default;
static GncGUID create_random () noexcept;
static GncGUID const & null_guid () noexcept;
static GncGUID from_string (std::string const &) throw (guid_syntax_exception);