project_files/frontlib/buffer.h
author Medo <smaxein@googlemail.com>
Sat, 02 Jun 2012 22:54:09 +0200
changeset 7169 b66eef8c8092
parent 7162 fe76d24a25d7
permissions -rw-r--r--
Merge
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
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
 * Simple variable-capacity data structure (opaque type).
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;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
typedef struct _flib_vector *flib_vector;
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
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
flib_vector flib_vector_create();
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
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * Free the memory of this vector and set it to NULL.
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).
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
 * The vector remains unchanged if an out of memory situation occurs.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
int flib_vector_append(flib_vector vec, const void *data, size_t len);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
/**
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
 * Return a buffer or constbuffer pointing to the current contents of the vector.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
 * 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
    53
 */
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
flib_buffer flib_vector_as_buffer(flib_vector vec);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
flib_constbuffer flib_vector_as_constbuffer(flib_vector vec);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
#endif