unique_ptr in multiSlsDetectorClient

This commit is contained in:
Erik Frojdh
2019-01-16 14:27:08 +01:00
parent 901785b818
commit 0f688bb249

View File

@ -1,160 +1,116 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "container_utils.h"
#include "multiSlsDetector.h" #include "multiSlsDetector.h"
#include "multiSlsDetectorCommand.h" #include "multiSlsDetectorCommand.h"
#include "sls_detector_exceptions.h" #include "sls_detector_exceptions.h"
#include <cstdlib>
#include <memory>
#include <stdlib.h> 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: int id = -1, pos = -1, iv = 0;
multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector=NULL) { \ bool verify = true, update = true;
std::string answer; \ char cmd[100] = "";
// multiSlsDetectorCommand *myCmd;
int id = -1, pos = -1, iv = 0; \
bool verify = true, update = true; \
int del = 0; \
char cmd[100] = ""; \
if (action==slsDetectorDefs::PUT_ACTION && argc<2) { \ if (action == slsDetectorDefs::PUT_ACTION && argc < 2) {
std::cout << "Wrong usage - should be: "<< argv[0] << \ std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl;
"[id-][pos:]channel arg" << std::endl; \ std::cout << std::endl;
std::cout << std::endl; \ return;
return; \ };
}; \ if (action == slsDetectorDefs::GET_ACTION && argc < 1) {
if (action==slsDetectorDefs::GET_ACTION && argc<1) { \ std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl;
std::cout << "Wrong usage - should be: "<< argv[0] << \ std::cout << std::endl;
"[id-][pos:]channel arg" << std::endl; \ return;
std::cout << std::endl; \ };
return; \
}; \
if (action==slsDetectorDefs::READOUT_ACTION) { \ if (action == slsDetectorDefs::READOUT_ACTION) {
id = 0; \ id = 0;
pos = -1; \ pos = -1;
if (argc) { \ if (argc) { // multi id scanned
// multi id scanned if (strchr(argv[0], '-')) {
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
iv=sscanf(argv[0],"%d-%s",&id, cmd); \ if (iv >= 1 && id >= 0) {
//%s needn't be there (if not 1:), so 1 or 2 arguments scanned argv[0] = cmd;
if (iv >= 1 && id >= 0) { \ std::cout << id << "-";
argv[0] = cmd; \ } else {
std::cout << id << "-" ; \ id = 0;
} else { \ }
id = 0; \ } // single id scanned
} \ if (strchr(argv[0], ':')) {
} \ iv = sscanf(argv[0], "%d:", &pos);
// single id scanned if (iv == 1 && pos >= 0) {
if (strchr(argv[0],':')) { \ std::cout << "pos " << pos << " is not allowed for readout!" << std::endl;
iv=sscanf(argv[0],"%d:",&pos); \ return;
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) {
} else { \ argv[0] = cmd;
// multi id scanned std::cout << id << "-";
iv=sscanf(argv[0],"%d-%s",&id, cmd); \ } else {
// scan success id = 0;
if (iv == 2 && id >= 0) { \ } // sls pos scanned
argv[0] = cmd; \ iv = sscanf(argv[0], "%d:%s", &pos, cmd); // scan success
std::cout << id << "-" ; \ if (iv == 2 && pos >= 0) {
} else { \ argv[0] = cmd;
id = 0; \ std::cout << pos << ":";
} \ }
// sls pos scanned if (iv != 2) {
iv=sscanf(argv[0],"%d:%s", &pos, cmd); \ pos = -1;
// scan success } // remove the %d- and %d:
if (iv == 2 && pos >= 0) { \ if (!strlen(cmd)) {
argv[0] = cmd; \ strcpy(cmd, argv[0]);
std::cout << pos << ":" ; \ } // special commands
} \ std::string scmd = cmd; // free without calling multiSlsDetector constructor
if (iv != 2) { \ if (scmd == "free") {
pos = -1; \ multiSlsDetector::freeSharedMemory(id, pos);
} \ return;
// remove the %d- and %d: } // get user details without verify sharedMultiSlsDetector version
if (!strlen(cmd)) { \ else if ((scmd == "user") && (action == slsDetectorDefs::GET_ACTION)) {
strcpy(cmd, argv[0]); \ verify = false;
} \ update = false;
// special commands }
std::string scmd = cmd; \ }
// free without calling multiSlsDetector constructor //std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
if (scmd == "free") { \ // create multiSlsDetector class if required
multiSlsDetector::freeSharedMemory(id, pos); \ std::unique_ptr<multiSlsDetector> localDet;
return; \ if (myDetector == nullptr) {
} \ try {
// get user details without verify sharedMultiSlsDetector version localDet = sls::make_unique<multiSlsDetector>(id, verify, update);
else if ((scmd == "user") && (action==slsDetectorDefs::GET_ACTION)) { \ myDetector = localDet.get();
verify = false; \ } catch (const SlsDetectorPackageExceptions &e) {
update = false; \ /*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);
if (action != slsDetectorDefs::READOUT_ACTION) {
//std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl; std::cout << argv[0] << " ";
// create multiSlsDetector class if required }
if (myDetector==NULL) { \ std::cout << answer << std::endl;
try { \ };
multiSlsDetector* m = new multiSlsDetector(id, verify, update); \
myDetector = m; \
} catch (const SlsDetectorPackageExceptions & e) { \
/*std::cout << e.GetMessage() << std::endl;*/ \
return; \
} catch (...) { \
std::cout << " caught exception" << std::endl; \
return; \
} \
del=1; \
} \
if (pos >= 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; \
};
}; };