Skip to content

Commit

Permalink
Merge pull request #208 from SwissTPH/c++11
Browse files Browse the repository at this point in the history
Update to C++11
  • Loading branch information
dhardy authored Sep 13, 2018
2 parents dd6eab5 + e78e44b commit b205311
Show file tree
Hide file tree
Showing 69 changed files with 50,146 additions and 50,061 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ set (CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})

# ----- Compile options -----

# C++11 would be nice, but we have to compile for relatively old platforms
if (NOT MSVC)
# Enable builds on Raspberry pi (although without optimizations)
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98 -msse2 -mfpmath=sse")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -msse2 -mfpmath=sse")
endif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm")
endif (NOT MSVC)

Expand Down
10 changes: 4 additions & 6 deletions doc/coding practices.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ Memory management
There are two forms of memory management in C++:

* do-it-yourself C-style with pointers
* managed containers: vector, auto_ptr, etc.
* managed containers: vector, unique_ptr, etc.

I've tended towards the latter. Usually it's possible to arrange data into a
hierarchical structure, and in this case these containers will do most of the
work.

There is however one caveat to watch out for: auto_ptr doesn't have the usual
copy semantics, and so cannot be used in lists (std::vector, std::map, etc.)!
There are several ways of getting around this: boost::shared_ptr, storing
pointers in lists rather than the objects themselves (boost::ptr_container is
useful for this, since at some point the objects also need to be deleted).
TODO: boost::shared_ptr has been used in the past to get around the limitations
of `std::auto_ptr`; now that we are using a later C++ standard we can migrate
away from some of these BOOST facilities.
1 change: 1 addition & 0 deletions model/Clinical/CMDecisionTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class CMDTDeploy : public CMDecisionTree, interventions::HumanIntervention {
// ——— static functions ———

// Memory management: lists all decisions and frees memory at program exit
// TODO: use C++11 move semantics
ptr_vector<CMDecisionTree> decision_library;

// Saves a decision to decision_library, making it const.
Expand Down
2 changes: 1 addition & 1 deletion model/Clinical/ClinicalModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ClinicalModel::init( const Parameters& parameters, const scnXml::Scenario&
if( scenario.getHealthSystem().getImmediateOutcomes().present() ){
opt_imm_outcomes = true;

if( util::CommandLine::DEPRECATION_WARNINGS ){
if( util::CommandLine::option(util::CommandLine::DEPRECATION_WARNINGS) ){
cerr << "Deprecation warning: healthSystem: use of ImmediateOutcomes can be replaced by the more flexible DecisionTree5Day (optional)" << endl;
}
}
Expand Down
6 changes: 0 additions & 6 deletions model/Clinical/ESCaseManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
#include "WithinHost/WHInterface.h"
#include "schema/pharmacology.h"

#include <cassert>
#include <list>
#include <limits>
#include <boost/ptr_container/ptr_unordered_map.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

namespace OM { namespace Clinical {
using WithinHost::WHInterface;

Expand Down
6 changes: 2 additions & 4 deletions model/Host/Human.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ bool Human::update(bool doUpdate) {
// report removal due to expiry
mon::reportEventMHI( mon::MHR_SUB_POP_REM_TOO_OLD, *this, 1 );
m_cohortSet = mon::updateCohortSet( m_cohortSet, expIt->first, false );
// erase element, but continue iteration (note: this is simpler in C++11)
map<ComponentId,SimTime>::iterator toErase = expIt;
++expIt;
m_subPopExp.erase( toErase );
// erase element, but continue iteration
expIt = m_subPopExp.erase( expIt );
}else{
++expIt;
}
Expand Down
4 changes: 2 additions & 2 deletions model/PkPd/Drug/LSTMDrugType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ using WithinHost::Genotypes;
// ----- Static variables and functions -----

// The list of drugTypes drugs. Not checkpointed.
//TODO: do we need to store by pointer?
// TODO: use C++11 move semantics
typedef boost::ptr_vector<LSTMDrugType> DrugTypesT;
DrugTypesT drugTypes;
map<string,size_t> drugTypeNames;
Expand Down Expand Up @@ -189,7 +189,7 @@ LSTMDrugType::LSTMDrugType (size_t index, const scnXml::PKPDDrug& drugData) :
const scnXml::Conversion& conv = pk.getConversion().get();
try{
metabolite = findDrug(conv.getMetabolite());
}catch( util::xml_scenario_error e ){
}catch( util::xml_scenario_error& e ){
throw util::xml_scenario_error( "PK: metabolite drug not found; metabolite must be defined *before* parent drug!" );
}
conversion_rate.setParams( conv.getRate() );
Expand Down
3 changes: 2 additions & 1 deletion model/PkPd/Drug/LSTMDrugType.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class LSTMDrugType {
// TODO: at the moment we're storing all PK & PD data regardless of which
// model is used. Evaluate whether this is sensible or not.

// TODO: use C++11 move semantics
/* PD parameters (may vary based on infection genotype). */
boost::ptr_vector<LSTMDrugPD> PD;

Expand Down Expand Up @@ -203,4 +204,4 @@ class LSTMDrugType {
};

} }
#endif
#endif
3 changes: 2 additions & 1 deletion model/PkPd/LSTMModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class LSTMModel {
void checkpoint (istream& stream);
void checkpoint (ostream& stream);

// TODO: use C++11 move semantics
typedef ptr_vector<LSTMDrug> DrugVec;
/// Drugs with non-zero blood concentrations:
DrugVec m_drugs;
Expand All @@ -166,4 +167,4 @@ class LSTMModel {
};

} }
#endif
#endif
1 change: 1 addition & 0 deletions model/Population.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Population

/// Type of population list. Store pointers to humans only to avoid copy
/// operations which (AFAIAA) are otherwise required in C++98.
// TODO: use C++11 move semantics
typedef boost::ptr_list<Host::Human> HumanPop;
/// Iterator type of population
typedef HumanPop::iterator Iter;
Expand Down
8 changes: 4 additions & 4 deletions model/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ bool Simulator::startedFromCheckpoint; // static

const char* CHECKPOINT = "checkpoint";

std::auto_ptr<Population> sim::p_humanPop;
std::auto_ptr<TransmissionModel> sim::p_transmission;
std::unique_ptr<Population> sim::p_humanPop;
std::unique_ptr<TransmissionModel> sim::p_transmission;

enum Phase {
STARTING_PHASE = 0,
Expand Down Expand Up @@ -111,9 +111,9 @@ Simulator::Simulator( util::Checksum ck, const scnXml::Scenario& scenario ) :
// genotypes (both from Human, from Population::init()) and
// Monitoring::AgeGroup (from Surveys.init()):
// Note: PerHost dependency can be postponed; it is only used to set adultAge
sim::p_humanPop = auto_ptr<Population>(
sim::p_humanPop = unique_ptr<Population>(
new Population( scenario.getDemography().getPopSize() ));
sim::p_transmission = auto_ptr<TransmissionModel>(
sim::p_transmission = unique_ptr<TransmissionModel>(
TransmissionModel::createTransmissionModel(scenario.getEntomology(), sim::humanPop().size()) );

// Depends on transmission model (for species indexes):
Expand Down
2 changes: 1 addition & 1 deletion model/Transmission/Anopheles/AnophelesModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void AnophelesModel::initVectorTrap(const scnXml::Description1& desc, size_t ins
TrapParams params;
params.relAvail = desc.getRelativeAvailability().getValue();
params.availDecay= DecayFunction::makeObject(desc.getDecayOfAvailability(), "decayOfAvailability");
trapParams.push_back(params);
trapParams.push_back(move(params));
}

void AnophelesModel::deployVectorPopInterv (size_t instance){
Expand Down
39 changes: 36 additions & 3 deletions model/Transmission/Anopheles/AnophelesModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include <vector>
#include <limits>
#include <boost/shared_ptr.hpp>

namespace OM {
namespace Transmission {
Expand Down Expand Up @@ -82,6 +81,38 @@ class AnophelesModel
nhh_sigma_dff(numeric_limits<double>::signaling_NaN())
{}

AnophelesModel (AnophelesModel&& o):
humanBase(move(o.humanBase)),
mosqSeekingDuration(move(o.mosqSeekingDuration)),
mosqSeekingDeathRate(move(o.mosqSeekingDeathRate)),
probMosqSurvivalOvipositing(move(o.probMosqSurvivalOvipositing)),
nhh_avail(move(o.nhh_avail)),
nhh_sigma_df(move(o.nhh_sigma_df)),
nhh_sigma_dff(move(o.nhh_sigma_dff)),
trapParams(move(o.trapParams)),
transmission(move(o.transmission)),
seekingDeathRateIntervs(move(o.seekingDeathRateIntervs)),
probDeathOvipositingIntervs(move(o.probDeathOvipositingIntervs)),
baitedTraps(move(o.baitedTraps)),
partialEIR(move(o.partialEIR))
{}

void operator= (AnophelesModel&& o) {
humanBase = move(o.humanBase);
mosqSeekingDuration = move(o.mosqSeekingDuration);
mosqSeekingDeathRate = move(o.mosqSeekingDeathRate);
probMosqSurvivalOvipositing = move(o.probMosqSurvivalOvipositing);
nhh_avail = move(o.nhh_avail);
nhh_sigma_df = move(o.nhh_sigma_df);
nhh_sigma_dff = move(o.nhh_sigma_dff);
trapParams = move(o.trapParams);
transmission = move(o.transmission);
seekingDeathRateIntervs = move(o.seekingDeathRateIntervs);
probDeathOvipositingIntervs = move(o.probDeathOvipositingIntervs);
baitedTraps = move(o.baitedTraps);
partialEIR = move(o.partialEIR);
}

/** Called to initialise variables instead of a constructor. At this point,
* the size of the human population is known but that population has not
* yet been constructed. Called whether data is loaded from a check-point
Expand Down Expand Up @@ -300,11 +331,13 @@ class AnophelesModel
//@}

struct TrapParams {
TrapParams(): relAvail(numeric_limits<double>::signaling_NaN()) {}
TrapParams(TrapParams&& o): relAvail(o.relAvail), availDecay(move(o.availDecay)) {}

// Initial availability of a trap relative to an adult
double relAvail;
// Decay of availability
// note: can't use "auto_ptr" inside a vector, otherwise that would suffice
boost::shared_ptr<util::DecayFunction> availDecay;
unique_ptr<util::DecayFunction> availDecay;
};
// Parameters for trap interventions. Doesn't need checkpointing.
vector<TrapParams> trapParams;
Expand Down
4 changes: 2 additions & 2 deletions model/Transmission/Anopheles/FixedEmergence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void FixedEmergence::init2( double tsP_A, double tsP_df, double tsP_dff, double
transmission.initState ( tsP_A, tsP_df, tsP_dff, initNvFromSv, initOvFromSv, forcedS_v );

// Crude estimate of mosqEmergeRate: (1 - P_A(t) - P_df(t)) / (T * ρ_S) * S_T(t)
mosqEmergeRate = forcedS_v;
mosqEmergeRate = move(forcedS_v);
vectors::scale (mosqEmergeRate, initNv0FromSv);

// All set up to drive simulation from forcedS_v
Expand Down Expand Up @@ -112,7 +112,7 @@ bool FixedEmergence::initIterate (MosqTransmission& transmission) {
FSRotateAngle -= rAngle;
vectors::expIDFT (forcedS_v, FSCoeffic, FSRotateAngle);
// We use the stored initXxFromYy calculated from the ideal population age-structure (at init).
mosqEmergeRate = forcedS_v;
mosqEmergeRate = move(forcedS_v);
vectors::scale (mosqEmergeRate, initNv0FromSv);

const double LIMIT = 0.1;
Expand Down
6 changes: 3 additions & 3 deletions model/Transmission/Anopheles/MosqTransmission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ void MosqTransmission::initialise ( const scnXml::AnophelesParams::LifeCycleOpti
throw util::xml_scenario_error(
"VECTOR_LIFE_CYCLE_MODEL: requires <lifeCycle> element with "
"model parameters for each anopheles species");
emergence = boost::shared_ptr<EmergenceModel>( new LCEmergence() );
emergence = unique_ptr<EmergenceModel>( new LCEmergence() );
emergence->initLifeCycle( lcOpt.get() );
*/
}else if (util::ModelOptions::option( util::VECTOR_SIMPLE_MPD_MODEL )){
if (!simpleMPDOpt.present())
throw util::xml_scenario_error(
"VECTOR_SIMPLE_MPD_MODEL: requires <simpleMPD> element with "
"model parameters for each anopheles species");
emergence = boost::shared_ptr<EmergenceModel>(new SimpleMPDEmergence(simpleMPDOpt.get()) );
emergence = unique_ptr<EmergenceModel>(new SimpleMPDEmergence(simpleMPDOpt.get()) );
}else
emergence = boost::shared_ptr<EmergenceModel>( new FixedEmergence() );
emergence = unique_ptr<EmergenceModel>( new FixedEmergence() );


// ----- Set model variables -----
Expand Down
39 changes: 37 additions & 2 deletions model/Transmission/Anopheles/MosqTransmission.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "schema/entomology.h"

#include <limits>
#include <boost/shared_ptr.hpp>

class MosqLifeCycleSuite;

Expand All @@ -53,6 +52,42 @@ class MosqTransmission {
//@{
MosqTransmission();

MosqTransmission (MosqTransmission&& o):
emergence(move(o.emergence)),
mosqRestDuration(move(o.mosqRestDuration)),
EIPDuration(move(o.EIPDuration)),
N_v_length(move(o.N_v_length)),
minInfectedThreshold(move(o.minInfectedThreshold)),
P_A(move(o.P_A)),
P_df(move(o.P_df)),
P_dif(move(o.P_dif)),
P_dff(move(o.P_dff)),
N_v(move(o.N_v)),
S_v(move(o.S_v)),
fArray(move(o.fArray)),
ftauArray(move(o.ftauArray)),
uninfected_v(move(o.uninfected_v)),
timeStep_N_v0(move(o.timeStep_N_v0))
{}

void operator= (MosqTransmission&& o) {
emergence = move(o.emergence);
mosqRestDuration = move(o.mosqRestDuration);
EIPDuration = move(o.EIPDuration);
N_v_length = move(o.N_v_length);
minInfectedThreshold = move(o.minInfectedThreshold);
P_A = move(o.P_A);
P_df = move(o.P_df);
P_dif = move(o.P_dif);
P_dff = move(o.P_dff);
N_v = move(o.N_v);
S_v = move(o.S_v);
fArray = move(o.fArray);
ftauArray = move(o.ftauArray);
uninfected_v = move(o.uninfected_v);
timeStep_N_v0 = move(o.timeStep_N_v0);
}

/** Initialise parameters and variables.
*
* This is only a fraction of parameter initialisation; see also
Expand Down Expand Up @@ -153,7 +188,7 @@ class MosqTransmission {
/** @brief Emergence model
*
* Code to calculate emergence of mosquitoes from water bodies goes here. */
boost::shared_ptr<EmergenceModel> emergence;
unique_ptr<EmergenceModel> emergence;

private:
// ----- parameters (constant after initialisation) -----
Expand Down
2 changes: 1 addition & 1 deletion model/Transmission/Anopheles/SimpleMPDEmergence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool SimpleMPDEmergence::initIterate (MosqTransmission& transmission) {
FSRotateAngle -= rAngle;
vectors::expIDFT (forcedS_v, FSCoeffic, FSRotateAngle);
// We use the stored initXxFromYy calculated from the ideal population age-structure (at init).
mosqEmergeRate = forcedS_v;
mosqEmergeRate = move(forcedS_v);
vectors::scale (mosqEmergeRate, initNv0FromSv);

// Finally, update nOvipositingDelayed and invLarvalResources
Expand Down
2 changes: 1 addition & 1 deletion model/Transmission/PerHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void PerHost::checkpointIntervs( istream& stream ){
throw util::base_exception( "" ); // see catch block below
PerHostInterventionData *v = params->makeHumanPart( stream, id );
activeComponents.push_back( v );
}catch( util::base_exception e ){
}catch( util::base_exception& e ){
// two causes, both boil down to index being wrong
throw util::checkpoint_error( "bad value in checkpoint file" );
}
Expand Down
2 changes: 1 addition & 1 deletion model/Transmission/PerHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "util/DecayFunction.h"
#include "util/checkpoint_containers.h"
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/shared_ptr.hpp>

namespace OM {
namespace Transmission {
Expand Down Expand Up @@ -280,6 +279,7 @@ class PerHost
// entoAvailability param stored in HostMosquitoInteraction.
double _relativeAvailabilityHet;

// TODO: use C++11 move semantics
typedef boost::ptr_list<PerHostInterventionData> ListActiveComponents;
ListActiveComponents activeComponents;

Expand Down
2 changes: 1 addition & 1 deletion model/Transmission/VectorModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ VectorModel::VectorModel (const scnXml::Entomology& entoData,
numSpecies = anophelesList.size();
if (numSpecies < 1)
throw util::xml_scenario_error ("Can't use Vector model without data for at least one anopheles species!");
species.resize (numSpecies, AnophelesModel());
species.resize (numSpecies);

for(size_t i = 0; i < numSpecies; ++i) {
string name = species[i].initialise (anophelesList[i],
Expand Down
1 change: 1 addition & 0 deletions model/WithinHost/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ bool Diagnostic::isPositive( double dens ) const {

// ——— diagnostics (static) ———

// TODO: use C++11 move semantics
typedef boost::ptr_map<string,Diagnostic> Diagnostic_set;
Diagnostic_set diagnostic_set;
const Diagnostic* diagnostics::monitoring_diagnostic = 0;
Expand Down
1 change: 1 addition & 0 deletions model/WithinHost/Treatments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace WithinHost {

// ——— static ———

// TODO: use C++11 move semantics
boost::ptr_vector<Treatments> Treatments::treatments;

TreatmentId Treatments::addTreatment( const scnXml::TreatmentOption& desc ){
Expand Down
1 change: 1 addition & 0 deletions model/WithinHost/Treatments.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Treatments : public interventions::TriggeredDeployments {

private:
// static:
// TODO: use C++11 move semantics
static boost::ptr_vector<Treatments> treatments;

// non-static:
Expand Down
Loading

0 comments on commit b205311

Please sign in to comment.