diff --git a/chinookfile b/chinookfile index d0f6f8c..84128e6 100644 --- a/chinookfile +++ b/chinookfile @@ -4,15 +4,10 @@ semv: 1.0.0 targets: - name: malunal.types - type: interface - -tests: -- name: assertions - type: program - deps: - - malunal.types + type: archive srcs: - - ./tests/assertions.c + - ./source/object.c + - ./source/uuid.c exports: - malunal.types diff --git a/include/malunal/type.h b/include/malunal/type.h new file mode 100644 index 0000000..4df2bcc --- /dev/null +++ b/include/malunal/type.h @@ -0,0 +1,64 @@ +/** + * @file types.h + * @brief Contains the definitions of various types used within malunal code + * bases and libraries. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#ifndef MALUNAL_TYPE_HEADER +#define MALUNAL_TYPE_HEADER + +#if __STDC_VERSION__ < 201112L +# error "This library requires C11 or later" +#endif + +#if defined(__SIZEOF_POINTER__) +# define MALUNAL_POINTER_SIZE __SIZEOF_POINTER__ +#elif defined(_MSC_VER) +# if defined(_M_X64) || defined(_M_ARM64) +# define MY_PTR_SIZE 8 +# elif defined(_M_IX86) || defined(_M_ARM) +# define MY_PTR_SIZE 4 +# else /* Unrecognized MSVC target */ +# error "Unrecognized MSVC target architecture" +# endif /* MSVC compiler check */ +#else /* Unrecognized compiler, no support */ +# error "Unrecognized compiler - add support explicitly" +#endif /* Compiler dependent pointer size check */ + +/** + * @def null + * @brief Defines the value of the null pointer. + * @details This is here just in case it is not defined anywhere else, to make + * pointer usage easier. + */ +#ifndef null +#define null ((malunal_mptr_t*)0) +#endif /* null */ + +/** + * @brief Our own definition of @c void. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef void malunal_void_t; + +/** + * @brief Our own definition of a mutable pointer. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef void* malunal_mptr_t; + +/** + * @brief Our own definition of an immutable pointer. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef const void* malunal_iptr_t; + + +#endif /* MALUNAL_TYPES_HEADER */ diff --git a/include/malunal/types/bool.h b/include/malunal/types/bool.h new file mode 100644 index 0000000..7924fdf --- /dev/null +++ b/include/malunal/types/bool.h @@ -0,0 +1,38 @@ +/** + * @file bool.h + * @brief Contains the definition of a boolean type and its possible values. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#ifndef MALUNAL_TYPES_BOOL_HEADER +#define MALUNAL_TYPES_BOOL_HEADER + +/** + * @def true + * @brief Defines the value of the boolean true. + * @details This is here just in case it is not defined anywhere else, to make + * boolean usage easier. + */ +#ifndef true +#define true 1 +#endif /* true */ + +/** + * @def false + * @brief Defines the value of the boolean false. + * @details This is here just in case it is not defined anywhere else, to make + * boolean usage easier. + */ +#ifndef false +#define false 0 +#endif /* false */ + +/** + * @brief Our own definition of @c bool. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef _Bool malunal_bool_t; + +#endif /* MALUNAL_TYPES_BOOL_HEADER */ diff --git a/include/malunal/types/error.h b/include/malunal/types/error.h new file mode 100644 index 0000000..8c3491b --- /dev/null +++ b/include/malunal/types/error.h @@ -0,0 +1,106 @@ +/** + * @file error.h + * @brief Defines the error domain and error structures for pedantically defining + * errors that belong to namespaces or domains of possible issues. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#include "integer.h" +#include "string.h" + +#ifndef MALUNAL_TYPES_ERROR_HEADER +#define MALUNAL_TYPES_ERROR_HEADER + + +/** + * @def NO_ERROR + * @brief A macro to easily define no error. + * @details This is a default constructed @c error_t object with both of its + * internal fields, @c domain and @c code, set to zero. + */ +#define NO_ERROR ((error_t){0}) + + +/** + * @brief Defines an error domain. + * @details An error domain is treated like a namespace for error codes. + * It provides the name of the domain and a function to describe the + * error code such that the receiver of the error knows where the error + * came from and what it means. + */ +typedef struct { + /** + * @brief A pointer to a function that is capable of describing an error code + * that belongs to this domain, by providing a string message of the + * provided error. + * @param code The error code for this error domain to describe. + * @returns A string message that describes the error code provided. + */ + malunal_cstr_t (*const describe)(malunal_int32_t code); + + /** + * @brief The name of the error domain. + * @details Outside of using the @c domain-as-pointer method of identifying an + * error's domain, this name is also provided whether for debugging + * or logging purposes, or doubly verifying the pointer source. + */ + malunal_cstr_t name; +} error_domain_t; + +/** + * @brief A pointer to a mutable error domain. + * @details This is provided to simplify type declarations for functions + * requiring error domains that are meant to be mutable. + */ +typedef error_domain_t* error_domain_mptr_t; + +/** + * @brief A pointer to an immutable error domain. + * @details This is provided to simplify type declarations for functions + * requiring error domains that are meant to be immutable. + */ +typedef const error_domain_t* error_domain_iptr_t; + + +/** + * @brief Defines an error. + * @details An error is the combination of a domain, describing where it came + * from or where it belongs to, and the actual error code. This makes + * it so error codes are distinguishable from each other, at the cost + * of a minimal amount of memory overhead. + */ +typedef struct { + /** + * @brief The domain of the error. + * @details This is where the error belong to, kind of like its namespace. It + * is set once by the provider of the error and not intended to be + * changed. When null, it means that there is no error regardless of + * what code is present. + */ + error_domain_iptr_t const domain; + + /** + * @brief The code of the error within its domain. + * @details This defines what the actual error was. The domain is able to + * decode it for the caller to provide more information about it, + * specifically a string message. It should be zero, when the domain + * is null to represent no error. + */ + malunal_int32_t code; +} error_t; + +/** + * @brief A pointer to a mutable error. + * @details This is provided to simplify type declarations for functions + * requiring error that are meant to be mutable. + */ +typedef error_t* error_mptr_t; + +/** + * @brief A pointer to an immutable error. + * @details This is provided to simplify type declarations for functions + * requiring error that are meant to be immutable. + */ +typedef const error_t* error_iptr_t; + +#endif /* MALUNAL_TYPES_ERROR_HEADER */ diff --git a/include/malunal/types.h b/include/malunal/types/integer.h similarity index 63% rename from include/malunal/types.h rename to include/malunal/types/integer.h index 4008c28..4a05691 100644 --- a/include/malunal/types.h +++ b/include/malunal/types/integer.h @@ -1,87 +1,14 @@ /** - * @file types.h - * @brief Contains the definitions of various types used within malunal code - * bases and libraries. + * @file int.h + * @brief Contains all of the integer related type definitions. * @author John Christman (sorakatadzuma@gmail.com) * @copyright Malunal Studios, LLC. */ -#ifndef MALUNAL_TYPES_HEADER -#define MALUNAL_TYPES_HEADER +#include "../type.h" -#if __STDC_VERSION__ < 201112L -# error "This library requires C11 or later" -#endif +#ifndef MALUNAL_TYPES_INT_HEADER +#define MALUNAL_TYPES_INT_HEADER -#if defined(__SIZEOF_POINTER__) -# define MALUNAL_POINTER_SIZE __SIZEOF_POINTER__ -#elif defined(_MSC_VER) -# if defined(_M_X64) || defined(_M_ARM64) -# define MY_PTR_SIZE 8 -# elif defined(_M_IX86) || defined(_M_ARM) -# define MY_PTR_SIZE 4 -# else /* Unrecognized MSVC target */ -# error "Unrecognized MSVC target architecture" -# endif /* MSVC compiler check */ -#else /* Unrecognized compiler, no support */ -# error "Unrecognized compiler - add support explicitly" -#endif /* Compiler dependent pointer size check */ - - -/** - * @brief Our own definition of @c void. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef void malunal_void_t; - -/** - * @brief Our own definition of a mutable pointer. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef void* malunal_mptr_t; - -/** - * @brief Our own definition of an immutable pointer. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef const void* malunal_iptr_t; - -/** - * @brief Our own definition of @c char. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef char malunal_char_t; - -/** - * @brief Our own definition of a mutable string. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef char malunal_str_t; - -/** - * @brief Our own definition of a constant string. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef const char* malunal_cstr_t; - -/** - * @brief Our own definition of @c bool. - * @details This is provided, like all of the types in this header, to meet - * modern standards and to exist specifically to match how we name - * many of our other types. - */ -typedef _Bool malunal_bool_t; /** * @brief Our own definition of an 8-bit signed integer. @@ -228,5 +155,4 @@ _Static_assert( "malunal_uintptr_t must match pointer width" ); - -#endif /* MALUNAL_TYPES_HEADER */ +#endif /* MALUNAL_TYPES_INT_HEADER */ diff --git a/include/malunal/types/object.h b/include/malunal/types/object.h new file mode 100644 index 0000000..43113d0 --- /dev/null +++ b/include/malunal/types/object.h @@ -0,0 +1,91 @@ +/** + * @file object.h + * @brief Contains the definition of the virtual function table for the object + * interface and the object interface itself. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#include "error.h" +#include "uuid.h" + +#ifndef MALUNAL_TYPES_OBJECT_HEADER +#define MALUNAL_TYPES_OBJECT_HEADER + +/** + * @brief Imports an external constant for the UUID of @c object_t. + * @details This is needed to properly be able to be able to cast @c object_t + * compliant types to an @c object_t. + */ +extern +const uuid_t +UUID_OBJECT_T; + +/** + * @brief Defines the object virtual function table. + * @details Implementing classes of the object interface are expected to provide + * valid pointers to these functions that are implementation specific. + */ +typedef struct { + /** + * @brief A pointer to a function which can will attempt to cast the given + * type specified by the @c uuid pointer, placing the address of the + * casted type into the @c out parameter, and indicating any error in + * the return value. + * @param self A pointer to the object to be scrutinized. + * @param uuid A pointer to The UUID of the interface or class from which + * @c self inherits. + * @param out A pointer to the location where a pointer to the base type that + * the @c uuid represents will be stored. + * @returns An error code if the object could not be casted. + */ + error_t(*const cast)(malunal_mptr_t self, uuid_iptr_t uuid, malunal_mptr_t* out); + + /** + * @brief A pointer to a function which can retain a reference on the given + * generic object, returning the current number of references. + * @param self A pointer to the object that will be reference counted. + * @returns The number of references attached to the object after retaining a + * new reference. + */ + malunal_uint32_t(*const retain)(malunal_mptr_t self); + + /** + * @brief A pointer to a function which can release a reference on the given + * generic object, returning the current number of references. + * @param self A pointer to the object that will be reference counted. + * @returns The number of reference still attached to the object after releasing + * an old reference. + */ + malunal_uint32_t(*const release)(malunal_mptr_t self); +} object_vtable_t; + +/** + * @brief Defines the object interface object. + * @details Since this is only an interface, it only contains a pointer to the + * virtual table for itself. Other object which implement this, may + * come with member variables. + */ +typedef struct { + /** + * @brief A pointer to an immutable object virtual function table. + * @details Contains function pointers to the object specific functions that + * make this object function as one. + */ + const object_vtable_t* vtable; +} object_t; + +/** + * @brief A pointer to a mutable object. + * @details This is provided to simplify type declarations for functions + * requiring objects that are meant to be mutable. + */ +typedef object_t* object_mptr_t; + +/** + * @brief A pointer to an immutable object. + * @details This is provided to simplify type declarations for functions + * requiring objects that are meant to be immutable. + */ +typedef const object_t* object_iptr_t; + +#endif /* MALUNAL_TYPES_OBJECT_HEADER */ diff --git a/include/malunal/types/string.h b/include/malunal/types/string.h new file mode 100644 index 0000000..d6b6404 --- /dev/null +++ b/include/malunal/types/string.h @@ -0,0 +1,37 @@ +/** + * @file string.h + * @brief Contains the definitions of character and string types. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#include "../type.h" + +// TODO: add support for wide characters. +#ifndef MALUNAL_TYPES_STRING_HEADER +#define MALUNAL_TYPES_STRING_HEADER + +/** + * @brief Our own definition of @c char. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef char malunal_char_t; + +/** + * @brief Our own definition of a mutable string. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef char* malunal_str_t; + +/** + * @brief Our own definition of a immutable string. + * @details This is provided, like all of the types in this header, to meet + * modern standards and to exist specifically to match how we name + * many of our other types. + */ +typedef const char* malunal_cstr_t; + +#endif /* MALUNAL_TYPES_STRING_HEADER */ diff --git a/include/malunal/types/uuid.h b/include/malunal/types/uuid.h new file mode 100644 index 0000000..e5353a3 --- /dev/null +++ b/include/malunal/types/uuid.h @@ -0,0 +1,60 @@ +/** + * @file uuid.h + * @brief Contains the utility macros and definition of the UUID structure. + * @author John Christman (sorakatadzuma@gmail.com) + * @copyright Malunal Studios, LLC. + */ +#include "bool.h" +#include "integer.h" + +#ifndef MALUNAL_TYPES_UUID_HEADER +#define MALUNAL_TYPES_UUID_HEADER + + +/** + * @brief Defines a universally unique identifier structure. + * @details This may be used for many things. However, for the purposes of this + * library specifically it is used to help identify different types and + * enable a simplistic type identification system. + */ +typedef union { + struct { + malunal_uint32_t tl; + malunal_uint16_t tm; + malunal_uint16_t thv; + malunal_uint8_t csr; + malunal_uint8_t csl; + malunal_uint8_t nbs[6]; + }; + + malunal_uint8_t bytes[16]; +} uuid_t; + +/** + * @brief A pointer to a mutable UUID. + * @details This is provided to simplify type declarations for functions + * requiring UUIDs that are meant to be mutable. + */ +typedef uuid_t* uuid_mptr_t; + +/** + * @brief A pointer to an immutable UUID. + * @details This is provided to simplify type declarations for functions + * requiring UUIDs that are meant to be immutable. + */ +typedef const uuid_t* uuid_iptr_t; + + +/** + * @brief Compares two UUIDs, checking if they are equal. + * @param uuidA The UUID being compared againt. + * @param uuidB The UUID being compared with. + * @returns True if the UUIDs are equal, byte for byte; otherwise, false. + */ +malunal_bool_t +uuid_equals( + uuid_iptr_t uuidA, + uuid_iptr_t uuidB +); + +#endif /* MALUNAL_TYPES_UUID_HEADER */ diff --git a/source/object.c b/source/object.c new file mode 100644 index 0000000..7547434 --- /dev/null +++ b/source/object.c @@ -0,0 +1,14 @@ +#include "malunal/types/object.h" + + +const uuid_t UUID_OBJECT_T = { + .tl = 0xB84510EE, + .tm = 0xBCDB, + .thv = 0x4B26, + .csr = 0x9E, + .csl = 0xEF, + .nbs = { + 0xBB, 0x5A, 0xDB, + 0x6C, 0x9C, 0x3A + } +}; diff --git a/source/uuid.c b/source/uuid.c new file mode 100644 index 0000000..3c5c282 --- /dev/null +++ b/source/uuid.c @@ -0,0 +1,10 @@ +#include +#include "malunal/types/uuid.h" + +malunal_bool_t +uuid_equals( + uuid_iptr_t uuidA, + uuid_iptr_t uuidB +) { + return memcmp(uuidA, uuidB, sizeof(uuid_t)) == 0; +} diff --git a/tests/assertions.c b/tests/assertions.c deleted file mode 100644 index 4a2bb1c..0000000 --- a/tests/assertions.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "malunal/types.h" - -malunal_int32_t -main( - malunal_int32_t argc, - malunal_str_t* argv -) { - return 0; -}