宏获取当前命名空间和函数名称(但不是完整签名)?

Fra*_*ank 14 c++ macros boost

是否有一个C++宏获取当前的命名空间和函数名称?例:

namespace foo {
  namespace bar {
    void baz(int i, double d) {
      std::cout << MACRO << std::endl;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

会打印foo::bar::baz.我知道,__FUNCTION__但它没有给出命名空间.并BOOST_CURRENT_FUNCTION给出了整个签名,包括.参数和返回类型:

void foo::bar::baz(int, double)
Run Code Online (Sandbox Code Playgroud)

也许,是否可以编写一个从中提取命名空间和函数名称的宏BOOST_CURRENT_FUNCTION

我想将其用于日志记录,以获取日志字符串

foo::bar::baz -- blah logging message blah
Run Code Online (Sandbox Code Playgroud)

Mat*_* M. 5

据我所知,这是不可能的(不可移植).但是,从完整签名中,您可以提取这些参数.当然它需要解析所述签名......这不是那么容易:x

这是我目前使用的功能:

// What we want to consume:
//  void
//  signed short
//  unsigned int
//  Test::Bar<T, N>
//
static char const* consumeType(char const* const begin, char const* const end){
  static StringRef const Signed("signed");
  static StringRef const Unsigned("unsigned");

  char const* it = begin;

  if (startsWith(it, Signed)) { it += Signed.size() + 1; }
  else if (startsWith(it, Unsigned)) { it += Unsigned.size() + 1; }

  // jump over the return type
  size_t templateNest = 0;
  while (it != end) {
    if (*it == ' ' and templateNest == 0) { break; }
    if (*it == '<') { ++templateNest; }
    if (*it == '>' and templateNest > 0) { --templateNest; }

    ++it;
  }

  return it;
} // consumeType

//
// \param signature: signature as returned by __func___ on gcc
// \return: full name, included namespace qualifier and class (if any)
//
// void Test::Bar<T, N>::parameterized(U) const
//   [with unsigned int O = 4u, U = Test::Foo,
//    T = Test::Foo, unsigned int N = 3u]
//    -> Test::Bar<T, N>::parameterized
//
StringRef parseFunctionName(StringRef const signature) {
  char const* begin = signature.begin();
  char const* end = signature.end();

  // Jump over the return type
  begin = consumeType(begin, end);
  if (begin == end) { return signature; }

  // skip the space right after the return type
  ++begin;
  if (begin == end) { return signature; }

  // if we encounter a '(' then it means that we return a function,
  // and we once again need to jump over the return type
  if (*begin == '(') {
    begin = consumeType(++begin, end);

    // skip the space
    ++begin;
    if (begin == end) { return signature; }
  }

  // and finally, we got the beginning, and we need to get the end, which is
  // the first opening '('
  char const* e = std::find(begin, end, '(');
  return StringRef(begin, e - begin);
} // parseFunctionName
Run Code Online (Sandbox Code Playgroud)

它的伴随测试:

#define UT_FUNCTION_CHECK(Signature_, Name_) \
  UT_CHECK(parseFunctionName(StringRef(Signature_)) == Name_);

void Function() {
  // Regular functions
  UT_FUNCTION_CHECK("int main()", "main")
  UT_FUNCTION_CHECK("int foo(int, double)", "foo")
  UT_FUNCTION_CHECK("unsigned int foo(int, double)", "foo")

  // Templates
  UT_FUNCTION_CHECK("unsigned int Test::Bar<T, N>::print() const"
                    " [with T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::print")
  UT_FUNCTION_CHECK("Test::Bar<T, N> Test::Bar<T, N>::print() const"
                    " [with T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::print")
  UT_FUNCTION_CHECK("void Test::Bar<T, N>::parameterized(U) const"
                    " [with unsigned int O = 4u, U = Test::Foo,"
                    " T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::parameterized")

  // Functions returning functions
  UT_FUNCTION_CHECK("void (* Test::Foo::func() const)()",
                    "Test::Foo::func")
  UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::method() const)(int)volatile",
                    "Test::Foo::method")
  UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::super())"
                    "(void (Test::Foo::*)(int)volatile)const",
                    "Test::Foo::super")
  } // Function
Run Code Online (Sandbox Code Playgroud)

它与gcc的__func__宏结合使用.

StringRef堂课类似于llvm::StringRef.

我认为如果你能负担得起额外的解析,它应该满足你的需求.这是非常快的:没有回溯和没有动态分配,所以不应该是一个问题(特别是与写入文件相比......).

希望能帮助到你.