element.cpp (1829B)
1 #include "hemplate/element.hpp" 2 3 namespace hemplate { 4 5 element& element::add(const element& elem) 6 { 7 m_embeded.add(elem); 8 return *this; 9 } 10 11 element& element::add(std::unique_ptr<element> elem) 12 { 13 m_embeded.add(std::move(elem)); 14 return *this; 15 } 16 17 element& element::set(const std::string& name) 18 { 19 m_attributes.set(name); 20 return *this; 21 } 22 23 element& element::set(const std::string& name, const std::string& value) 24 { 25 m_attributes.set(name, value); 26 return *this; 27 } 28 29 void element::render(std::ostream& out) const 30 { 31 const auto open_tag = [this, &out](bool atomic) 32 { 33 out << '<' << get_name(); 34 if (!m_attributes.empty()) out << ' ', m_attributes.render(out); 35 out << (atomic ? " />" : ">"); 36 }; 37 38 const auto close_tag = [this, &out]() { out << "</" << get_name() << '>'; }; 39 40 if (m_type == Type::Atomic) 41 { 42 open_tag(true); 43 return; 44 } 45 46 if (!m_data.empty()) 47 { 48 open_tag(false); 49 if (!m_embeded.empty()) m_embeded.render(out); 50 else out << m_data; 51 close_tag(); 52 return; 53 } 54 55 if (m_embeded.empty()) 56 { 57 tgl_state(); 58 get_state() ? open_tag(false) : close_tag(); 59 } 60 else 61 { 62 open_tag(false); 63 m_embeded.render(out); 64 close_tag(); 65 } 66 } 67 68 elementList::elementList(const elementList& rhs) 69 { 70 this->operator=(rhs); 71 } 72 73 elementList& elementList::operator=(const elementList& rhs) 74 { 75 if (this == &rhs) return *this; 76 77 m_elems.clear(); 78 for (const auto& elem : rhs.m_elems) add(*elem); 79 80 return *this; 81 } 82 83 elementList& elementList::add(const element& elem) 84 { 85 m_elems.push_back(elem.clone()); 86 return *this; 87 } 88 89 elementList& elementList::add(std::unique_ptr<element> elem) 90 { 91 m_elems.push_back(std::move(elem)); 92 return *this; 93 } 94 95 void elementList::render(std::ostream& out) const 96 { 97 for (const auto& elem : m_elems) elem->render(out); 98 } 99 100 } // namespace hemplate