mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Separate C guid from C++ guid
This commit is contained in:
parent
e1e85cee16
commit
0a5a0ab7ab
@ -83,17 +83,24 @@ gnc_value_get_guid (const GValue *value)
|
||||
return val;
|
||||
}
|
||||
|
||||
static GncGUID s_null_guid {{{0}}};
|
||||
GncGUID * guid_convert_create (gnc::GUID const &);
|
||||
|
||||
/*It looks like we are expected to provide the same pointer every time from this function*/
|
||||
const GncGUID *
|
||||
guid_null (void)
|
||||
{
|
||||
return &s_null_guid;
|
||||
}
|
||||
static gnc::GUID s_null_guid {boost::uuids::uuid { {0}}};
|
||||
static GncGUID * s_null_gncguid {guid_convert_create (s_null_guid)};
|
||||
|
||||
/* Memory management routines ***************************************/
|
||||
|
||||
/**
|
||||
* Allocates and returns a new GncGUID containing the same value as the
|
||||
* gnc::GUID passed in.
|
||||
*/
|
||||
GncGUID *
|
||||
guid_convert_create (gnc::GUID const & guid)
|
||||
{
|
||||
GncGUID temp = guid;
|
||||
return guid_copy (&temp);
|
||||
}
|
||||
|
||||
GncGUID *
|
||||
guid_malloc (void)
|
||||
{
|
||||
@ -104,9 +111,6 @@ void
|
||||
guid_free (GncGUID *guid)
|
||||
{
|
||||
if (!guid) return;
|
||||
if (guid == &s_null_guid)
|
||||
/*!!Don't delete that!!*/
|
||||
return;
|
||||
delete guid;
|
||||
}
|
||||
|
||||
@ -117,32 +121,47 @@ guid_copy (const GncGUID *guid)
|
||||
return new GncGUID {*guid};
|
||||
}
|
||||
|
||||
/*It looks like we are expected to provide the same pointer every time from this function*/
|
||||
const GncGUID *
|
||||
guid_null (void)
|
||||
{
|
||||
return s_null_gncguid;
|
||||
}
|
||||
|
||||
void
|
||||
guid_assign (GncGUID & target, gnc::GUID const & source)
|
||||
{
|
||||
memcpy (&target, &source, sizeof (GncGUID));
|
||||
}
|
||||
|
||||
/*Takes an allocated guid pointer and constructs it in place*/
|
||||
void
|
||||
guid_replace (GncGUID *guid)
|
||||
{
|
||||
if (!guid) return;
|
||||
auto other = GncGUID::create_random();
|
||||
guid->swap (other);
|
||||
gnc::GUID temp_random {gnc::GUID::create_random ()};
|
||||
guid_assign (*guid, temp_random);
|
||||
}
|
||||
|
||||
GncGUID *
|
||||
guid_new (void)
|
||||
{
|
||||
return new GncGUID {GncGUID::create_random ()};
|
||||
return new GncGUID {guid_new_return ()};
|
||||
}
|
||||
|
||||
GncGUID
|
||||
guid_new_return (void)
|
||||
{
|
||||
return GncGUID::create_random ();
|
||||
return gnc::GUID::create_random ();
|
||||
}
|
||||
|
||||
gchar *
|
||||
guid_to_string (const GncGUID * guid)
|
||||
{
|
||||
if (!guid) return nullptr;
|
||||
return g_strdup (guid->to_string ().c_str ());
|
||||
gnc::GUID temp {*guid};
|
||||
auto temp_str = temp.to_string ();
|
||||
return g_strdup (temp_str.c_str ());
|
||||
}
|
||||
|
||||
gchar *
|
||||
@ -150,10 +169,11 @@ guid_to_string_buff (const GncGUID * guid, gchar *str)
|
||||
{
|
||||
if (!str || !guid) return NULL;
|
||||
|
||||
auto val = guid->to_string ();
|
||||
gnc::GUID temp {*guid};
|
||||
auto val = temp.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();
|
||||
std::copy (val.c_str (), val.c_str () + val.size () + 1, str);
|
||||
return str + val.size ();
|
||||
}
|
||||
|
||||
gboolean
|
||||
@ -163,8 +183,7 @@ string_to_guid (const char * str, GncGUID * guid)
|
||||
|
||||
try
|
||||
{
|
||||
auto other = GncGUID::from_string (str);
|
||||
guid->swap (other);
|
||||
guid_assign (*guid, gnc::GUID::from_string (str));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@ -178,15 +197,21 @@ guid_equal (const GncGUID *guid_1, const GncGUID *guid_2)
|
||||
{
|
||||
if (!guid_1 || !guid_2)
|
||||
return !guid_1 && !guid_2;
|
||||
return *guid_1 == *guid_2;
|
||||
gnc::GUID temp1 {*guid_1};
|
||||
gnc::GUID temp2 {*guid_2};
|
||||
return temp1 == temp2;
|
||||
}
|
||||
|
||||
gint
|
||||
guid_compare (const GncGUID *guid_1, const GncGUID *guid_2)
|
||||
{
|
||||
if (*guid_1 < *guid_2)
|
||||
if (!guid_1 || !guid_2)
|
||||
return !guid_1 && !guid_2;
|
||||
gnc::GUID temp1 {*guid_1};
|
||||
gnc::GUID temp2 {*guid_2};
|
||||
if (temp1 < temp2)
|
||||
return -1;
|
||||
if (*guid_1 == *guid_2)
|
||||
if (temp1 == temp2)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
@ -200,10 +225,11 @@ guid_hash_to_guint (gconstpointer ptr)
|
||||
return 0;
|
||||
}
|
||||
GncGUID const & guid = * reinterpret_cast <GncGUID const *> (ptr);
|
||||
gnc::GUID const & temp {guid};
|
||||
|
||||
guint hash {0};
|
||||
unsigned retspot {0};
|
||||
std::for_each (guid.begin (), guid.end (), [&hash] (unsigned char a) {
|
||||
std::for_each (temp.begin (), temp.end (), [&hash] (unsigned char a) {
|
||||
hash <<=4;
|
||||
hash |= a;
|
||||
});
|
||||
@ -278,26 +304,29 @@ gnc_guid_get_type (void)
|
||||
return type;
|
||||
}
|
||||
|
||||
GncGUID
|
||||
GncGUID::create_random () noexcept
|
||||
namespace gnc
|
||||
{
|
||||
|
||||
GUID
|
||||
GUID::create_random () noexcept
|
||||
{
|
||||
static boost::uuids::random_generator gen;
|
||||
return {gen ()};
|
||||
}
|
||||
|
||||
GncGUID::GncGUID (boost::uuids::uuid const & other) noexcept
|
||||
GUID::GUID (boost::uuids::uuid const & other) noexcept
|
||||
: boost::uuids::uuid (other)
|
||||
{
|
||||
}
|
||||
|
||||
GncGUID const &
|
||||
GncGUID::null_guid () noexcept
|
||||
GUID const &
|
||||
GUID::null_guid () noexcept
|
||||
{
|
||||
return s_null_guid;
|
||||
}
|
||||
|
||||
std::string
|
||||
GncGUID::to_string () const noexcept
|
||||
GUID::to_string () const noexcept
|
||||
{
|
||||
auto const & val = boost::uuids::to_string (*this);
|
||||
std::string ret;
|
||||
@ -307,8 +336,8 @@ GncGUID::to_string () const noexcept
|
||||
return ret;
|
||||
}
|
||||
|
||||
GncGUID
|
||||
GncGUID::from_string (std::string const & str) throw (guid_syntax_exception)
|
||||
GUID
|
||||
GUID::from_string (std::string const & str) throw (guid_syntax_exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -325,3 +354,26 @@ guid_syntax_exception::guid_syntax_exception () noexcept
|
||||
: invalid_argument {"Invalid syntax for guid."}
|
||||
{
|
||||
}
|
||||
|
||||
GUID::GUID(GncGUID const & other) noexcept
|
||||
: boost::uuids::uuid {other.reserved[0] , other.reserved[1]
|
||||
, other.reserved[2], other.reserved[3]
|
||||
, other.reserved[4], other.reserved[5]
|
||||
, other.reserved[6], other.reserved[7]
|
||||
, other.reserved[8], other.reserved[9]
|
||||
, other.reserved[10], other.reserved[11]
|
||||
, other.reserved[12], other.reserved[13]
|
||||
, other.reserved[14], other.reserved[15]
|
||||
}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
GUID::operator GncGUID() const noexcept
|
||||
{
|
||||
GncGUID ret;
|
||||
guid_assign (ret, *this);
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace gnc
|
||||
|
@ -66,13 +66,15 @@ extern "C"
|
||||
* found in guid.hpp
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
struct GncGUID;
|
||||
#else
|
||||
namespace gnc {
|
||||
struct GUID;
|
||||
}
|
||||
#endif
|
||||
|
||||
/** The type used to store guids in C */
|
||||
typedef struct _gncGuid {
|
||||
unsigned char reserved[GUID_DATA_SIZE];
|
||||
} GncGUID;
|
||||
#endif
|
||||
|
||||
GType gnc_guid_get_type (void);
|
||||
const GncGUID* gnc_value_get_guid (const GValue *value);
|
||||
|
@ -24,20 +24,30 @@
|
||||
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <stdexcept>
|
||||
extern "C" {
|
||||
#include "guid.h"
|
||||
}
|
||||
|
||||
namespace gnc {
|
||||
struct guid_syntax_exception : public std::invalid_argument
|
||||
{
|
||||
guid_syntax_exception () noexcept;
|
||||
};
|
||||
|
||||
struct GncGUID : public boost::uuids::uuid
|
||||
struct GUID : public boost::uuids::uuid
|
||||
{
|
||||
GncGUID (boost::uuids::uuid const &) 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);
|
||||
GUID (boost::uuids::uuid const &) noexcept;
|
||||
GUID (GncGUID const &) noexcept;
|
||||
GUID () noexcept = default;
|
||||
operator GncGUID() const noexcept;
|
||||
static GUID create_random () noexcept;
|
||||
static GUID const & null_guid () noexcept;
|
||||
static GUID from_string (std::string const &) throw (guid_syntax_exception);
|
||||
std::string to_string () const noexcept;
|
||||
};
|
||||
|
||||
bool operator==(GUID const &, GncGUID const &) noexcept;
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
|
@ -33,24 +33,24 @@
|
||||
|
||||
TEST (GncGUID, creation)
|
||||
{
|
||||
auto guid = GncGUID::create_random ();
|
||||
EXPECT_NE (guid, GncGUID::null_guid ());
|
||||
auto guid = gnc::GUID::create_random ();
|
||||
EXPECT_NE (guid, gnc::GUID::null_guid ());
|
||||
// There should be a default constructor.
|
||||
GncGUID other;
|
||||
}
|
||||
|
||||
TEST (GncGUID, copy)
|
||||
{
|
||||
auto guid = GncGUID::create_random ();
|
||||
auto guid = gnc::GUID::create_random ();
|
||||
auto cpy = guid;
|
||||
EXPECT_EQ (guid, cpy);
|
||||
GncGUID cpy2 {cpy};
|
||||
gnc::GUID cpy2 {cpy};
|
||||
EXPECT_EQ (guid, cpy2);
|
||||
}
|
||||
|
||||
TEST (GncGUID, move)
|
||||
{
|
||||
auto guid = GncGUID::create_random ();
|
||||
auto guid = gnc::GUID::create_random ();
|
||||
auto cpy = guid;
|
||||
auto mv = std::move(guid);
|
||||
EXPECT_EQ (cpy, mv);
|
||||
@ -59,24 +59,24 @@ TEST (GncGUID, move)
|
||||
TEST (GncGUID, to_string)
|
||||
{
|
||||
std::string fixture (32, '0');
|
||||
auto str = GncGUID::null_guid ().to_string ();
|
||||
auto str = gnc::GUID::null_guid ().to_string ();
|
||||
EXPECT_EQ (str, fixture);
|
||||
}
|
||||
|
||||
TEST (GncGUID, from_string)
|
||||
{
|
||||
std::string fixture (32, '0');
|
||||
auto guid = GncGUID::from_string (fixture);
|
||||
EXPECT_EQ (guid, GncGUID::null_guid ());
|
||||
auto guid = gnc::GUID::from_string (fixture);
|
||||
EXPECT_EQ (guid, gnc::GUID::null_guid ());
|
||||
|
||||
guid = GncGUID::create_random ();
|
||||
guid = gnc::GUID::create_random ();
|
||||
std::string bogus {"Have a great big roast beef sandwich, if you please!"};
|
||||
bool fail = false;
|
||||
try
|
||||
{
|
||||
auto guid = GncGUID::from_string (bogus);
|
||||
auto guid = gnc::GUID::from_string (bogus);
|
||||
}
|
||||
catch (guid_syntax_exception const &)
|
||||
catch (gnc::guid_syntax_exception const &)
|
||||
{
|
||||
fail = true;
|
||||
}
|
||||
@ -88,9 +88,9 @@ TEST (GncGUID, from_string)
|
||||
|
||||
TEST (GncGUID, round_trip)
|
||||
{
|
||||
auto guid1 = GncGUID::create_random ();
|
||||
auto guid1 = gnc::GUID::create_random ();
|
||||
auto str = guid1.to_string ();
|
||||
auto guid2 = GncGUID::from_string (str);
|
||||
auto guid2 = gnc::GUID::from_string (str);
|
||||
EXPECT_EQ (guid1, guid2);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user