xml_node.hpp
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 #ifndef CASADI_XML_NODE_HPP
27 #define CASADI_XML_NODE_HPP
28 
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include "exception.hpp"
33 #include "casadi_common.hpp"
34 
36 
37 namespace casadi {
38 
39 struct CASADI_EXPORT XmlNode {
40  // All attributes
41  std::map<std::string, std::string> attributes;
42 
43  // All children
44  std::vector<XmlNode> children;
45 
46  // Name of the node
47  std::string name;
48 
49  // Comment
50  std::string comment;
51 
52  // Line number
53  casadi_int line;
54 
55  // Text
56  std::string text;
57 
61  bool has_attribute(const std::string& att_name) const;
62 
66  void set_attribute(const std::string& att_name, const std::string& att);
67 
71  void set_attribute(const std::string& att_name, casadi_int att) {
72  set_attribute(att_name, std::to_string(att));
73  }
74 
78  void set_attribute(const std::string& att_name, double att);
79 
83  void set_attribute(const std::string& att_name, const std::vector<casadi_int>& att);
84 
88  std::vector<std::string> child_names() const;
89 
93  std::vector<std::string> attribute_names() const;
94 
98  template<typename T>
99  T attribute(const std::string& att_name) const {
100  // Find the attribute, if any
101  auto it = this->attributes.find(att_name);
102  casadi_assert(it != this->attributes.end(), "Could not find attribute " + att_name);
103  // Attribute found, read it
104  T ret;
105  read(it->second, &ret);
106  return ret;
107  }
108 
112  template<typename T>
113  T attribute(const std::string& att_name, const T& def_att) const {
114  // Find the attribute, if any
115  auto it = this->attributes.find(att_name);
116  if (it == this->attributes.end()) {
117  // No such attribute, return default value
118  return def_att;
119  } else {
120  // Attribute found, read it
121  T ret;
122  read(it->second, &ret);
123  return ret;
124  }
125  }
126 
130  const XmlNode& operator[](size_t i) const { return this->children.at(i);}
131 
135  XmlNode& operator[](size_t i) { return this->children.at(i);}
136 
140  const XmlNode& operator[](const std::string& childname) const;
141 
145  XmlNode& operator[](const std::string& childname);
146 
150  bool has_child(const std::string& childname) const;
151 
155  size_t size() const { return this->children.size();}
156 
160  template<typename T>
161  void get(T* val) const { read(this->text, val);}
162 
166  static void read(const std::string& str, std::string* val);
167 
171  static void read(const std::string& str, bool* val);
172 
176  static void read(const std::string& str, casadi_int* val);
177 
181  static void read(const std::string& str, size_t* val);
182 
186  static void read(const std::string& str, double* val);
187 
191  static void read(const std::string& str, std::vector<casadi_int>* val);
192 
196  static void read(const std::string& str, std::vector<std::string>* val);
197 
201  CASADI_EXPORT friend std::ostream& operator<<(std::ostream &stream, const XmlNode& node);
202 
206  void dump(std::ostream &stream, casadi_int indent = 0) const;
207 };
208 
209 } // namespace casadi
211 
212 #endif // CASADI_XML_NODE_HPP
The casadi namespace.
Definition: archiver.cpp:28
std::ostream & operator<<(std::ostream &stream, const XmlNode &node)
Definition: xml_node.cpp:79
std::string str(const T &v)
String representation, any type.
T attribute(const std::string &att_name, const T &def_att) const
Get an attribute by its name, default value if not found.
Definition: xml_node.hpp:113
casadi_int line
Definition: xml_node.hpp:53
std::vector< XmlNode > children
Definition: xml_node.hpp:44
void set_attribute(const std::string &att_name, casadi_int att)
Add an integer attribute.
Definition: xml_node.hpp:71
T attribute(const std::string &att_name) const
Get an attribute by its name.
Definition: xml_node.hpp:99
void get(T *val) const
Get value of text field.
Definition: xml_node.hpp:161
std::string comment
Definition: xml_node.hpp:50
std::string text
Definition: xml_node.hpp:56
std::map< std::string, std::string > attributes
Definition: xml_node.hpp:41
XmlNode & operator[](size_t i)
Get a reference to a child by its index.
Definition: xml_node.hpp:135
const XmlNode & operator[](size_t i) const
Get a reference to a child by its index.
Definition: xml_node.hpp:130
size_t size() const
Get the number of children.
Definition: xml_node.hpp:155
std::string name
Definition: xml_node.hpp:47