00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2018, University of Luxembourg 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of University of Luxembourg nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 * Author: Maciej Zurad 00036 *********************************************************************/ 00046 #ifndef ROBOT_ACTIVITY_RESOURCE_MANAGED_RESOURCE_H 00047 #define ROBOT_ACTIVITY_RESOURCE_MANAGED_RESOURCE_H 00048 00049 #include <atomic> 00050 00051 #include <ros/ros.h> 00052 #include <ros/console.h> 00053 00054 namespace robot_activity 00055 { 00056 namespace resource 00057 { 00058 00059 class ManagedSubscriber; 00060 class ManagedServiceServer; 00061 00076 template <class Derived, class Resource> 00077 class Managed 00078 { 00079 public: 00084 Managed() = delete; 00085 00089 ~Managed(); 00090 00106 template<typename... Args> 00107 explicit Managed(Args&& ...args) 00108 : acquired_(false), paused_(true), resource_(), lazy_acquirer_() 00109 { 00110 ROS_DEBUG("Managed::ctor"); 00111 lazy_acquirer_ = makeLazyAcquirer(std::forward<Args>(args)...); 00112 } 00113 00119 void acquire(const ros::NodeHandlePtr& node_handle); 00120 00125 void release(); 00126 00132 void pause(); 00133 00138 void resume(); 00139 00140 typedef std::shared_ptr<Managed<Derived, Resource>> SharedPtr; 00141 00142 protected: 00143 typedef std::function<Resource(const ros::NodeHandlePtr&)> LazyAcquirer; 00144 00153 template<typename... Args> 00154 LazyAcquirer makeLazyAcquirer(Args&& ...args) const 00155 { 00156 return static_cast<const Derived*>(this) 00157 ->makeLazyAcquirer(std::forward<Args>(args)...); 00158 } 00159 00163 std::atomic<bool> acquired_; 00164 00168 std::atomic<bool> paused_; 00169 00170 00174 Resource resource_; 00175 00179 LazyAcquirer lazy_acquirer_; 00180 }; 00181 00182 } // namespace resource 00183 } // namespace robot_activity 00184 00185 #endif // ROBOT_ACTIVITY_RESOURCE_MANAGED_RESOURCE_H