diff options
author | Hieu Hoang <hieuhoang@gmail.com> | 2015-05-10 12:19:26 +0300 |
---|---|---|
committer | Hieu Hoang <hieuhoang@gmail.com> | 2015-05-10 12:19:26 +0300 |
commit | 4f02f19ab43f8f566bc621943a2be8fa95edf45c (patch) | |
tree | e09a98a8d2c7c6c503b538dbddadf2ee2f2bfbd2 /moses/ContextScope.h | |
parent | 91e699f90d677e7a36cf7fd9a4504b9738ad37bb (diff) |
beautify
Diffstat (limited to 'moses/ContextScope.h')
-rw-r--r-- | moses/ContextScope.h | 89 |
1 files changed, 42 insertions, 47 deletions
diff --git a/moses/ContextScope.h b/moses/ContextScope.h index cd12d6d32..e1173cbdc 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -20,20 +20,20 @@ namespace Moses { class ContextScope { - protected: - typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t; - typedef scratchpad_t::iterator iter_t; - typedef scratchpad_t::value_type entry_t; - typedef scratchpad_t::const_iterator const_iter_t; - scratchpad_t m_scratchpad; +protected: + typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t; + typedef scratchpad_t::iterator iter_t; + typedef scratchpad_t::value_type entry_t; + typedef scratchpad_t::const_iterator const_iter_t; + scratchpad_t m_scratchpad; #ifdef WITH_THREADS - mutable boost::shared_mutex m_lock; + mutable boost::shared_mutex m_lock; #endif - public: - // class write_access - // { - // boost::unique_lock<boost::shared_mutex> m_lock; - // public: +public: + // class write_access + // { + // boost::unique_lock<boost::shared_mutex> m_lock; + // public: // write_access(boost::shared_mutex& lock) // : m_lock(lock) @@ -50,53 +50,48 @@ class ContextScope // return write_access(m_lock); // } - template<typename T> - boost::shared_ptr<void> const& - set(void const* const key, boost::shared_ptr<T> const& val) - { + template<typename T> + boost::shared_ptr<void> const& + set(void const* const key, boost::shared_ptr<T> const& val) { #ifdef WITH_THREADS - boost::unique_lock<boost::shared_mutex> lock(m_lock); + boost::unique_lock<boost::shared_mutex> lock(m_lock); #endif - return (m_scratchpad[key] = val); - } + return (m_scratchpad[key] = val); + } - template<typename T> - boost::shared_ptr<T> const - get(void const* key, bool CreateNewIfNecessary=false) - { -#ifdef WITH_THREADS - using boost::shared_mutex; - using boost::upgrade_lock; - // T const* key = reinterpret_cast<T const*>(xkey); - upgrade_lock<shared_mutex> lock(m_lock); - #endif - iter_t m = m_scratchpad.find(key); - boost::shared_ptr< T > ret; - if (m != m_scratchpad.end()) - { - if (m->second == NULL && CreateNewIfNecessary) - { + template<typename T> + boost::shared_ptr<T> const + get(void const* key, bool CreateNewIfNecessary=false) { #ifdef WITH_THREADS - boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); + using boost::shared_mutex; + using boost::upgrade_lock; + // T const* key = reinterpret_cast<T const*>(xkey); + upgrade_lock<shared_mutex> lock(m_lock); #endif - m->second.reset(new T); - } - ret = boost::static_pointer_cast< T >(m->second); - return ret; - } - if (!CreateNewIfNecessary) return ret; + iter_t m = m_scratchpad.find(key); + boost::shared_ptr< T > ret; + if (m != m_scratchpad.end()) { + if (m->second == NULL && CreateNewIfNecessary) { #ifdef WITH_THREADS - boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); #endif - ret.reset(new T); - m_scratchpad[key] = ret; + m->second.reset(new T); + } + ret = boost::static_pointer_cast< T >(m->second); return ret; } + if (!CreateNewIfNecessary) return ret; +#ifdef WITH_THREADS + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); +#endif + ret.reset(new T); + m_scratchpad[key] = ret; + return ret; + } ContextScope() { } - ContextScope(ContextScope const& other) - { + ContextScope(ContextScope const& other) { #ifdef WITH_THREADS boost::unique_lock<boost::shared_mutex> lock1(this->m_lock); boost::unique_lock<boost::shared_mutex> lock2(other.m_lock); |