Skip to content

Commit

Permalink
Added value type (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe authored Oct 24, 2023
1 parent c515507 commit 43b2923
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 56 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ add_subdirectory(tests/map)
add_subdirectory(tests/panic)
add_subdirectory(tests/pool)
add_subdirectory(tests/random)
add_subdirectory(tests/value)

# Tests - picofuse
add_subdirectory(tests/blink)
add_subdirectory(tests/temperature)
if(${TARGET} STREQUAL "pico")
add_subdirectory(tests/blink)
add_subdirectory(tests/temperature)
endif()


2 changes: 1 addition & 1 deletion include/fuse/fuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct fuse_instance fuse_t;
#include <fuse/pool.h>
#include <fuse/random.h>
#include <fuse/sleep.h>
#include <fuse/string.h>
#include <fuse/value.h>

/** @brief Create a new fuse application
*
Expand Down
56 changes: 31 additions & 25 deletions include/fuse/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,79 @@
#ifndef FUSE_LIST_H
#define FUSE_LIST_H
#include "fuse.h"
#include "value.h"

/** @brief Linked list defintion
*/
typedef struct fuse_list_instance fuse_list_t;

/** @brief Create a new empty linked list
* @param fuse The fuse application
* @param file The file from which the linked list is allocated
* @param line The line in the file from which the linked list is allocated
* @returns A new empty linked list, or NULL if memory could not be allocated
*/
fuse_list_t *fuse_list_new_ex(fuse_t *fuse, const char *file, int line);

/** @brief Create a new empty linked list
* @param fuse The fuse application
* @returns A new empty linked list, or NULL if memory could not be allocated
*/
fuse_list_t *fuse_list_new(fuse_t *fuse);

#ifndef NO_DOC
#ifdef DEBUG
#define fuse_list_new(self) \
(fuse_list_new_ex((self), , __FILE__, __LINE__))
#else
#define fuse_list_new(self) \
(fuse_list_new_ex((self), __FILE__, __LINE__))
(fuse_list_new_ex((self), NULL, 0))
#endif /* DEBUG */
#endif /* NO_DOC */

/** @brief Create a new empty linked list
* @param fuse The fuse application
* @param file The file from which the linked list is allocated
* @param line The line in the file from which the linked list is allocated
* @returns A new empty linked list, or NULL if memory could not be allocated
*/
fuse_list_t *fuse_list_new_ex(fuse_t *fuse, const char *file, int line);

/** @brief Deallocate a fuse linked list
*
* @param fuse The fuse application
* @param self The linked list to deallocate
*/
void fuse_list_destroy(fuse_t *fuse, fuse_list_t *self);

/** @brief Return the number of nodes in the list
/** @brief Return the number of values in the list
*
* @param self The linked list
* @returns The number of nodes in the list
* @returns The number of values in the list
*/
size_t fuse_list_count(fuse_list_t *self);

/** @brief Return the next node in the list
/** @brief Return the next value in the list
*
* @param self The linked list
* @param ptr The current node, or NULL if the first node should be returned
* @returns The next node in the linked list, or NULL if there is no next node
* @param cur The current value, or NULL if the first value should be returned
* @returns The next value in the linked list, or NULL if there is no next value
*/
void *fuse_list_next(fuse_list_t *self, void *ptr);
fuse_value_t *fuse_list_next(fuse_list_t *self, fuse_value_t *cur);

/** @brief Return the previous node in the list
/** @brief Return the previous value in the list
*
* @param self The linked list
* @param ptr The current node, or NULL if the last node should be returned
* @returns The previous node in the linked list, or NULL if there is no previous node
* @param ptr The current value, or NULL if the last value should be returned
* @returns The previous value in the linked list, or NULL if there is no previous value
*/
void *fuse_list_prev(fuse_list_t *self, void *ptr);
fuse_value_t *fuse_list_prev(fuse_list_t *self, fuse_value_t *ptr);

/** @brief Add a node at the end of the list
/** @brief Add a value at the end of the list
*
* @param self The linked list
* @param ptr The new node to add to the end of the list
* @param ptr The new value to add to the end of the list
* @returns Returns true if successful, false otherwise
*/
void *fuse_list_push(fuse_list_t *self, void *ptr);
fuse_value_t *fuse_list_push(fuse_list_t *self, fuse_value_t *ptr);

/** @brief Remove a node from the beginning of the list
/** @brief Remove a value from the beginning of the list
*
* @param self The linked list
* @returns Returns the node removed from the beginning of the list, or NULL if the list is empty
* @returns Returns the value removed from the beginning of the list, or NULL if the list is empty
*/
void *fuse_list_pop(fuse_list_t *self);
fuse_value_t *fuse_list_pop(fuse_list_t *self);

#endif // FUSE_LIST_H
26 changes: 14 additions & 12 deletions include/fuse/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,33 @@
typedef struct fuse_map_instance fuse_map_t;

/** @brief Create a new empty map
*
*
* @param fuse The fuse application
* @param size The initial size of the map
* @param hashfunc The hash function to use for the map. If NULL, then the default hash function is used
* @param file The file from which the map is allocated
* @param line The line in the file from which the map is allocated
* @returns A new empty map, or NULL if memory could not be allocated
*/
fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, size_t (*hashfunc)(void *), const char *file, int line);
fuse_map_t *fuse_map_new(fuse_t *fuse, size_t size);

#ifndef NO_DOC
#ifdef DEBUG
#define fuse_map_new(self, size) \
(fuse_map_new_ex((self), (size), NULL, __FILE__, __LINE__))
#else
#define fuse_map_new(self) \
(fuse_map_new_ex((self), 0, __FILE__, __LINE__))
#endif /* DEBUG */
#endif /* NO_DOC */

/** @brief Create a new empty map
*
/** @brief Create a new empty map
*
* @param fuse The fuse application
* @param size The initial size of the map
* @param hashfunc The hash function to use for the map. If NULL, then the default hash function is used
* @param file The file from which the map is allocated
* @param line The line in the file from which the map is allocated
* @returns A new empty map, or NULL if memory could not be allocated
*/
#define fuse_map_new(self, size) \
(fuse_map_new_ex((self), (size), NULL, 0, 0))
#endif
*/
fuse_map_t *fuse_map_new_ex(fuse_t *fuse, size_t size, size_t (*hashfunc)(void *), const char *file, int line);

/** @brief Deallocate a map
*
Expand All @@ -64,7 +67,6 @@ void fuse_map_destroy(fuse_t *fuse, fuse_map_t *self);
*/
size_t fuse_map_stats(fuse_map_t *self, size_t *size);


/** @brief Return the number of elements in the map
*
* @param self The map
Expand Down
45 changes: 45 additions & 0 deletions include/fuse/value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/** @file value.h
* @brief Value function prototypes
*
* This file contains the function prototypes for arbitary values,
* which are used for linked lists.
*/
#ifndef FUSE_VALUE_H
#define FUSE_VALUE_H

#include "fuse.h"

/** @brief Value defintion
*/
typedef struct fuse_value_instance fuse_value_t;

/** @brief Create a new NULL value
* @param fuse The fuse application
* @returns A NULL value
*/
fuse_value_t *fuse_new_null(fuse_t *fuse);

/** @brief Create a new pointer value
*
* The pointer value can be used to store a pointer to an arbitary value,
* including a NULL pointer.
*
* @param fuse The fuse application
* @param ptr The pointer to store
* @returns A pointer value
*/
fuse_value_t *fuse_new_ptr(fuse_t *fuse, void *ptr);

/** @brief Return the pointer value
*/
void *fuse_ptr(fuse_value_t *value);

/** @brief Return the value as a zero-terminated JSON value
*/
const char *fuse_jsonstring(fuse_value_t *value);

/** @brief Return true if the value contains NULL
*/
bool fuse_equal_null(fuse_value_t *value);

#endif // FUSE_VALUE_H
2 changes: 1 addition & 1 deletion src/fuse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_library(${NAME} STATIC
random_pico.c
random_posix.c
sleep_posix.c
string.c
value.c
)

target_include_directories(${NAME} PRIVATE
Expand Down
5 changes: 2 additions & 3 deletions src/fuse/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fuse_list_t *fuse_list_new_ex(fuse_t *fuse, const char *file, int line)

// Set the instance properties
self->count = 0;
self->first = NULL;
self->last = NULL;
self->head = NULL;
self->tail = NULL;

// Return success
return self;
Expand All @@ -41,4 +41,3 @@ inline size_t fuse_list_count(fuse_list_t *self)
assert(self);
return self->count;
}

16 changes: 4 additions & 12 deletions src/fuse/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
*/
#ifndef FUSE_PRIVATE_LIST_H
#define FUSE_PRIVATE_LIST_H
#include <fuse/fuse.h>

#include "value.h"

/* @brief Represents an instance of a fuse linked list
*/
struct fuse_list_instance
{
struct fuse_list_node *first; ///< The first node in the list, or NULL if the list is empty
struct fuse_list_node *last; ///< The last node in the list, or NULL if the list is empty
struct fuse_value_instance *head; ///< The first node in the list, or NULL if the list is empty
struct fuse_value_instance *tail; ///< The last node in the list, or NULL if the list is empty
size_t count; ///< The number of nodes in the list
};

/* @brief Represents a node in a fuse linked list
*/
struct fuse_list_node
{
void *ptr; ///< The pointer to the node data
struct fuse_list_node *prev; ///< The previous node in the list, or NULL if this is the first node
struct fuse_list_node *next; ///< The next node in the list, or NULL if this is the last node
};

#endif
60 changes: 60 additions & 0 deletions src/fuse/value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <string.h>
#include <fuse/fuse.h>

// Private includes
#include "value.h"

///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS

inline fuse_value_t *fuse_new_null(fuse_t *fuse)
{
assert(fuse);
return fuse_new_ptr(fuse, NULL);
}

fuse_value_t *fuse_new_ptr(fuse_t *fuse, void *ptr) {
assert(fuse);

// Allocate memory for the value
fuse_value_t *value = fuse_alloc(fuse, sizeof(fuse_value_t));
if (value == NULL)
{
return NULL;
}

// Zero all data structures
memset(value, 0, sizeof(fuse_value_t));

// Set the pointer
value->ptr = ptr;

// Return the value
return value;
}

inline bool fuse_equal_null(fuse_value_t *value)
{
assert(value);
return value->ptr == NULL;
}

inline void* fuse_ptr(fuse_value_t *value)
{
assert(value);
return value->ptr;
}

const char *fuse_jsonstring(fuse_value_t *value)
{
assert(value);
// Simple case where value is NULL
if(value->ptr == NULL)
{
return "null";
}

// Other use-cases here
assert(false);
return NULL;
}
20 changes: 20 additions & 0 deletions src/fuse/value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @file value.h
* @brief Private function prototypes and structure definitions for values
*/
#ifndef FUSE_PRIVATE_VALUE_H
#define FUSE_PRIVATE_VALUE_H

#include <fuse/fuse.h>

/** @brief Represents a value
*
* This data structure represents an arbitary value, including NULL values
*/
struct fuse_value_instance
{
void *ptr; ///< A pointer to the value storage
struct fuse_value_instance *prev; ///< The previous value in the list (when the value is part of a list)
struct fuse_value_instance *next; ///< The next value in the list (when the value is part of a list)
};

#endif
12 changes: 12 additions & 0 deletions tests/value/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(NAME "value")
add_executable(${NAME}
main.c
)

target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../../include
)

target_link_libraries(${NAME}
fuse
)
Loading

0 comments on commit 43b2923

Please sign in to comment.