diff --git a/Core/CppMicroServices/src/util/usFunctor_p.h b/Core/CppMicroServices/src/util/usFunctor_p.h index ba4daeb92e..0fc5a5c78b 100644 --- a/Core/CppMicroServices/src/util/usFunctor_p.h +++ b/Core/CppMicroServices/src/util/usFunctor_p.h @@ -1,144 +1,160 @@ /*============================================================================= Library: CppMicroServices Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef USFUNCTOR_H #define USFUNCTOR_H #include US_BEGIN_NAMESPACE template class FunctorImpl { public: virtual void operator()(Arg) = 0; virtual FunctorImpl* Clone() const = 0; bool operator==(const FunctorImpl& o) const { return typeid(*this) == typeid(o) && IsEqual(o); } virtual ~FunctorImpl() {} + virtual void* target() const = 0; + private: virtual bool IsEqual(const FunctorImpl& o) const = 0; }; template class FunctorHandler : public FunctorImpl { public: FunctorHandler(const Fun& fun) : m_Fun(fun) {} FunctorHandler* Clone() const { return new FunctorHandler(*this); } void operator()(Arg a) { m_Fun(a); } + void* target() const + { + void* result = NULL; + std::memcpy(&result, &m_Fun, sizeof(void*)); + return result; + } + private: bool IsEqual(const FunctorImpl& o) const { return this->m_Fun == static_cast(o).m_Fun; } Fun m_Fun; }; template class MemFunHandler : public FunctorImpl { public: MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn) : m_pObj(pObj), m_pMemFn(pMemFn) {} MemFunHandler* Clone() const { return new MemFunHandler(*this); } void operator()(Arg a) { ((*m_pObj).*m_pMemFn)(a); } + void* target() const + { + void* result = NULL; + std::memcpy(&result, &m_pMemFn, sizeof(void*)); + return result; + } + private: bool IsEqual(const FunctorImpl& o) const { return this->m_pObj == static_cast(o).m_pObj && this->m_pMemFn == static_cast(o).m_pMemFn; } PointerToObj m_pObj; PointerToMemFn m_pMemFn; }; template class Functor { public: Functor() : m_Impl(0) {} template Functor(const Fun& fun) : m_Impl(new FunctorHandler(fun)) {} template Functor(const PtrObj& p, MemFn memFn) : m_Impl(new MemFunHandler(p, memFn)) {} Functor(const Functor& f) : m_Impl(f.m_Impl->Clone()) {} Functor& operator=(const Functor& f) { Impl* tmp = f.m_Impl->Clone(); std::swap(tmp, m_Impl); delete tmp; } bool operator==(const Functor& f) const { return (*m_Impl) == (*f.m_Impl); } ~Functor() { delete m_Impl; } void operator()(Arg a) { (*m_Impl)(a); } void* target() const { - return reinterpret_cast(m_Impl); + return m_Impl->target(); } private: typedef FunctorImpl Impl; Impl* m_Impl; }; US_END_NAMESPACE #endif // USFUNCTOR_H