Skip to content

Commit

Permalink
Removed the need for const_cast in SiStripObjects
Browse files Browse the repository at this point in the history
The static analyzer was complaining about the use of const_cast on
member data. Even though these const_casts were in non-const member
functions it aids future maintenance to remove their use.
  • Loading branch information
Dr15Jones committed Jan 26, 2015
1 parent de497df commit ecf5bec
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 63 deletions.
6 changes: 4 additions & 2 deletions CalibFormats/SiStripObjects/interface/SiStripCcu.h
Expand Up @@ -20,9 +20,10 @@ class SiStripCcu {

/** */
~SiStripCcu() {;}
/** */

/** */
inline const std::vector<SiStripModule>& modules() const;
inline std::vector<SiStripModule>& modules();

/** */
inline const uint16_t& ccuAddr() const;
Expand All @@ -46,6 +47,7 @@ class SiStripCcu {
// ---------- inline methods ----------

const std::vector<SiStripModule>& SiStripCcu::modules() const { return modules_; }
std::vector<SiStripModule>& SiStripCcu::modules() { return modules_; }
const uint16_t& SiStripCcu::ccuAddr() const { return ccuAddr_; }

#endif // CalibTracker_SiStripObjects_SiStripCcu_H
Expand Down
2 changes: 2 additions & 0 deletions CalibFormats/SiStripObjects/interface/SiStripFec.h
Expand Up @@ -23,6 +23,7 @@ class SiStripFec {

/** */
inline const std::vector<SiStripRing>& rings() const;
inline std::vector<SiStripRing>& rings();

/** */
inline const uint16_t& fecSlot() const;
Expand All @@ -46,6 +47,7 @@ class SiStripFec {
// ---------- inline methods ----------

const std::vector<SiStripRing>& SiStripFec::rings() const { return rings_; }
std::vector<SiStripRing>& SiStripFec::rings() { return rings_; }
const uint16_t& SiStripFec::fecSlot() const { return fecSlot_; }

#endif // CalibTracker_SiStripObjects_SiStripFec_H
Expand Down
12 changes: 9 additions & 3 deletions CalibFormats/SiStripObjects/interface/SiStripFecCabling.h
Expand Up @@ -38,12 +38,14 @@ class SiStripFecCabling {

/** */
inline const std::vector<SiStripFecCrate>& crates() const;
inline std::vector<SiStripFecCrate>& crates();
/** */
inline const std::vector<SiStripFec>& fecs() const; //@@ TEMPORARY: to maintain backward compatibility!
/** */
void connections( std::vector<FedChannelConnection>& ) const;
/** */
const SiStripModule& module( const FedChannelConnection& conn ) const;
SiStripModule* module( const FedChannelConnection& conn );
/** */
const SiStripModule& module( const uint32_t& dcu_id ) const;
/** */
Expand Down Expand Up @@ -76,6 +78,7 @@ class SiStripFecCabling {
// ---------- Inline methods ----------

const std::vector<SiStripFecCrate>& SiStripFecCabling::crates() const { return crates_; }
std::vector<SiStripFecCrate>& SiStripFecCabling::crates() { return crates_; }

// TEMPORARY method to maintain backward compatibility!
const std::vector<SiStripFec>& SiStripFecCabling::fecs() const {
Expand All @@ -85,15 +88,18 @@ const std::vector<SiStripFec>& SiStripFecCabling::fecs() const {
}

void SiStripFecCabling::dcuId( const FedChannelConnection& conn ) {
const_cast<SiStripModule&>(module(conn)).dcuId(conn.dcuId());
auto m = module(conn);
if(m) {m->dcuId(conn.dcuId());}
}

void SiStripFecCabling::detId( const FedChannelConnection& conn ) {
const_cast<SiStripModule&>(module(conn)).detId(conn.detId());
auto m = module(conn);
if(m) { m->detId(conn.detId()); }
}

void SiStripFecCabling::nApvPairs( const FedChannelConnection& conn ) {
const_cast<SiStripModule&>(module(conn)).nApvPairs(conn.nApvPairs());
auto m = module(conn);
if(m) { m->nApvPairs(conn.nApvPairs()); }
}

#endif // CalibTracker_SiStripObjects_SiStripFecCabling_H
Expand Down
2 changes: 2 additions & 0 deletions CalibFormats/SiStripObjects/interface/SiStripFecCrate.h
Expand Up @@ -23,6 +23,7 @@ class SiStripFecCrate {

/** */
inline const std::vector<SiStripFec>& fecs() const;
inline std::vector<SiStripFec>& fecs();

/** */
inline const uint16_t& fecCrate() const;
Expand All @@ -46,6 +47,7 @@ class SiStripFecCrate {
// ---------- inline methods ----------

const std::vector<SiStripFec>& SiStripFecCrate::fecs() const { return fecs_; }
std::vector<SiStripFec>& SiStripFecCrate::fecs() { return fecs_; }
const uint16_t& SiStripFecCrate::fecCrate() const { return fecCrate_; }

#endif // CalibTracker_SiStripObjects_SiStripFecCrate_H
Expand Down
2 changes: 2 additions & 0 deletions CalibFormats/SiStripObjects/interface/SiStripRing.h
Expand Up @@ -23,6 +23,7 @@ class SiStripRing {

/** */
inline const std::vector<SiStripCcu>& ccus() const;
inline std::vector<SiStripCcu>& ccus();

/** */
inline const uint16_t& fecRing() const;
Expand All @@ -46,6 +47,7 @@ class SiStripRing {
// ---------- inline methods ----------

const std::vector<SiStripCcu>& SiStripRing::ccus() const { return ccus_; }
std::vector<SiStripCcu>& SiStripRing::ccus() { return ccus_; }
const uint16_t& SiStripRing::fecRing() const { return fecRing_; }

#endif // CalibTracker_SiStripObjects_SiStripRing_H
Expand Down
8 changes: 4 additions & 4 deletions CalibFormats/SiStripObjects/src/SiStripCcu.cc
Expand Up @@ -15,11 +15,11 @@ SiStripCcu::SiStripCcu( const FedChannelConnection& conn )
// -----------------------------------------------------------------------------
//
void SiStripCcu::addDevices( const FedChannelConnection& conn ) {
std::vector<SiStripModule>::const_iterator imod = modules().begin();
while ( imod != modules().end() && (*imod).ccuChan() != conn.ccuChan() ) { imod++; }
if ( imod == modules().end() ) {
auto imod = modules_.begin();
while ( imod != modules_.end() && (*imod).ccuChan() != conn.ccuChan() ) { imod++; }
if ( imod == modules_.end() ) {
modules_.push_back( SiStripModule( conn ) );
} else {
const_cast<SiStripModule&>(*imod).addDevices( conn );
imod->addDevices( conn );
}
}
8 changes: 4 additions & 4 deletions CalibFormats/SiStripObjects/src/SiStripFec.cc
Expand Up @@ -15,11 +15,11 @@ SiStripFec::SiStripFec( const FedChannelConnection& conn )
// -----------------------------------------------------------------------------
//
void SiStripFec::addDevices( const FedChannelConnection& conn ) {
std::vector<SiStripRing>::const_iterator iring = rings().begin();
while ( iring != rings().end() && (*iring).fecRing() != conn.fecRing() ) { iring++; }
if ( iring == rings().end() ) {
auto iring = rings_.begin();
while ( iring != rings_.end() && (*iring).fecRing() != conn.fecRing() ) { iring++; }
if ( iring == rings_.end() ) {
rings_.push_back( SiStripRing( conn ) );
} else {
const_cast<SiStripRing&>(*iring).addDevices( conn );
iring->addDevices( conn );
}
}
103 changes: 60 additions & 43 deletions CalibFormats/SiStripObjects/src/SiStripFecCabling.cc
Expand Up @@ -61,12 +61,12 @@ void SiStripFecCabling::buildFecCabling( const SiStripFedCabling& fed_cabling )
// -----------------------------------------------------------------------------
//
void SiStripFecCabling::addDevices( const FedChannelConnection& conn ) {
std::vector<SiStripFecCrate>::const_iterator icrate = crates().begin();
while ( icrate != crates().end() && (*icrate).fecCrate() != conn.fecCrate() ) { icrate++; }
if ( icrate == crates().end() ) {
auto icrate = crates_.begin();
while ( icrate != crates_.end() && (*icrate).fecCrate() != conn.fecCrate() ) { icrate++; }
if ( icrate == crates_.end() ) {
crates_.push_back( SiStripFecCrate( conn ) );
} else {
const_cast<SiStripFecCrate&>(*icrate).addDevices( conn );
icrate->addDevices( conn );
}
}

Expand Down Expand Up @@ -114,52 +114,69 @@ void SiStripFecCabling::connections( std::vector<FedChannelConnection>& conns )

// -----------------------------------------------------------------------------
//
const SiStripModule& SiStripFecCabling::module( const FedChannelConnection& conn ) const {

std::stringstream ss;
std::vector<SiStripFecCrate>::const_iterator icrate = crates().begin();
while ( icrate != crates().end() && icrate->fecCrate() != conn.fecCrate() ) { icrate++; }
if ( icrate != crates().end() ) {
std::vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin();
while ( ifec != icrate->fecs().end() && ifec->fecSlot() != conn.fecSlot() ) { ifec++; }
if ( ifec != icrate->fecs().end() ) {
std::vector<SiStripRing>::const_iterator iring = ifec->rings().begin();
while ( iring != ifec->rings().end() && iring->fecRing() != conn.fecRing() ) { iring++; }
if ( iring != ifec->rings().end() ) {
std::vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin();
while ( iccu != iring->ccus().end() && iccu->ccuAddr() != conn.ccuAddr() ) { iccu++; }
if ( iccu != iring->ccus().end() ) {
std::vector<SiStripModule>::const_iterator imod = iccu->modules().begin();
while ( imod != iccu->modules().end() && imod->ccuChan() != conn.ccuChan() ) { imod++; }
if ( imod != iccu->modules().end() ) {
return *imod;
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " CCU channel " << conn.ccuChan()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " CCU address " << conn.ccuAddr()
<< " not found!"; }
namespace {
// Using a template allows the const and non-const version to share the same code
template<typename T>
auto moduleFrom( T& crates, const FedChannelConnection& conn ) -> decltype(& (crates[0].fecs()[0].rings()[0].ccus()[0].modules()[0])) {
std::stringstream ss;
auto icrate = crates.begin();
while ( icrate != crates.end() && icrate->fecCrate() != conn.fecCrate() ) { icrate++; }
if ( icrate != crates.end() ) {
auto ifec = icrate->fecs().begin();
while ( ifec != icrate->fecs().end() && ifec->fecSlot() != conn.fecSlot() ) { ifec++; }
if ( ifec != icrate->fecs().end() ) {
auto iring = ifec->rings().begin();
while ( iring != ifec->rings().end() && iring->fecRing() != conn.fecRing() ) { iring++; }
if ( iring != ifec->rings().end() ) {
auto iccu = iring->ccus().begin();
while ( iccu != iring->ccus().end() && iccu->ccuAddr() != conn.ccuAddr() ) { iccu++; }
if ( iccu != iring->ccus().end() ) {
auto imod = iccu->modules().begin();
while ( imod != iccu->modules().end() && imod->ccuChan() != conn.ccuChan() ) { imod++; }
if ( imod != iccu->modules().end() ) {
return &(*imod);
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " CCU channel " << conn.ccuChan()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " CCU address " << conn.ccuAddr()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " FEC ring " << conn.fecRing()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " FEC ring " << conn.fecRing()
<< " not found!"; }
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " FEC slot " << conn.fecSlot()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " FEC slot " << conn.fecSlot()
<< " not found!"; }
} else {
ss << "[SiStripFecCabling::" << __func__ << "]"
<< " FEC crate " << conn.fecCrate()
<< " not found!";
<< " FEC crate " << conn.fecCrate()
<< " not found!";
}

if ( !ss.str().empty() ) { edm::LogWarning(mlCabling_) << ss.str(); }
return nullptr;
}
}

if ( !ss.str().empty() ) { edm::LogWarning(mlCabling_) << ss.str(); }
static const SiStripModule module{FedChannelConnection{}};
return module;
const SiStripModule& SiStripFecCabling::module( const FedChannelConnection& conn ) const {
auto module = moduleFrom(crates(), conn);
if(module) {
return *module;
}

static const SiStripModule s_module{FedChannelConnection{}};
return s_module;
}

SiStripModule* SiStripFecCabling::module( const FedChannelConnection& conn ) {
return moduleFrom(crates(), conn);
}


// -----------------------------------------------------------------------------
//
const SiStripModule& SiStripFecCabling::module( const uint32_t& dcu_id ) const {
Expand Down
8 changes: 4 additions & 4 deletions CalibFormats/SiStripObjects/src/SiStripFecCrate.cc
Expand Up @@ -15,12 +15,12 @@ SiStripFecCrate::SiStripFecCrate( const FedChannelConnection& conn )
// -----------------------------------------------------------------------------
//
void SiStripFecCrate::addDevices( const FedChannelConnection& conn ) {
std::vector<SiStripFec>::const_iterator ifec = fecs().begin();
while ( ifec != fecs().end() && (*ifec).fecSlot() != conn.fecSlot() ) { ifec++; }
if ( ifec == fecs().end() ) {
auto ifec = fecs_.begin();
while ( ifec != fecs_.end() && (*ifec).fecSlot() != conn.fecSlot() ) { ifec++; }
if ( ifec == fecs_.end() ) {
fecs_.push_back( SiStripFec( conn ) );
} else {
const_cast<SiStripFec&>(*ifec).addDevices( conn );
ifec->addDevices( conn );
}
}

Expand Down
6 changes: 3 additions & 3 deletions CalibFormats/SiStripObjects/src/SiStripRing.cc
Expand Up @@ -15,12 +15,12 @@ SiStripRing::SiStripRing( const FedChannelConnection& conn )
// -----------------------------------------------------------------------------
//
void SiStripRing::addDevices( const FedChannelConnection& conn ) {
std::vector<SiStripCcu>::const_iterator iccu = ccus().begin();
while ( iccu != ccus().end() && (*iccu).ccuAddr() != conn.ccuAddr() ) { iccu++; }
auto iccu = ccus_.begin();
while ( iccu != ccus_.end() && (*iccu).ccuAddr() != conn.ccuAddr() ) { iccu++; }
if ( iccu == ccus().end() ) {
ccus_.push_back( SiStripCcu( conn ) );
} else {
const_cast<SiStripCcu&>(*iccu).addDevices( conn );
iccu->addDevices( conn );
}
}

0 comments on commit ecf5bec

Please sign in to comment.