C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 宏来获取当前的命名空间和函数名(但不是全签名)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有获取当前命名空间和函数名称的C宏?例:
namespace foo {
  namespace bar {
    void baz(int i,double d) {
      std::cout << MACRO << std::endl;
    }
  }
}

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

void foo::bar::baz(int,doublE)

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

我想要用于记录目的,获取一个日志字符串

foo::bar::baz -- blah logging message blah

解决方法

据我所知,这是不可能的(不可移植的).但是从完整的签名可以提取这些参数.当然,它需要解析所述签名,这不是那么容易: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

及其伴随测试:

#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,"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")
  UT_FUNCTION_checK("void Test::Bar<T,N>::parameterized(U) const"
                    " [with unsigned int O = 4u,"
                    " T = Test::Foo,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)(@R_450_10185@volatile","Test::Foo::method")
  UT_FUNCTION_checK("void (Test::Foo::* Test::Foo::super())"
                    "(void (Test::Foo::*)(@R_450_10185@volatilE)const","Test::Foo::super")
  } // Function

它与gcc的__func__宏结合使用.

StringRef类与llvm::StringRef类似.

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

希望它有帮助.

大佬总结

以上是大佬教程为你收集整理的c – 宏来获取当前的命名空间和函数名(但不是全签名)?全部内容,希望文章能够帮你解决c – 宏来获取当前的命名空间和函数名(但不是全签名)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。