project_files/frontlib/util/buffer.h
author Medo <smaxein@googlemail.com>
Tue, 12 Jun 2012 11:25:05 +0200
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7234 613998625a3c
permissions -rw-r--r--
Cleanup, refactoring and generally more development in the frontlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#ifndef BUFFER_H_
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#define BUFFER_H_
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include <stdint.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include <stddef.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
 * A simple struct to hold both the pointer to an array and its size,
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
 * for e.g. conveniently returning it from a function.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 *
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * Convention: Size is zero iff data is a NULL pointer.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
typedef struct {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	void *data;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
	size_t size;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
} flib_buffer;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
 * Just like flib_buffer, but the contents are not supposed to be modified.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
typedef struct {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	const void *data;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	size_t size;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
} flib_constbuffer;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
/**
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    27
 * Simple variable-capacity data structure that can be efficiently appended to.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
struct _flib_vector;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    30
typedef struct _flib_vector flib_vector;
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
 * Create a new vector. Needs to be destroyed again later with flib_vector_destroy.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
 * May return NULL if memory runs out.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    36
flib_vector *flib_vector_create();
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
/**
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    39
 * Free the memory of this vector
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
void flib_vector_destroy(flib_vector *vec);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
 * Append the provided data to the end of the vector, enlarging it as required.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
 * Returns the ammount of data appended, which is either len (success) or 0 (out of memory).
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    46
 * The vector remains unchanged if appending fails.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    48
int flib_vector_append(flib_vector *vec, const void *data, size_t len);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    49
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    50
/**
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    51
 * Return a pointer to the current data buffer of the vector. This pointer can
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    52
 * become invalid if the vector size or capacity is changed.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    53
 */
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    54
void *flib_vector_data(flib_vector *vec);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
/**
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    57
 * Return the current size of the vector.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    58
 */
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    59
size_t flib_vector_size(flib_vector *vec);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    60
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    61
/**
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    62
 * Return a buffer pointing to the current contents of the vector.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
 * These will become invalid if the vector size or capacity is changed.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    65
flib_buffer flib_vector_as_buffer(flib_vector *vec);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    67
/**
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    68
 * Return a constbuffer pointing to the current contents of the vector.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    69
 * These will become invalid if the vector size or capacity is changed.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    70
 */
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    71
flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
#endif