diff --git a/src/ctemplate/find_ptr.h.in b/src/ctemplate/find_ptr.h.in index 095c538..e67e532 100644 --- a/src/ctemplate/find_ptr.h.in +++ b/src/ctemplate/find_ptr.h.in @@ -33,10 +33,19 @@ #ifndef TEMPLATE_FIND_PTR_H_ #define TEMPLATE_FIND_PTR_H_ +#include + @ac_windows_dllexport_defines@ namespace ctemplate { +template +const typename T::value_type* find_ptr0(const T& c, U v) +{ + typename T::const_iterator i = c.find(v); + return i == c.end() ? NULL : &*i; +} + template typename T::value_type::second_type* find_ptr(T& c, U v) { diff --git a/src/per_expand_data.cc b/src/per_expand_data.cc index a38f65f..2c81c3f 100644 --- a/src/per_expand_data.cc +++ b/src/per_expand_data.cc @@ -34,6 +34,7 @@ // introduce new include dependencies. #include +#include #include #include @@ -71,10 +72,7 @@ void PerExpandData::InsertForModifiers(const char* key, const void* value) { // Retrieve data specific to this Expand call. Returns NULL if key // is not found. This should only be used by template modifiers. const void* PerExpandData::LookupForModifiers(const char* key) const { - if (!map_) - return NULL; - const DataMap::const_iterator it = map_->find(key); - return it == map_->end() ? NULL : it->second; + return map_ ? find_ptr2(*map_, key) : NULL; } _END_GOOGLE_NAMESPACE_ diff --git a/src/template_string.cc b/src/template_string.cc index ecced46..bb4e751 100644 --- a/src/template_string.cc +++ b/src/template_string.cc @@ -33,6 +33,7 @@ #include #include "base/mutex.h" // This has to come first to get _XOPEN_SOURCE +#include #include #include HASH_SET_H #include "base/arena.h" @@ -173,9 +174,9 @@ void TemplateString::AddToGlobalIdToNameMap() LOCKS_EXCLUDED(mutex) { // Check to see if it's already here. ReaderMutexLock reader_lock(&mutex); if (template_string_set) { - TemplateStringSet::const_iterator iter = - template_string_set->find(*this); - if (iter != template_string_set->end()) { + const TemplateString* iter = + find_ptr0(*template_string_set, *this); + if (iter) { DCHECK_EQ(TemplateString(ptr_, length_), TemplateString(iter->ptr_, iter->length_)) << "TemplateId collision!"; @@ -193,7 +194,7 @@ void TemplateString::AddToGlobalIdToNameMap() LOCKS_EXCLUDED(mutex) { arena = new UnsafeArena(1024); // 1024 was picked out of a hat. } - if (template_string_set->find(*this) != template_string_set->end()) { + if (template_string_set->count(*this)) { return; } // If we are immutable, we can store ourselves directly in the map. @@ -225,15 +226,10 @@ TemplateString TemplateString::IdToString(TemplateId id) LOCKS_EXCLUDED(mutex) { // TemplateString. This may seem weird, but it lets us use a // hash_set instead of a hash_map. TemplateString id_as_template_string(NULL, 0, false, id); - TemplateStringSet::const_iterator iter = - template_string_set->find(id_as_template_string); - if (iter == template_string_set->end()) { - return TemplateString(kStsEmpty); - } - return *iter; + const TemplateString* iter = find_ptr0(*template_string_set, id_as_template_string); + return iter ? *iter : TemplateString(kStsEmpty); } - StaticTemplateStringInitializer::StaticTemplateStringInitializer( const StaticTemplateString* sts) { // Compute the sts's id if it wasn't specified at static-init