using shared_ptr in local storage

This commit is contained in:
Marcin Junczys-Dowmunt 2015-01-09 12:12:40 +01:00
parent 7d5cf002ee
commit f4bcd56dc4
3 changed files with 10 additions and 14 deletions

View File

@ -14,8 +14,10 @@ namespace Moses
template <class Value>
struct DefaultFactory {
Value* operator()() {
return new Value();
typedef boost::shared_ptr<Value> ValuePtr;
ValuePtr operator()() {
return ValuePtr(new Value());
}
};
@ -24,22 +26,15 @@ class ThreadLocalByFeatureStorage
{
public:
typedef std::map<std::string, Value*> NameValueMap;
typedef boost::shared_ptr<Value> ValuePtr;
typedef std::map<std::string, ValuePtr> NameValueMap;
typedef boost::thread_specific_ptr<NameValueMap> TSNameValueMap;
ThreadLocalByFeatureStorage(FeatureFunction* ff,
Factory factory = Factory())
: m_ff(ff), m_factory(factory) {}
~ThreadLocalByFeatureStorage() {
if(m_nameMap.get()) {
for(typename NameValueMap::iterator it = m_nameMap->begin();
it != m_nameMap->end(); it++)
delete it->second;
}
}
virtual Value* GetStored() {
virtual ValuePtr GetStored() {
if(!m_nameMap.get())
m_nameMap.reset(new NameValueMap());
@ -58,7 +53,7 @@ class ThreadLocalByFeatureStorage
}
}
virtual const Value* GetStored() const {
virtual const ValuePtr GetStored() const {
UTIL_THROW_IF2(!m_nameMap.get(),
"No thread local storage has been created for: "
<< m_ff->GetScoreProducerDescription());

View File

@ -55,7 +55,6 @@ class VWFeatureSourceExternalFeatures : public VWFeatureSource
}
private:
typedef std::vector<std::string> Features;
typedef ThreadLocalByFeatureStorage<Features> TLSFeatures;

View File

@ -28,6 +28,8 @@ public:
{
StaticData::Instance().InitializeForInput(*m_source);
std::cerr << *m_source << std::endl;
TranslationOptionCollection *transOptColl = m_source->CreateTranslationOptionCollection();
transOptColl->CreateTranslationOptions();
delete transOptColl;