Skip to content
Snippets Groups Projects
Commit 5ba483d9 authored by Steven Murray's avatar Steven Murray
Browse files

Added castor::utils::SmartArrayPtr

parent e23812b7
No related branches found
No related tags found
No related merge requests found
/******************************************************************************
* castor/utils/SmartArrayPtr.hpp
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Steven.Murray@cern.ch
*****************************************************************************/
#ifndef CASTOR_UTILS_SMARTARRAYPTR
#define CASTOR_UTILS_SMARTARRAYPTR
#include "castor/exception/NotAnOwner.hpp"
#include <errno.h>
#include <stdio.h>
namespace castor {
namespace utils {
/**
* A smart pointer that owns a pointer to an array and unlike std::auto_ptr
* will call delete[] instead of calling delete.
*/
template <typename T> class SmartArrayPtr {
public:
/**
* Constructor.
*/
SmartArrayPtr() throw(): m_arrayPtr(NULL) {
}
/**
* Constructor.
*
* @param arrayPtr The pointer to an array that is to be owned by the smart
* pointer.
*/
SmartArrayPtr(T *const arrayPtr) throw(): m_arrayPtr(arrayPtr) {
}
/**
* Takes ownership of the specified pointer to an array. If this smart
* pointer already owns a pointer that is not the same as the one specified
* then it will be deleted using delete[].
*
* @param arrayPtr The pointer to be owned. If no pointer is specified then
* the default value of NULL is used. In this default case the smart pointer
* will not own a pointer after the reset() method returns.
*/
void reset(T *const arrayPtr = NULL) throw() {
// If the new pointer is not the one already owned
if(arrayPtr != m_arrayPtr) {
// If this smart pointer still owns a pointer then call delete[] on it
if(m_arrayPtr != NULL) {
delete[] m_arrayPtr;
}
// Take ownership of the new pointer
m_arrayPtr = arrayPtr;
}
}
/**
* SmartArrayPtr assignment operator.
*
* This function does the following:
* <ul>
* <li> Calls release on the previous owner (obj);
* <li> Resets this smart pointer to the released pointer of the previous
* owner (obj).
* </ul>
*/
SmartArrayPtr &operator=(SmartArrayPtr& obj)
throw(castor::exception::NotAnOwner) {
reset(obj.release());
return *this;
}
/**
* Destructor.
*
* Resets this smart pointer with the default value of NULL.
*/
~SmartArrayPtr() throw() {
reset();
}
/**
* Returns the owned pointer or NULL if this smart pointer does not own one.
*
* @return The owned pointer or NULL if this smart pointer does not own one.
*/
T *get() const throw() {
return m_arrayPtr;
}
/**
* Releases the owned pointer.
*
* @return The released pointer.
*/
T *release() throw(castor::exception::NotAnOwner) {
// If this smart pointer does not own a pointer
if(NULL == m_arrayPtr) {
castor::exception::NotAnOwner ex;
ex.getMessage() << "Smart pointer does not own a pointer";
throw(ex);
}
// Assigning NULL to m_arrayPtr indicates this smart pointer does not own a
// pointer
T *const tmpArrayPtr = m_arrayPtr;
m_arrayPtr = NULL;
return tmpArrayPtr;
}
private:
/**
* The owned pointer. A value of NULL means this smart pointer does not own
* a pointer.
*/
T *m_arrayPtr;
/**
* Private copy-constructor to prevent users from trying to create a new
* copy of an object of this class.
*
* Not implemented so that it cannot be called
*/
SmartArrayPtr(const SmartArrayPtr &obj) throw();
}; // class SmartArrayPtr
} // namespace utils
} // namespace castor
#endif // CASTOR_UTILS_SMARTARRAYPTR
/******************************************************************************
* castor/utils/SmartArrayPtrTest.cpp
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Steven.Murray@cern.ch
*****************************************************************************/
#include "castor/utils/SmartArrayPtr.hpp"
#include <gtest/gtest.h>
#include <list>
#include <stdlib.h>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
namespace unitTests {
class castor_utils_SmartArrayPtrTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(castor_utils_SmartArrayPtrTest, constructor) {
char *ptr = new char[10];
castor::utils::SmartArrayPtr<char> smartPtr(ptr);
ASSERT_EQ(ptr, smartPtr.get());
}
TEST_F(castor_utils_SmartArrayPtrTest, reset) {
char *ptr = new char[10];
castor::utils::SmartArrayPtr<char> smartPtr;
ASSERT_EQ((char *)0, smartPtr.get());
smartPtr.reset(ptr);
ASSERT_EQ(ptr, smartPtr.get());
}
TEST_F(castor_utils_SmartArrayPtrTest, assignment) {
castor::utils::SmartArrayPtr<char> smartPtr1;
castor::utils::SmartArrayPtr<char> smartPtr2;
ASSERT_EQ((char *)0, smartPtr1.get());
ASSERT_EQ((char *)0, smartPtr2.get());
char *ptr = new char[10];
smartPtr1.reset(ptr);
ASSERT_EQ(ptr, smartPtr1.get());
smartPtr2 = smartPtr1;
ASSERT_EQ((char *)0, smartPtr1.get());
ASSERT_EQ(ptr, smartPtr2.get());
}
} // namespace unitTests
......@@ -65,7 +65,7 @@ void castor::utils::SmartFILEPtr::reset(FILE *const file = NULL)
//-----------------------------------------------------------------------------
castor::utils::SmartFILEPtr
&castor::utils::SmartFILEPtr::operator=(SmartFILEPtr& obj)
throw(castor::exception::Exception) {
throw(castor::exception::NotAnOwner) {
reset(obj.release());
return *this;
}
......@@ -88,14 +88,11 @@ FILE *castor::utils::SmartFILEPtr::get() const throw() {
// release
//-----------------------------------------------------------------------------
FILE *castor::utils::SmartFILEPtr::release()
throw(castor::exception::Exception) {
throw(castor::exception::NotAnOwner) {
// If this smart pointer does not own a pointer
if(m_file == NULL) {
castor::exception::Exception ex(EPERM);
ex.getMessage() <<
"Smart pointer does not own a FILE pointer";
castor::exception::NotAnOwner ex;
ex.getMessage() << "Smart pointer does not own a FILE pointer";
throw(ex);
}
......
......@@ -25,7 +25,7 @@
#ifndef CASTOR_UTILS_SMARTFILEPTR
#define CASTOR_UTILS_SMARTFILEPTR
#include "castor/exception/Exception.hpp"
#include "castor/exception/NotAnOwner.hpp"
#include <stdio.h>
......@@ -76,7 +76,7 @@ public:
* </ul>
*/
SmartFILEPtr &operator=(SmartFILEPtr& obj)
throw(castor::exception::Exception) ;
throw(castor::exception::NotAnOwner);
/**
* Destructor.
......@@ -97,8 +97,7 @@ public:
*
* @return The released FILE pointer.
*/
FILE *release() throw(castor::exception::Exception);
FILE *release() throw(castor::exception::NotAnOwner);
private:
......
......@@ -80,7 +80,7 @@ void castor::utils::SmartFd::reset(const int fd = -1) throw() {
// SmartFd assignment operator
//-----------------------------------------------------------------------------
castor::utils::SmartFd &castor::utils::SmartFd::operator=(SmartFd& obj)
throw() {
throw(castor::exception::NotAnOwner) {
reset(obj.release());
return *this;
}
......@@ -102,12 +102,11 @@ int castor::utils::SmartFd::get() const throw() {
//-----------------------------------------------------------------------------
// release
//-----------------------------------------------------------------------------
int castor::utils::SmartFd::release() throw(castor::exception::Exception) {
int castor::utils::SmartFd::release() throw(castor::exception::NotAnOwner) {
// If this SmartFd does not own a file descriptor
if(m_fd < 0) {
castor::exception::Exception ex(EPERM);
ex.getMessage() <<
"Smart file-descriptor does not own a file-descriptor";
castor::exception::NotAnOwner ex;
ex.getMessage() << "Smart file-descriptor does not own a file-descriptor";
throw(ex);
}
......
......@@ -25,8 +25,7 @@
#ifndef CASTOR_UTILS_SMARTFD
#define CASTOR_UTILS_SMARTFD
#include "castor/exception/Exception.hpp"
#include "castor/exception/NotAnOwner.hpp"
namespace castor {
namespace utils {
......@@ -105,7 +104,7 @@ public:
* the previous owner (obj).
* </ul>
*/
SmartFd &operator=(SmartFd& obj) throw();
SmartFd &operator=(SmartFd& obj) throw(castor::exception::NotAnOwner);
/**
* Destructor.
......@@ -127,7 +126,7 @@ public:
*
* @return The released file descriptor.
*/
int release() throw(castor::exception::Exception);
int release() throw(castor::exception::NotAnOwner);
private:
......
......@@ -91,6 +91,7 @@ add_executable(castorUnitTests
../castor/tape/tapeserver/file/StructuresTest.cpp
../castor/tape/tapeserver/file/FileTest.cpp
../castor/tape/tapeserver/threading/ThreadingTests.cpp
../castor/utils/SmartArrayPtrTest.cpp
../castor/utils/SmartFdTest.cpp
../castor/utils/UtilsTest.cpp
${STK_UNITTEST_SRC_FILES})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment