vsl_basic_xml_element.h
Go to the documentation of this file.
1 // This is core/vsl/vsl_basic_xml_element.h
2 #ifndef vsl_basic_xml_element_h_
3 #define vsl_basic_xml_element_h_
4 //:
5 // \file
6 // \brief creates basic xml nodes and writes them out to the stream
7 // A basic node contains only text content, and optionally has attribute(s).
8 // \author Gamze Tunali (gamze@lems.brown.edu)
9 // \date Dec 21, 2005
10 
11 #include <iomanip>
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 #include <vxl_config.h>
18 
19 template<typename T> std::string toString(const T& t)
20 {
21  std::stringstream strm;
22 
23  strm << std::fixed << t;
24  return strm.str();
25 }
26 
28 {
29  public:
30  //: constructs with a name
31  vsl_basic_xml_element(std::string tag)
32  : tag_(std::move(tag)) {}
33 
34  //: constructs with a name and a list of (attribute,value) pair
35  vsl_basic_xml_element(std::string tag, std::vector<std::pair<std::string, std::string> > attrs)
36  : tag_(std::move(tag)), attrs_(std::move(attrs)) {}
37 
38  //destructor
39  ~vsl_basic_xml_element() = default;
40 
41  //: overloaded methods to add attribute values
42  void add_attribute(std::string attr_name, std::string value);
43  void add_attribute(std::string attr_name, double value);
44  void add_attribute(std::string attr_name, float value) { add_attribute(attr_name, (double)value); }
45  void add_attribute(std::string attr_name, long value);
46  void add_attribute(std::string attr_name, int value) { add_attribute(attr_name, (long)value); }
47  void add_attribute(std::string attr_name, size_t value) { add_attribute(attr_name, (long)value); }
48 #if VXL_ADDRESS_BITS != 32 //defined(_WIN32) && defined(_MSC_VER) && !defined(_WIN64) ) // Cannot have this overloading for MSVC or GCC 32-bit build as it collides with size_t
49  void add_attribute(std::string attr_name, unsigned int value) { add_attribute(attr_name, (long)value); }
50 #endif
51  void add_attribute_list(std::vector<std::pair<std::string, std::string> > attrs);
52 #if 0
53  bool delete_attribute(std::string attr_name);
54 #endif
55  void append_cdata(const std::string& cdata);
56  void append_cdata(double cdata);
57  void append_cdata(int cdata);
58 
59  void x_write(std::ostream& ostr);
60  //: writes the opening tag for this node to the stream
61  void x_write_open(std::ostream& ostr);
62 
63  //: writes the closing tag for this node to the stream
64  void x_write_close(std::ostream& ostr);
65 
66  protected:
67  std::string tag_; //!< the node name
68  std::vector<std::pair<std::string, std::string> > attrs_; //!< the node attributes
69  std::string cdata_; //!< the text() content of this node
70 };
71 
72 #endif // vsl_basic_xml_element_h_
void add_attribute(std::string attr_name, unsigned int value)
void add_attribute(std::string attr_name, std::string value)
overloaded methods to add attribute values.
std::vector< std::pair< std::string, std::string > > attrs_
the node attributes
void x_write_open(std::ostream &ostr)
writes the opening tag for this node to the stream.
void append_cdata(const std::string &cdata)
void add_attribute(std::string attr_name, size_t value)
std::string tag_
the node name
vsl_basic_xml_element(std::string tag)
constructs with a name.
void add_attribute(std::string attr_name, int value)
void x_write(std::ostream &ostr)
std::string cdata_
the text() content of this node
vsl_basic_xml_element(std::string tag, std::vector< std::pair< std::string, std::string > > attrs)
constructs with a name and a list of (attribute,value) pair.
~vsl_basic_xml_element()=default
void x_write_close(std::ostream &ostr)
writes the closing tag for this node to the stream.
void add_attribute(std::string attr_name, float value)
void add_attribute_list(std::vector< std::pair< std::string, std::string > > attrs)
std::string toString(const T &t)