xref: /libCEED/interface/ceed-object.c (revision b0f67a9c1aeeb4d82b4724afaae1227ff4e81f15)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 
12 /// @file
13 /// Implementation of CeedObject functionality
14 
15 /// ----------------------------------------------------------------------------
16 /// CeedObject Backend API
17 /// ----------------------------------------------------------------------------
18 /// @addtogroup CeedBackend
19 /// @{
20 
21 /**
22   @brief Create a `CeedObject`
23 
24   @param[in]  ceed          `Ceed` object to reference
25   @param[in]  view_function `Ceed*` function for viewing the `obj`
26   @param[out] obj           Address of the variable where is `CeedObject` exists
27 
28   @return An error code: 0 - success, otherwise - failure
29 
30   @ref Backend
31 **/
32 int CeedObjectCreate(Ceed ceed, int (*view_function)(CeedObject, FILE *), CeedObject obj) {
33   obj->ceed = NULL;
34   if (ceed) CeedCall(CeedReferenceCopy(ceed, &obj->ceed));
35   obj->ViewFunction = view_function;
36   obj->ref_count    = 1;
37   return CEED_ERROR_SUCCESS;
38 }
39 
40 /**
41   @brief Increment the reference counter for a `CeedObject`
42 
43   @param[in,out] obj `CeedObject` to increment the reference counter
44 
45   @return An error code: 0 - success, otherwise - failure
46 
47   @ref Backend
48 **/
49 int CeedObjectReference(CeedObject obj) {
50   obj->ref_count++;
51   return CEED_ERROR_SUCCESS;
52 }
53 
54 /**
55   @brief Decrement the reference counter for a `CeedObject`
56 
57   @param[in,out] obj `CeedObject` to decrement the reference counter
58 
59   @return The new reference count
60 
61   @ref Backend
62 **/
63 int CeedObjectDereference(CeedObject obj) {
64   return --obj->ref_count;  // prefix notation, to get new number of references
65 }
66 
67 /**
68   @brief Destroy a @ref CeedObject
69 
70   @param[in,out] obj `CeedObject` to destroy
71 
72   @return An error code: 0 - success, otherwise - failure
73 
74   @ref Backend
75 **/
76 int CeedObjectDestroy(CeedObject obj) {
77   CeedCheck(obj->ref_count == 0, CeedObjectReturnCeed(obj), CEED_ERROR_ACCESS, "Cannot destroy CeedObject, it is still referenced by another object");
78   if (obj->ceed) CeedCall(CeedDestroy(&obj->ceed));
79   return CEED_ERROR_SUCCESS;
80 }
81 
82 /// @}
83 
84 /// ----------------------------------------------------------------------------
85 /// CeedObject Public API
86 /// ----------------------------------------------------------------------------
87 /// @addtogroup CeedUser
88 /// @{
89 
90 /**
91   @brief View a `CeedObject`
92 
93   @param[in] obj    `CeedObject` to view
94   @param[in] stream Stream to view to, e.g., `stdout`
95 
96   @return An error code: 0 - success, otherwise - failure
97 
98   @ref User
99 **/
100 int CeedObjectView(CeedObject obj, FILE *stream) {
101   if (obj->ViewFunction) CeedCall(obj->ViewFunction(obj, stream));
102   return CEED_ERROR_SUCCESS;
103 }
104 
105 /**
106   @brief Get the `Ceed` associated with a `CeedObject`
107 
108   @param[in]  obj   `CeedObject`
109   @param[out] ceed  Variable to store `Ceed`
110 
111   @return An error code: 0 - success, otherwise - failure
112 
113   @ref Advanced
114 **/
115 int CeedObjectGetCeed(CeedObject obj, Ceed *ceed) {
116   *ceed = NULL;
117   CeedCall(CeedReferenceCopy(CeedObjectReturnCeed(obj), ceed));
118   return CEED_ERROR_SUCCESS;
119 }
120 
121 /**
122   @brief Return the `Ceed` associated with a `CeedObject`
123 
124   @param[in] obj `CeedObject`
125 
126   @return `Ceed` associated with the `basis`
127 
128   @ref Advanced
129 **/
130 Ceed CeedObjectReturnCeed(CeedObject obj) { return (obj->ceed) ? obj->ceed : (Ceed)obj; }
131 
132 /// @}
133