List of all members | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions
casadi::TinyXmlInterface Class Reference

'tinyxml' plugin for XmlFile More...

#include <tinyxml_interface.hpp>

Detailed Description

XmlFile using TinyXml

Extra doc: https://github.com/casadi/casadi/wiki/L_22i

Definition at line 55 of file tinyxml_interface.hpp.

Inheritance diagram for casadi::TinyXmlInterface:
Inheritance graph
[legend]
Collaboration diagram for casadi::TinyXmlInterface:
Collaboration graph
[legend]

Public Types

typedef XmlFileInternal *(* Creator) ()
 
using weak_ref_type = WeakRefInternal
 
typedef int(* RegFcn) (Plugin *plugin)
 

Public Member Functions

 TinyXmlInterface ()
 
const char * plugin_name () const override
 
std::string class_name () const override
 Readable name of the internal class. More...
 
XmlNode parse (const std::string &filename) override
 
void dump (const std::string &filename, const XmlNode &node) override
 
XmlNode import_node (TiXmlNode *n)
 
void export_node (TiXmlNode *n, const XmlNode &node)
 
 ~TinyXmlInterface () override
 
void disp (std::ostream &stream, bool more=false) const override
 Print a description. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
void serialize_type (SerializingStream &s) const
 Serialize type information. More...
 

Static Public Member Functions

static XmlFileInternalcreator ()
 Create a new XmlFile. More...
 
static bool has_plugin (const std::string &pname, bool verbose=false)
 Check if a plugin is available or can be loaded. More...
 
static const Optionsplugin_options (const std::string &pname)
 Get the plugin options. More...
 
static Deserialize plugin_deserialize (const std::string &pname)
 Get the plugin deserialize_map. More...
 
static Plugin pluginFromRegFcn (RegFcn regfcn)
 Instantiate a Plugin struct from a factory function. More...
 
static Plugin load_plugin (const std::string &pname, bool register_plugin=true, bool needs_lock=true)
 Load a plugin dynamically. More...
 
static handle_t load_library (const std::string &libname, std::string &resultpath, bool global)
 Load a library dynamically. More...
 
static void registerPlugin (const Plugin &plugin, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static void registerPlugin (RegFcn regfcn, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static Plugin & getPlugin (const std::string &pname)
 Load and get the creator function. More...
 
static XmlFileInternalinstantiate (const std::string &fname, const std::string &pname, Problem problem)
 
static ProtoFunctiondeserialize (DeserializingStream &s)
 Deserialize with type disambiguation. More...
 

Public Attributes

TiXmlDocument doc_
 XML file. More...
 

Static Public Attributes

static const std::string meta_doc
 A documentation string. More...
 
static std::map< std::string, Plugin > solvers_
 Collection of solvers. More...
 
static const std::string infix_ = "xmlfile"
 Infix. More...
 

Protected Member Functions

void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Member Typedef Documentation

◆ Creator

typedef XmlFileInternal*(* casadi::XmlFileInternal::Creator) ()
inherited

Definition at line 57 of file xml_file_internal.hpp.

◆ RegFcn

typedef int(* casadi::PluginInterface< XmlFileInternal >::RegFcn) (Plugin *plugin)
inherited

Definition at line 73 of file plugin_interface.hpp.

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ TinyXmlInterface()

casadi::TinyXmlInterface::TinyXmlInterface ( )

Definition at line 45 of file tinyxml_interface.cpp.

45  {
46 }

◆ ~TinyXmlInterface()

casadi::TinyXmlInterface::~TinyXmlInterface ( )
override

Definition at line 48 of file tinyxml_interface.cpp.

48  {
49 }

Member Function Documentation

◆ class_name()

std::string casadi::TinyXmlInterface::class_name ( ) const
inlineoverridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 69 of file tinyxml_interface.hpp.

69 { return "TinyXmlInterface";}

◆ creator()

static XmlFileInternal* casadi::TinyXmlInterface::creator ( )
inlinestatic

Definition at line 62 of file tinyxml_interface.hpp.

63  { return new TinyXmlInterface();}

Referenced by casadi::casadi_register_xmlfile_tinyxml().

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

162  {
163  // Note: i != this because of something something multiple inheritance
164  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
165  }
std::string str(const T &v)
String representation, any type.

◆ deserialize()

static ProtoFunction* casadi::PluginInterface< XmlFileInternal >::deserialize ( DeserializingStream s)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rr

Definition at line 121 of file plugin_interface.hpp.

121  {
122  std::string class_name, plugin_name;
123  s.unpack("PluginInterface::plugin_name", plugin_name);
125  return deserialize(s);
126  }
static Deserialize plugin_deserialize(const std::string &pname)
Get the plugin deserialize_map.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
virtual const char * plugin_name() const=0
ProtoFunction *(* Deserialize)(DeserializingStream &)

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::XmlFileInternal::disp ( std::ostream &  stream,
bool  more = false 
) const
overridevirtualinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_ed

Implements casadi::SharedObjectInternal.

Definition at line 44 of file xml_file_internal.cpp.

44  {
45 }

◆ dump()

void casadi::TinyXmlInterface::dump ( const std::string &  filename,
const XmlNode node 
)
overridevirtual

Reimplemented from casadi::XmlFileInternal.

Definition at line 65 of file tinyxml_interface.cpp.

65  {
66  // Add encoding declaration
67  doc_.InsertEndChild(doc_.NewDeclaration());
68  // Export from CasADi representation
69  try {
70  export_node(&doc_, node);
71  } catch (std::exception& e) {
72  casadi_error("Cannot export " + filename + ": " + std::string(e.what()));
73  }
74  // Save XML file to disk
75  XMLError err = doc_.SaveFile(filename.c_str());
76  casadi_assert(!err, "Cannot save " + filename);
77 }
void export_node(TiXmlNode *n, const XmlNode &node)
TiXmlDocument doc_
XML file.
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References doc_, export_node(), and casadi::filename().

◆ export_node()

void casadi::TinyXmlInterface::export_node ( TiXmlNode *  n,
const XmlNode node 
)

Definition at line 130 of file tinyxml_interface.cpp.

130  {
131  // Loop over children
132  for (const XmlNode& c : node.children) {
133  // Create new node for the child
134  TiXmlElement* tc = doc_.NewElement(c.name.c_str());
135  n->InsertEndChild(tc);
136  // Save all attributes
137  for (auto&& a : c.attributes) {
138  tc->SetAttribute(a.first.c_str(), a.second.c_str());
139  }
140  // Add (grand)children, if any
141  export_node(tc, c);
142  }
143 }

References casadi::XmlNode::attributes, casadi::XmlNode::children, doc_, and casadi::XmlNode::name.

Referenced by dump().

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

186  {
187  return static_cast<const Internal*>(this)->count;
188  }

◆ getPlugin()

PluginInterface< XmlFileInternal >::Plugin & casadi::PluginInterface< XmlFileInternal >::getPlugin ( const std::string &  pname)
staticinherited

Definition at line 102 of file plugin_interface.hpp.

291  {
292 
293 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
294  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
295 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
296 
297  // Check if the solver has been loaded
298  auto it=Derived::solvers_.find(pname);
299 
300  // Load the solver if needed
301  if (it==Derived::solvers_.end()) {
302  load_plugin(pname, true, false);
303  it=Derived::solvers_.find(pname);
304  }
305  casadi_assert_dev(it!=Derived::solvers_.end());
306  return it->second;
307  }
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.

◆ has_plugin()

bool casadi::PluginInterface< XmlFileInternal >::has_plugin ( const std::string &  pname,
bool  verbose = false 
)
staticinherited

Definition at line 76 of file plugin_interface.hpp.

131  {
132 
133 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
134  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
135 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
136 
137  // Quick return if available
138  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
139  return true;
140  }
141 
142  // Try loading the plugin
143  try {
144  (void)load_plugin(pname, false, false);
145  return true;
146  } catch (CasadiException& ex) {
147  if (verbose) {
148  casadi_warning(ex.what());
149  }
150  return false;
151  }
152  }
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros

◆ import_node()

XmlNode casadi::TinyXmlInterface::import_node ( TiXmlNode *  n)

Definition at line 79 of file tinyxml_interface.cpp.

79  {
80  if (!n) casadi_error("Node is 0");
81  XmlNode ret;
82 
83  ret.line = n->GetLineNum();
84 
85  // Save name
86  if (n->Value()) {
87  ret.name = n->Value();
88  }
89 
90  // Save attributes
91  if (n->ToElement()) {
92  for (const TiXmlAttribute* pAttrib=n->ToElement()->FirstAttribute();
93  pAttrib; pAttrib=pAttrib->Next()) {
94  ret.set_attribute(pAttrib->Name(), pAttrib->Value());
95  }
96  } else if (n->ToDocument()) {
97  // do nothing
98  } else {
99  casadi_error("TinyXmlInterface::import_node");
100  }
101 
102  // Count the number of children
103  casadi_int num_children = 0;
104  for (TiXmlNode* child = n->FirstChild(); child != nullptr; child = child->NextSibling()) {
105  num_children++;
106  }
107  ret.children.reserve(num_children);
108 
109  // add children
110  for (TiXmlNode* child = n->FirstChild(); child != nullptr; child = child->NextSibling()) {
111  if (child->ToElement()) {
112  ret.children.push_back(import_node(child));
113  } else if (child->ToComment()) {
114  ret.comment = child->Value();
115  } else if (child->ToText()) {
116  ret.text = child->ToText()->Value();
117  } else if (child->ToDeclaration()) {
118  // pass
119  } else if (child->ToDocument()) {
120  // pass
121  } else {
122  casadi_error("Unknown node type");
123  }
124  }
125 
126  // Note: Return value optimization
127  return ret;
128 }
XmlNode import_node(TiXmlNode *n)

References casadi::XmlNode::children, casadi::XmlNode::comment, casadi::XmlNode::line, casadi::XmlNode::name, casadi::XmlNode::set_attribute(), and casadi::XmlNode::text.

Referenced by parse().

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ instantiate()

XmlFileInternal * casadi::PluginInterface< XmlFileInternal >::instantiate ( const std::string &  fname,
const std::string &  pname,
Problem  problem 
)
staticinherited

Definition at line 106 of file plugin_interface.hpp.

313  {
314 
315  // Assert the plugin exists (needed for adaptors)
316  if (!has_plugin(pname, true)) {
317  casadi_error("Plugin '" + pname + "' is not found.");
318  }
319  return getPlugin(pname).creator(fname, problem);
320  }
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.

◆ load_library()

handle_t casadi::PluginInterface< XmlFileInternal >::load_library ( const std::string &  libname,
std::string &  resultpath,
bool  global 
)
staticinherited

Definition at line 92 of file plugin_interface.hpp.

184  {
185 
186 #ifndef WITH_DL
187  casadi_error("WITH_DL option needed for dynamic loading");
188 #else // WITH_DL
189 
190  // Get the name of the shared library
191  std::string lib = SHARED_LIBRARY_PREFIX + libname + SHARED_LIBRARY_SUFFIX;
192 
193  // Build up search paths;
194  std::vector<std::string> search_paths = get_search_paths();
195  return open_shared_library(lib, search_paths, resultpath,
196  "PluginInterface::load_plugin", global);
197 
198 #endif // WITH_DL
199  }
std::vector< std::string > get_search_paths()
Definition: casadi_os.cpp:49

◆ load_plugin()

PluginInterface< XmlFileInternal >::Plugin casadi::PluginInterface< XmlFileInternal >::load_plugin ( const std::string &  pname,
bool  register_plugin = true,
bool  needs_lock = true 
)
staticinherited

Definition at line 88 of file plugin_interface.hpp.

204  {
205 
206 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
207  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
208 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
209 
210  // Issue warning and quick return if already loaded
211  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
212  casadi_warning("PluginInterface: Solver " + pname + " is already in use. Ignored.");
213  return Plugin();
214  }
215 
216  // Logger singletons are lazily instantiated on first uout()/uerr() calls
217  // This instantation may lead to a data race with potential instatiations in plugin
218  // To be safe, trigger instantatin before any plugin loading
219  uout();
220  uerr();
221 
222 #ifndef WITH_DL
223  casadi_error("WITH_DL option needed for dynamic loading");
224 #else // WITH_DL
225  // Retrieve the registration function
226  RegFcn reg;
227 
228  // Load the dll
229  std::string regName = "casadi_register_" + Derived::infix_ + "_" + pname;
230 
231  std::string searchpath;
232  handle_t handle = load_library("casadi_" + Derived::infix_ + "_" + pname, searchpath,
233  false);
234 
235 #ifdef _WIN32
236 
237 #if __GNUC__
238 #pragma GCC diagnostic push
239 #pragma GCC diagnostic ignored "-Wcast-function-type"
240 #endif
241  reg = reinterpret_cast<RegFcn>(GetProcAddress(handle, TEXT(regName.c_str())));
242 #if __GNUC__
243 #pragma GCC diagnostic pop
244 #endif
245 
246 #else // _WIN32
247  // Reset error
248  dlerror();
249 
250  // Load creator
251  reg = reinterpret_cast<RegFcn>(dlsym(handle, regName.c_str()));
252 #endif // _WIN32
253  casadi_assert(reg!=nullptr,
254  "PluginInterface::load_plugin: no \"" + regName + "\" found in " + searchpath + ".");
255 
256  // Create a temporary struct
257  Plugin plugin = pluginFromRegFcn(reg);
258  // Register the plugin
259  if (register_plugin) {
260  registerPlugin(plugin, false);
261  }
262 
263  return plugin;
264 
265 #endif // WITH_DL
266  }
static handle_t load_library(const std::string &libname, std::string &resultpath, bool global)
Load a library dynamically.
static Plugin pluginFromRegFcn(RegFcn regfcn)
Instantiate a Plugin struct from a factory function.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
std::ostream & uerr()
std::ostream & uout()

◆ parse()

XmlNode casadi::TinyXmlInterface::parse ( const std::string &  filename)
overridevirtual

Reimplemented from casadi::XmlFileInternal.

Definition at line 51 of file tinyxml_interface.cpp.

51  {
52  // Load XML file from disk
53  XMLError err = doc_.LoadFile(filename.c_str());
54  casadi_assert(!err, "Cannot load " + filename);
55  // Import to CasADi representation
56  XmlNode n;
57  try {
58  n = import_node(&doc_);
59  } catch (std::exception& e) {
60  casadi_error("Cannot import " + filename + ": " + std::string(e.what()));
61  }
62  return n;
63 }

References doc_, casadi::filename(), and import_node().

◆ plugin_deserialize()

Deserialize casadi::PluginInterface< XmlFileInternal >::plugin_deserialize ( const std::string &  pname)
staticinherited

Definition at line 82 of file plugin_interface.hpp.

162  {
163  Deserialize m = getPlugin(pname).deserialize;
164  casadi_assert(m, "Plugin \"" + pname + "\" does not support deserialize");
165  return m;
166  }

◆ plugin_name()

const char* casadi::TinyXmlInterface::plugin_name ( ) const
inlineoverridevirtual

Implements casadi::PluginInterface< XmlFileInternal >.

Definition at line 66 of file tinyxml_interface.hpp.

66 { return "tinyxml";}

◆ plugin_options()

const Options & casadi::PluginInterface< XmlFileInternal >::plugin_options ( const std::string &  pname)
staticinherited

Definition at line 79 of file plugin_interface.hpp.

155  {
156  const Options *op = getPlugin(pname).options;
157  casadi_assert(op!=nullptr, "Plugin \"" + pname + "\" does not support options");
158  return *op;
159  }

◆ pluginFromRegFcn()

PluginInterface< XmlFileInternal >::Plugin casadi::PluginInterface< XmlFileInternal >::pluginFromRegFcn ( RegFcn  regfcn)
staticinherited

Definition at line 85 of file plugin_interface.hpp.

170  {
171  // Create a temporary struct
172  Plugin plugin;
173 
174  // Set the fields
175  int flag = regfcn(&plugin);
176  casadi_assert(flag==0, "Registration of plugin failed.");
177 
178  return plugin;
179  }

◆ registerPlugin() [1/2]

void casadi::PluginInterface< XmlFileInternal >::registerPlugin ( const Plugin &  plugin,
bool  needs_lock = true 
)
staticinherited

Definition at line 96 of file plugin_interface.hpp.

274  {
275 
276 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
277  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
278 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
279 
280  // Check if the solver name is in use
281  typename std::map<std::string, Plugin>::iterator it=Derived::solvers_.find(plugin.name);
282  casadi_assert(it==Derived::solvers_.end(),
283  "Solver " + str(plugin.name) + " is already in use");
284 
285  // Add to list of solvers
286  Derived::solvers_[plugin.name] = plugin;
287  }

◆ registerPlugin() [2/2]

void casadi::PluginInterface< XmlFileInternal >::registerPlugin ( RegFcn  regfcn,
bool  needs_lock = true 
)
staticinherited

Definition at line 99 of file plugin_interface.hpp.

269  {
270  registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
271  }

◆ serialize_type()

void casadi::PluginInterface< XmlFileInternal >::serialize_type ( SerializingStream s) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rq

Definition at line 114 of file plugin_interface.hpp.

114  {
115  s.pack("PluginInterface::plugin_name", std::string(plugin_name()));
116  }

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 67 of file generic_shared_internal.hpp.

191  {
192  if (weak_ref_==nullptr) {
193  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
194  }
195  return weak_ref_;
196  }

Member Data Documentation

◆ doc_

TiXmlDocument casadi::TinyXmlInterface::doc_

Definition at line 90 of file tinyxml_interface.hpp.

Referenced by dump(), export_node(), and parse().

◆ infix_

const std::string casadi::XmlFileInternal::infix_ = "xmlfile"
staticinherited

Definition at line 70 of file xml_file_internal.hpp.

◆ meta_doc

const std::string casadi::TinyXmlInterface::meta_doc
static
Initial value:
=
"\n"
"\n"
"\n"
"XmlFile using TinyXml\n"
"\n"
"Extra doc: https://github.com/casadi/casadi/wiki/L_22i \n"
"\n"
"\n"

Definition at line 87 of file tinyxml_interface.hpp.

Referenced by casadi::casadi_register_xmlfile_tinyxml().

◆ solvers_

std::map< std::string, XmlFileInternal::Plugin > casadi::XmlFileInternal::solvers_
staticinherited

Definition at line 63 of file xml_file_internal.hpp.


The documentation for this class was generated from the following files: