From 659df12389d2c51fa2ee569d61d6033fbf8a09e0 Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 6 Apr 2012 13:50:24 -0700 Subject: Small improvements on the SimpleMustache processor, in order to be able to make it const, and to construct template out of constant strings. --- includes/SimpleMustache.h | 14 ++++++++------ src/HttpServer.cc | 2 ++ src/SimpleMustache.cc | 12 ++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/includes/SimpleMustache.h b/includes/SimpleMustache.h index 2bb0cb1..6156923 100644 --- a/includes/SimpleMustache.h +++ b/includes/SimpleMustache.h @@ -80,11 +80,13 @@ class SimpleMustache { IO b(new Buffer(str, s)); setTemplate(b); } - void setTemplate(const char * str, ssize_t s = -1) { setTemplate((const uint8_t *) str, s); } + template + void setTemplate(const char str[S]) { setTemplate((const uint8_t *) str, S); } + void setTemplate(const char * str, ssize_t s) { setTemplate((const uint8_t *) str, s); } void setTemplate(const String & str) { setTemplate((const uint8_t *) str.to_charp(), str.strlen()); } - void render(IO h, Context * ctx) { AAssert(ctx, "Please pass on a context to render"); render_r(h, ctx, "", m_fragments.begin(), false, -1); } + void render(IO h, Context * ctx) const { AAssert(ctx, "Please pass on a context to render"); render_r(h, ctx, "", m_fragments.begin(), false, -1); } void empty() { while (!m_fragments.empty()) { delete m_fragments.front(); m_fragments.pop_front(); } } - void checkTemplate() { Fragments::iterator end = checkTemplate_r(m_fragments.begin()); AAssert(end == m_fragments.end(), "The template wasn't fully checked; possibly mismatched sections"); } + void checkTemplate() { Fragments::const_iterator end = checkTemplate_r(m_fragments.begin()); AAssert(end == m_fragments.end(), "The template wasn't fully checked; possibly mismatched sections"); } ~SimpleMustache() { empty(); } private: struct Fragment { @@ -102,10 +104,10 @@ class SimpleMustache { typedef std::list Fragments; Fragments m_fragments; - Fragments::iterator render_r(IO h, Context * ctx, const String & endSection, Fragments::iterator begin, bool noWrite, int forceIdx); - String escape(const String & s); + Fragments::const_iterator render_r(IO h, Context * ctx, const String & endSection, Fragments::const_iterator begin, bool noWrite, int forceIdx) const; + static String escape(const String & s); - Fragments::iterator checkTemplate_r(Fragments::iterator begin, const String & endSection = ""); + Fragments::const_iterator checkTemplate_r(Fragments::const_iterator begin, const String & endSection = "") const; }; }; diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 2e3f703..69d58cd 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -35,6 +35,8 @@ class HttpWorker : public Task { HttpWorker(IO io, void * server); ~HttpWorker(); static void buildErrorTemplate(IO h) { m_errorTemplate.setTemplate(h); } + template + static void buildErrorTemplate(const char str[S]) { m_errorTemplate.setTemplate(str, S); } static void buildErrorTemplate(const char * str, ssize_t s) { m_errorTemplate.setTemplate(str, s); } static void buildErrorTemplate(const String & str) { m_errorTemplate.setTemplate(str); } private: diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index 92fefeb..d2db99f 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -328,9 +328,9 @@ void Balau::SimpleMustache::setTemplate(IO _h) { m_fragments.push_back(curFragment); } -Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::checkTemplate_r(Fragments::iterator begin, const String & endSection) { - Fragments::iterator cur; - Fragments::iterator end = m_fragments.end(); +Balau::SimpleMustache::Fragments::const_iterator Balau::SimpleMustache::checkTemplate_r(Fragments::const_iterator begin, const String & endSection) const { + Fragments::const_iterator cur; + Fragments::const_iterator end = m_fragments.end(); for (cur = begin; cur != end; cur++) { Fragment * fr = *cur; @@ -346,9 +346,9 @@ Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::checkTemplate_ return end; } -Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::render_r(IO h, Context * ctx, const String & endSection, Fragments::iterator begin, bool noWrite, int forceIdx) { - Fragments::iterator cur; - Fragments::iterator end = m_fragments.end(); +Balau::SimpleMustache::Fragments::const_iterator Balau::SimpleMustache::render_r(IO h, Context * ctx, const String & endSection, Fragments::const_iterator begin, bool noWrite, int forceIdx) const { + Fragments::const_iterator cur; + Fragments::const_iterator end = m_fragments.end(); if (endSection.strlen() != 0) { int depth = 0; -- cgit v1.2.3