Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:43

0001 #ifndef defaultModuleLabel_h
0002 #define defaultModuleLabel_h
0003 
0004 // C++ headers
0005 #include <string>
0006 
0007 // boost headers
0008 #include <boost/version.hpp>
0009 #if BOOST_VERSION < 105200
0010 #include <boost/units/detail/utility.hpp>
0011 #else
0012 #include <boost/core/demangle.hpp>
0013 #endif
0014 
0015 template <typename T>
0016 std::string defaultModuleLabel() {
0017   // start with the demangled name for T
0018 #if BOOST_VERSION < 105200
0019   std::string name = boost::units::detail::demangle(typeid(T).name());
0020 #else
0021   std::string name = boost::core::demangle(typeid(T).name());
0022 #endif
0023 
0024   // expected size of the label
0025   unsigned int size = 0;
0026   for (char c : name)
0027     if (std::isalnum(c))
0028       ++size;
0029   std::string label;
0030   label.reserve(size);
0031 
0032   // tokenize the demangled name, keeping only alphanumeric characters,
0033   // and convert the tokens to lowerCamelCase.
0034   bool new_token = false;
0035   for (char c : name) {
0036     if (std::isalnum(c)) {
0037       if (new_token)
0038         label.push_back((char)std::toupper(c));
0039       else
0040         label.push_back(c);
0041       new_token = false;
0042     } else {
0043       new_token = true;
0044     }
0045   }
0046 
0047   // if the label is all uppercase, change it to all lowercase
0048   // if the label starts with more than one uppercase letter, change n-1 to lowercase
0049   // otherwise, change the first letter to lowercase
0050   unsigned int ups = 0;
0051   for (char c : label)
0052     if (std::isupper(c))
0053       ++ups;
0054     else
0055       break;
0056   if (ups > 1 and ups != label.size())
0057     --ups;
0058   for (unsigned int i = 0; i < ups; ++i)
0059     label[i] = std::tolower(label[i]);
0060 
0061   return label;
0062 }
0063 
0064 #endif  // defaultModuleLabel_h