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