1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
11 * A structure instance which can be closed.
16 #ifndef SQUIRRELJME_CLOSEABLE_H
17 #define SQUIRRELJME_CLOSEABLE_H
19 #include "sjme/stdTypes.h"
20 #include "sjme/atomic.h"
21 #include "sjme/error.h"
22 #include "sjme/alloc.h"
26 #ifndef SJME_CXX_IS_EXTERNED
27 #define SJME_CXX_IS_EXTERNED
28 #define SJME_CXX_SQUIRRELJME_CLOSEABLE_H
31 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
32 #endif /* #ifdef __cplusplus */
34 /*--------------------------------------------------------------------------*/
37 * The closeable base data.
41 typedef struct sjme_closeableBase sjme_closeableBase
;
44 * The closeable pointer type.
48 typedef sjme_closeableBase
* sjme_closeable
;
50 /** Cast to a closeable. */
51 #define SJME_AS_CLOSEABLE(x) ((sjme_closeable)(x))
53 /** Cast to a closeable pointer. */
54 #define SJME_AS_CLOSEABLEP(x) ((sjme_closeable*)(x))
57 * This function is called when a closeable has been closed.
59 * @param closeable The current closeable being closed.
60 * @return Any resultant error, if any.
63 typedef sjme_errorCode (*sjme_closeable_closeHandlerFunc
)(
64 sjme_attrInNotNull sjme_closeable closeable
);
66 struct sjme_closeableBase
68 /** Has this been closed? */
69 sjme_atomic_sjme_jint isClosed
;
71 /** Is this a reference counting closeable? */
72 sjme_jboolean refCounting
;
74 /** The handler for close. */
75 sjme_closeable_closeHandlerFunc closeHandler
;
79 * Allocates a new closeable.
81 * @param inPool The pool to allocate within.
82 * @param allocSize The allocation size.
83 * @param handler The close handler to use.
84 * @param refCounting Is reference counting used? If not then this is
86 * @param outCloseable The resultant closeable.
87 * @return On any resultant error, if any.
90 sjme_errorCode
sjme_closeable_allocR(
91 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
92 sjme_attrInPositiveNonZero sjme_jint allocSize
,
93 sjme_attrInNotNull sjme_closeable_closeHandlerFunc handler
,
94 sjme_attrInValue sjme_jboolean refCounting
,
95 sjme_attrOutNotNull sjme_closeable
* outCloseable
96 SJME_DEBUG_ONLY_COMMA SJME_DEBUG_DECL_FILE_LINE_FUNC_OPTIONAL
);
99 * Allocates a new closeable.
101 * @param inPool The pool to allocate within.
102 * @param allocSize The allocation size.
103 * @param handler The close handler to use.
104 * @param refCounting Is reference counting used? If not then this is
106 * @param outCloseable The resultant closeable.
107 * @return On any resultant error, if any.
110 #define sjme_closeable_alloc(inPool, allocSize, handler, refCounting, \
112 (sjme_closeable_allocR((inPool), (allocSize), (handler), (refCounting), \
114 SJME_DEBUG_ONLY_COMMA SJME_DEBUG_FILE_LINE_FUNC_OPTIONAL))
117 * Closes the given closeable and un-references the weak reference.
119 * @param closeable The closeable to close.
120 * @return Any resultant error, if any.
123 sjme_errorCode
sjme_closeable_close(
124 sjme_attrInNotNull sjme_closeable closeable
);
126 /*--------------------------------------------------------------------------*/
130 #ifdef SJME_CXX_SQUIRRELJME_CLOSEABLE_H
132 #undef SJME_CXX_SQUIRRELJME_CLOSEABLE_H
133 #undef SJME_CXX_IS_EXTERNED
134 #endif /* #ifdef SJME_CXX_SQUIRRELJME_CLOSEABLE_H */
135 #endif /* #ifdef __cplusplus */
137 #endif /* SQUIRRELJME_CLOSEABLE_H */