WebRTCWebRTC源码分析

WebRTC源码分析-定位之Location

2019-11-16  本文已影响0人  ice_ly000

Location类提供了一个对象构建时所在位置的基础信息,是chromium项目中的https://code.google.com/p/chromium/codesearch#chromium/src/base/location.h精简版本。在WebRTC项目中位于rtc_base/location.h和rtc_base/location.cc中。

class Location {
 public:
  // Constructor should be called with a long-lived char*, such as __FILE__.
  // It assumes the provided value will persist as a global constant, and it
  // will not make a copy of it.
  //
  // TODO(deadbeef): Tracing is currently limited to 2 arguments, which is
  // why the file name and line number are combined into one argument.
  //
  // Once TracingV2 is available, separate the file name and line number.
  Location(const char* function_name, const char* file_and_line);
  Location();
  Location(const Location& other);
  Location& operator=(const Location& other);
  const char* function_name() const { return function_name_; }
  const char* file_and_line() const { return file_and_line_; }
  std::string ToString() const;

 private:
  const char* function_name_;
  const char* file_and_line_;
};

// Define a macro to record the current source location.
#define RTC_FROM_HERE RTC_FROM_HERE_WITH_FUNCTION(__FUNCTION__)
#define RTC_FROM_HERE_WITH_FUNCTION(function_name) \
  ::rtc::Location(function_name, __FILE__ ":" STRINGIZE(__LINE__))
} 

该类简单易懂,但是简单之中也存在一些值得注意之处:

Location类的源码如下所示

Location::Location(const char* function_name, const char* file_and_line)
    : function_name_(function_name), file_and_line_(file_and_line) {}

Location::Location() : function_name_("Unknown"), file_and_line_("Unknown") {}

Location::Location(const Location& other)
    : function_name_(other.function_name_),
      file_and_line_(other.file_and_line_) {}

Location& Location::operator=(const Location& other) {
  function_name_ = other.function_name_;
  file_and_line_ = other.file_and_line_;
  return *this;
}

std::string Location::ToString() const {
  char buf[256];
  snprintf(buf, sizeof(buf), "%s@%s", function_name_, file_and_line_);
  return buf;
}

总结

上一篇 下一篇

猜你喜欢

热点阅读