1#ifndef NGEN_BMI_UTILITIES_HPP
2#define NGEN_BMI_UTILITIES_HPP
13#include <boost/type_index.hpp>
14#include "bmi/Bmi_Adapter.hpp"
15#include "utilities/bmi_primitive_type.hpp"
58 std::string var_name(name);
59 const size_t item_size =
static_cast<size_t>(model.
GetVarItemsize(var_name));
60 const std::string reported = model.
GetVarType(std::move(var_name));
64 if (
size_of(type) != item_size) {
65 throw std::runtime_error(
"Cannot use variable '" + std::string(name) +
"' of model '" +
68 " bytes, but the model reports an item size of " +
69 std::to_string(item_size) +
" bytes.");
87 template<
typename TO,
typename FROM>
88 static inline std::vector<TO>
make_vector(
const FROM* data,
const size_t& count){
90 return std::vector<TO>(data, data+count);
108 template <
typename T,
typename Iterator>
113 std::shared_ptr<T> ptr(
new T[std::distance(begin, end)], [](T *p) {
delete[] p; } );
114 std::transform(begin, end, ptr.get(), [](
auto const& in){ return static_cast<T>(in); });
127 template <
typename Iterator>
147 return std::make_shared<U>(
static_cast<U
>(value));
169 template <
typename T>
172 const size_t item_size =
static_cast<size_t>(model.
GetVarItemsize(name));
173 if (item_size !=
sizeof(T)) {
174 throw std::runtime_error(
"Cannot get pointer to variable " + name +
" of model " +
176 " byte type; its items are " + std::to_string(item_size) +
" bytes.");
190 template <
typename T>
195 if( total_mem == 0 || item_size == 0){
205 throw std::runtime_error(
"Unable to get value of variable "+name+
". Model "+ model.
get_model_name() +
206 " reports no valid items (Nbytes = "+std::to_string(total_mem) +
207 ", Itemsize = "+std::to_string(item_size)+
".");
209 int num_items = total_mem/item_size;
216 throw std::runtime_error(
"Unable to get value of variable " + name +
" of model " +
218 boost::typeindex::type_id<T>().pretty_name() +
219 ": no logic for converting variable type " + e.
type_name);
223 void* data = ::operator
new(total_mem);
226 auto sptr = std::shared_ptr<void>(data, [](
void *p) { ::operator
delete(p); });
251 template <
typename T>
254 const int num_items = item_size > 0 ? model.
GetVarNbytes(name) / item_size : 0;
255 if (index < 0 || index >= num_items) {
256 throw std::out_of_range(
"Cannot get item " + std::to_string(index) +
" of variable " + name +
258 std::to_string(num_items) +
" items.");
265 throw std::runtime_error(
"Unable to get value of variable " + name +
" of model " +
267 boost::typeindex::type_id<T>().pretty_name() +
268 ": no logic for converting variable type " + e.
type_name);
274 return static_cast<T
>(item);
virtual std::string GetVarType(std::string name)=0
virtual void * GetValuePtr(std::string name)=0
virtual void GetValueAtIndices(std::string name, void *dest, int *inds, int count)=0
virtual void GetValue(std::string name, void *dest)=0
virtual int GetVarItemsize(std::string name)=0
virtual int GetVarNbytes(std::string name)=0
Abstract adapter interface for C++ classes to interact with the essential aspects of external models ...
Definition Bmi_Adapter.hpp:17
std::string get_model_name()
Get the model name.
Definition Bmi_Adapter.cpp:124
virtual primitive_type get_primitive_type(std::string_view external_type_name, const size_t item_size)
Resolve a type name the backing model reports to a models::bmi::primitive_type.
Definition Bmi_Adapter.hpp:84
static std::vector< TO > make_vector(const FROM *data, const size_t &count)
make a vector of type
Definition bmi_utilities.hpp:88
Definition deserialization.hpp:40
std::vector< T > GetValue(Bmi_Adapter &model, const std::string &name)
Copy values from a BMI model adapter and box them into a vector.
Definition bmi_utilities.hpp:191
T * GetValuePtr(Bmi_Adapter &model, const std::string &name)
Get a pointer to a model variable's own storage, addressed as.
Definition bmi_utilities.hpp:170
auto with_primitive_type(primitive_type type, F &&f)
Call f with the C++ type that type denotes.
Definition bmi_primitive_type.hpp:172
std::shared_ptr< void > get_value_as_type(primitive_type type, T value)
Gets a single value, casted based on type then returned as a typeless (void) pointer.
Definition bmi_utilities.hpp:144
std::shared_ptr< T > as_c_array(Iterator begin, Iterator end)
Template function for copying iterator range into contiguous array.
Definition bmi_utilities.hpp:109
primitive_type
The type of one item of a BMI variable, independent of how any language spells it.
Definition bmi_primitive_type.hpp:50
primitive_type resolve_var_type(Bmi_Adapter &model, std::string_view name)
Resolve the type to address a model's variable as, checked against its item size.
Definition bmi_utilities.hpp:55
std::shared_ptr< void > get_values_as_type(primitive_type type, Iterator begin, Iterator end)
Gets values in iterator range, casted based on type then returned as typeless (void) pointer.
Definition bmi_utilities.hpp:128
constexpr size_t size_of(primitive_type type)
The size in bytes of one item of type.
Definition bmi_primitive_type.hpp:85
Definition AbstractCLibBmiAdapter.hpp:6
Thrown when a BMI type name has no C++ type behind it.
Definition bmi_primitive_type.hpp:26
std::string type_name
Definition bmi_primitive_type.hpp:31