//------------------------------------------------------------------------------
// <copyright file="LqrSMACAutoDestroy.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declaration/implementation of the AutoDestroy template class, which is
//   initialized with a reference of an instance of the template parameter and
//   which calls the "destroy" method when AutoDestroy's destructor is called
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LqrSMACAutoDestroy_h__
#define __LqrSMACAutoDestroy_h__

namespace LiquidRobotics {
namespace SMACFramework {

		/**
		 * This class is provided as a utility to automatically call the destroy() method
		 * of the parameterized object when the instance goes out of scope.
		 * @author mike.cookson
		 * @version 1.0
		 * @updated 03-Sep-2010 5:16:35 PM
		 */
		template<typename T>
		class AutoDestroy
		{
		public:

			/**
			 * Constructor with a reference to the instance for which destroy() will be called
			 * upon destruction
			 *
			 * @param var_    References the instance of type T that will be destroyed when
			 * the AutoDestroy destructor runs
			 */
			AutoDestroy(T& var_)
				: var(var_)
			{
			}

			/**
			 * Class destructor, which calls the destroy member of var
			 */
			~AutoDestroy()
			{
				var->destroy();
			}

		private:

			/**
			 * Saved copy of the constructor parameter, which references the instance of T
			 * that will be destroyed when ~AutoDestroy() runs
			 */
			T& var;
		};

}
}

#endif
