diff --git a/Makefile.am b/Makefile.am index 1eec885..96863a3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,7 +37,8 @@ nodist_ctemplateinclude_HEADERS = \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h + src/ctemplate/per_expand_data.h \ + src/ctemplate/str_ref.h noinst_HEADERS = \ src/ctemplate/template.h.in \ src/ctemplate/template_cache.h.in \ @@ -50,7 +51,8 @@ noinst_HEADERS = \ src/ctemplate/template_annotator.h.in \ src/ctemplate/template_emitter.h.in \ src/ctemplate/template_namelist.h.in \ - src/ctemplate/per_expand_data.h.in + src/ctemplate/per_expand_data.h.in \ + src/ctemplate/str_ref.h.in docdir = $(prefix)/share/doc/$(PACKAGE)-$(VERSION) ## This is for HTML and other documentation you want to install. diff --git a/Makefile.in b/Makefile.in index 81c0aa0..b4363cd 100644 --- a/Makefile.in +++ b/Makefile.in @@ -66,6 +66,7 @@ DIST_COMMON = README $(am__configure_deps) $(dist_doc_DATA) \ $(top_srcdir)/configure $(top_srcdir)/src/config.h.in \ $(top_srcdir)/src/ctemplate/find_ptr.h.in \ $(top_srcdir)/src/ctemplate/per_expand_data.h.in \ + $(top_srcdir)/src/ctemplate/str_ref.h.in \ $(top_srcdir)/src/ctemplate/template.h.in \ $(top_srcdir)/src/ctemplate/template_annotator.h.in \ $(top_srcdir)/src/ctemplate/template_cache.h.in \ @@ -105,7 +106,7 @@ CONFIG_CLEAN_FILES = src/ctemplate/template_string.h \ src/ctemplate/template_dictionary.h \ src/ctemplate/template_pathops.h \ src/ctemplate/template_namelist.h src/ctemplate/find_ptr.h \ - src/ctemplate/per_expand_data.h \ + src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h \ src/ctemplate/template_dictionary_interface.h CONFIG_CLEAN_VPATH_FILES = am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; @@ -145,21 +146,21 @@ am__libctemplate_la_SOURCES_DIST = src/ctemplate/template.h \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h src/base/arena-inl.h \ - src/base/arena.cc src/base/arena.h src/base/fileutil.h \ - src/base/macros.h src/base/manual_constructor.h \ - src/base/mutex.h src/base/small_map.h \ - src/base/thread_annotations.h src/base/util.h \ - src/indented_writer.h src/per_expand_data.cc src/template.cc \ - src/template_annotator.cc src/template_cache.cc \ - src/template_dictionary.cc src/template_modifiers.cc \ - src/template_modifiers_internal.h src/template_namelist.cc \ - src/template_pathops.cc src/template_string.cc \ - src/htmlparser/htmlparser.cc src/htmlparser/htmlparser.h \ - src/htmlparser/htmlparser_cpp.h src/htmlparser/jsparser.cc \ - src/htmlparser/jsparser.h src/htmlparser/statemachine.cc \ - src/htmlparser/statemachine.h src/windows/port.h \ - src/windows/port.cc + src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h \ + src/base/arena-inl.h src/base/arena.cc src/base/arena.h \ + src/base/fileutil.h src/base/macros.h \ + src/base/manual_constructor.h src/base/mutex.h \ + src/base/small_map.h src/base/thread_annotations.h \ + src/base/util.h src/indented_writer.h src/per_expand_data.cc \ + src/template.cc src/template_annotator.cc \ + src/template_cache.cc src/template_dictionary.cc \ + src/template_modifiers.cc src/template_modifiers_internal.h \ + src/template_namelist.cc src/template_pathops.cc \ + src/template_string.cc src/htmlparser/htmlparser.cc \ + src/htmlparser/htmlparser.h src/htmlparser/htmlparser_cpp.h \ + src/htmlparser/jsparser.cc src/htmlparser/jsparser.h \ + src/htmlparser/statemachine.cc src/htmlparser/statemachine.h \ + src/windows/port.h src/windows/port.cc am__objects_1 = @MINGW_TRUE@am__objects_2 = libctemplate_la-port.lo am_libctemplate_la_OBJECTS = $(am__objects_1) libctemplate_la-arena.lo \ @@ -192,21 +193,21 @@ am__libctemplate_debug_la_SOURCES_DIST = src/ctemplate/template.h \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h src/base/arena-inl.h \ - src/base/arena.cc src/base/arena.h src/base/fileutil.h \ - src/base/macros.h src/base/manual_constructor.h \ - src/base/mutex.h src/base/small_map.h \ - src/base/thread_annotations.h src/base/util.h \ - src/indented_writer.h src/per_expand_data.cc src/template.cc \ - src/template_annotator.cc src/template_cache.cc \ - src/template_dictionary.cc src/template_modifiers.cc \ - src/template_modifiers_internal.h src/template_namelist.cc \ - src/template_pathops.cc src/template_string.cc \ - src/htmlparser/htmlparser.cc src/htmlparser/htmlparser.h \ - src/htmlparser/htmlparser_cpp.h src/htmlparser/jsparser.cc \ - src/htmlparser/jsparser.h src/htmlparser/statemachine.cc \ - src/htmlparser/statemachine.h src/windows/port.h \ - src/windows/port.cc + src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h \ + src/base/arena-inl.h src/base/arena.cc src/base/arena.h \ + src/base/fileutil.h src/base/macros.h \ + src/base/manual_constructor.h src/base/mutex.h \ + src/base/small_map.h src/base/thread_annotations.h \ + src/base/util.h src/indented_writer.h src/per_expand_data.cc \ + src/template.cc src/template_annotator.cc \ + src/template_cache.cc src/template_dictionary.cc \ + src/template_modifiers.cc src/template_modifiers_internal.h \ + src/template_namelist.cc src/template_pathops.cc \ + src/template_string.cc src/htmlparser/htmlparser.cc \ + src/htmlparser/htmlparser.h src/htmlparser/htmlparser_cpp.h \ + src/htmlparser/jsparser.cc src/htmlparser/jsparser.h \ + src/htmlparser/statemachine.cc src/htmlparser/statemachine.h \ + src/windows/port.h src/windows/port.cc @MINGW_TRUE@am__objects_3 = libctemplate_debug_la-port.lo am__objects_4 = $(am__objects_1) libctemplate_debug_la-arena.lo \ libctemplate_debug_la-per_expand_data.lo \ @@ -238,21 +239,21 @@ am__libctemplate_nothreads_la_SOURCES_DIST = src/ctemplate/template.h \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h src/base/arena-inl.h \ - src/base/arena.cc src/base/arena.h src/base/fileutil.h \ - src/base/macros.h src/base/manual_constructor.h \ - src/base/mutex.h src/base/small_map.h \ - src/base/thread_annotations.h src/base/util.h \ - src/indented_writer.h src/per_expand_data.cc src/template.cc \ - src/template_annotator.cc src/template_cache.cc \ - src/template_dictionary.cc src/template_modifiers.cc \ - src/template_modifiers_internal.h src/template_namelist.cc \ - src/template_pathops.cc src/template_string.cc \ - src/htmlparser/htmlparser.cc src/htmlparser/htmlparser.h \ - src/htmlparser/htmlparser_cpp.h src/htmlparser/jsparser.cc \ - src/htmlparser/jsparser.h src/htmlparser/statemachine.cc \ - src/htmlparser/statemachine.h src/windows/port.h \ - src/windows/port.cc + src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h \ + src/base/arena-inl.h src/base/arena.cc src/base/arena.h \ + src/base/fileutil.h src/base/macros.h \ + src/base/manual_constructor.h src/base/mutex.h \ + src/base/small_map.h src/base/thread_annotations.h \ + src/base/util.h src/indented_writer.h src/per_expand_data.cc \ + src/template.cc src/template_annotator.cc \ + src/template_cache.cc src/template_dictionary.cc \ + src/template_modifiers.cc src/template_modifiers_internal.h \ + src/template_namelist.cc src/template_pathops.cc \ + src/template_string.cc src/htmlparser/htmlparser.cc \ + src/htmlparser/htmlparser.h src/htmlparser/htmlparser_cpp.h \ + src/htmlparser/jsparser.cc src/htmlparser/jsparser.h \ + src/htmlparser/statemachine.cc src/htmlparser/statemachine.h \ + src/windows/port.h src/windows/port.cc @MINGW_TRUE@am__objects_5 = libctemplate_nothreads_la-port.lo am__objects_6 = $(am__objects_1) libctemplate_nothreads_la-arena.lo \ libctemplate_nothreads_la-per_expand_data.lo \ @@ -285,21 +286,21 @@ am__libctemplate_nothreads_debug_la_SOURCES_DIST = \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h src/base/arena-inl.h \ - src/base/arena.cc src/base/arena.h src/base/fileutil.h \ - src/base/macros.h src/base/manual_constructor.h \ - src/base/mutex.h src/base/small_map.h \ - src/base/thread_annotations.h src/base/util.h \ - src/indented_writer.h src/per_expand_data.cc src/template.cc \ - src/template_annotator.cc src/template_cache.cc \ - src/template_dictionary.cc src/template_modifiers.cc \ - src/template_modifiers_internal.h src/template_namelist.cc \ - src/template_pathops.cc src/template_string.cc \ - src/htmlparser/htmlparser.cc src/htmlparser/htmlparser.h \ - src/htmlparser/htmlparser_cpp.h src/htmlparser/jsparser.cc \ - src/htmlparser/jsparser.h src/htmlparser/statemachine.cc \ - src/htmlparser/statemachine.h src/windows/port.h \ - src/windows/port.cc + src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h \ + src/base/arena-inl.h src/base/arena.cc src/base/arena.h \ + src/base/fileutil.h src/base/macros.h \ + src/base/manual_constructor.h src/base/mutex.h \ + src/base/small_map.h src/base/thread_annotations.h \ + src/base/util.h src/indented_writer.h src/per_expand_data.cc \ + src/template.cc src/template_annotator.cc \ + src/template_cache.cc src/template_dictionary.cc \ + src/template_modifiers.cc src/template_modifiers_internal.h \ + src/template_namelist.cc src/template_pathops.cc \ + src/template_string.cc src/htmlparser/htmlparser.cc \ + src/htmlparser/htmlparser.h src/htmlparser/htmlparser_cpp.h \ + src/htmlparser/jsparser.cc src/htmlparser/jsparser.h \ + src/htmlparser/statemachine.cc src/htmlparser/statemachine.h \ + src/windows/port.h src/windows/port.cc @MINGW_TRUE@am__objects_7 = libctemplate_nothreads_debug_la-port.lo am__objects_8 = $(am__objects_1) \ libctemplate_nothreads_debug_la-arena.lo \ @@ -837,7 +838,8 @@ nodist_ctemplateinclude_HEADERS = \ src/ctemplate/template_annotator.h \ src/ctemplate/template_emitter.h \ src/ctemplate/template_namelist.h \ - src/ctemplate/per_expand_data.h + src/ctemplate/per_expand_data.h \ + src/ctemplate/str_ref.h noinst_HEADERS = \ src/ctemplate/template.h.in \ @@ -851,7 +853,8 @@ noinst_HEADERS = \ src/ctemplate/template_annotator.h.in \ src/ctemplate/template_emitter.h.in \ src/ctemplate/template_namelist.h.in \ - src/ctemplate/per_expand_data.h.in + src/ctemplate/per_expand_data.h.in \ + src/ctemplate/str_ref.h.in dist_doc_DATA = AUTHORS COPYING ChangeLog INSTALL NEWS README README_windows.txt \ doc/designstyle.css doc/index.html \ @@ -1239,6 +1242,8 @@ src/ctemplate/find_ptr.h: $(top_builddir)/config.status $(top_srcdir)/src/ctempl cd $(top_builddir) && $(SHELL) ./config.status $@ src/ctemplate/per_expand_data.h: $(top_builddir)/config.status $(top_srcdir)/src/ctemplate/per_expand_data.h.in cd $(top_builddir) && $(SHELL) ./config.status $@ +src/ctemplate/str_ref.h: $(top_builddir)/config.status $(top_srcdir)/src/ctemplate/str_ref.h.in + cd $(top_builddir) && $(SHELL) ./config.status $@ src/ctemplate/template_dictionary_interface.h: $(top_builddir)/config.status $(top_srcdir)/src/ctemplate/template_dictionary_interface.h.in cd $(top_builddir) && $(SHELL) ./config.status $@ install-libLTLIBRARIES: $(lib_LTLIBRARIES) diff --git a/configure b/configure index c3a5df2..5ad7371 100755 --- a/configure +++ b/configure @@ -16271,7 +16271,7 @@ _ACEOF # Write generated configuration file, and also .h files -ac_config_files="$ac_config_files Makefile src/ctemplate/template_string.h src/ctemplate/template_enums.h src/ctemplate/template.h src/ctemplate/template_cache.h src/ctemplate/template_modifiers.h src/ctemplate/template_emitter.h src/ctemplate/template_annotator.h src/ctemplate/template_dictionary.h src/ctemplate/template_pathops.h src/ctemplate/template_namelist.h src/ctemplate/find_ptr.h src/ctemplate/per_expand_data.h src/ctemplate/template_dictionary_interface.h" +ac_config_files="$ac_config_files Makefile src/ctemplate/template_string.h src/ctemplate/template_enums.h src/ctemplate/template.h src/ctemplate/template_cache.h src/ctemplate/template_modifiers.h src/ctemplate/template_emitter.h src/ctemplate/template_annotator.h src/ctemplate/template_dictionary.h src/ctemplate/template_pathops.h src/ctemplate/template_namelist.h src/ctemplate/find_ptr.h src/ctemplate/per_expand_data.h src/ctemplate/str_ref.h src/ctemplate/template_dictionary_interface.h" cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure @@ -17373,6 +17373,7 @@ do "src/ctemplate/template_namelist.h") CONFIG_FILES="$CONFIG_FILES src/ctemplate/template_namelist.h" ;; "src/ctemplate/find_ptr.h") CONFIG_FILES="$CONFIG_FILES src/ctemplate/find_ptr.h" ;; "src/ctemplate/per_expand_data.h") CONFIG_FILES="$CONFIG_FILES src/ctemplate/per_expand_data.h" ;; + "src/ctemplate/str_ref.h") CONFIG_FILES="$CONFIG_FILES src/ctemplate/str_ref.h" ;; "src/ctemplate/template_dictionary_interface.h") CONFIG_FILES="$CONFIG_FILES src/ctemplate/template_dictionary_interface.h" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5 ;; diff --git a/configure.ac b/configure.ac index bc35386..f877b8d 100644 --- a/configure.ac +++ b/configure.ac @@ -198,6 +198,7 @@ AC_CONFIG_FILES([Makefile \ src/ctemplate/template_namelist.h \ src/ctemplate/find_ptr.h \ src/ctemplate/per_expand_data.h \ + src/ctemplate/str_ref.h \ src/ctemplate/template_dictionary_interface.h \ ]) AC_OUTPUT diff --git a/src/ctemplate/str_ref.h.in b/src/ctemplate/str_ref.h.in new file mode 100644 index 0000000..1de6e8a --- /dev/null +++ b/src/ctemplate/str_ref.h.in @@ -0,0 +1,123 @@ +// Copyright (c) 2012, Olaf van der Spek +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// --- +// Author: Olaf van der Spek + +#ifndef TEMPLATE_STR_REF_H_ +#define TEMPLATE_STR_REF_H_ + +#include + +@ac_windows_dllexport_defines@ + +@ac_google_start_namespace@ + +template +class str_ref_basic +{ +public: + str_ref_basic() + { + clear(); + } + + template + str_ref_basic(const U& c) + { + assign(&*c.begin(), &*c.end()); + } + + str_ref_basic(const void* b, const void* e) + { + assign(b, e); + } + + str_ref_basic(const void* b, size_t sz) + { + assign(b, sz); + } + + str_ref_basic(const char* b) + { + assign(b, strlen(b)); + } + + void clear() + { + begin_ = end_ = NULL; + } + + void assign(const void* b, const void* e) + { + begin_ = reinterpret_cast(b); + end_ = reinterpret_cast(e); + } + + void assign(const void* b, size_t sz) + { + begin_ = reinterpret_cast(b); + end_ = begin_ + sz; + } + + T begin() const + { + return begin_; + } + + T end() const + { + return end_; + } + + T data() const + { + return begin(); + } + + size_t size() const + { + return end() - begin(); + } + + bool empty() const + { + return begin() == end(); + } +private: + T begin_; + T end_; +}; + +typedef str_ref_basic data_ref; +typedef str_ref_basic str_ref; + +@ac_google_end_namespace@ + +#endif // TEMPLATE_STR_REF_H_ diff --git a/src/ctemplate/template_dictionary.h.in b/src/ctemplate/template_dictionary.h.in index d81b403..8612f82 100644 --- a/src/ctemplate/template_dictionary.h.in +++ b/src/ctemplate/template_dictionary.h.in @@ -55,6 +55,7 @@ #include #include +#include #include #include #include @@ -109,6 +110,30 @@ class @ac_windows_dllexport@ TemplateDictionary : public TemplateDictionaryInter #endif ; // starts at 3 because of implicit 1st arg 'this' + class SetProxy { + public: + SetProxy(TemplateDictionary& dict, const TemplateString& variable) : + dict_(dict), + variable_(variable) { + } + + void operator=(str_ref value) { + dict_.SetValue(variable_, TemplateString(value.data(), value.size())); + } + + void operator=(long value) { + dict_.SetIntValue(variable_, value); + } + + private: + TemplateDictionary& dict_; + const TemplateString& variable_; + }; + + SetProxy operator[](const TemplateString& variable) { + return SetProxy(*this, variable); + } + // We also let you set values in the 'global' dictionary which is // referenced when all other dictionaries fail. Note this is a // static method: no TemplateDictionary instance needed. Since diff --git a/src/tests/template_dictionary_unittest.cc b/src/tests/template_dictionary_unittest.cc index 03fb8c5..07c143d 100644 --- a/src/tests/template_dictionary_unittest.cc +++ b/src/tests/template_dictionary_unittest.cc @@ -134,6 +134,11 @@ TEST(TemplateDictionary, SetValueAndTemplateStringAndArena) { // Test copying char*s, strings, and explicit TemplateStrings dict.SetValue("FOO", "foo"); dict.SetValue(string("FOO2"), TemplateString("foo2andmore", 4)); + dict["FOO3"] = "foo3"; + dict[string("FOO4")] = TemplateString("foo4andmore", 4); + dict["FOO5"] = string("Olaf"); + dict["FOO6"] = 6; + dict["FOO7"] = long(7); TemplateDictionaryPeer peer(&dict); // verify what happened @@ -152,6 +157,11 @@ TEST(TemplateDictionary, SetValueAndTemplateStringAndArena) { "dictionary 'test_arena%d' {\n" " FOO: >foo<\n" " FOO2: >foo2<\n" + " FOO3: >foo3<\n" + " FOO4: >foo4<\n" + " FOO5: >Olaf<\n" + " FOO6: >6<\n" + " FOO7: >7<\n" "}\n"), i); EXPECT_STREQ(dump.c_str(), expected); }