Introduce typedefs gmio_streamsize_t and gmio_streamoffset_t

This commit is contained in:
Hugues Delorme 2015-11-06 13:43:03 +01:00
parent 82ca359f62
commit b356ced118
19 changed files with 70 additions and 22 deletions

View File

@ -127,7 +127,7 @@ static void allocate_stl_scene(aiScene* pScene)
} }
static void func_ascii_begin_solid( static void func_ascii_begin_solid(
void* cookie, size_t stream_size, const char* solid_name) void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
{ {
aiSceneHelper* helper = (aiSceneHelper*)cookie; aiSceneHelper* helper = (aiSceneHelper*)cookie;
helper->hasToCountTriangle = 1; // true helper->hasToCountTriangle = 1; // true

View File

@ -77,7 +77,7 @@ typedef struct stl_readwrite_conv
} stl_readwrite_conv_t; } stl_readwrite_conv_t;
static void readwrite_ascii_begin_solid( static void readwrite_ascii_begin_solid(
void* cookie, size_t stream_size, const char* solid_name) void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
{ {
stl_readwrite_conv_t* rw_conv = (stl_readwrite_conv_t*)cookie; stl_readwrite_conv_t* rw_conv = (stl_readwrite_conv_t*)cookie;
gmio_stream_t* stream = &rw_conv->trsf.stream; gmio_stream_t* stream = &rw_conv->trsf.stream;

View File

@ -53,7 +53,7 @@ GMIO_INLINE size_t gmio_stream_write(
} }
/*! Safe and convenient function for gmio_stream::func_size() */ /*! Safe and convenient function for gmio_stream::func_size() */
GMIO_INLINE size_t gmio_stream_size(gmio_stream_t* stream) GMIO_INLINE gmio_streamsize_t gmio_stream_size(gmio_stream_t* stream)
{ {
if (stream != NULL && stream->func_size != NULL) if (stream != NULL && stream->func_size != NULL)
return stream->func_size(stream->cookie); return stream->func_size(stream->cookie);

View File

@ -17,17 +17,35 @@
#define GMIO_INTERNAL_SAFE_CAST_H #define GMIO_INTERNAL_SAFE_CAST_H
#include "../global.h" #include "../global.h"
#include "../stream.h"
#include <stddef.h> #include <stddef.h>
/*! Returns \p val safely casted to unsigned 32b integer */ /*! Returns \p val safely casted to unsigned 32b integer */
GMIO_INLINE uint32_t gmio_size_to_uint32(size_t val) GMIO_INLINE uint32_t gmio_size_to_uint32(size_t val)
{ {
#if GMIO_TARGET_ARCH_BIT_SIZE > 32 #if GMIO_TARGET_ARCH_BIT_SIZE > 32
/* TODO : eliminate branch */
return val > 0xFFFFFFFF ? 0xFFFFFFFF : (uint32_t)val; return val > 0xFFFFFFFF ? 0xFFFFFFFF : (uint32_t)val;
#else #else
return val; return val;
#endif #endif
} }
#define GMIO_MAX_SIZET ((size_t)-1)
/*! Returns \p val safely casted to \c size_t */
GMIO_INLINE size_t gmio_streamsize_to_size(gmio_streamsize_t val)
{
#if GMIO_TARGET_ARCH_BIT_SIZE < 64 \
&& defined(GMIO_HAVE_INT64_TYPE)
/* TODO : eliminate branch */
const uint64_t uval = val;
return uval > GMIO_MAX_SIZET ? GMIO_MAX_SIZET : (size_t)uval;
#else
return (size_t)val;
#endif
}
#endif /* GMIO_INTERNAL_SAFE_CAST_H */ #endif /* GMIO_INTERNAL_SAFE_CAST_H */

View File

@ -73,7 +73,7 @@ static size_t gmio_stream_stdio_write(
return fwrite(ptr, item_size, item_count, (FILE*)cookie); return fwrite(ptr, item_size, item_count, (FILE*)cookie);
} }
static size_t gmio_stream_stdio_size(void* cookie) static gmio_streamsize_t gmio_stream_stdio_size(void* cookie)
{ {
FILE* file = (FILE*)cookie; FILE* file = (FILE*)cookie;

View File

@ -27,6 +27,14 @@
#include "stream_pos.h" #include "stream_pos.h"
#include <stdio.h> #include <stdio.h>
#ifdef GMIO_HAVE_INT64_TYPE
typedef int64_t gmio_streamsize_t;
typedef int64_t gmio_streamoffset_t;
#else
typedef long gmio_streamsize_t;
typedef long gmio_streamoffset_t;
#endif
/*! Stream that can get input from an arbitrary data source or can write /*! Stream that can get input from an arbitrary data source or can write
* output to an arbitrary data sink. * output to an arbitrary data sink.
* *
@ -93,7 +101,7 @@ struct gmio_stream
size_t (*func_write)(void* cookie, const void* ptr, size_t size, size_t count); size_t (*func_write)(void* cookie, const void* ptr, size_t size, size_t count);
/*! Pointer on a function that returns the size(in bytes) of the stream */ /*! Pointer on a function that returns the size(in bytes) of the stream */
size_t (*func_size)(void* cookie); gmio_streamsize_t (*func_size)(void* cookie);
/*! Pointer on a function that retrieves the current position in the stream /*! Pointer on a function that retrieves the current position in the stream
* *

View File

@ -26,7 +26,7 @@
#include "global.h" #include "global.h"
/*! Size of the byte array gmio_stream_pos::cookie */ /*! Size of the byte array gmio_stream_pos::cookie */
enum { GMIO_STREAM_POS_COOKIE_SIZE = 32 }; /* 32 bytes */ enum { GMIO_STREAM_POS_COOKIE_SIZE = 32 /* bytes */ };
/*! Specifies a position within a stream /*! Specifies a position within a stream
* *

View File

@ -22,7 +22,7 @@
* gmio_stl_mesh_creator::func_ascii_begin_solid() */ * gmio_stl_mesh_creator::func_ascii_begin_solid() */
GMIO_INLINE void gmio_stl_mesh_creator_ascii_begin_solid( GMIO_INLINE void gmio_stl_mesh_creator_ascii_begin_solid(
gmio_stl_mesh_creator_t* creator, gmio_stl_mesh_creator_t* creator,
size_t stream_size, gmio_streamsize_t stream_size,
const char* solid_name) const char* solid_name)
{ {
if (creator != NULL && creator->func_ascii_begin_solid != NULL) { if (creator != NULL && creator->func_ascii_begin_solid != NULL) {

View File

@ -30,7 +30,7 @@ typedef void (*gmio_stl_mesh_func_get_triangle_t)(
/* gmio_stl_mesh_creator */ /* gmio_stl_mesh_creator */
typedef void (*gmio_stl_mesh_creator_func_ascii_begin_solid_t)( typedef void (*gmio_stl_mesh_creator_func_ascii_begin_solid_t)(
void*, size_t, const char*); void*, gmio_streamsize_t, const char*);
typedef void (*gmio_stl_mesh_creator_func_binary_begin_solid_t)( typedef void (*gmio_stl_mesh_creator_func_binary_begin_solid_t)(
void*, uint32_t, const gmio_stlb_header_t*); void*, uint32_t, const gmio_stlb_header_t*);
typedef void (*gmio_stl_mesh_creator_func_add_triangle_t)( typedef void (*gmio_stl_mesh_creator_func_add_triangle_t)(

View File

@ -49,7 +49,7 @@ gmio_stl_format_t gmio_stl_get_format(gmio_stream_t *stream)
/* Binary STL ? */ /* Binary STL ? */
if (read_size >= (GMIO_STLB_HEADER_SIZE + 4)) { if (read_size >= (GMIO_STLB_HEADER_SIZE + 4)) {
const size_t stream_size = gmio_stream_size(stream); const gmio_streamsize_t stream_size = gmio_stream_size(stream);
/* Try with little-endian format */ /* Try with little-endian format */
uint32_t facet_count = uint32_t facet_count =

View File

@ -26,6 +26,7 @@
#include "stl_global.h" #include "stl_global.h"
#include "stl_triangle.h" #include "stl_triangle.h"
#include "stlb_header.h" #include "stlb_header.h"
#include "../gmio_core/stream.h"
#include <stddef.h> #include <stddef.h>
@ -48,7 +49,7 @@ struct gmio_stl_mesh_creator
* stream * stream
*/ */
void (*func_ascii_begin_solid)( void (*func_ascii_begin_solid)(
void* cookie, size_t stream_size, const char* solid_name); void* cookie, gmio_streamsize_t stream_size, const char* solid_name);
/*! Pointer on a function that handles declaration of a mesh with /*! Pointer on a function that handles declaration of a mesh with
* \p tri_count number of triangles * \p tri_count number of triangles

View File

@ -134,9 +134,9 @@ typedef struct
/* Copy of gmio_stla_read() corresponding argument */ /* Copy of gmio_stla_read() corresponding argument */
gmio_transfer_t* transfer; gmio_transfer_t* transfer;
/* Cache for gmio_stream_size(&transfer->stream) */ /* Cache for gmio_stream_size(&transfer->stream) */
size_t stream_size; gmio_streamsize_t stream_size;
/* Offset (in bytes) from beginning of stream : current position */ /* Offset (in bytes) from beginning of stream : current position */
size_t stream_offset; gmio_streamoffset_t stream_offset;
/* Cache for gmio_transfer::func_is_stop_requested() */ /* Cache for gmio_transfer::func_is_stop_requested() */
gmio_bool_t is_stop_requested; gmio_bool_t is_stop_requested;
} gmio_string_stream_fwd_iterator_cookie_t; } gmio_string_stream_fwd_iterator_cookie_t;

View File

@ -20,6 +20,10 @@
* @{ * @{
*/ */
#ifndef __cplusplus
# error C++ compiler required
#endif
#ifndef GMIO_SUPPORT_STREAM_CPP_H #ifndef GMIO_SUPPORT_STREAM_CPP_H
#define GMIO_SUPPORT_STREAM_CPP_H #define GMIO_SUPPORT_STREAM_CPP_H
@ -68,7 +72,7 @@ size_t istream_cpp_read(
{ {
STREAM* s = static_cast<STREAM*>(cookie); STREAM* s = static_cast<STREAM*>(cookie);
s->read(static_cast<char*>(ptr), item_size * item_count); s->read(static_cast<char*>(ptr), item_size * item_count);
return s->gcount() / item_size; return static_cast<size_t>(s->gcount() / item_size);
} }
template<typename STREAM> template<typename STREAM>
@ -82,7 +86,7 @@ size_t ostream_cpp_write(
} }
template<typename STREAM> template<typename STREAM>
size_t istream_cpp_size(void* cookie) gmio_streamsize_t istream_cpp_size(void* cookie)
{ {
STREAM* s = static_cast<STREAM*>(cookie); STREAM* s = static_cast<STREAM*>(cookie);
std::streampos pos = s->tellg(); std::streampos pos = s->tellg();
@ -95,7 +99,7 @@ size_t istream_cpp_size(void* cookie)
} }
template<typename STREAM> template<typename STREAM>
size_t ostream_cpp_size(void* cookie) gmio_streamsize_t ostream_cpp_size(void* cookie)
{ {
STREAM* s = static_cast<STREAM*>(cookie); STREAM* s = static_cast<STREAM*>(cookie);
std::streampos pos = s->tellp(); std::streampos pos = s->tellp();

View File

@ -46,7 +46,7 @@ static size_t gmio_stream_qiodevice_read(
{ {
QIODevice* device = static_cast<QIODevice*>(cookie); QIODevice* device = static_cast<QIODevice*>(cookie);
const qint64 c = device->read(static_cast<char*>(ptr), item_size * item_count); const qint64 c = device->read(static_cast<char*>(ptr), item_size * item_count);
return c / item_size; return static_cast<size_t>(c / item_size);
} }
static size_t gmio_stream_qiodevice_write( static size_t gmio_stream_qiodevice_write(
@ -55,10 +55,10 @@ static size_t gmio_stream_qiodevice_write(
QIODevice* device = static_cast<QIODevice*>(cookie); QIODevice* device = static_cast<QIODevice*>(cookie);
const qint64 c = device->write( const qint64 c = device->write(
static_cast<const char*>(ptr), item_size * item_count); static_cast<const char*>(ptr), item_size * item_count);
return c / item_size; return static_cast<size_t>(c / item_size);
} }
static size_t gmio_stream_qiodevice_size(void* cookie) static gmio_streamsize_t gmio_stream_qiodevice_size(void* cookie)
{ {
QIODevice* device = static_cast<QIODevice*>(cookie); QIODevice* device = static_cast<QIODevice*>(cookie);
return device->size(); return device->size();

View File

@ -20,6 +20,10 @@
* @{ * @{
*/ */
#ifndef __cplusplus
# error C++ compiler required
#endif
#ifndef GMIO_SUPPORT_STREAM_QT_H #ifndef GMIO_SUPPORT_STREAM_QT_H
#define GMIO_SUPPORT_STREAM_QT_H #define GMIO_SUPPORT_STREAM_QT_H

View File

@ -44,7 +44,7 @@ gmio_stl_triangle_array_t gmio_stl_triangle_array_malloc(size_t tri_count)
} }
static void gmio_stl_data__ascii_begin_solid( static void gmio_stl_data__ascii_begin_solid(
void* cookie, size_t stream_size, const char* solid_name) void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
{ {
gmio_stl_data_t* data = (gmio_stl_data_t*)cookie; gmio_stl_data_t* data = (gmio_stl_data_t*)cookie;
@ -59,7 +59,8 @@ static void gmio_stl_data__ascii_begin_solid(
* for each face */ * for each face */
{ {
const size_t facet_size = 200; const size_t facet_size = 200;
const size_t facet_count = GMIO_MAX(1, stream_size / facet_size); const size_t facet_count =
gmio_streamsize_to_size(GMIO_MAX(1, stream_size / facet_size));
data->tri_array = gmio_stl_triangle_array_malloc(facet_count); data->tri_array = gmio_stl_triangle_array_malloc(facet_count);
} }
} }

View File

@ -78,7 +78,7 @@ static size_t gmio_stream_buffer_write(
} }
} }
static size_t gmio_stream_buffer_size(void* cookie) static gmio_streamsize_t gmio_stream_buffer_size(void* cookie)
{ {
const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie; const gmio_stream_buffer_t* buff = (const gmio_stream_buffer_t*)cookie;
return buff->len; return buff->len;

View File

@ -132,6 +132,18 @@ const char* test_internal__safe_cast()
UTEST_ASSERT(gmio_size_to_uint32(0xFFFFFFFF) == 0xFFFFFFFF); UTEST_ASSERT(gmio_size_to_uint32(0xFFFFFFFF) == 0xFFFFFFFF);
UTEST_ASSERT(gmio_size_to_uint32(100) == 100); UTEST_ASSERT(gmio_size_to_uint32(100) == 100);
#endif #endif
UTEST_ASSERT(gmio_streamsize_to_size(-1) == ((size_t)-1));
#ifdef GMIO_HAVE_INT64_TYPE
# if GMIO_TARGET_ARCH_BIT_SIZE < 64
const gmio_streamsize_t overMaxSizet =
((gmio_streamsize_t)GMIO_MAX_SIZET) + 1;
UTEST_ASSERT(gmio_streamsize_to_size(overMaxSizet) == GMIO_MAX_SIZET);
# endif
UTEST_ASSERT(gmio_streamsize_to_size(GMIO_MAX_SIZET) == GMIO_MAX_SIZET);
UTEST_ASSERT(gmio_streamsize_to_size(150) == 150);
#endif
return NULL; return NULL;
} }

View File

@ -36,7 +36,7 @@ struct stl_testcase_result
typedef struct stl_testcase_result stl_testcase_result_t; typedef struct stl_testcase_result stl_testcase_result_t;
void stl_testcase_result__ascii_begin_solid( void stl_testcase_result__ascii_begin_solid(
void* cookie, size_t stream_size, const char* solid_name) void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
{ {
stl_testcase_result_t* res = (stl_testcase_result_t*)cookie; stl_testcase_result_t* res = (stl_testcase_result_t*)cookie;
GMIO_UNUSED(stream_size); GMIO_UNUSED(stream_size);