pvif: DBCH cleanup

avoid some duplication
This commit is contained in:
Michael Davidsaver
2018-02-14 11:10:09 -08:00
parent 392a44b791
commit d229efbb66
3 changed files with 25 additions and 7 deletions

16
common/sb.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef SB_H
#define SB_H
#include <sstream>
// in-line string builder (eg. for exception messages)
// throw std::runtime_error(SB()<<"Answer: !"<<42);
struct SB {
std::ostringstream strm;
SB() {}
operator std::string() const { return strm.str(); }
template<typename T>
SB& operator<<(T i) { strm<<i; return *this; }
};
#endif // SB_H

View File

@ -19,6 +19,7 @@
#include <pv/reftrack.h>
#define epicsExportSharedSymbols
#include "sb.h"
#include "pvif.h"
#include <epicsExport.h>
@ -37,22 +38,22 @@ namespace pvd = epics::pvData;
DBCH::DBCH(dbChannel *ch) :chan(ch)
{
if(!chan)
throw std::invalid_argument("NULL channel");
if(dbChannelOpen(chan)) {
dbChannelDelete(chan);
throw std::invalid_argument("Failed to open channel");
}
prepare();
}
DBCH::DBCH(const std::string& name)
:chan(dbChannelCreate(name.c_str()))
{
prepare();
}
void DBCH::prepare()
{
if(!chan)
throw std::invalid_argument("NULL channel");
if(dbChannelOpen(chan)) {
dbChannelDelete(chan);
throw std::invalid_argument("Failed to open channel "+name);
throw std::invalid_argument(SB()<<"Failed to open channel "<<dbChannelName(chan));
}
}

View File

@ -61,6 +61,7 @@ struct epicsShareClass DBCH {
private:
DBCH(const DBCH&);
DBCH& operator=(const DBCH&);
void prepare();
};
struct pdbRecordInfo {