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