1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
/* -----------------------------------------------------------------------------
* Programmer(s): David J. Gardner @ LLNL
* -----------------------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2022, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------------------
* Utility functions for C++ examples
* ---------------------------------------------------------------------------*/
#include <algorithm>
#include <iostream>
#include <vector>
// Check function return flag
int check_flag(const int flag, const std::string funcname)
{
if (!flag) return 0;
if (flag < 0) std::cerr << "ERROR: ";
std::cerr << funcname << " returned " << flag << std::endl;
return 1;
}
// Check if a function returned a NULL pointer
int check_ptr(const void* ptr, const std::string funcname)
{
if (ptr) return 0;
std::cerr << "ERROR: " << funcname << " returned NULL" << std::endl;
return 1;
}
inline void find_arg(std::vector<std::string>& args, const std::string key, sunrealtype& dest)
{
auto it = std::find(args.begin(), args.end(), key);
if (it != args.end()) {
#if defined(SUNDIALS_SINGLE_PRECISION)
dest = stof(*(it + 1));
#elif defined(SUNDIALS_DOUBLE_PRECISION)
dest = stod(*(it + 1));
#elif defined(SUNDIALS_EXTENDED_PRECISION)
dest = stold(*(it + 1));
#endif
args.erase(it, it + 2);
}
}
inline void find_arg(std::vector<std::string>& args, const std::string key, long long& dest)
{
auto it = std::find(args.begin(), args.end(), key);
if (it != args.end()) {
dest = stoll(*(it + 1));
args.erase(it, it + 2);
}
}
inline void find_arg(std::vector<std::string>& args, const std::string key, long int& dest)
{
auto it = std::find(args.begin(), args.end(), key);
if (it != args.end()) {
dest = stol(*(it + 1));
args.erase(it, it + 2);
}
}
inline void find_arg(std::vector<std::string>& args, const std::string key, int& dest)
{
auto it = std::find(args.begin(), args.end(), key);
if (it != args.end()) {
dest = stoi(*(it + 1));
args.erase(it, it + 2);
}
}
inline void find_arg(std::vector<std::string>& args, const std::string key, bool& dest, bool store = true)
{
auto it = std::find(args.begin(), args.end(), key);
if (it != args.end()) {
dest = store;
args.erase(it);
}
}
|