Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#ifndef defaultModuleLabel_h
#define defaultModuleLabel_h

// C++ headers
#include <string>

// boost headers
#include <boost/version.hpp>
#if BOOST_VERSION < 105200
#include <boost/units/detail/utility.hpp>
#else
#include <boost/core/demangle.hpp>
#endif

template <typename T>
std::string defaultModuleLabel() {
  // start with the demangled name for T
#if BOOST_VERSION < 105200
  std::string name = boost::units::detail::demangle(typeid(T).name());
#else
  std::string name = boost::core::demangle(typeid(T).name());
#endif

  // expected size of the label
  unsigned int size = 0;
  for (char c : name)
    if (std::isalnum(c))
      ++size;
  std::string label;
  label.reserve(size);

  // tokenize the demangled name, keeping only alphanumeric characters,
  // and convert the tokens to lowerCamelCase.
  bool new_token = false;
  for (char c : name) {
    if (std::isalnum(c)) {
      if (new_token)
        label.push_back((char)std::toupper(c));
      else
        label.push_back(c);
      new_token = false;
    } else {
      new_token = true;
    }
  }

  // if the label is all uppercase, change it to all lowercase
  // if the label starts with more than one uppercase letter, change n-1 to lowercase
  // otherwise, change the first letter to lowercase
  unsigned int ups = 0;
  for (char c : label)
    if (std::isupper(c))
      ++ups;
    else
      break;
  if (ups > 1 and ups != label.size())
    --ups;
  for (unsigned int i = 0; i < ups; ++i)
    label[i] = std::tolower(label[i]);

  return label;
}

#endif  // defaultModuleLabel_h