X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2FLockable.h;h=c10c2bf6451c433938c2f4b9742649a56225cf50;hb=97ef7cf72ced1670dcb077130146b1b8e567ec51;hp=d7e2f260dfa840f4528ef4fde96f742599addffd;hpb=26a08e6ab6631b2a5f91ac84ead591752434a9bc;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/Lockable.h b/xmltooling/Lockable.h index d7e2f26..c10c2bf 100644 --- a/xmltooling/Lockable.h +++ b/xmltooling/Lockable.h @@ -1,81 +1,92 @@ -/* - * Copyright 2001-2006 Internet2 - * - * 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. - */ - -/** - * @file Lockable.h - * - * Locking abstraction - */ - -#ifndef __xmltooling_lockable_h__ -#define __xmltooling_lockable_h__ - -#include - -namespace xmltooling { - - /** - * Abstract mixin interface for interfaces that support locking - */ - struct XMLTOOL_API Lockable - { - virtual ~Lockable() {} - - /** - * Lock the associated object for exclusive access. - * - * @return a reference to the object being locked - */ - virtual Lockable& lock()=0; - - /** - * Unlock the associated object from exclusive access. - */ - virtual void unlock()=0; - }; - - /** - * RAII wrapper for lockable objects to ensure lock release - */ - class XMLTOOL_API Locker - { - MAKE_NONCOPYABLE(Locker); - public: - /** - * Locks an object and stores it for later release. - * - * @param lockee Pointer to an object to lock and hold - */ - Locker(Lockable* lockee) : m_lockee(lockee->lock()) {} - - /** - * Locks an object and stores it for later release. - * - * @param lockee Reference to an object to lock and hold - */ - Locker(Lockable& lockee) : m_lockee(lockee.lock()) {} - - /** - * Releases lock on held pointer, if any. - */ - ~Locker() {m_lockee.unlock();} - private: - Lockable& m_lockee; - }; - -}; - -#endif /* __xmltooling_lockable_h__ */ +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * UCAID licenses this file to you 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. + */ + +/** + * @file xmltooling/Lockable.h + * + * Locking abstraction. + */ + +#ifndef __xmltooling_lockable_h__ +#define __xmltooling_lockable_h__ + +#include + +namespace xmltooling { + + /** + * Abstract mixin interface for interfaces that support locking + */ + class XMLTOOL_API Lockable + { + protected: + Lockable(); + public: + virtual ~Lockable(); + + /** + * Lock the associated object for exclusive access. + * + * @return a pointer to the object being locked + */ + virtual Lockable* lock()=0; + + /** + * Unlock the associated object from exclusive access. + */ + virtual void unlock()=0; + }; + + /** + * RAII wrapper for lockable objects to ensure lock release + */ + class XMLTOOL_API Locker + { + MAKE_NONCOPYABLE(Locker); + public: + /** + * Optionally locks an object and stores it for later release. + * + * @param lockee pointer to an object to hold, and optionally lock + * @param lock true iff object is not yet locked + */ + Locker(Lockable* lockee=nullptr, bool lock=true); + + /** + * Optionally locks an object and stores it for later release. + * If an object is already held, it is unlocked and detached. + * + * @param lockee pointer to an object to hold, and optionally lock + * @param lock true iff object is not yet locked + */ + void assign(Lockable* lockee=nullptr, bool lock=true); + + /** + * Destructor releases lock on held pointer, if any. + */ + ~Locker(); + + private: + Lockable* m_lockee; + }; + +}; + +#endif /* __xmltooling_lockable_h__ */