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 /** 22b0f67a9cSJeremy L Thompson @brief Create a `CeedObject` 23b0f67a9cSJeremy L Thompson 24b0f67a9cSJeremy L Thompson @param[in] ceed `Ceed` object to reference 25b0f67a9cSJeremy L Thompson @param[in] view_function `Ceed*` function for viewing the `obj` 26*6c328a79SJeremy L Thompson @param[in] destroy_function `Ceed*` function for destroying the `obj` 27b0f67a9cSJeremy L Thompson @param[out] obj Address of the variable where is `CeedObject` exists 28b0f67a9cSJeremy L Thompson 29b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 30b0f67a9cSJeremy L Thompson 31b0f67a9cSJeremy L Thompson @ref Backend 32b0f67a9cSJeremy L Thompson **/ 33*6c328a79SJeremy L Thompson int CeedObjectCreate(Ceed ceed, int (*view_function)(CeedObject, FILE *), int (*destroy_function)(CeedObject *), CeedObject obj) { 34b0f67a9cSJeremy L Thompson obj->ceed = NULL; 35b0f67a9cSJeremy L Thompson if (ceed) CeedCall(CeedReferenceCopy(ceed, &obj->ceed)); 36*6c328a79SJeremy L Thompson obj->View = view_function; 37*6c328a79SJeremy L Thompson CeedCheck(destroy_function, CeedObjectReturnCeed(obj), CEED_ERROR_UNSUPPORTED, "Must provide destroy function to create CeedObject"); 38*6c328a79SJeremy L Thompson obj->Destroy = destroy_function; 39b0f67a9cSJeremy L Thompson obj->ref_count = 1; 40b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 41b0f67a9cSJeremy L Thompson } 42b0f67a9cSJeremy L Thompson 43b0f67a9cSJeremy L Thompson /** 44b0f67a9cSJeremy L Thompson @brief Increment the reference counter for a `CeedObject` 45b0f67a9cSJeremy L Thompson 46b0f67a9cSJeremy L Thompson @param[in,out] obj `CeedObject` to increment the reference counter 47b0f67a9cSJeremy L Thompson 48b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 49b0f67a9cSJeremy L Thompson 50b0f67a9cSJeremy L Thompson @ref Backend 51b0f67a9cSJeremy L Thompson **/ 52b0f67a9cSJeremy L Thompson int CeedObjectReference(CeedObject obj) { 53b0f67a9cSJeremy L Thompson obj->ref_count++; 54b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 55b0f67a9cSJeremy L Thompson } 56b0f67a9cSJeremy L Thompson 57b0f67a9cSJeremy L Thompson /** 58b0f67a9cSJeremy L Thompson @brief Decrement the reference counter for a `CeedObject` 59b0f67a9cSJeremy L Thompson 60b0f67a9cSJeremy L Thompson @param[in,out] obj `CeedObject` to decrement the reference counter 61b0f67a9cSJeremy L Thompson 62b0f67a9cSJeremy L Thompson @return The new reference count 63b0f67a9cSJeremy L Thompson 64b0f67a9cSJeremy L Thompson @ref Backend 65b0f67a9cSJeremy L Thompson **/ 66b0f67a9cSJeremy L Thompson int CeedObjectDereference(CeedObject obj) { 67b0f67a9cSJeremy L Thompson return --obj->ref_count; // prefix notation, to get new number of references 68b0f67a9cSJeremy L Thompson } 69b0f67a9cSJeremy L Thompson 70b0f67a9cSJeremy L Thompson /** 71b0f67a9cSJeremy L Thompson @brief Destroy a @ref CeedObject 72b0f67a9cSJeremy L Thompson 73b0f67a9cSJeremy L Thompson @param[in,out] obj `CeedObject` to destroy 74b0f67a9cSJeremy L Thompson 75b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 76b0f67a9cSJeremy L Thompson 77b0f67a9cSJeremy L Thompson @ref Backend 78b0f67a9cSJeremy L Thompson **/ 79*6c328a79SJeremy L Thompson int CeedObjectDestroy_Private(CeedObject obj) { 80*6c328a79SJeremy L Thompson CeedCheck(obj->ref_count == 0, CeedObjectReturnCeed(obj), CEED_ERROR_UNSUPPORTED, 81*6c328a79SJeremy L Thompson "Cannot destroy CeedObject, it is still referenced by another object"); 82b0f67a9cSJeremy L Thompson if (obj->ceed) CeedCall(CeedDestroy(&obj->ceed)); 83b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 84b0f67a9cSJeremy L Thompson } 85b0f67a9cSJeremy L Thompson 86b0f67a9cSJeremy L Thompson /// @} 87b0f67a9cSJeremy L Thompson 88b0f67a9cSJeremy L Thompson /// ---------------------------------------------------------------------------- 89b0f67a9cSJeremy L Thompson /// CeedObject Public API 90b0f67a9cSJeremy L Thompson /// ---------------------------------------------------------------------------- 91b0f67a9cSJeremy L Thompson /// @addtogroup CeedUser 92b0f67a9cSJeremy L Thompson /// @{ 93b0f67a9cSJeremy L Thompson 94b0f67a9cSJeremy L Thompson /** 95b0f67a9cSJeremy L Thompson @brief View a `CeedObject` 96b0f67a9cSJeremy L Thompson 97b0f67a9cSJeremy L Thompson @param[in] obj `CeedObject` to view 98b0f67a9cSJeremy L Thompson @param[in] stream Stream to view to, e.g., `stdout` 99b0f67a9cSJeremy L Thompson 100b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 101b0f67a9cSJeremy L Thompson 102b0f67a9cSJeremy L Thompson @ref User 103b0f67a9cSJeremy L Thompson **/ 104b0f67a9cSJeremy L Thompson int CeedObjectView(CeedObject obj, FILE *stream) { 105*6c328a79SJeremy L Thompson if (obj->View) CeedCall(obj->View(obj, stream)); 106b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 107b0f67a9cSJeremy L Thompson } 108b0f67a9cSJeremy L Thompson 109b0f67a9cSJeremy L Thompson /** 110a299a25bSJeremy L Thompson @brief Set the number of tabs to indent for @ref CeedObjectView() output 111a299a25bSJeremy L Thompson 112a299a25bSJeremy L Thompson @param[in] obj `CeedObject` to set the number of view tabs 113a299a25bSJeremy L Thompson @param[in] num_tabs Number of view tabs to set 114a299a25bSJeremy L Thompson 115a299a25bSJeremy L Thompson @return Error code: 0 - success, otherwise - failure 116a299a25bSJeremy L Thompson 117a299a25bSJeremy L Thompson @ref User 118a299a25bSJeremy L Thompson **/ 119a299a25bSJeremy L Thompson int CeedObjectSetNumViewTabs(CeedObject obj, CeedInt num_tabs) { 120a299a25bSJeremy L Thompson CeedCheck(num_tabs >= 0, CeedObjectReturnCeed(obj), CEED_ERROR_MINOR, "Number of view tabs must be non-negative"); 121a299a25bSJeremy L Thompson obj->num_view_tabs = num_tabs; 122a299a25bSJeremy L Thompson return CEED_ERROR_SUCCESS; 123a299a25bSJeremy L Thompson } 124a299a25bSJeremy L Thompson 125a299a25bSJeremy L Thompson /** 126a299a25bSJeremy L Thompson @brief Get the number of tabs to indent for @ref CeedObjectView() output 127a299a25bSJeremy L Thompson 128a299a25bSJeremy L Thompson @param[in] obj `CeedObject` to get the number of view tabs 129a299a25bSJeremy L Thompson @param[out] num_tabs Number of view tabs 130a299a25bSJeremy L Thompson 131a299a25bSJeremy L Thompson @return Error code: 0 - success, otherwise - failure 132a299a25bSJeremy L Thompson 133a299a25bSJeremy L Thompson @ref User 134a299a25bSJeremy L Thompson **/ 135a299a25bSJeremy L Thompson int CeedObjectGetNumViewTabs(CeedObject obj, CeedInt *num_tabs) { 136a299a25bSJeremy L Thompson *num_tabs = obj->num_view_tabs; 137a299a25bSJeremy L Thompson return CEED_ERROR_SUCCESS; 138a299a25bSJeremy L Thompson } 139a299a25bSJeremy L Thompson 140a299a25bSJeremy L Thompson /** 141b0f67a9cSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedObject` 142b0f67a9cSJeremy L Thompson 143b0f67a9cSJeremy L Thompson @param[in] obj `CeedObject` 144b0f67a9cSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 145b0f67a9cSJeremy L Thompson 146b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 147b0f67a9cSJeremy L Thompson 148b0f67a9cSJeremy L Thompson @ref Advanced 149b0f67a9cSJeremy L Thompson **/ 150b0f67a9cSJeremy L Thompson int CeedObjectGetCeed(CeedObject obj, Ceed *ceed) { 151b0f67a9cSJeremy L Thompson *ceed = NULL; 152b0f67a9cSJeremy L Thompson CeedCall(CeedReferenceCopy(CeedObjectReturnCeed(obj), ceed)); 153b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 154b0f67a9cSJeremy L Thompson } 155b0f67a9cSJeremy L Thompson 156b0f67a9cSJeremy L Thompson /** 157b0f67a9cSJeremy L Thompson @brief Return the `Ceed` associated with a `CeedObject` 158b0f67a9cSJeremy L Thompson 159b0f67a9cSJeremy L Thompson @param[in] obj `CeedObject` 160b0f67a9cSJeremy L Thompson 161b0f67a9cSJeremy L Thompson @return `Ceed` associated with the `basis` 162b0f67a9cSJeremy L Thompson 163b0f67a9cSJeremy L Thompson @ref Advanced 164b0f67a9cSJeremy L Thompson **/ 165b0f67a9cSJeremy L Thompson Ceed CeedObjectReturnCeed(CeedObject obj) { return (obj->ceed) ? obj->ceed : (Ceed)obj; } 166b0f67a9cSJeremy L Thompson 167*6c328a79SJeremy L Thompson /** 168*6c328a79SJeremy L Thompson @brief Destroy a @ref CeedObject 169*6c328a79SJeremy L Thompson 170*6c328a79SJeremy L Thompson @param[in,out] obj Address of `CeedObject` to destroy 171*6c328a79SJeremy L Thompson 172*6c328a79SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 173*6c328a79SJeremy L Thompson 174*6c328a79SJeremy L Thompson @ref Backend 175*6c328a79SJeremy L Thompson **/ 176*6c328a79SJeremy L Thompson int CeedObjectDestroy(CeedObject *obj) { 177*6c328a79SJeremy L Thompson CeedCall((*obj)->Destroy(obj)); 178*6c328a79SJeremy L Thompson return CEED_ERROR_SUCCESS; 179*6c328a79SJeremy L Thompson } 180*6c328a79SJeremy L Thompson 181b0f67a9cSJeremy L Thompson /// @} 182