libdap++  Updated for version 3.13.1
D4Group.cc
Go to the documentation of this file.
1 /*
2  * D4Group.cc
3  *
4  * Created on: Sep 27, 2012
5  * Author: jimg
6  */
7 
8 #include "D4Group.h"
9 
10 #include "BaseType.h"
11 
12 namespace libdap {
13 
20 D4Group::D4Group(const string &n) : Constructor(n, dods_group_c, /*is_dap4*/true)
21 {}
22 
30 D4Group::D4Group(const string &n, const string &d)
31  : Constructor(n, d, dods_group_c, /*is_dap4*/true)
32 {}
33 
36 {
37  m_duplicate(rhs);
38 }
39 
40 static void enum_del(D4EnumDef *ed)
41 {
42  delete ed;
43 }
44 
46 {
47  for_each(d_enums.begin(), d_enums.end(), enum_del);
48 }
49 
50 BaseType *
52 {
53  return new D4Group(*this);
54 }
55 
56 D4Group &
58 {
59  if (this == &rhs)
60  return *this;
61 
62  dynamic_cast<Constructor &>(*this) = rhs; // run Constructor=
63 
64  m_duplicate(rhs);
65 
66  return *this;
67 }
68 
69 void
71 {
72  d_enums.push_back(enum_def);
73 }
74 
75 class PrintVariable : public unary_function<BaseType *, void>
76 {
77  XMLWriter &d_xml;
78  bool d_constrained;
79 public:
80  PrintVariable(XMLWriter &x, bool c) : d_xml(x), d_constrained(c) {}
81 
82  void operator()(BaseType *btp)
83  {
84  btp->print_xml_writer(d_xml, d_constrained);
85  }
86 };
87 
88 class PrintEnum : public unary_function<D4EnumDef *, void>
89 {
90  XMLWriter &d_xml;
91 
92 public:
93  PrintEnum(XMLWriter &x) : d_xml(x){}
94 
95  void operator()(D4EnumDef *e)
96  {
97  e->print_xml_writer(d_xml);
98  }
99 };
100 
101 void
102 D4Group::print_xml_writer(XMLWriter &xml, bool constrained)
103 {
104  if (constrained && !send_p())
105  return;
106 
107  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)type_name().c_str()) < 0)
108  throw InternalErr(__FILE__, __LINE__, "Could not write " + type_name() + " element");
109 
110  if (!name().empty())
111  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)name().c_str()) < 0)
112  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
113 
114  // If the Group has Enumeration definitions
115  if (d_enums.size() > 0)
116  for_each(d_enums.begin(), d_enums.end(), PrintEnum(xml));
117 
118  // If it has attributes
119  if (get_attr_table().get_size() > 0)
121 
122  // If it has variables
123  if (var_begin() != var_end())
124  for_each(var_begin(), var_end(), PrintVariable(xml, constrained));
125 
126  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
127  throw InternalErr(__FILE__, __LINE__, "Could not end " + type_name() + " element");
128 }
129 
130 } /* namespace libdap */
void add_enumeration_nocopy(D4EnumDef *enum_def)
Definition: D4Group.cc:70
xmlTextWriterPtr get_writer()
Definition: XMLWriter.h:60
void m_duplicate(const Constructor &s)
Definition: Constructor.cc:59
void print_xml_writer(XMLWriter &xml, bool constrained)
Definition: D4Group.cc:102
void print_xml_writer(XMLWriter &xml)
Definition: AttrTable.cc:1415
A class for software fault reporting.
Definition: InternalErr.h:64
string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:296
virtual BaseType * ptr_duplicate()
Definition: D4Group.cc:51
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:254
virtual AttrTable & get_attr_table()
Definition: BaseType.cc:666
D4Group & operator=(const D4Group &rhs)
Definition: D4Group.cc:57
virtual ~D4Group()
Definition: D4Group.cc:45
The basic data type for the DODS DAP types.
Definition: BaseType.h:199
Vars_iter var_begin()
Definition: Constructor.cc:270
Vars_iter var_end()
Definition: Constructor.cc:278
D4Group(const string &n)
Definition: D4Group.cc:20
virtual bool send_p()
Should this variable be sent?
Definition: BaseType.cc:638
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:235