From bd1481656adcd8d79bc0d23672df1599ca8e9e50 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Thu, 7 Aug 2014 05:55:31 +0900 Subject: improve mustache.h --- include/logging.h | 1 - include/mustache.h | 37 +++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/logging.h b/include/logging.h index b43a969..2984cce 100644 --- a/include/logging.h +++ b/include/logging.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include diff --git a/include/mustache.h b/include/mustache.h index 7218ae8..fb4d664 100644 --- a/include/mustache.h +++ b/include/mustache.h @@ -505,18 +505,22 @@ namespace crow std::string body_; }; - template_t compile(const std::string& body) + inline template_t compile(const std::string& body) { return template_t(body); } namespace detail { - std::string template_base_directory = "templates"; + inline std::string& get_template_base_directory_ref() + { + static std::string template_base_directory = "templates"; + return template_base_directory; + } } - std::string default_loader(const std::string& filename) + inline std::string default_loader(const std::string& filename) { - std::ifstream inf(detail::template_base_directory + filename); + std::ifstream inf(detail::get_template_base_directory_ref() + filename); if (!inf) return {}; return {std::istreambuf_iterator(inf), std::istreambuf_iterator()}; @@ -524,27 +528,32 @@ namespace crow namespace detail { - std::function loader = default_loader; + inline std::function get_loader_ref() + { + static std::function loader = default_loader; + return loader; + } } - void set_base(const std::string& path) + inline void set_base(const std::string& path) { - detail::template_base_directory = path; - if (detail::template_base_directory.back() != '\\' && - detail::template_base_directory.back() != '/') + auto& base = detail::get_template_base_directory_ref(); + base = path; + if (base.back() != '\\' && + base.back() != '/') { - detail::template_base_directory += '/'; + base += '/'; } } - void set_loader(std::function loader) + inline void set_loader(std::function loader) { - detail::loader = std::move(loader); + detail::get_loader_ref() = std::move(loader); } - template_t load(const std::string& filename) + inline template_t load(const std::string& filename) { - return compile(detail::loader(filename)); + return compile(detail::get_loader_ref()(filename)); } } } -- cgit v1.2.3-54-g00ecf