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, double* val);
182 
186  static void read(const std::string& str, std::vector<casadi_int>* val);
187 
191  static void read(const std::string& str, std::vector<std::string>* val);
192 
196  CASADI_EXPORT friend std::ostream& operator<<(std::ostream &stream, const XmlNode& node);
197 
201  void dump(std::ostream &stream, casadi_int indent = 0) const;
202 };
203 
204 } // namespace casadi
206 
207 #endif // CASADI_XML_NODE_HPP
The casadi namespace.