Replace std::stringstream with llvm::raw_svector_ostream (#345)

A few locations were changed to use std::ostringstream.
This commit is contained in:
Tyler Veness
2017-05-15 23:10:40 -07:00
committed by Peter Johnson
parent 7006672b06
commit b433d98c02
39 changed files with 242 additions and 174 deletions

View File

@@ -7,11 +7,11 @@
#include "AnalogGyro.h"
#include <sstream>
#include "LiveWindow/LiveWindow.h"
#include "Timer.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -34,9 +34,10 @@ const double AnalogGyro::kDefaultVoltsPerDegreePerSecond = 0.007;
void AnalogGyro::InitAnalogGyro(int channel) {
SetPIDSourceType(PIDSourceType::kDisplacement);
std::stringstream ss;
ss << "analog/" << channel;
impl = new SimGyro(ss.str());
llvm::SmallString<128> buf;
llvm::raw_svector_ostream oss(buf);
oss << "analog/" << channel;
impl = new SimGyro(oss.str());
LiveWindow::GetInstance()->AddSensor("AnalogGyro", channel, this);
}

View File

@@ -7,10 +7,10 @@
#include "AnalogInput.h"
#include <sstream>
#include "LiveWindow/LiveWindow.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -20,9 +20,10 @@ using namespace frc;
* @param channel The channel number to represent.
*/
AnalogInput::AnalogInput(int channel) : m_channel(channel) {
std::stringstream ss;
ss << "analog/" << channel;
m_impl = new SimFloatInput(ss.str());
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
oss << "analog/" << channel;
m_impl = new SimFloatInput(oss.str());
LiveWindow::GetInstance()->AddSensor("AnalogInput", channel, this);
}

View File

@@ -20,9 +20,9 @@ using namespace frc;
* @param channel The digital channel (1..14).
*/
DigitalInput::DigitalInput(int channel) : m_channel(channel) {
std::stringstream ss;
ss << "dio/" << channel;
m_impl = new SimDigitalInput(ss.str());
std::ostringstream oss;
oss << "dio/" << channel;
m_impl = new SimDigitalInput(oss.str());
}
/**

View File

@@ -9,6 +9,8 @@
#include "LiveWindow/LiveWindow.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -37,10 +39,11 @@ DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel,
forwardChannel = channel;
m_reversed = true;
}
std::stringstream ss;
ss << "pneumatic/" << moduleNumber << "/" << forwardChannel << "/"
<< moduleNumber << "/" << reverseChannel;
m_impl = new SimContinuousOutput(ss.str());
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
oss << "pneumatic/" << moduleNumber << "/" << forwardChannel << "/"
<< moduleNumber << "/" << reverseChannel;
m_impl = new SimContinuousOutput(oss.str());
LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", moduleNumber,
forwardChannel, this);

View File

@@ -7,10 +7,10 @@
#include "Encoder.h"
#include <sstream>
#include "LiveWindow/LiveWindow.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -52,9 +52,10 @@ void Encoder::InitEncoder(int channelA, int channelB, bool reverseDirection,
} else {
m_reverseDirection = reverseDirection;
}
std::stringstream ss;
ss << "dio/" << channelA << "/" << channelB;
impl = new SimEncoder(ss.str());
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
oss << "dio/" << channelA << "/" << channelB;
impl = new SimEncoder(oss.str());
impl->Start();
}

View File

@@ -7,12 +7,12 @@
#include "MotorSafetyHelper.h"
#include <sstream>
#include "DriverStation.h"
#include "MotorSafety.h"
#include "Timer.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -100,10 +100,11 @@ void MotorSafetyHelper::Check() {
std::lock_guard<hal::priority_recursive_mutex> sync(m_syncMutex);
if (m_stopTime < Timer::GetFPGATimestamp()) {
std::ostringstream desc;
llvm::SmallString<128> buf;
llvm::raw_svector_ostream desc(buf);
m_safeObject->GetDescription(desc);
desc << "... Output not updated often enough.";
wpi_setWPIErrorWithContext(Timeout, desc.str().c_str());
wpi_setWPIErrorWithContext(Timeout, desc.str());
m_safeObject->StopMotor();
}
}

View File

@@ -7,10 +7,10 @@
#include "PWM.h"
#include <sstream>
#include "Utility.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
using namespace frc;
@@ -30,16 +30,17 @@ const int PWM::kPwmDisabled = 0;
* port
*/
PWM::PWM(int channel) {
std::stringstream ss;
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
if (!CheckPWMChannel(channel)) {
ss << "PWM Channel " << channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, ss.str());
oss << "PWM Channel " << channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, oss.str());
return;
}
ss << "pwm/" << channel;
impl = new SimContinuousOutput(ss.str());
oss << "pwm/" << channel;
impl = new SimContinuousOutput(oss.str());
m_channel = channel;
m_eliminateDeadband = false;

View File

@@ -7,11 +7,10 @@
#include "Relay.h"
#include <sstream>
#include "LiveWindow/LiveWindow.h"
#include "MotorSafetyHelper.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
using namespace frc;
@@ -26,21 +25,22 @@ using namespace frc;
*/
Relay::Relay(int channel, Relay::Direction direction)
: m_channel(channel), m_direction(direction) {
std::stringstream ss;
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
if (!SensorBase::CheckRelayChannel(m_channel)) {
ss << "Relay Channel " << m_channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, ss.str());
oss << "Relay Channel " << m_channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, oss.str());
return;
}
m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
m_safetyHelper->SetSafetyEnabled(false);
ss << "relay/" << m_channel;
impl = new SimContinuousOutput(ss.str()); // TODO: Allow two different relays
// (targetting the different halves
// of a relay) to be combined to
// control one motor.
oss << "relay/" << m_channel;
// TODO: Allow two different relays (targetting the different halves of a
// relay) to be combined to control one motor.
impl = new SimContinuousOutput(oss.str());
LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this);
go_pos = go_neg = false;
}
@@ -194,7 +194,7 @@ bool Relay::IsSafetyEnabled() const {
return m_safetyHelper->IsSafetyEnabled();
}
void Relay::GetDescription(std::ostringstream& desc) const {
void Relay::GetDescription(llvm::raw_ostream& desc) const {
desc << "Relay " << GetChannel();
}

View File

@@ -715,7 +715,7 @@ void RobotDrive::SetSafetyEnabled(bool enabled) {
// FIXME: m_safetyHelper->SetSafetyEnabled(enabled);
}
void RobotDrive::GetDescription(std::ostringstream& desc) const {
void RobotDrive::GetDescription(llvm::raw_ostream& desc) const {
desc << "RobotDrive";
}

View File

@@ -8,7 +8,6 @@
#include "SafePWM.h"
#include <memory>
#include <sstream>
using namespace frc;
@@ -76,7 +75,7 @@ bool SafePWM::IsSafetyEnabled() const {
return m_safetyHelper->IsSafetyEnabled();
}
void SafePWM::GetDescription(std::ostringstream& desc) const {
void SafePWM::GetDescription(llvm::raw_ostream& desc) const {
desc << "PWM " << GetChannel();
}

View File

@@ -7,10 +7,10 @@
#include "Solenoid.h"
#include <sstream>
#include "LiveWindow/LiveWindow.h"
#include "WPIErrors.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
#include "simulation/simTime.h"
using namespace frc;
@@ -29,9 +29,10 @@ Solenoid::Solenoid(int channel) : Solenoid(1, channel) {}
* @param channel The channel on the solenoid module to control (1..8).
*/
Solenoid::Solenoid(int moduleNumber, int channel) {
std::stringstream ss;
ss << "pneumatic/" << moduleNumber << "/" << channel;
m_impl = new SimContinuousOutput(ss.str());
llvm::SmallString<32> buf;
llvm::raw_svector_ostream oss(buf);
oss << "pneumatic/" << moduleNumber << "/" << channel;
m_impl = new SimContinuousOutput(oss.str());
LiveWindow::GetInstance()->AddActuator("Solenoid", moduleNumber, channel,
this);

View File

@@ -15,10 +15,10 @@
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include "Timer.h"
#include "llvm/SmallString.h"
#include "llvm/raw_ostream.h"
#include "simulation/simTime.h"
using namespace frc;
@@ -61,7 +61,8 @@ bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText,
llvm::StringRef message, llvm::StringRef fileName,
int lineNumber, llvm::StringRef funcName) {
if (!conditionValue) {
std::stringstream errorStream;
llvm::SmallString<1024> errorBuf;
llvm::raw_svector_ostream errorStream(errorBuf);
errorStream << "Assertion \"" << conditionText << "\" ";
errorStream << "on line " << lineNumber << " ";
@@ -70,9 +71,9 @@ bool wpi_assert_impl(bool conditionValue, llvm::StringRef conditionText,
errorStream << "of " << basename(fileName.c_str(fileTemp)) << " ";
if (!message.empty()) {
errorStream << "failed: " << message << std::endl;
errorStream << "failed: " << message << "\n";
} else {
errorStream << "failed." << std::endl;
errorStream << "failed.\n";
}
// Print to console and send to remote dashboard
@@ -94,7 +95,8 @@ void wpi_assertEqual_common_impl(int valueA, int valueB,
llvm::StringRef fileName, int lineNumber,
llvm::StringRef funcName) {
// Error string buffer
std::stringstream error;
llvm::SmallString<1024> buf;
llvm::raw_svector_ostream error(buf);
// If an error message was specified, include it
// Build error string
@@ -192,12 +194,13 @@ std::string GetStackTrace(int offset) {
void* stackTrace[128];
int stackSize = backtrace(stackTrace, 128);
char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
std::stringstream trace;
llvm::SmallString<1024> buf;
llvm::raw_svector_ostream trace(buf);
for (int i = offset; i < stackSize; i++) {
// Only print recursive functions once in a row.
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
trace << "\tat " << demangle(mangledSymbols[i]) << std::endl;
trace << "\tat " << demangle(mangledSymbols[i]) << "\n";
}
}