From 0f688bb249956ce17c0126c591119a6dd5d20cd8 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 16 Jan 2019 14:27:08 +0100 Subject: [PATCH] unique_ptr in multiSlsDetectorClient --- .../multiSlsDetector/multiSlsDetectorClient.h | 244 +++++++----------- 1 file changed, 100 insertions(+), 144 deletions(-) diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index ed546aa4b..a6f5c3d24 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -1,160 +1,116 @@ #include #include - +#include "container_utils.h" #include "multiSlsDetector.h" #include "multiSlsDetectorCommand.h" #include "sls_detector_exceptions.h" +#include +#include -#include - - -int dummyCallback(detectorData* d, int p,void*) { - std::cout << "got data " << p << std::endl; - return 0; +int dummyCallback(detectorData *d, int p, void *) { + std::cout << "got data " << p << std::endl; + return 0; }; -class multiSlsDetectorClient { +class multiSlsDetectorClient { + public: + multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr) { + std::string answer; -public: - multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector=NULL) { \ - std::string answer; \ - // multiSlsDetectorCommand *myCmd; - int id = -1, pos = -1, iv = 0; \ - bool verify = true, update = true; \ - int del = 0; \ - char cmd[100] = ""; \ + int id = -1, pos = -1, iv = 0; + bool verify = true, update = true; + char cmd[100] = ""; - if (action==slsDetectorDefs::PUT_ACTION && argc<2) { \ - std::cout << "Wrong usage - should be: "<< argv[0] << \ - "[id-][pos:]channel arg" << std::endl; \ - std::cout << std::endl; \ - return; \ - }; \ - if (action==slsDetectorDefs::GET_ACTION && argc<1) { \ - std::cout << "Wrong usage - should be: "<< argv[0] << \ - "[id-][pos:]channel arg" << std::endl; \ - std::cout << std::endl; \ - return; \ - }; \ + if (action == slsDetectorDefs::PUT_ACTION && argc < 2) { + std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl; + std::cout << std::endl; + return; + }; + if (action == slsDetectorDefs::GET_ACTION && argc < 1) { + std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl; + std::cout << std::endl; + return; + }; - if (action==slsDetectorDefs::READOUT_ACTION) { \ - id = 0; \ - pos = -1; \ - if (argc) { \ - // multi id scanned - if (strchr(argv[0],'-')) { \ - iv=sscanf(argv[0],"%d-%s",&id, cmd); \ - //%s needn't be there (if not 1:), so 1 or 2 arguments scanned - if (iv >= 1 && id >= 0) { \ - argv[0] = cmd; \ - std::cout << id << "-" ; \ - } else { \ - id = 0; \ - } \ - } \ - // single id scanned - if (strchr(argv[0],':')) { \ - iv=sscanf(argv[0],"%d:",&pos); \ - if (iv == 1 && pos >= 0) { \ - std::cout << "pos " << pos << " is not allowed for readout!" << std::endl; \ - return; \ - } \ - } \ - } \ - } else { \ - // multi id scanned - iv=sscanf(argv[0],"%d-%s",&id, cmd); \ - // scan success - if (iv == 2 && id >= 0) { \ - argv[0] = cmd; \ - std::cout << id << "-" ; \ - } else { \ - id = 0; \ - } \ - // sls pos scanned - iv=sscanf(argv[0],"%d:%s", &pos, cmd); \ - // scan success - if (iv == 2 && pos >= 0) { \ - argv[0] = cmd; \ - std::cout << pos << ":" ; \ - } \ - if (iv != 2) { \ - pos = -1; \ - } \ - // remove the %d- and %d: - if (!strlen(cmd)) { \ - strcpy(cmd, argv[0]); \ - } \ - // special commands - std::string scmd = cmd; \ - // free without calling multiSlsDetector constructor - if (scmd == "free") { \ - multiSlsDetector::freeSharedMemory(id, pos); \ - return; \ - } \ - // get user details without verify sharedMultiSlsDetector version - else if ((scmd == "user") && (action==slsDetectorDefs::GET_ACTION)) { \ - verify = false; \ - update = false; \ - } \ - } \ + if (action == slsDetectorDefs::READOUT_ACTION) { + id = 0; + pos = -1; + if (argc) { // multi id scanned + if (strchr(argv[0], '-')) { + iv = sscanf(argv[0], "%d-%s", &id, cmd); //%s needn't be there (if not 1:), so 1 or 2 arguments scanned + if (iv >= 1 && id >= 0) { + argv[0] = cmd; + std::cout << id << "-"; + } else { + id = 0; + } + } // single id scanned + if (strchr(argv[0], ':')) { + iv = sscanf(argv[0], "%d:", &pos); + if (iv == 1 && pos >= 0) { + std::cout << "pos " << pos << " is not allowed for readout!" << std::endl; + return; + } + } + } + } else { // multi id scanned + iv = sscanf(argv[0], "%d-%s", &id, cmd); // scan success + if (iv == 2 && id >= 0) { + argv[0] = cmd; + std::cout << id << "-"; + } else { + id = 0; + } // sls pos scanned + iv = sscanf(argv[0], "%d:%s", &pos, cmd); // scan success + if (iv == 2 && pos >= 0) { + argv[0] = cmd; + std::cout << pos << ":"; + } + if (iv != 2) { + pos = -1; + } // remove the %d- and %d: + if (!strlen(cmd)) { + strcpy(cmd, argv[0]); + } // special commands + std::string scmd = cmd; // free without calling multiSlsDetector constructor + if (scmd == "free") { + multiSlsDetector::freeSharedMemory(id, pos); + return; + } // get user details without verify sharedMultiSlsDetector version + else if ((scmd == "user") && (action == slsDetectorDefs::GET_ACTION)) { + verify = false; + update = false; + } + } + //std::cout<<"id:"< localDet; + if (myDetector == nullptr) { + try { + localDet = sls::make_unique(id, verify, update); + myDetector = localDet.get(); + } catch (const SlsDetectorPackageExceptions &e) { + /*std::cout << e.GetMessage() << std::endl;*/ + return; + } catch (...) { + std::cout << " caught exception" << std::endl; + return; + } + } + if (pos >= myDetector->getNumberOfDetectors()) { + std::cout << "position is out of bounds." << std::endl; + return; + } + // call multi detector command line + multiSlsDetectorCommand myCmd(myDetector); + answer = myCmd.executeLine(argc, argv, action, pos); - - //std::cout<<"id:"<= myDetector->getNumberOfDetectors()) { \ - std::cout << "position is out of bounds." << std::endl; \ - return; \ - } \ - - try { - // call multi detector command line - multiSlsDetectorCommand myCmd(myDetector); \ - answer=myCmd.executeLine(argc, argv, action, pos); \ - } catch (const SlsDetectorPackageExceptions & e) { \ - /*std::cout << e.GetMessage() << std::endl; */ \ - - if (del) delete myDetector; \ - return; \ - } catch (...) { \ - std::cout << " caught exception" << std::endl; \ - \ - if (del) delete myDetector; \ - return; \ - } \ - if (action!=slsDetectorDefs::READOUT_ACTION) { \ - std::cout << argv[0] << " " ; \ - } \ - std::cout << answer<< std::endl; \ - \ - if (del) delete myDetector; \ - }; - + if (action != slsDetectorDefs::READOUT_ACTION) { + std::cout << argv[0] << " "; + } + std::cout << answer << std::endl; + }; }; - - - - - - - - - - -