IODA
Dimensions_c.cpp
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020-2021 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 /*! \addtogroup ioda_dimensions
8  * @{
9  * \file Dimensions_c.cpp
10  * \brief @link ioda_dimensions C bindings @endlink for ioda::Dimensions
11  */
12 #include "ioda/C/Dimensions_c.h"
13 
14 #include <numeric>
15 
16 #include "./structs_c.h"
17 
18 extern "C" {
19 
21  C_TRY;
22  Expects(d != nullptr);
23  delete d;
25 }
26 
28  C_TRY;
29  Expects(d != nullptr);
30  Expects(res != nullptr);
31  *res = gsl::narrow<size_t>(d->d.dimensionality);
32  C_CATCH_AND_RETURN(true, false);
33 }
34 
36  C_TRY;
37  Expects(d != nullptr);
38  d->d.dimensionality = gsl::narrow<ioda::Dimensions_t>(N);
39  d->d.dimsCur.resize(N, 0);
40  d->d.dimsMax.resize(N, 0);
41  d->d.numElements = gsl::narrow<size_t>(std::accumulate(d->d.dimsCur.begin(), d->d.dimsCur.end(),
42  (ioda::Dimensions_t)1,
43  std::multiplies<ioda::Dimensions_t>()));
44  C_CATCH_AND_RETURN(true, false);
45 }
46 
48  C_TRY;
49  Expects(d != nullptr);
50  Expects(res != nullptr);
51  *res = gsl::narrow<size_t>(d->d.numElements);
52  C_CATCH_AND_RETURN(true, false);
53 }
54 
55 bool ioda_dimensions_get_dim_cur(const ioda_dimensions* d, size_t n, ptrdiff_t* res) {
56  C_TRY;
57  Expects(d != nullptr);
58  Expects(d->d.dimsCur.size() > n);
59  Expects(res != nullptr);
60  *res = gsl::narrow<ptrdiff_t>(d->d.dimsCur.at(n));
61  C_CATCH_AND_RETURN(true, false);
62 }
63 
64 bool ioda_dimensions_set_dim_cur(ioda_dimensions* d, size_t n, ptrdiff_t sz) {
65  C_TRY;
66  Expects(d != nullptr);
67  Expects(d->d.dimsCur.size() > n);
68  d->d.dimsCur[n] = gsl::narrow<ioda::Dimensions_t>(sz);
69  C_CATCH_AND_RETURN(true, false);
70 }
71 
72 bool ioda_dimensions_get_dim_max(const ioda_dimensions* d, size_t n, ptrdiff_t* res) {
73  C_TRY;
74  Expects(d != nullptr);
75  Expects(d->d.dimsMax.size() > n);
76  Expects(res != nullptr);
77  *res = gsl::narrow<ptrdiff_t>(d->d.dimsMax.at(n));
78  C_CATCH_AND_RETURN(true, false);
79 }
80 
81 bool ioda_dimensions_set_dim_max(ioda_dimensions* d, size_t n, ptrdiff_t sz) {
82  C_TRY;
83  Expects(d != nullptr);
84  Expects(d->d.dimsMax.size() > n);
85  d->d.dimsMax[n] = gsl::narrow<ioda::Dimensions_t>(sz);
86  C_CATCH_AND_RETURN(true, false);
87 }
88 }
89 
90 /// @}
C bindings for ioda::Dimensions
#define C_CATCH_AND_RETURN(retval_on_success, retval_on_error)
This macro catches C++ exceptions.
#define C_CATCH_AND_TERMINATE
Catch C++ exceptions before they go across code boundaries.
#define C_TRY
Goes with C_CATCH_AND_TERMINATE.
bool ioda_dimensions_get_num_elements(const ioda_dimensions *d, size_t *res)
bool ioda_dimensions_get_dim_cur(const ioda_dimensions *d, size_t n, ptrdiff_t *res)
IODA_DL bool ioda_dimensions_set_dimensionality(struct ioda_dimensions *d, size_t N)
Set dimensionality of the dimensions container.
bool ioda_dimensions_get_dimensionality(const ioda_dimensions *d, size_t *res)
IODA_DL bool ioda_dimensions_set_dim_max(struct ioda_dimensions *d, size_t n, ptrdiff_t sz)
Set the maximum size of the n-th dimension.
IODA_DL bool ioda_dimensions_set_dim_cur(struct ioda_dimensions *d, size_t n, ptrdiff_t sz)
Set the current size of the n-th dimension.
bool ioda_dimensions_get_dim_max(const ioda_dimensions *d, size_t n, ptrdiff_t *res)
IODA_DL void ioda_dimensions_destruct(struct ioda_dimensions *d)
Deallocates a dimensions container.
std::vector< Dimensions_t > dimsCur
The dimensions of the data.
Definition: Dimensions.h:23
Dimensions_t numElements
Definition: Dimensions.h:26
Dimensions_t dimensionality
The dimensionality (rank) of the data.
Definition: Dimensions.h:25
std::vector< Dimensions_t > dimsMax
This must always equal dimsCur for Attribute.
Definition: Dimensions.h:24
ioda::Dimensions d
Definition: structs_c.h:36
C wrappers for ioda classes and structures. Private header. Can have C++!