xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-object.c (revision f2989f2b3b8649d855bed22b6730ee0ecfa6b31b)
1b0f67a9cSJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2b0f67a9cSJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3b0f67a9cSJeremy L Thompson //
4b0f67a9cSJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5b0f67a9cSJeremy L Thompson //
6b0f67a9cSJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7b0f67a9cSJeremy L Thompson 
8b0f67a9cSJeremy L Thompson #include <ceed-impl.h>
9b0f67a9cSJeremy L Thompson #include <ceed.h>
10b0f67a9cSJeremy L Thompson #include <ceed/backend.h>
11b0f67a9cSJeremy L Thompson 
12b0f67a9cSJeremy L Thompson /// @file
13b0f67a9cSJeremy L Thompson /// Implementation of CeedObject functionality
14b0f67a9cSJeremy L Thompson 
15b0f67a9cSJeremy L Thompson /// ----------------------------------------------------------------------------
16b0f67a9cSJeremy L Thompson /// CeedObject Backend API
17b0f67a9cSJeremy L Thompson /// ----------------------------------------------------------------------------
18b0f67a9cSJeremy L Thompson /// @addtogroup CeedBackend
19b0f67a9cSJeremy L Thompson /// @{
20b0f67a9cSJeremy L Thompson 
21b0f67a9cSJeremy L Thompson /**
22*75c339d6SJeremy L Thompson   @brief Create a `CeedObject`.
23*75c339d6SJeremy L Thompson 
24*75c339d6SJeremy L Thompson   Note: This interface takes a `CeedObject` and not a pointer to a `CeedObject` like other `Ceed*Create` interfaces.
25*75c339d6SJeremy L Thompson           This `CeedObject` will have already been allocated a the first part of the `Ceed*` struct.
26*75c339d6SJeremy L Thompson           This function is only intended to be called inside of `Ceed*Create` functions.
27b0f67a9cSJeremy L Thompson 
28b0f67a9cSJeremy L Thompson   @param[in]  ceed             `Ceed` object to reference
29b0f67a9cSJeremy L Thompson   @param[in]  view_function    `Ceed*` function for viewing the `obj`
306c328a79SJeremy L Thompson   @param[in]  destroy_function `Ceed*` function for destroying the `obj`
31b0f67a9cSJeremy L Thompson   @param[out] obj              Address of the variable where is `CeedObject` exists
32b0f67a9cSJeremy L Thompson 
33b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
34b0f67a9cSJeremy L Thompson 
35b0f67a9cSJeremy L Thompson   @ref Backend
36b0f67a9cSJeremy L Thompson **/
CeedObjectCreate(Ceed ceed,int (* view_function)(CeedObject,FILE *),int (* destroy_function)(CeedObject *),CeedObject obj)376c328a79SJeremy L Thompson int CeedObjectCreate(Ceed ceed, int (*view_function)(CeedObject, FILE *), int (*destroy_function)(CeedObject *), CeedObject obj) {
38b0f67a9cSJeremy L Thompson   obj->ceed = NULL;
39b0f67a9cSJeremy L Thompson   if (ceed) CeedCall(CeedReferenceCopy(ceed, &obj->ceed));
406c328a79SJeremy L Thompson   obj->View = view_function;
416c328a79SJeremy L Thompson   CeedCheck(destroy_function, CeedObjectReturnCeed(obj), CEED_ERROR_UNSUPPORTED, "Must provide destroy function to create CeedObject");
426c328a79SJeremy L Thompson   obj->Destroy   = destroy_function;
43b0f67a9cSJeremy L Thompson   obj->ref_count = 1;
44b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
45b0f67a9cSJeremy L Thompson }
46b0f67a9cSJeremy L Thompson 
47b0f67a9cSJeremy L Thompson /**
48b0f67a9cSJeremy L Thompson   @brief Increment the reference counter for a `CeedObject`
49b0f67a9cSJeremy L Thompson 
50b0f67a9cSJeremy L Thompson   @param[in,out] obj `CeedObject` to increment the reference counter
51b0f67a9cSJeremy L Thompson 
52b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
53b0f67a9cSJeremy L Thompson 
54b0f67a9cSJeremy L Thompson   @ref Backend
55b0f67a9cSJeremy L Thompson **/
CeedObjectReference(CeedObject obj)56b0f67a9cSJeremy L Thompson int CeedObjectReference(CeedObject obj) {
57b0f67a9cSJeremy L Thompson   obj->ref_count++;
58b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
59b0f67a9cSJeremy L Thompson }
60b0f67a9cSJeremy L Thompson 
61b0f67a9cSJeremy L Thompson /**
62b0f67a9cSJeremy L Thompson   @brief Decrement the reference counter for a `CeedObject`
63b0f67a9cSJeremy L Thompson 
64b0f67a9cSJeremy L Thompson   @param[in,out] obj `CeedObject` to decrement the reference counter
65b0f67a9cSJeremy L Thompson 
66b0f67a9cSJeremy L Thompson   @return The new reference count
67b0f67a9cSJeremy L Thompson 
68b0f67a9cSJeremy L Thompson   @ref Backend
69b0f67a9cSJeremy L Thompson **/
CeedObjectDereference(CeedObject obj)70b0f67a9cSJeremy L Thompson int CeedObjectDereference(CeedObject obj) {
71b0f67a9cSJeremy L Thompson   return --obj->ref_count;  // prefix notation, to get new number of references
72b0f67a9cSJeremy L Thompson }
73b0f67a9cSJeremy L Thompson 
74b0f67a9cSJeremy L Thompson /**
75b0f67a9cSJeremy L Thompson   @brief Destroy a @ref CeedObject
76b0f67a9cSJeremy L Thompson 
77b0f67a9cSJeremy L Thompson   @param[in,out] obj `CeedObject` to destroy
78b0f67a9cSJeremy L Thompson 
79b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80b0f67a9cSJeremy L Thompson 
81b0f67a9cSJeremy L Thompson   @ref Backend
82b0f67a9cSJeremy L Thompson **/
CeedObjectDestroy_Private(CeedObject obj)836c328a79SJeremy L Thompson int CeedObjectDestroy_Private(CeedObject obj) {
846c328a79SJeremy L Thompson   CeedCheck(obj->ref_count == 0, CeedObjectReturnCeed(obj), CEED_ERROR_UNSUPPORTED,
856c328a79SJeremy L Thompson             "Cannot destroy CeedObject, it is still referenced by another object");
86b0f67a9cSJeremy L Thompson   if (obj->ceed) CeedCall(CeedDestroy(&obj->ceed));
87b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
88b0f67a9cSJeremy L Thompson }
89b0f67a9cSJeremy L Thompson 
90b0f67a9cSJeremy L Thompson /// @}
91b0f67a9cSJeremy L Thompson 
92b0f67a9cSJeremy L Thompson /// ----------------------------------------------------------------------------
93b0f67a9cSJeremy L Thompson /// CeedObject Public API
94b0f67a9cSJeremy L Thompson /// ----------------------------------------------------------------------------
95b0f67a9cSJeremy L Thompson /// @addtogroup CeedUser
96b0f67a9cSJeremy L Thompson /// @{
97b0f67a9cSJeremy L Thompson 
98b0f67a9cSJeremy L Thompson /**
99b0f67a9cSJeremy L Thompson   @brief View a `CeedObject`
100b0f67a9cSJeremy L Thompson 
101b0f67a9cSJeremy L Thompson   @param[in] obj    `CeedObject` to view
102b0f67a9cSJeremy L Thompson   @param[in] stream Stream to view to, e.g., `stdout`
103b0f67a9cSJeremy L Thompson 
104b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105b0f67a9cSJeremy L Thompson 
106b0f67a9cSJeremy L Thompson   @ref User
107b0f67a9cSJeremy L Thompson **/
CeedObjectView(CeedObject obj,FILE * stream)108b0f67a9cSJeremy L Thompson int CeedObjectView(CeedObject obj, FILE *stream) {
1096c328a79SJeremy L Thompson   if (obj->View) CeedCall(obj->View(obj, stream));
110b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
111b0f67a9cSJeremy L Thompson }
112b0f67a9cSJeremy L Thompson 
113b0f67a9cSJeremy L Thompson /**
114a299a25bSJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedObjectView() output
115a299a25bSJeremy L Thompson 
116a299a25bSJeremy L Thompson   @param[in] obj      `CeedObject` to set the number of view tabs
117a299a25bSJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
118a299a25bSJeremy L Thompson 
119a299a25bSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
120a299a25bSJeremy L Thompson 
121a299a25bSJeremy L Thompson   @ref User
122a299a25bSJeremy L Thompson **/
CeedObjectSetNumViewTabs(CeedObject obj,CeedInt num_tabs)123a299a25bSJeremy L Thompson int CeedObjectSetNumViewTabs(CeedObject obj, CeedInt num_tabs) {
124a299a25bSJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedObjectReturnCeed(obj), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
125a299a25bSJeremy L Thompson   obj->num_view_tabs = num_tabs;
126a299a25bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
127a299a25bSJeremy L Thompson }
128a299a25bSJeremy L Thompson 
129a299a25bSJeremy L Thompson /**
130a299a25bSJeremy L Thompson   @brief Get the number of tabs to indent for @ref CeedObjectView() output
131a299a25bSJeremy L Thompson 
132a299a25bSJeremy L Thompson   @param[in]  obj      `CeedObject` to get the number of view tabs
133a299a25bSJeremy L Thompson   @param[out] num_tabs Number of view tabs
134a299a25bSJeremy L Thompson 
135a299a25bSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
136a299a25bSJeremy L Thompson 
137a299a25bSJeremy L Thompson   @ref User
138a299a25bSJeremy L Thompson **/
CeedObjectGetNumViewTabs(CeedObject obj,CeedInt * num_tabs)139a299a25bSJeremy L Thompson int CeedObjectGetNumViewTabs(CeedObject obj, CeedInt *num_tabs) {
140a299a25bSJeremy L Thompson   *num_tabs = obj->num_view_tabs;
141a299a25bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
142a299a25bSJeremy L Thompson }
143a299a25bSJeremy L Thompson 
144a299a25bSJeremy L Thompson /**
145b0f67a9cSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedObject`
146b0f67a9cSJeremy L Thompson 
147b0f67a9cSJeremy L Thompson   @param[in]  obj   `CeedObject`
148b0f67a9cSJeremy L Thompson   @param[out] ceed  Variable to store `Ceed`
149b0f67a9cSJeremy L Thompson 
150b0f67a9cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
151b0f67a9cSJeremy L Thompson 
152b0f67a9cSJeremy L Thompson   @ref Advanced
153b0f67a9cSJeremy L Thompson **/
CeedObjectGetCeed(CeedObject obj,Ceed * ceed)154b0f67a9cSJeremy L Thompson int CeedObjectGetCeed(CeedObject obj, Ceed *ceed) {
155b0f67a9cSJeremy L Thompson   *ceed = NULL;
156b0f67a9cSJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedObjectReturnCeed(obj), ceed));
157b0f67a9cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
158b0f67a9cSJeremy L Thompson }
159b0f67a9cSJeremy L Thompson 
160b0f67a9cSJeremy L Thompson /**
161b0f67a9cSJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedObject`
162b0f67a9cSJeremy L Thompson 
163b0f67a9cSJeremy L Thompson   @param[in] obj `CeedObject`
164b0f67a9cSJeremy L Thompson 
165b0f67a9cSJeremy L Thompson   @return `Ceed` associated with the `basis`
166b0f67a9cSJeremy L Thompson 
167b0f67a9cSJeremy L Thompson   @ref Advanced
168b0f67a9cSJeremy L Thompson **/
CeedObjectReturnCeed(CeedObject obj)169b0f67a9cSJeremy L Thompson Ceed CeedObjectReturnCeed(CeedObject obj) { return (obj->ceed) ? obj->ceed : (Ceed)obj; }
170b0f67a9cSJeremy L Thompson 
1716c328a79SJeremy L Thompson /**
1726c328a79SJeremy L Thompson   @brief Destroy a @ref CeedObject
1736c328a79SJeremy L Thompson 
1746c328a79SJeremy L Thompson   @param[in,out] obj Address of `CeedObject` to destroy
1756c328a79SJeremy L Thompson 
1766c328a79SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1776c328a79SJeremy L Thompson 
1786c328a79SJeremy L Thompson   @ref Backend
1796c328a79SJeremy L Thompson **/
CeedObjectDestroy(CeedObject * obj)1806c328a79SJeremy L Thompson int CeedObjectDestroy(CeedObject *obj) {
1816c328a79SJeremy L Thompson   CeedCall((*obj)->Destroy(obj));
1826c328a79SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1836c328a79SJeremy L Thompson }
1846c328a79SJeremy L Thompson 
185b0f67a9cSJeremy L Thompson /// @}
186