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 <string>
#include "container_utils.h"
#include "multiSlsDetector.h"
#include "multiSlsDetectorCommand.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:
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:"<<id<<" pos:"<<pos<<std::endl;
// create multiSlsDetector class if required
std::unique_ptr<multiSlsDetector> localDet;
if (myDetector == nullptr) {
try {
localDet = sls::make_unique<multiSlsDetector>(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:"<<id<<" pos:"<<pos<<std::endl;
// create multiSlsDetector class if required
if (myDetector==NULL) { \
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; \
};
if (action != slsDetectorDefs::READOUT_ACTION) {
std::cout << argv[0] << " ";
}
std::cout << answer << std::endl;
};
};