7#include <boost/algorithm/algorithm.hpp>
8#include <boost/format.hpp>
20 auto found = qualifiedName.find_last_of(
'/');
21 if(found == std::string::npos) {
24 return qualifiedName.substr(found + 1);
30 auto found = qualifiedName.find_last_of(
'/');
31 if(found == std::string::npos) {
34 return qualifiedName.substr(0, found);
40 constexpr std::string_view legalChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
45 std::string
escapeName(
const std::string& name,
bool allowDotsAndSlashes) {
46 std::string myLegalChars(legalChars);
49 assert(myLegalChars[myLegalChars.size() - 1] ==
'_');
50 myLegalChars.resize(myLegalChars.size() - 1);
52 if(allowDotsAndSlashes) {
56 std::string name_stripped;
59 while((i = name.find_first_not_of(myLegalChars, iLast)) != std::string::npos) {
60 name_stripped += name.substr(iLast, i - iLast);
61 name_stripped += (boost::format(
"_%03d") % int(name[i])).str();
64 name_stripped += name.substr(iLast);
75 while((i = name_stripped.find(
'_', iLast)) != std::string::npos) {
76 name += name_stripped.substr(iLast, i - iLast);
77 char code = char(std::stoi(name_stripped.substr(i + 1, 3)));
81 name += name_stripped.substr(iLast);
88 bool checkName(
const std::string& name,
bool allowDotsAndSlashes) {
94 std::string myLegalChars(legalChars);
96 if(allowDotsAndSlashes) {
100 return (name.find_first_not_of(myLegalChars) == std::string::npos);
106#if defined(__linux__)
107 pthread_setname_np(pthread_self(), name.substr(0, std::min<std::string::size_type>(name.length(), 15)).c_str());
108#elif defined(__APPLE__)
109 pthread_setname_np(name.substr(0, std::min<std::string::size_type>(name.length(), 15)).c_str());
118 if(isModule && name ==
"/") {
121 if(boost::ends_with(name,
"/")) {
122 throw ChimeraTK::logic_error(name +
": " + (isModule ?
"module" :
"variable") +
" names cannot end with /");
124 if(name.find(
"//") != std::string::npos) {
125 throw ChimeraTK::logic_error(name +
" variable names cannot contain consecutive slashes");
134 std::ifstream status_file(
"/proc/self/status");
136 while(std::getline(status_file, line)) {
137 if(line.substr(0, 10) ==
"TracerPid:") {
138 int tracer_pid = std::stoi(line.substr(10));
139 return tracer_pid != 0;
static constexpr std::string_view namePrefixConstant
Prefix for constants created by constant().
std::string raiseIftrailingSlash(const std::string &name, bool isModule)
Raises logic error if name ends in a slash, or if it contains consecutive slashes.
bool checkName(const std::string &name, bool allowDotsAndSlashes)
Check given name for characters which are not allowed in variable or module names.
std::string escapeName(const std::string &name, bool allowDotsAndSlashes)
Convert all characters which are not allowed in variable or module names into underscores followed by...
std::string getPathName(const std::string &qualifiedName)
Return all but the last components of the given qualified name.
void setThreadName(const std::string &name)
Set name of the current thread.
std::string getUnqualifiedName(const std::string &qualifiedName)
Return the last component of the given qualified path name.
bool isBeingDebugged()
Checks whether the current process is being debugged.
std::string unescapeName(const std::string &name_stripped)
Undo the escaping done by escapeName().