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_os.hpp"
32 #include "casadi_meta.hpp"
33 
34 namespace casadi {
35 
36 void callback_stdout(const char* s) {
37  uout() << s << std::flush;
38 }
39 
40 void callback_stderr(const char* s) {
41  uerr() << s << std::flush;
42 }
43 
44 Function external_transform(const std::string& name,
45  const std::string& op,
46  const Function& f,
47  const Dict& opts) {
48  std::string signature = "f";
49  Importer li(name + CasadiMeta::shared_library_suffix(), "dll");
50  std::string op_full = op + "__" + signature;
52  if (!t) {
53  casadi_error("Transformation '" + op + "' not defined in library '" + name +
54  "' for the following signature: " + signature);
55  }
56 
58  ss.pack(f);
59  ss.pack(opts);
60 
61  char api_version = 0;
62  std::string casadi_version = CasadiMeta::version();
63 
64  std::string s = ss.encode();
65  const char* out = t(api_version, casadi_version.c_str(), s.c_str(),
67  if (!out) {
68  casadi_error("Transformation '" + op + "' defined in library '" + name +
69  "' failed.");
70  }
71 
72  StringDeserializer sd(out);
73  Function r = sd.unpack_function();
74 
75  return r;
76 }
77 
78 } // namespace casadi
79 
80 const char* external_transform_test_success__f(char api_version, const char* casadi_version,
81  const char* in,
83  if (api_version != 0) {
84  cb_stderr("version mismatch");
85  return nullptr;
86  }
88  casadi::Function f = sd.unpack_function();
89  casadi::Dict opts = sd.unpack_generictype();
90 
91  std::string msg = "passed options: " + str(opts) + "\n";
92 
93  cb_stdout(msg.c_str());
94 
95  cb_stdout("Doing a lot of stuff...\n");
96  cb_stderr("Warning here...\n");
97 
99  ss.pack(f);
100  static std::string s = ss.encode();
101 
102  return s.c_str();
103 }
104 
105 const char* external_transform_test_fail__f(char api_version, const char* casadi_version,
106  const char* in,
108  cb_stdout("This is going to fail\n");
109  cb_stderr("Fatal error\n");
110  return nullptr;
111 }
static const char * version()
Obtain the version number of CasADi.
Definition: casadi_meta.cpp:30
static const char * shared_library_suffix()
Obtain shared library suffix.
Definition: casadi_meta.cpp:54
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:84
void pack(const Sparsity &e)
std::string encode()
Returns a string that holds the serialized objects.
Definition: serializer.cpp:86
The casadi namespace.
Definition: archiver.cpp:28
std::ostream & uerr()
void callback_stdout(const char *s)
Definition: tools.cpp:36
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:40
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:44