hemplate

Simple XML template engine
git clone git://git.dimitrijedobrota.com/hemplate.git
Log | Files | Refs | README | LICENSE

element.cpp (1971B)


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