1#ifndef NGEN_BMI_UTILITIES_HPP
2#define NGEN_BMI_UTILITIES_HPP
6#include <boost/type_index.hpp>
7#include "Bmi_Adapter.hpp"
24 template<
typename TO,
typename FROM>
25 static inline std::vector<TO>
make_vector(
const FROM* data,
const size_t& count){
27 return std::vector<TO>(data, data+count);
45 if( total_mem == 0 || item_size == 0){
55 throw std::runtime_error(
"Unable to get value of variable "+name+
". Model "+ model.
get_model_name() +
56 " reports no valid items (Nbytes = "+std::to_string(total_mem) +
57 ", Itemsize = "+std::to_string(item_size)+
".");
59 int num_items = total_mem/item_size;
64 void* data = ::operator
new(total_mem);
67 auto sptr = std::shared_ptr<void>(data, [](
void *p) { ::operator
delete(p); });
72 std::vector<T> result;
83 if (type ==
"long double"){
86 else if (type ==
"double"){
89 else if (type ==
"float"){
92 else if (type ==
"short" || type ==
"short int" || type ==
"signed short" || type ==
"signed short int"){
95 else if (type ==
"unsigned short" || type ==
"unsigned short int"){
98 else if (type ==
"int" || type ==
"signed" || type ==
"signed int"){
101 else if (type ==
"unsigned" || type ==
"unsigned int"){
104 else if (type ==
"long" || type ==
"long int" || type ==
"signed long" || type ==
"signed long int"){
107 else if (type ==
"unsigned long" || type ==
"unsigned long int"){
110 else if (type ==
"long long" || type ==
"long long int" || type ==
"signed long long" || type ==
"signed long long int"){
113 else if (type ==
"unsigned long long" || type ==
"unsigned long long int"){
117 throw std::runtime_error(
"Unable to get value of variable " + name +
118 " as " + boost::typeindex::type_id<T>().pretty_name() +
": no logic for converting variable type " + type);
virtual std::string GetVarType(std::string name)=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:123
virtual const std::string get_analogous_cxx_type(const std::string &external_type_name, const size_t item_size)=0
Get the name string for the C++ type analogous to the described type in the backing model.
static std::vector< TO > make_vector(const FROM *data, const size_t &count)
make a vector of type
Definition bmi_utilities.hpp:25
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:41
Definition AbstractCLibBmiAdapter.hpp:6