mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-11-18 17:24:04 +01:00
Merge branch 'fix/color_bar_pyctbgui' of github.com:slsdetectorgroup/slsDetectorPackage into fix/color_bar_pyctbgui
This commit is contained in:
commit
37da7e3d2d
15 files changed
+204
-119
No files matched your search
@@ -3,6 +3,7 @@
|
||||
#include "helpers/Helpers.hpp"
|
||||
#include "sls/logger.h"
|
||||
#include "sls/sls_detector_exceptions.h"
|
||||
#include "sls/thread_utils.h"
|
||||
#include "sls/versionAPI.h"
|
||||
#include <semaphore.h>
|
||||
|
||||
@@ -12,20 +13,6 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if defined(__APPLE__)
|
||||
#include <cstdint>
|
||||
#include <pthread.h>
|
||||
static inline uint64_t gettid() {
|
||||
uint64_t tid = 0;
|
||||
pthread_threadid_np(nullptr, &tid);
|
||||
return tid;
|
||||
}
|
||||
#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
using namespace sls;
|
||||
|
||||
pid_t pid = -1;
|
||||
@@ -90,7 +77,7 @@ int main(int argc, char *argv[]) {
|
||||
// also return EXIT_FAILURE
|
||||
}
|
||||
LOG(TLogLevel::logINFOBLUE)
|
||||
<< "Exiting Stop Server [ Tid: " << gettid() << " ]";
|
||||
<< "Exiting Stop Server [ Tid: " << getThreadId() << " ]";
|
||||
LOG(sls::logINFO) << "Exiting Stop Server";
|
||||
} else if (pid > 0) {
|
||||
// parent
|
||||
@@ -108,7 +95,7 @@ int main(int argc, char *argv[]) {
|
||||
} catch (...) {
|
||||
LOG(logERROR) << "Some Error occured in Control Server, exiting";
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Control Server [ Tid: " << gettid() << " ]";
|
||||
<< "Exiting Control Server [ Tid: " << getThreadId() << " ]";
|
||||
LOG(sls::logINFO) << "Exiting Detector Server";
|
||||
kill(pid, SIGINT); // tell child to exit
|
||||
waitpid(pid, nullptr, 0); // wait for child to exit
|
||||
@@ -116,7 +103,8 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
waitpid(pid, nullptr, 0); // wait for child to exit
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Detector Control Server [ Tid: " << gettid() << " ]";
|
||||
<< "Exiting Detector Control Server [ Tid: " << getThreadId()
|
||||
<< " ]";
|
||||
LOG(sls::logINFO) << "Exiting Detector Server";
|
||||
} else {
|
||||
LOG(sls::logERROR)
|
||||
|
||||
@@ -424,41 +424,43 @@ std::string Caller::threshold(int action) {
|
||||
throw RuntimeError("Not implemented for this detector\n");
|
||||
}
|
||||
} else if (action == defs::PUT_ACTION) {
|
||||
if (args.empty()) {
|
||||
WrongNumberOfParameters(1);
|
||||
}
|
||||
defs::detectorType type = det->getDetectorType().squash();
|
||||
if (type == defs::EIGER && args.size() != 1 && args.size() != 2) {
|
||||
WrongNumberOfParameters(1);
|
||||
}
|
||||
if (type == defs::MYTHEN3 && (args.size() < 1 || args.size() > 4)) {
|
||||
WrongNumberOfParameters(1);
|
||||
}
|
||||
|
||||
bool trimbits = (cmd == "thresholdnotb") ? false : true;
|
||||
std::array<int, 3> energy = {StringTo<int>(args[0]), 0, 0};
|
||||
energy[1] = energy[0];
|
||||
energy[2] = energy[0];
|
||||
defs::detectorSettings sett = defs::STANDARD;
|
||||
|
||||
// check if argument has settings or get it
|
||||
if (args.size() == 2 || args.size() == 4) {
|
||||
sett = StringTo<defs::detectorSettings>(args[args.size() - 1]);
|
||||
// settings is optional, last and the only argument starting with a
|
||||
// letter. The energies before it accept "a b c" as well as "a,b,c"
|
||||
auto energyArgs = args;
|
||||
defs::detectorSettings sett = defs::STANDARD;
|
||||
if (std::isalpha(static_cast<unsigned char>(args.back()[0]))) {
|
||||
sett = StringTo<defs::detectorSettings>(args.back());
|
||||
energyArgs.pop_back();
|
||||
} else {
|
||||
sett = det->getSettings(std::vector<int>{det_id})
|
||||
.tsquash("Inconsistent settings between detectors");
|
||||
}
|
||||
auto energy = StringTo<std::vector<int>>(join(energyArgs, ','));
|
||||
|
||||
// get other threshold values
|
||||
if (args.size() > 2) {
|
||||
energy[1] = StringTo<int>(args[1]);
|
||||
energy[2] = StringTo<int>(args[2]);
|
||||
}
|
||||
switch (type) {
|
||||
case defs::EIGER:
|
||||
if (energy.size() != 1) {
|
||||
throw RuntimeError("Expected one threshold energy");
|
||||
}
|
||||
det->setThresholdEnergy(energy[0], sett, trimbits,
|
||||
std::vector<int>{det_id});
|
||||
break;
|
||||
case defs::MYTHEN3:
|
||||
det->setThresholdEnergy(energy, sett, trimbits,
|
||||
std::vector<int>{det_id});
|
||||
// one energy applies to all three counters
|
||||
if (energy.size() == 1) {
|
||||
energy.resize(3, energy[0]);
|
||||
}
|
||||
if (energy.size() != 3) {
|
||||
throw RuntimeError("Expected one or three threshold energies");
|
||||
}
|
||||
det->setThresholdEnergy({energy[0], energy[1], energy[2]}, sett,
|
||||
trimbits, std::vector<int>{det_id});
|
||||
break;
|
||||
default:
|
||||
throw RuntimeError("Not implemented for this detector\n");
|
||||
@@ -485,19 +487,16 @@ std::string Caller::trimen(int action) {
|
||||
auto t = det->getTrimEnergies(std::vector<int>{det_id});
|
||||
os << OutString(t) << '\n';
|
||||
} else if (action == defs::PUT_ACTION) {
|
||||
std::vector<int> t(args.size());
|
||||
if (!args.empty()) {
|
||||
for (size_t i = 0; i < t.size(); ++i) {
|
||||
t[i] = StringTo<int>(args[i]);
|
||||
}
|
||||
}
|
||||
// accepts both "4500 5400" and "4500,5400", empty list clears
|
||||
auto t = StringTo<std::vector<int>>(join(args, ','));
|
||||
det->setTrimEnergies(t, std::vector<int>{det_id});
|
||||
os << ToString(args) << '\n';
|
||||
os << ToString(t) << '\n';
|
||||
} else {
|
||||
throw RuntimeError("Unknown action");
|
||||
}
|
||||
return os.str();
|
||||
}
|
||||
|
||||
std::string Caller::badchannels(int action) {
|
||||
std::ostringstream os;
|
||||
if (action == defs::HELP_ACTION) {
|
||||
@@ -1013,6 +1012,9 @@ std::string Caller::counters(int action) {
|
||||
"enabled. Each element in list can be 0 - 2 and must be non "
|
||||
"repetitive. Enabling counters sets vth dacs to remembered "
|
||||
"values and disabling sets them to disabled values."
|
||||
"Accepts both space separated and comma separated list of indices"
|
||||
"optionally enclosed in brackets. Example: 0 1 2 or 0,1,2 or "
|
||||
"[0,1,2] or [0 1 2]"
|
||||
<< '\n';
|
||||
} else if (action == defs::GET_ACTION) {
|
||||
if (!args.empty()) {
|
||||
@@ -1024,15 +1026,15 @@ std::string Caller::counters(int action) {
|
||||
if (args.empty()) {
|
||||
WrongNumberOfParameters(1);
|
||||
}
|
||||
if (std::any_of(args.cbegin(), args.cend(), [](std::string s) {
|
||||
return (StringTo<int>(s) < 0 || StringTo<int>(s) > 2);
|
||||
})) {
|
||||
// accepts both "0 1 2" and "0,1,2"
|
||||
auto counters = StringTo<std::vector<int>>(join(args, ','));
|
||||
if (std::any_of(counters.cbegin(), counters.cend(),
|
||||
[](int val) { return (val < 0 || val > 2); })) {
|
||||
throw RuntimeError("Invalid counter indices list. Example: 0 1 2");
|
||||
}
|
||||
// convert vector to counter enable mask
|
||||
uint32_t mask = 0;
|
||||
for (size_t i = 0; i < args.size(); ++i) {
|
||||
int val = StringTo<int>(args[i]);
|
||||
for (auto val : counters) {
|
||||
// already enabled earlier
|
||||
if (mask & (1 << val)) {
|
||||
std::ostringstream oss;
|
||||
@@ -1042,7 +1044,7 @@ std::string Caller::counters(int action) {
|
||||
mask |= (1 << val);
|
||||
}
|
||||
det->setCounterMask(mask, std::vector<int>{det_id});
|
||||
os << ToString(args) << '\n';
|
||||
os << ToString(counters) << '\n';
|
||||
} else {
|
||||
throw RuntimeError("Unknown action");
|
||||
}
|
||||
@@ -1216,6 +1218,9 @@ std::string Caller::rx_dbitlist(int action) {
|
||||
"receiver list, the data size will be bigger if the number of "
|
||||
"samples is not divisible by 8 as every signal bit is padded to "
|
||||
"the next byte when combining all the samples in the receiver."
|
||||
"Accepts both space separated and comma separated list of indices"
|
||||
"optionally enclosed in brackets. Example: 0 11 37 or 3,10,15 or "
|
||||
"[62,21] or [0 1 2]"
|
||||
<< '\n';
|
||||
} else if (action == defs::GET_ACTION) {
|
||||
if (!args.empty()) {
|
||||
@@ -1236,14 +1241,10 @@ std::string Caller::rx_dbitlist(int action) {
|
||||
}
|
||||
// 'none' option already covered as t is empty by default
|
||||
else if (args[0] != "none") {
|
||||
unsigned int ntrim = args.size();
|
||||
t.resize(ntrim);
|
||||
for (unsigned int i = 0; i < ntrim; ++i) {
|
||||
t[i] = StringTo<int>(args[i]);
|
||||
}
|
||||
t = StringTo<std::vector<int>>(join(args, ','));
|
||||
}
|
||||
det->setRxDbitList(t, std::vector<int>{det_id});
|
||||
os << ToString(args) << '\n';
|
||||
os << ToString(t) << '\n';
|
||||
} else {
|
||||
throw RuntimeError("Unknown action");
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
||||
|
||||
#include "Arping.h"
|
||||
#include "sls/thread_utils.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <signal.h>
|
||||
@@ -11,12 +12,6 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
void func(int signum) { wait(NULL); }
|
||||
|
||||
Arping::Arping() {}
|
||||
@@ -59,7 +54,7 @@ void Arping::StartProcess() {
|
||||
childPid = fork();
|
||||
// child process
|
||||
if (childPid == 0) {
|
||||
LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << gettid()
|
||||
LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << getThreadId()
|
||||
<< " ]";
|
||||
ProcessExecution();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "sls/sls_detector_exceptions.h"
|
||||
#include "sls/string_utils.h"
|
||||
#include "sls/thread_utils.h"
|
||||
#include "sls/versionAPI.h"
|
||||
|
||||
#include <array>
|
||||
@@ -27,12 +28,6 @@ namespace sls {
|
||||
using ns = std::chrono::nanoseconds;
|
||||
using Interface = ServerInterface;
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
std::mutex ClientInterface::callbackMutex;
|
||||
|
||||
ClientInterface::~ClientInterface() {
|
||||
@@ -47,7 +42,7 @@ ClientInterface::ClientInterface(uint16_t portNumber)
|
||||
: detType(GENERIC), portNumber(portNumber), server(portNumber) {
|
||||
validatePortNumber(portNumber);
|
||||
functionTable();
|
||||
parentThreadId = gettid();
|
||||
parentThreadId = getThreadId();
|
||||
tcpThread =
|
||||
make_unique<std::thread>(&ClientInterface::startTCPServer, this);
|
||||
}
|
||||
@@ -79,7 +74,7 @@ void ClientInterface::registerCallBackRawDataReady(
|
||||
}
|
||||
|
||||
void ClientInterface::startTCPServer() {
|
||||
tcpThreadId = gettid();
|
||||
tcpThreadId = getThreadId();
|
||||
LOG(logINFOBLUE) << "Created [ TCP server Tid: " << tcpThreadId << "]";
|
||||
LOG(logINFO) << "SLS Receiver starting TCP Server on port " << portNumber
|
||||
<< '\n';
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/network_utils.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/thread_utils.h"
|
||||
|
||||
#include <csignal> //SIGINT
|
||||
#include <cstdio>
|
||||
@@ -31,12 +32,6 @@
|
||||
std::vector<std::thread> threads;
|
||||
sls::TLogLevel printHeadersLevel = sls::logDEBUG;
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
/** Define Colors to print data call back in different colors for different
|
||||
* recievers */
|
||||
#define PRINT_IN_COLOR(c, f, ...) \
|
||||
@@ -522,7 +517,8 @@ int main(int argc, char *argv[]) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
|
||||
<< ']';
|
||||
|
||||
// close files on ctrl+c
|
||||
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
|
||||
@@ -548,8 +544,8 @@ int main(int argc, char *argv[]) {
|
||||
sem_t *semaphore = &semaphores[i];
|
||||
threads.emplace_back(
|
||||
[i, semaphore, port, user_data, &threadException]() {
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Thread " << i << " [ Tid: " << gettid() << ']';
|
||||
LOG(sls::logINFOBLUE) << "Thread " << i << " [ Tid: "
|
||||
<< sls::getThreadId() << ']';
|
||||
try {
|
||||
sls::Receiver receiver(port);
|
||||
receiver.registerCallBackStartAcquisition(
|
||||
@@ -567,8 +563,8 @@ int main(int argc, char *argv[]) {
|
||||
threadException = std::current_exception();
|
||||
raise(SIGINT);
|
||||
}
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Thread " << i << " [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFOBLUE) << "Exiting Thread " << i << " [ Tid: "
|
||||
<< sls::getThreadId() << " ]";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/network_utils.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/thread_utils.h"
|
||||
|
||||
#include <csignal> //SIGINT
|
||||
#include <cstring>
|
||||
@@ -16,12 +17,6 @@
|
||||
#include <sys/wait.h> //wait
|
||||
#include <unistd.h>
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
/** Define Colors to print data call back in different colors for different
|
||||
* recievers */
|
||||
#define PRINT_IN_COLOR(c, f, ...) \
|
||||
@@ -156,7 +151,8 @@ int main(int argc, char *argv[]) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
|
||||
<< ']';
|
||||
|
||||
// close files on ctrl+c
|
||||
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
|
||||
@@ -182,7 +178,8 @@ int main(int argc, char *argv[]) {
|
||||
/** - if child process */
|
||||
else if (pid == 0) {
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Child process " << i << " [ Tid: " << gettid() << ']';
|
||||
<< "Child process " << i << " [ Tid: " << sls::getThreadId()
|
||||
<< ']';
|
||||
|
||||
try {
|
||||
uint16_t port = m.port + i;
|
||||
@@ -213,13 +210,13 @@ int main(int argc, char *argv[]) {
|
||||
// each child process gets a copy of the semaphore
|
||||
sem_wait(&semaphore);
|
||||
sem_destroy(&semaphore);
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Child Process [ Tid: " << gettid() << ']';
|
||||
LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: "
|
||||
<< sls::getThreadId() << ']';
|
||||
exit(EXIT_SUCCESS);
|
||||
} catch (...) {
|
||||
sem_destroy(&semaphore);
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting Child Process [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: "
|
||||
<< sls::getThreadId() << " ]";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,6 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
Receiver::~Receiver() = default;
|
||||
|
||||
Receiver::Receiver(uint16_t port) {
|
||||
|
||||
@@ -8,17 +8,12 @@
|
||||
#include "sls/logger.h"
|
||||
#include "sls/network_utils.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/thread_utils.h"
|
||||
|
||||
#include <csignal> //SIGINT
|
||||
#include <semaphore.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
sem_t semaphore;
|
||||
|
||||
/**
|
||||
@@ -44,7 +39,8 @@ int main(int argc, char *argv[]) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
|
||||
<< " ]";
|
||||
|
||||
// close files on ctrl+c
|
||||
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
|
||||
@@ -60,10 +56,11 @@ int main(int argc, char *argv[]) {
|
||||
sem_destroy(&semaphore);
|
||||
} catch (...) {
|
||||
sem_destroy(&semaphore);
|
||||
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFOBLUE)
|
||||
<< "Exiting [ Tid: " << sls::getThreadId() << " ]";
|
||||
throw;
|
||||
}
|
||||
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
|
||||
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << sls::getThreadId() << " ]";
|
||||
LOG(sls::logINFO) << "Exiting Receiver";
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -7,17 +7,12 @@
|
||||
|
||||
#include "ThreadObject.h"
|
||||
#include "sls/container_utils.h"
|
||||
#include "sls/thread_utils.h"
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace sls {
|
||||
|
||||
// gettid added in glibc 2.30
|
||||
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
|
||||
#include <sys/syscall.h>
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
#endif
|
||||
|
||||
ThreadObject::ThreadObject(int index, std::string type)
|
||||
: index(index), type(type) {
|
||||
LOG(logDEBUG) << type << " thread created: " << index;
|
||||
@@ -46,7 +41,7 @@ void ThreadObject::StartRunning() { runningFlag = true; }
|
||||
void ThreadObject::StopRunning() { runningFlag = false; }
|
||||
|
||||
void ThreadObject::RunningThread() {
|
||||
threadId = gettid();
|
||||
threadId = getThreadId();
|
||||
LOG(logINFOBLUE) << "Created [ " << type << "Thread " << index
|
||||
<< ", Tid: " << threadId << "]";
|
||||
while (!killThread) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-other
|
||||
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file ToString.h
|
||||
*
|
||||
@@ -353,7 +352,9 @@ T StringTo(const std::string &f, const std::string &unit) {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> T StringTo(const std::string &t) {
|
||||
template <typename T,
|
||||
std::enable_if_t<!is_container<T>::value, int> = 0>
|
||||
T StringTo(const std::string &t) {
|
||||
std::string tmp{t};
|
||||
auto unit = RemoveUnit(tmp);
|
||||
return StringTo<T>(tmp, unit);
|
||||
@@ -398,6 +399,7 @@ ToString(const T &obj) {
|
||||
return obj.str();
|
||||
}
|
||||
|
||||
/** Convert vector of strings to vector of type T */
|
||||
template <typename T>
|
||||
std::vector<T> StringTo(const std::vector<std::string> &strings) {
|
||||
std::vector<T> result;
|
||||
@@ -407,4 +409,53 @@ std::vector<T> StringTo(const std::vector<std::string> &strings) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Parse comma-separated string into vector type T (e.g.,
|
||||
* StringTo<std::vector<int>>()) */
|
||||
template <typename T, std::enable_if_t<is_container<T>::value &&
|
||||
!std::is_same_v<T, std::string>,
|
||||
int> = 0>
|
||||
T StringTo(const std::string &s) {
|
||||
using ElementType = typename T::value_type;
|
||||
// Enclosing brackets are optional but must match. Nested pairs are
|
||||
// accepted since the caller may already have wrapped the list once.
|
||||
std::string list = s;
|
||||
auto strip = [](std::string &str) {
|
||||
auto first = str.find_first_not_of(' ');
|
||||
if (first == std::string::npos) {
|
||||
str.clear();
|
||||
return;
|
||||
}
|
||||
str = str.substr(first, str.find_last_not_of(' ') - first + 1);
|
||||
};
|
||||
strip(list);
|
||||
while (list.size() >= 2 && list.front() == '[' && list.back() == ']') {
|
||||
list = list.substr(1, list.size() - 2);
|
||||
strip(list);
|
||||
}
|
||||
if (list.find_first_of("[]") != std::string::npos) {
|
||||
throw RuntimeError("Mismatched brackets in list '" + s + "'");
|
||||
}
|
||||
|
||||
T res;
|
||||
std::istringstream ss(list);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ',')) {
|
||||
item.erase(std::remove_if(item.begin(), item.end(),
|
||||
[](char c) { return c == '"' || c == ' '; }),
|
||||
item.end());
|
||||
|
||||
try {
|
||||
if (item.empty())
|
||||
continue;
|
||||
auto val = StringTo<ElementType>(item);
|
||||
res.push_back(val);
|
||||
} catch (const std::exception &e) {
|
||||
throw RuntimeError("Could not convert '" + item +
|
||||
"' to the container element type. " +
|
||||
std::string(e.what()));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -85,6 +85,9 @@ implementation should not be used in a performance critical place.
|
||||
*/
|
||||
std::vector<std::string> split(const std::string &strToSplit, char delimeter);
|
||||
|
||||
/** Join strings with the specified delimiter, e.g. {"a", "b"} -> "a,b" */
|
||||
std::string join(const std::vector<std::string> &strings, char delimeter);
|
||||
|
||||
std::string RemoveUnit(std::string &str);
|
||||
|
||||
bool is_int(const std::string &s);
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-other
|
||||
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file thread_utils.h
|
||||
* @short id of the calling thread
|
||||
*/
|
||||
|
||||
#include <sys/types.h> // pid_t
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <cstdint>
|
||||
#include <pthread.h>
|
||||
#elif defined(__linux__)
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#error "getThreadId is not implemented for this platform"
|
||||
#endif
|
||||
|
||||
namespace sls {
|
||||
|
||||
/** Kernel thread id of the calling thread. On macOS the 64-bit id is
|
||||
* truncated to pid_t. */
|
||||
inline pid_t getThreadId() noexcept {
|
||||
#if defined(__APPLE__)
|
||||
std::uint64_t tid = 0;
|
||||
pthread_threadid_np(nullptr, &tid);
|
||||
return static_cast<pid_t>(tid);
|
||||
#else
|
||||
return static_cast<pid_t>(::syscall(SYS_gettid));
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -24,6 +24,16 @@ std::vector<std::string> split(const std::string &strToSplit, char delimeter) {
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
std::string join(const std::vector<std::string> &strings, char delimeter) {
|
||||
std::string joined;
|
||||
for (const auto &s : strings) {
|
||||
if (!joined.empty())
|
||||
joined += delimeter;
|
||||
joined += s;
|
||||
}
|
||||
return joined;
|
||||
}
|
||||
|
||||
std::string RemoveUnit(std::string &str) {
|
||||
auto it = str.begin();
|
||||
while (it != str.end()) {
|
||||
|
||||
@@ -415,4 +415,24 @@ TEST_CASE("string to timingInfoDecoder") {
|
||||
defs::timingInfoDecoder::SHINE);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Convert string to vector of ints") {
|
||||
REQUIRE(StringTo<std::vector<int>>("1, 2, 3") == std::vector<int>{1, 2, 3});
|
||||
REQUIRE(StringTo<std::vector<int>>(" 4 ,5,6 ") == std::vector<int>{4, 5, 6});
|
||||
REQUIRE(StringTo<std::vector<int>>("[8]") == std::vector<int>{8});
|
||||
REQUIRE(StringTo<std::vector<int>>("9, ") == std::vector<int>{9});
|
||||
REQUIRE(StringTo<std::vector<int>>("") == std::vector<int>{});
|
||||
REQUIRE(StringTo<std::vector<int>>("[]") == std::vector<int>{});
|
||||
REQUIRE(StringTo<std::vector<int>>(" [ [1, 2] ] ") ==
|
||||
std::vector<int>{1, 2});
|
||||
}
|
||||
|
||||
TEST_CASE("Mismatched brackets in string to vector throws") {
|
||||
REQUIRE_THROWS(StringTo<std::vector<int>>("[1, 2"));
|
||||
REQUIRE_THROWS(StringTo<std::vector<int>>("1, 2]"));
|
||||
REQUIRE_THROWS(StringTo<std::vector<int>>("[[1, 2]"));
|
||||
REQUIRE_THROWS(StringTo<std::vector<int>>("1]2"));
|
||||
REQUIRE_THROWS(StringTo<std::vector<int>>("[1], [2]"));
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
@@ -52,6 +52,13 @@ TEST_CASE("split a string without end delimiter") {
|
||||
REQUIRE(r[2] == "filibom");
|
||||
}
|
||||
|
||||
TEST_CASE("join strings") {
|
||||
REQUIRE(join({"abra", "kadabra", "filibom"}, '+') ==
|
||||
"abra+kadabra+filibom");
|
||||
REQUIRE(join({"abra"}, '+') == "abra");
|
||||
REQUIRE(join({}, '+') == "");
|
||||
}
|
||||
|
||||
TEST_CASE("Remove char from string") {
|
||||
char str[] = "sometest";
|
||||
removeChar(str, 'e');
|
||||
|
||||
Reference in new issue
Block a user