-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add registry for numerical type size and endianness #17
Open
arteymix
wants to merge
6
commits into
master
Choose a base branch
from
wip/numeric-type-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3268d1
Add initial prototypes for storing byte size and endiannes
arteymix e121043
Add a prototype implementation for the type info table with decimal128
arteymix af66464
Register all NumericTypeInfo structs and reuse GLib byte order macros
arteymix 2b4c70d
Remove dynamic initialization of GLib numerical types and simplify API
arteymix 66dda91
Fix byte order information for _le and _be flavours
arteymix 8614cd6
Add init/clear for NumericTypeInfo
arteymix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,15 @@ | |
|
||
#include "numeric-types.h" | ||
|
||
#include <glib.h> | ||
#include <string.h> | ||
|
||
#define DEFINE_NUMERIC(type) \ | ||
G_DEFINE_BOXED_TYPE (numeric_##type, \ | ||
numeric_##type, \ | ||
numeric_##type##_copy, \ | ||
numeric_##type##_free) \ | ||
G_DEFINE_BOXED_TYPE_WITH_CODE (numeric_##type, \ | ||
numeric_##type, \ | ||
numeric_##type##_copy, \ | ||
numeric_##type##_free, \ | ||
numeric_type_register_static_simple (g_define_type_id, G_STRINGIFY(type), sizeof (numeric_##type), G_BYTE_ORDER)) \ | ||
\ | ||
numeric_##type * \ | ||
numeric_##type##_copy (numeric_##type *num) \ | ||
|
@@ -54,17 +56,6 @@ numeric_value_set_##type (GValue *val, numeric_##type x) \ | |
g_value_set_boxed (val, ptr); \ | ||
} | ||
|
||
DEFINE_NUMERIC (int128) | ||
DEFINE_NUMERIC (uint128) | ||
DEFINE_NUMERIC (float80) | ||
DEFINE_NUMERIC (float128) | ||
DEFINE_NUMERIC (decimal32) | ||
DEFINE_NUMERIC (decimal64) | ||
DEFINE_NUMERIC (decimal128) | ||
DEFINE_NUMERIC (complex) | ||
DEFINE_NUMERIC (complex80) | ||
DEFINE_NUMERIC (complex128) | ||
|
||
#define DEFINE_NUMERIC_WITH_BYTESWAP(type,gtype,stype,routine,order) \ | ||
DEFINE_NUMERIC (type) \ | ||
numeric_##type \ | ||
|
@@ -93,6 +84,114 @@ numeric_##type##_to_##gtype (numeric_##type num) \ | |
return data.v_##gtype; \ | ||
} | ||
|
||
static GArray *TYPE_TABLE = NULL; | ||
|
||
/** | ||
* | ||
*/ | ||
void numeric_type_register_static (GType type, | ||
const gchar *name, | ||
const NumericTypeInfo *type_info) | ||
{ | ||
if (TYPE_TABLE == NULL) | ||
{ | ||
TYPE_TABLE = g_array_new (FALSE, FALSE, sizeof (NumericTypeInfo)); | ||
} | ||
|
||
/* make sure we don't register the same type twice */ | ||
gint i; | ||
for (i = 0; i < TYPE_TABLE->len; i++) | ||
{ | ||
NumericTypeInfo* ti = &g_array_index (TYPE_TABLE, NumericTypeInfo, i); | ||
if (ti->type == type_info->type) | ||
return; | ||
} | ||
|
||
g_array_append_vals (TYPE_TABLE, type_info, 1); | ||
} | ||
|
||
void | ||
numeric_type_register_static_simple (GType type, | ||
const gchar *name, | ||
gsize width, | ||
gint byte_order) | ||
{ | ||
NumericTypeInfo ti = { | ||
.type = type, | ||
.name = g_strdup (name), | ||
.width = width, | ||
.byte_order = byte_order}; | ||
|
||
numeric_type_register_static (type, name, &ti); | ||
} | ||
|
||
/* | ||
* GLib numerical types already have existing definitions for *_get_type(), | ||
* preventing us from registering their numeric_{type}_get_type() counterpart | ||
* lazily. | ||
*/ | ||
static void | ||
register_glib_numerical_types () | ||
{ | ||
numeric_type_register_static_simple (G_TYPE_CHAR, "char", sizeof (gchar), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_UCHAR, "uchar", sizeof (guchar), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_INT, "int", sizeof (gint), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_UINT, "uint", sizeof (guint), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_LONG, "long", sizeof (glong), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_ULONG, "ulong", sizeof (gulong), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_INT64, "int64", sizeof (gint64), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_UINT64, "uint64", sizeof (guint64), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_FLOAT, "float", sizeof (gfloat), G_BYTE_ORDER); | ||
numeric_type_register_static_simple (G_TYPE_DOUBLE, "double", sizeof (gdouble), G_BYTE_ORDER); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great if we could enumerate these types at runtime so that the we could be forward compatibly if new types are defined in GLib. |
||
|
||
const NumericTypeInfo * | ||
numeric_get_type_info (GType type) | ||
{ | ||
register_glib_numerical_types (); | ||
|
||
gint i; | ||
for (i = 0; i < TYPE_TABLE->len; i++) | ||
{ | ||
NumericTypeInfo* ti = &g_array_index (TYPE_TABLE, NumericTypeInfo, i); | ||
if (ti->type == type) | ||
{ | ||
return ti; | ||
} | ||
} | ||
|
||
g_return_val_if_reached (NULL); | ||
} | ||
|
||
const NumericTypeInfo * | ||
numeric_get_type_info_from_name (const gchar *name) | ||
{ | ||
register_glib_numerical_types (); | ||
|
||
gint i; | ||
for (i = 0; i < TYPE_TABLE->len; i++) | ||
{ | ||
const NumericTypeInfo* ti = &g_array_index (TYPE_TABLE, NumericTypeInfo, i); | ||
if (g_strcmp0 (ti->name, name) == 0) | ||
{ | ||
return ti; | ||
} | ||
} | ||
|
||
g_return_val_if_reached (NULL); | ||
} | ||
|
||
DEFINE_NUMERIC (int128) | ||
DEFINE_NUMERIC (uint128) | ||
DEFINE_NUMERIC (float80) | ||
DEFINE_NUMERIC (float128) | ||
DEFINE_NUMERIC (decimal32) | ||
DEFINE_NUMERIC (decimal64) | ||
DEFINE_NUMERIC (decimal128) | ||
DEFINE_NUMERIC (complex) | ||
DEFINE_NUMERIC (complex80) | ||
DEFINE_NUMERIC (complex128) | ||
|
||
DEFINE_NUMERIC_WITH_BYTESWAP (int_le, int, gint, GINT, LE) | ||
DEFINE_NUMERIC_WITH_BYTESWAP (int_be, int, gint, GINT, BE) | ||
DEFINE_NUMERIC_WITH_BYTESWAP (uint_le, uint, guint, GUINT, LE) | ||
|
@@ -112,11 +211,13 @@ DEFINE_NUMERIC_WITH_BYTESWAP (double_be, double, gint32, GINT64, BE) | |
|
||
#if HAVE_LIBDFP | ||
extern void register_printf_dfp (void); | ||
#endif | ||
|
||
__attribute__ ((constructor)) | ||
static void | ||
numeric_types_init (void) | ||
{ | ||
#if HAVE_LIBDFP | ||
arteymix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
register_printf_dfp (); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions should be defined within the
TypeInfo
class as static.