From ee20cfab1e92bc45a708ff26d0de2cc1b15c92be Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 17 Apr 2019 16:19:18 +0200 Subject: [PATCH] removed pointers added constructor --- slsDetectorSoftware/include/slsDetector.h | 4 +- slsDetectorSoftware/src/slsDetector.cpp | 79 ++++++------------- slsSupportLib/include/sls_detector_defs.h | 42 ++++++++-- .../tests/test-sls_detector_defs.cpp | 26 +++++- 4 files changed, 83 insertions(+), 68 deletions(-) diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index 82335f29f..fc9c42b7a 100755 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -1321,7 +1321,7 @@ class slsDetector : public virtual slsDetectorDefs{ * @returns ok or fail * \sa ::sls_detector_module */ - int setModule(sls_detector_module module, int tb = 1); + int setModule(sls_detector_module& module, int tb = 1); /** * Get module structure from detector (all detectors) @@ -1805,7 +1805,7 @@ class slsDetector : public virtual slsDetectorDefs{ * @param tb 1 to include trimbits, 0 to exclude (used for eiger) * @returns the pointer to the module structure with interpolated values or NULL if error */ - sls_detector_module *interpolateTrim( + sls_detector_module interpolateTrim( sls_detector_module *a, sls_detector_module *b, const int energy, const int e1, const int e2, int tb = 1); diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index e9e1a234a..213508a46 100755 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -1280,10 +1280,7 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, break; } } - - // fill detector module structure - sls_detector_module *myMod = nullptr; - + sls_detector_module myMod{detector_shm()->myDetectorType}; // normal if (interpolate == false) { // find their directory names @@ -1294,13 +1291,7 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, std::string settingsfname = ostfn.str(); FILE_LOG(logDEBUG1) << "Settings File is " << settingsfname; - // read the files - // myMod = createModule(); // readSettings also checks if create module - // is null - if (readSettingsFile(settingsfname, myMod, tb) == nullptr) { - if (myMod != nullptr) { - deleteModule(myMod); - } + if (readSettingsFile(settingsfname, &myMod, tb) == nullptr) { return FAIL; } }else{ @@ -1330,51 +1321,33 @@ int slsDetector::setThresholdEnergyAndSettings(int e_eV, // read the files FILE_LOG(logDEBUG1) << "Settings Files are " << settingsfname1 << " and " << settingsfname2; - sls_detector_module *myMod1 = createModule(); - sls_detector_module *myMod2 = createModule(); - if (nullptr == readSettingsFile(settingsfname1, myMod1, tb)) { - - deleteModule(myMod1); - deleteModule(myMod2); + sls_detector_module myMod1{detector_shm()->myDetectorType}; + sls_detector_module myMod2{detector_shm()->myDetectorType}; + if (readSettingsFile(settingsfname1, &myMod1, tb) == nullptr) { throw RuntimeError( "setThresholdEnergyAndSettings: Could not open settings file"); } - if (nullptr == readSettingsFile(settingsfname2, myMod2, tb)) { - deleteModule(myMod1); - deleteModule(myMod2); + if (readSettingsFile(settingsfname2, &myMod2, tb) == nullptr) { throw RuntimeError( "setThresholdEnergyAndSettings: Could not open settings file"); } - if (myMod1->iodelay != myMod2->iodelay) { - deleteModule(myMod1); - deleteModule(myMod2); + if (myMod1.iodelay != myMod2.iodelay) { throw RuntimeError("setThresholdEnergyAndSettings: Iodelays do not " "match between files"); } // interpolate module - myMod = interpolateTrim(myMod1, myMod2, e_eV, trim1, trim2, tb); - if (myMod == nullptr) { - deleteModule(myMod1); - deleteModule(myMod2); - throw RuntimeError("setThresholdEnergyAndSettings: Could not " - "interpolate, different " - "dac values in files"); - } - // interpolate tau - myMod->iodelay = myMod1->iodelay; - myMod->tau = - linearInterpolation(e_eV, trim1, trim2, myMod1->tau, myMod2->tau); - // printf("new tau:%d\n",tau); + myMod = interpolateTrim(&myMod1, &myMod2, e_eV, trim1, trim2, tb); - deleteModule(myMod1); - deleteModule(myMod2); + // // interpolate tau + myMod.iodelay = myMod1.iodelay; + myMod.tau = + linearInterpolation(e_eV, trim1, trim2, myMod1.tau, myMod2.tau); } - myMod->reg = detector_shm()->currentSettings; - myMod->eV = e_eV; - setModule(*myMod, tb); - deleteModule(myMod); + myMod.reg = detector_shm()->currentSettings; + myMod.eV = e_eV; + setModule(myMod, tb); if (getSettings() != is) { throw RuntimeError("setThresholdEnergyAndSettings: Could not set " "settings in detector"); @@ -3670,7 +3643,7 @@ int slsDetector::getChanRegs(double *retval) { return n; } -int slsDetector::setModule(sls_detector_module module, int tb) { +int slsDetector::setModule(sls_detector_module& module, int tb) { int fnum = F_SET_MODULE; int ret = FAIL; int retval = -1; @@ -4810,7 +4783,7 @@ int slsDetector::setDigitalIODelay(uint64_t pinMask, int delay) { return ret; } -sls_detector_module * +sls_detector_module slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, const int energy, const int e1, const int e2, int tb) { @@ -4821,10 +4794,7 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, "Interpolation of Trim values not implemented for this detector!"); } - sls_detector_module *myMod = createModule(detector_shm()->myDetectorType); - if (myMod == nullptr) { - throw RuntimeError("Could not create module"); - } + sls_detector_module myMod{detector_shm()->myDetectorType}; enum eiger_DacIndex { SVP, VTR, @@ -4849,10 +4819,9 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, int num_dacs_to_copy = sizeof(dacs_to_copy) / sizeof(dacs_to_copy[0]); for (int i = 0; i < num_dacs_to_copy; ++i) { if (a->dacs[dacs_to_copy[i]] != b->dacs[dacs_to_copy[i]]) { - deleteModule(myMod); - return nullptr; + throw RuntimeError("Interpolate module: dacs different"); } - myMod->dacs[dacs_to_copy[i]] = a->dacs[dacs_to_copy[i]]; + myMod.dacs[dacs_to_copy[i]] = a->dacs[dacs_to_copy[i]]; } // Copy irrelevant dacs (without failing): CAL @@ -4864,7 +4833,7 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, "Taking first: " << a->dacs[CAL]; } - myMod->dacs[CAL] = a->dacs[CAL]; + myMod.dacs[CAL] = a->dacs[CAL]; // Interpolate vrf, vcmp, vcp int dacs_to_interpolate[] = {VRF, VCMP_LL, VCMP_LR, VCMP_RL, @@ -4872,15 +4841,15 @@ slsDetector::interpolateTrim(sls_detector_module *a, sls_detector_module *b, int num_dacs_to_interpolate = sizeof(dacs_to_interpolate) / sizeof(dacs_to_interpolate[0]); for (int i = 0; i < num_dacs_to_interpolate; ++i) { - myMod->dacs[dacs_to_interpolate[i]] = + myMod.dacs[dacs_to_interpolate[i]] = linearInterpolation(energy, e1, e2, a->dacs[dacs_to_interpolate[i]], b->dacs[dacs_to_interpolate[i]]); } // Interpolate all trimbits if (tb != 0) { - for (int i = 0; i < myMod->nchan; ++i) { - myMod->chanregs[i] = linearInterpolation( + for (int i = 0; i < myMod.nchan; ++i) { + myMod.chanregs[i] = linearInterpolation( energy, e1, e2, a->chanregs[i], b->chanregs[i]); } } diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index 2062e9451..88ed58e23 100755 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -16,6 +16,7 @@ #include #ifdef __cplusplus #include "sls_detector_exceptions.h" +#include #include #include #endif @@ -1043,22 +1044,47 @@ typedef struct { sls_detector_module() : serialnumber(0), nchan(0), nchip(0), ndac(0), reg(0), iodelay(0), tau(0), eV(0), dacs(nullptr), chanregs(nullptr) {} + sls_detector_module(slsDetectorDefs::detectorType type) { detParameters parameters{type}; int nch = parameters.nChanX * parameters.nChanY; int nc = parameters.nChipX * parameters.nChipY; - int nd = parameters.nDacs; + // int nd = parameters.nDacs; - ndac = nd; + ndac = parameters.nDacs; nchip = nc; nchan = nch * nc; - dacs = new int[nd]; - chanregs = new int[nch * nc]; + dacs = new int[ndac]; + chanregs = new int[nchan]; + } + + sls_detector_module(const sls_detector_module &other) + : dacs(nullptr), chanregs(nullptr) { + *this = other; + } + + sls_detector_module &operator=(const sls_detector_module &other) { + delete[] dacs; + delete[] chanregs; + serialnumber = other.serialnumber; + nchan = other.nchan; + nchip = other.nchip; + ndac = other.ndac; + reg = other.reg; + iodelay = other.iodelay; + tau = other.tau; + eV = other.eV; + dacs = new int[ndac]; + std::copy(other.dacs, other.dacs + ndac, dacs); + chanregs = new int[nchan]; + std::copy(other.chanregs, other.chanregs + nchan, chanregs); + return *this; + } + + ~sls_detector_module() { + delete[] dacs; + delete[] chanregs; } - ~sls_detector_module(){ - delete[] dacs; - delete[] chanregs; - } }; #else } sls_detector_module; diff --git a/slsSupportLib/tests/test-sls_detector_defs.cpp b/slsSupportLib/tests/test-sls_detector_defs.cpp index 4d9ba20c9..9b745a29e 100644 --- a/slsSupportLib/tests/test-sls_detector_defs.cpp +++ b/slsSupportLib/tests/test-sls_detector_defs.cpp @@ -1,6 +1,6 @@ #include "catch.hpp" -#include "sls_detector_defs.h" #include "slsDetector.h" +#include "sls_detector_defs.h" using dt = slsDetectorDefs::detectorType; @@ -18,10 +18,10 @@ TEST_CASE("sls_detector_module default construction", "[support][new]") { CHECK(m.chanregs == nullptr); } -TEST_CASE("sls_detector_module from type", "[support][new]") { +TEST_CASE("sls_detector_module from type", "[support]") { sls_detector_module m(dt::EIGER); CHECK(m.serialnumber == 0); - CHECK(m.nchan == 256*256*4); + CHECK(m.nchan == 256 * 256 * 4); CHECK(m.nchip == 4); CHECK(m.ndac == 16); CHECK(m.reg == 0); @@ -30,4 +30,24 @@ TEST_CASE("sls_detector_module from type", "[support][new]") { CHECK(m.eV == 0); CHECK(m.dacs != nullptr); CHECK(m.chanregs != nullptr); +} + +TEST_CASE("assign module", "[support]") { + sls_detector_module m0; + sls_detector_module m1(dt::EIGER); + m1.serialnumber = 14; + m1.reg = 500; + m1.iodelay = 750; + + m0 = m1; // Assignment operator + CHECK(m0.serialnumber == 14); + CHECK(m0.reg == 500); + CHECK(m0.iodelay == 750); + CHECK(m0.nchan == 256 * 256 * 4); + + auto m3 = m1; // Copy constructor + CHECK(m3.serialnumber == 14); + CHECK(m3.reg == 500); + CHECK(m3.iodelay == 750); + CHECK(m3.nchan == 256 * 256 * 4); } \ No newline at end of file