tools.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "tools.hpp"
27 
28 #include "casadi_misc.hpp"
29 #include "importer.hpp"
30 #include "serializer.hpp"
31 #include <casadi/config.h>
32 #include "casadi_os.hpp"
33 #include "casadi_meta.hpp"
34 
35 namespace casadi {
36 
37 void callback_stdout(const char* s) {
38  uout() << s << std::flush;
39 }
40 
41 void callback_stderr(const char* s) {
42  uerr() << s << std::flush;
43 }
44 
45 Function external_transform(const std::string& name,
46  const std::string& op,
47  const Function& f,
48  const Dict& opts) {
49  std::string signature = "f";
50  Importer li(name + SHARED_LIBRARY_SUFFIX, "dll");
51  std::string op_full = op + "__" + signature;
53  if (!t) {
54  casadi_error("Transformation '" + op + "' not defined in library '" + name +
55  "' for the following signature: " + signature);
56  }
57 
59  ss.pack(f);
60  ss.pack(opts);
61 
62  char api_version = 0;
63  std::string casadi_version = CasadiMeta::version();
64 
65  std::string s = ss.encode();
66  const char* out = t(api_version, casadi_version.c_str(), s.c_str(),
68  if (!out) {
69  casadi_error("Transformation '" + op + "' defined in library '" + name +
70  "' failed.");
71  }
72 
73  StringDeserializer sd(out);
74  Function r = sd.unpack_function();
75 
76  return r;
77 }
78 
79 } // namespace casadi
80 
81 const char* external_transform_test_success__f(char api_version, const char* casadi_version,
82  const char* in,
84  if (api_version != 0) {
85  cb_stderr("version mismatch");
86  return 0;
87  }
89  casadi::Function f = sd.unpack_function();
90  casadi::Dict opts = sd.unpack_generictype();
91 
92  std::string msg = "passed options: " + str(opts) + "\n";
93 
94  cb_stdout(msg.c_str());
95 
96  cb_stdout("Doing a lot of stuff...\n");
97  cb_stderr("Warning here...\n");
98 
100  ss.pack(f);
101  static std::string s = ss.encode();
102 
103  return s.c_str();
104 }
105 
106 const char* external_transform_test_fail__f(char api_version, const char* casadi_version,
107  const char* in,
109  cb_stdout("This is going to fail\n");
110  cb_stderr("Fatal error\n");
111  return 0;
112 }
static const char * version()
Obtain the version number of CasADi.
Definition: casadi_meta.cpp:30
Function object.
Definition: function.hpp:60
Importer.
Definition: importer.hpp:86
signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.
Definition: importer.cpp:79
void pack(const Sparsity &e)
std::string encode()
Returns a string that holds the serialized objects.
Definition: serializer.cpp:87
The casadi namespace.
Definition: archiver.cpp:28
std::ostream & uerr()
void callback_stdout(const char *s)
Definition: tools.cpp:37
void(* external_print_callback_t)(const char *s)
Definition: tools.hpp:47
const char *(* external_transform_t)(char api_version, const char *casadi_version, const char *in, external_print_callback_t cb_stdout, external_print_callback_t cb_stderr)
Definition: tools.hpp:48
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void callback_stderr(const char *s)
Definition: tools.cpp:41
std::ostream & uout()
Function external_transform(const std::string &name, const std::string &op, const Function &f, const Dict &opts)
Apply a transformation defined externally.
Definition: tools.cpp:45