Comment a little bit more the examples

This commit is contained in:
Patrick Pelissier committed 2020-02-10 06:47:24 +01:00
1 parent 0a0d707afd
commit f15203afb2
2 files changed
+17 -11

No files matched your search

+7 -3
View File
@@ -4,12 +4,14 @@
/* This example show how to use complex structure with array /* This example show how to use complex structure with array
embedding another library */ embedding another library */
/* This is a trivial library */ /* This is a typical trivial library with its interface */
typedef struct lib_ext_struct { typedef struct lib_ext_struct {
int id; int id;
// Other data // Other data may be added
} lib_ext_struct; } lib_ext_struct;
/* Method to create a duplicate of the object.
The new object is heap allocated */
static lib_ext_struct *lib_ext_struct_Duplicate(const lib_ext_struct *obj) static lib_ext_struct *lib_ext_struct_Duplicate(const lib_ext_struct *obj)
{ {
lib_ext_struct *p = malloc(sizeof(lib_ext_struct)); lib_ext_struct *p = malloc(sizeof(lib_ext_struct));
@@ -18,6 +20,7 @@ static lib_ext_struct *lib_ext_struct_Duplicate(const lib_ext_struct *obj)
return p; return p;
} }
/* Method to delete any object */
static void lib_ext_struct_Delete(lib_ext_struct *obj) static void lib_ext_struct_Delete(lib_ext_struct *obj)
{ {
free(obj); free(obj);
@@ -101,11 +104,12 @@ ARRAY_DEF(/* the array prefix */ array_data_node,
INIT_SET(API_6(data_node_init_set)), INIT_SET(API_6(data_node_init_set)),
CLEAR(API_2(data_node_clear)))) CLEAR(API_2(data_node_clear))))
/* Define a global array */ /* Define a global array with the created type */
array_data_node_t global_array; array_data_node_t global_array;
int main(void) int main(void)
{ {
/* Call the created method */
array_data_node_init(global_array); array_data_node_init(global_array);
/* Do stuff... */ /* Do stuff... */
array_data_node_clear(global_array); array_data_node_clear(global_array);
+10 -8
View File
@@ -4,6 +4,15 @@
#include "m-rbtree.h" #include "m-rbtree.h"
// Let's define one utility function.
void *xmalloc(size_t n);
void *xmalloc(size_t n)
{
void *p = malloc (n);
if (!p) abort();
return p;
}
// Let's define a new type. // Let's define a new type.
typedef struct { typedef struct {
int key; int key;
@@ -12,7 +21,7 @@ typedef struct {
#define SIZE 256 #define SIZE 256
void *xmalloc(size_t n); // Let's define the prototypes of the methods of this new type.
void my_type_init(my_type_t x); void my_type_init(my_type_t x);
void my_type_clear(my_type_t x); void my_type_clear(my_type_t x);
void my_type_init_set(my_type_t x, const my_type_t y); void my_type_init_set(my_type_t x, const my_type_t y);
@@ -24,13 +33,6 @@ void my_type_out_str(FILE *f, const my_type_t x);
bool my_type_in_str(my_type_t x, FILE *f); bool my_type_in_str(my_type_t x, FILE *f);
void my_type_rand(my_type_t x); void my_type_rand(my_type_t x);
void *xmalloc(size_t n)
{
void *p = malloc (n);
if (!p) abort();
return p;
}
// Let's define its constructor // Let's define its constructor
void my_type_init(my_type_t x) void my_type_init(my_type_t x)
{ {