1
0
mirror of https://github.com/OlafvdSpek/ctemplate.git synced 2025-10-05 19:16:54 +08:00

Use find_ptr

This commit is contained in:
olafvdspek 2015-02-15 00:20:22 +00:00
parent fdf31135e3
commit 3f45c961e9
3 changed files with 18 additions and 15 deletions

View File

@ -33,10 +33,19 @@
#ifndef TEMPLATE_FIND_PTR_H_ #ifndef TEMPLATE_FIND_PTR_H_
#define TEMPLATE_FIND_PTR_H_ #define TEMPLATE_FIND_PTR_H_
#include <cstddef>
@ac_windows_dllexport_defines@ @ac_windows_dllexport_defines@
namespace ctemplate { namespace ctemplate {
template <class T, class U>
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 <class T, class U> template <class T, class U>
typename T::value_type::second_type* find_ptr(T& c, U v) typename T::value_type::second_type* find_ptr(T& c, U v)
{ {

View File

@ -34,6 +34,7 @@
// introduce new include dependencies. // introduce new include dependencies.
#include <config.h> #include <config.h>
#include <ctemplate/find_ptr.h>
#include <ctemplate/per_expand_data.h> #include <ctemplate/per_expand_data.h>
#include <ctemplate/template_annotator.h> #include <ctemplate/template_annotator.h>
@ -71,10 +72,7 @@ void PerExpandData::InsertForModifiers(const char* key, const void* value) {
// Retrieve data specific to this Expand call. Returns NULL if key // Retrieve data specific to this Expand call. Returns NULL if key
// is not found. This should only be used by template modifiers. // is not found. This should only be used by template modifiers.
const void* PerExpandData::LookupForModifiers(const char* key) const { const void* PerExpandData::LookupForModifiers(const char* key) const {
if (!map_) return map_ ? find_ptr2(*map_, key) : NULL;
return NULL;
const DataMap::const_iterator it = map_->find(key);
return it == map_->end() ? NULL : it->second;
} }
_END_GOOGLE_NAMESPACE_ _END_GOOGLE_NAMESPACE_

View File

@ -33,6 +33,7 @@
#include <config.h> #include <config.h>
#include "base/mutex.h" // This has to come first to get _XOPEN_SOURCE #include "base/mutex.h" // This has to come first to get _XOPEN_SOURCE
#include <ctemplate/find_ptr.h>
#include <ctemplate/template_string.h> #include <ctemplate/template_string.h>
#include HASH_SET_H #include HASH_SET_H
#include "base/arena.h" #include "base/arena.h"
@ -173,9 +174,9 @@ void TemplateString::AddToGlobalIdToNameMap() LOCKS_EXCLUDED(mutex) {
// Check to see if it's already here. // Check to see if it's already here.
ReaderMutexLock reader_lock(&mutex); ReaderMutexLock reader_lock(&mutex);
if (template_string_set) { if (template_string_set) {
TemplateStringSet::const_iterator iter = const TemplateString* iter =
template_string_set->find(*this); find_ptr0(*template_string_set, *this);
if (iter != template_string_set->end()) { if (iter) {
DCHECK_EQ(TemplateString(ptr_, length_), DCHECK_EQ(TemplateString(ptr_, length_),
TemplateString(iter->ptr_, iter->length_)) TemplateString(iter->ptr_, iter->length_))
<< "TemplateId collision!"; << "TemplateId collision!";
@ -193,7 +194,7 @@ void TemplateString::AddToGlobalIdToNameMap() LOCKS_EXCLUDED(mutex) {
arena = new UnsafeArena(1024); // 1024 was picked out of a hat. 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; return;
} }
// If we are immutable, we can store ourselves directly in the map. // 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 // TemplateString. This may seem weird, but it lets us use a
// hash_set instead of a hash_map. // hash_set instead of a hash_map.
TemplateString id_as_template_string(NULL, 0, false, id); TemplateString id_as_template_string(NULL, 0, false, id);
TemplateStringSet::const_iterator iter = const TemplateString* iter = find_ptr0(*template_string_set, id_as_template_string);
template_string_set->find(id_as_template_string); return iter ? *iter : TemplateString(kStsEmpty);
if (iter == template_string_set->end()) {
return TemplateString(kStsEmpty);
}
return *iter;
} }
StaticTemplateStringInitializer::StaticTemplateStringInitializer( StaticTemplateStringInitializer::StaticTemplateStringInitializer(
const StaticTemplateString* sts) { const StaticTemplateString* sts) {
// Compute the sts's id if it wasn't specified at static-init // Compute the sts's id if it wasn't specified at static-init