|
From: <gb...@us...> - 2008-07-29 09:29:21
|
Revision: 299
http://gearbox.svn.sourceforge.net/gearbox/?rev=299&view=rev
Author: gbiggs
Date: 2008-07-29 09:29:25 +0000 (Tue, 29 Jul 2008)
Log Message:
-----------
Removed unnecessary NULL checks (yay, std::bad_alloc!)
Modified Paths:
--------------
gearbox/trunk/src/flexiport/flexiport.cpp
gearbox/trunk/src/flexiport/logfile.cpp
gearbox/trunk/src/flexiport/logreaderport.cpp
gearbox/trunk/src/flexiport/logwriterport.cpp
gearbox/trunk/src/flexiport/port.cpp
gearbox/trunk/src/hokuyo_aist/hokuyo_aist.cpp
Modified: gearbox/trunk/src/flexiport/flexiport.cpp
===================================================================
--- gearbox/trunk/src/flexiport/flexiport.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/flexiport/flexiport.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -4,17 +4,17 @@
* Copyright (c) 2008 Geoffrey Biggs
*
* flexiport flexible hardware data communications library.
- *
- * This distribution is licensed to you under the terms described in the LICENSE file included in
+ *
+ * This distribution is licensed to you under the terms described in the LICENSE file included in
* this distribution.
*
* This work is a product of the National Institute of Advanced Industrial Science and Technology,
* Japan. Registration number: H20PRO-881
- *
+ *
* This file is part of flexiport.
*
* flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU
- * Lesser General Public License as published by the Free Software Foundation, either version 3 of
+ * Lesser General Public License as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
@@ -103,49 +103,28 @@
#ifdef FLEXIPORT_INCLUDE_SERIAL
if (type == "serial")
- {
- SerialPort *newPort = new SerialPort (options);
- if (newPort == NULL)
- throw PortException ("Failed to allocate new port");
- return newPort;
- }
+ return new SerialPort (options);
#endif // FLEXIPORT_INCLUDE_SERIAL
#ifdef FLEXIPORT_INCLUDE_TCP
if (type == "tcp")
- {
- TCPPort *newPort = new TCPPort (options);
- if (newPort == NULL)
- throw PortException ("Failed to allocate new port");
- return newPort;
- }
+ return new TCPPort (options);
#endif // FLEXIPORT_INCLUDE_TCP
#ifdef FLEXIPORT_INCLUDE_LOGGING
if (type == "logreader")
- {
- LogReaderPort *newPort = new LogReaderPort (options);
- if (newPort == NULL)
- throw PortException ("Failed to allocate new port");
- return newPort;
- }
+ return new LogReaderPort (options);
#ifdef FLEXIPORT_INCLUDE_SERIAL
if (type == "seriallog")
{
options["type"] = "serial";
- LogWriterPort *newPort = new LogWriterPort (options);
- if (newPort == NULL)
- throw PortException ("Failed to allocate new port");
- return newPort;
+ return new LogWriterPort (options);
}
#endif // FLEXIPORT_INCLUDE_SERIAL
#ifdef FLEXIPORT_INCLUDE_TCP
if (type == "tcplog")
{
options["type"] = "tcp";
- LogWriterPort *newPort = new LogWriterPort (options);
- if (newPort == NULL)
- throw PortException ("Failed to allocate new port");
- return newPort;
+ return new LogWriterPort (options);
}
#endif // FLEXIPORT_INCLUDE_TCP
#endif // FLEXIPORT_INCLUDE_LOGGING
Modified: gearbox/trunk/src/flexiport/logfile.cpp
===================================================================
--- gearbox/trunk/src/flexiport/logfile.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/flexiport/logfile.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -633,11 +633,7 @@
// Allocate space to store the data to compare with
uint8_t *fileData;
- if ((fileData = new uint8_t[count]) == NULL)
- {
- throw PortException (string ("LogFile::") + __func__ +
- string ("Failed to allocate temporary space for write comparison."));
- }
+ fileData = new uint8_t[count];
// Pull any data out of the overflow first
bool needMore = true;
Modified: gearbox/trunk/src/flexiport/logreaderport.cpp
===================================================================
--- gearbox/trunk/src/flexiport/logreaderport.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/flexiport/logreaderport.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -52,11 +52,7 @@
ProcessOptions (options);
// Initialise the log file
- if ((_logFile = new LogFile (_debug)) == NULL)
- {
- throw PortException (string ("LogReaderPort::") + __func__ +
- string ("() Failed to allocate LogFile object."));
- }
+ _logFile = new LogFile (_debug);
_logFile->Open (_logFileName, true, _ignoreTimes);
if (_alwaysOpen)
Modified: gearbox/trunk/src/flexiport/logwriterport.cpp
===================================================================
--- gearbox/trunk/src/flexiport/logwriterport.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/flexiport/logwriterport.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -71,11 +71,7 @@
_port = CreatePort (options);
// Initialise the log file
- if ((_logFile = new LogFile (_debug)) == NULL)
- {
- throw PortException (string ("LogWriterPort::") + __func__ +
- string ("() Failed to allocate LogFile object."));
- }
+ _logFile = new LogFile (_debug);
_logFile->Open (_logFileName, false);
}
Modified: gearbox/trunk/src/flexiport/port.cpp
===================================================================
--- gearbox/trunk/src/flexiport/port.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/flexiport/port.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -4,17 +4,17 @@
* Copyright (c) 2008 Geoffrey Biggs
*
* flexiport flexible hardware data communications library.
- *
- * This distribution is licensed to you under the terms described in the LICENSE file included in
+ *
+ * This distribution is licensed to you under the terms described in the LICENSE file included in
* this distribution.
*
* This work is a product of the National Institute of Advanced Industrial Science and Technology,
* Japan. Registration number: H20PRO-881
- *
+ *
* This file is part of flexiport.
*
* flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU
- * Lesser General Public License as published by the Free Software Foundation, either version 3 of
+ * Lesser General Public License as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
@@ -73,7 +73,7 @@
{
char *charBuffer = NULL;
ssize_t bytesAvailable = 0, numRead = 0;
-
+
buffer.clear ();
CheckPort (true);
@@ -86,17 +86,13 @@
if (_debug >= 2)
{
- cerr << "Port::" << __func__ << "() Got " << bytesAvailable <<
+ cerr << "Port::" << __func__ << "() Got " << bytesAvailable <<
" bytes waiting to be read into a string" << endl;
}
// Read this many characters into a string - include space for a NULL incase one doesn't
// come in the transmitted data.
- if ((charBuffer = new char[bytesAvailable + 1]) == NULL)
- {
- throw PortException (string ("Port::") + __func__ +
- string ("() Failed to allocate temporary string storage."));
- }
+ charBuffer = new char[bytesAvailable + 1];
if ((numRead = Read (charBuffer, bytesAvailable)) < 0)
return -1; // Timeout
charBuffer[numRead] = '\0';
@@ -104,8 +100,8 @@
if (numRead != bytesAvailable && _debug >= 1)
{
- cerr << "WARNING: Port::" << __func__ <<
- " Read different number of bytes than peek said were available: " <<
+ cerr << "WARNING: Port::" << __func__ <<
+ " Read different number of bytes than peek said were available: " <<
numRead << " != " << bytesAvailable << endl;
}
if (_debug >= 2)
@@ -125,7 +121,7 @@
if (_debug >= 2)
{
- cerr << "Port::" << __func__ << "() Reading until '" << terminator << "' or " <<
+ cerr << "Port::" << __func__ << "() Reading until '" << terminator << "' or " <<
count << " bytes." << endl;
}
// Read bytes one at a time until either a timeout occurs, we hit the terminator byte, or
@@ -170,7 +166,7 @@
if (_debug >= 2)
{
- cerr << "Port::" << __func__ << "() Reading string until receive '" << terminator <<
+ cerr << "Port::" << __func__ << "() Reading string until receive '" << terminator <<
"'" << endl;
}
// Read bytes one at a time until either a timeout occurs or we hit the terminator byte
@@ -256,7 +252,7 @@
if (_debug >= 2)
{
- cerr << "Port::" << __func__ << "() Skipping until '" << terminator << "' is seen " <<
+ cerr << "Port::" << __func__ << "() Skipping until '" << terminator << "' is seen " <<
count << " times." << endl;
}
// Read bytes one at a time until either a timeout occurs or we hit the terminator byte
@@ -309,15 +305,15 @@
if (!IsOpen ())
{
stringstream ss;
- ss << "Port::" << __func__ << "() Port closed while trying to write " <<
+ ss << "Port::" << __func__ << "() Port closed while trying to write " <<
count << " bytes";
throw PortException (ss.str ());
}
// If it is open we can keep going, but with a warning
if (_debug >= 1)
{
- cerr << "WARNING: Port::" << __func__ <<
- " Port closed during WriteFull operation; data may be missing/corrupted." <<
+ cerr << "WARNING: Port::" << __func__ <<
+ " Port closed during WriteFull operation; data may be missing/corrupted." <<
endl;
}
}
@@ -342,7 +338,7 @@
return -1; // Timeout
if (numWritten < numToWrite && _debug >= 1)
{
- cerr << "WARNING: Port::" << __func__ << "() Did not write whole string; only wrote " <<
+ cerr << "WARNING: Port::" << __func__ << "() Did not write whole string; only wrote " <<
numWritten << " of " << numToWrite << " bytes" << endl;
}
Modified: gearbox/trunk/src/hokuyo_aist/hokuyo_aist.cpp
===================================================================
--- gearbox/trunk/src/hokuyo_aist/hokuyo_aist.cpp 2008-07-25 13:09:43 UTC (rev 298)
+++ gearbox/trunk/src/hokuyo_aist/hokuyo_aist.cpp 2008-07-29 09:29:25 UTC (rev 299)
@@ -566,10 +566,14 @@
}
else
{
- if ((_ranges = new uint32_t[_length]) == NULL)
+ try
{
+ _ranges = new uint32_t[_length];
+ }
+ catch (std::bad_alloc &e)
+ {
_length = 0;
- throw HokuyoError (HOKUYO_ERR_MEMORY, "Failed to allocate space to copy range data.");
+ throw;
}
memcpy (_ranges, ranges, sizeof (uint32_t) * _length);
@@ -589,18 +593,18 @@
}
else
{
- if ((_ranges = new uint32_t[_length]) == NULL)
+ try
{
+ _ranges = new uint32_t[_length];
+ }
+ catch (std::bad_alloc &e)
+ {
_length = 0;
- throw HokuyoError (HOKUYO_ERR_MEMORY, "Failed to allocate space to copy range data.");
+ throw;
}
memcpy (_ranges, ranges, sizeof (uint32_t) * _length);
- if ((_intensities = new uint32_t[_length]) == NULL)
- {
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space to copy intensity data.");
- }
+ _intensities = new uint32_t[_length];
memcpy (_intensities, intensities, sizeof (uint32_t) * _length);
}
}
@@ -612,20 +616,20 @@
_ranges = NULL;
else
{
- if ((_ranges = new uint32_t[_length]) == NULL)
+ try
{
+ _ranges = new uint32_t[_length];
+ }
+ catch (std::bad_alloc &e)
+ {
_length = 0;
- throw HokuyoError (HOKUYO_ERR_MEMORY, "Failed to allocate space to copy data.");
+ throw;
}
memcpy (_ranges, rhs.Ranges (), sizeof (uint32_t) * _length);
if (rhs.Intensities () != NULL)
{
- if ((_intensities = new uint32_t[_length]) == NULL)
- {
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space to copy intensity data.");
- }
+ _intensities = new uint32_t[_length];
memcpy (_intensities, rhs.Intensities (), sizeof (uint32_t) * _length);
}
}
@@ -739,11 +743,7 @@
{
// Copy the data into a temporary variable pointing to new space (prevents dangling
// pointers on allocation error and prevents self-assignment making a mess).
- if ((newData = new uint32_t[rhsLength]) == NULL)
- {
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space to copy range data.");
- }
+ newData = new uint32_t[rhsLength];
memcpy (newData, rhs.Ranges (), sizeof (uint32_t) * rhsLength);
if (_ranges != NULL)
delete[] _ranges;
@@ -752,14 +752,17 @@
if (rhs.Intensities () != NULL)
{
- if ((newData = new uint32_t[rhsLength]) == NULL)
+ try
{
+ newData = new uint32_t[rhsLength];
+ }
+ catch (std::bad_alloc &e)
+ {
// We have to remove any old intensity data or the length won't match
if (_intensities != NULL)
delete[] _intensities;
_intensities = NULL;
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space to copy intensity data.");
+ throw;
}
memcpy (newData, rhs.Intensities (), sizeof (uint32_t) * rhsLength);
if (_intensities != NULL)
@@ -837,10 +840,14 @@
// If no data yet, allocate new
if (_ranges == NULL)
{
- if ((_ranges = new uint32_t[length]) == NULL)
+ try
{
+ _ranges = new uint32_t[length];
+ }
+ catch (std::bad_alloc &e)
+ {
_length = 0;
- throw HokuyoError (HOKUYO_ERR_MEMORY, "Failed to allocate space for range data.");
+ throw;
}
_length = length;
}
@@ -848,10 +855,14 @@
else if (length != _length)
{
delete[] _ranges;
- if ((_ranges = new uint32_t[length]) == NULL)
+ try
{
+ _ranges = new uint32_t[length];
+ }
+ catch (std::bad_alloc &e)
+ {
_length = 0;
- throw HokuyoError (HOKUYO_ERR_MEMORY, "Failed to allocate space for range data.");
+ throw;
}
_length = length;
}
@@ -862,21 +873,13 @@
// If no data yet, allocate new
if (_intensities == NULL)
{
- if ((_intensities = new uint32_t[length]) == NULL)
- {
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space for intensity data.");
- }
+ _intensities = new uint32_t[length];
}
// If there is data, reallocate only if the length is different
else if (length != _length)
{
delete[] _intensities;
- if ((_intensities = new uint32_t[length]) == NULL)
- {
- throw HokuyoError (HOKUYO_ERR_MEMORY,
- "Failed to allocate space for intensity data.");
- }
+ _intensities = new uint32_t[length];
}
// Else data is already allocated to the right length, so do nothing
}
@@ -1497,7 +1500,7 @@
// Mx commands will perform a scan, then send the data prefixed with another command echo
// Read back the command echo (minimum of 3 bytes, maximum of 16 bytes)
char response[17];
- SkipLines (1); // End of the command echo message
+ SkipLines (1); // End of the command echo message
ReadLine (response, 16); // Size is command (2) + params (13) + new line (1)
// Check the echo is correct
if (response[0] != 'M' || response[1] != 'D')
@@ -1618,7 +1621,7 @@
// Mx commands will perform a scan, then send the data prefixed with another command echo
// Read back the command echo (minimum of 3 bytes, maximum of 16 bytes)
char response[17];
- SkipLines (1); // End of the command echo message
+ SkipLines (1); // End of the command echo message
ReadLine (response, 16); // Size is command (2) + params (13) + new line (1)
// Check the echo is correct
if (response[0] != 'M' || response[1] != 'E')
@@ -2038,8 +2041,8 @@
void HokuyoLaser::GetAndSetSCIPVersion (void)
{
bool scip1Failed = false;
-
+
if (_verbose)
cerr << "HokuyoLaser::" << __func__ << "() Testing SCIP protocol version." << endl;
// Try SCIP version 1 first by sending an info command
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|