This Node源码分析

看军哥博客有Rtos的源码分析,手痒耍宝把自己读的源码笔记分享出来.愿与众君互相讨论学习



namespace ros
{

namespace names
{
void init(const M_string& remappings);
}

namespace this_node
{

std::string g_name = "empty";
std::string g_namespace;

const std::string& getName()    //获取当前的名字
{
  return g_name;
}

const std::string& getNamespace()   //获取当前命名空间
{
  return g_namespace;
}

void getAdvertisedTopics(V_string& topics)  //返回实例的发布者话题名
{
  TopicManager::instance()->getAdvertisedTopics(topics);
}

void getSubscribedTopics(V_string& topics)  //返回实例的订阅者话题名
{
  TopicManager::instance()->getSubscribedTopics(topics);
}

void init(const std::string& name, const M_string& remappings, uint32_t options)    //这前面还有argc argv 才是节点名
{
  char *ns_env = NULL;
#ifdef _MSC_VER //ROS绝逼是在Win下开发的`_^`
  _dupenv_s(&ns_env, NULL, "ROS_NAMESPACE");    //从环境中取字符串,获取环境变量的值
#else
  ns_env = getenv("ROS_NAMESPACE"); //在这里去环境变量应该指的就是命名空间惹
#endif

  if (ns_env)
  {
    g_namespace = ns_env;   //获取的命名空间值和当前命名空间相等
#ifdef _MSC_VER
    free(ns_env);
#endif
  }

  g_name = name;    //节点名给全局

  bool disable_anon = false;
  M_string::const_iterator it = remappings.find("__name");  //关联容器重命名,不知道它想干啥,看着一堆宏头大了
  if (it != remappings.end())
  {
    g_name = it->second;
    disable_anon = true;
  }

  it = remappings.find("__ns");
  if (it != remappings.end())
  {
    g_namespace = it->second;
  }

  if (g_namespace.empty())
  {
    g_namespace = "/";
  }

  g_namespace = (g_namespace == "/")
    ? std::string("/") 
    : ("/" + g_namespace)
    ;


  std::string error;
  if (!names::validate(g_namespace, error)) //若检测到命名空间非有效数据,报错
  {
    std::stringstream ss;
    ss << "Namespace [" << g_namespace << "] is invalid: " << error;
    throw InvalidNameException(ss.str());
  }

  // names must be initialized here, because it requires the namespace to already be known so that it can properly resolve names.
  // It must be done before we resolve g_name, because otherwise the name will not get remapped.
  //还不太明白它的重映射功能
  names::init(remappings);  //还是不懂重映射==

  if (g_name.find("/") != std::string::npos)    //nops的定义static const size_type npos = -1;
  {
    throw InvalidNodeNameException(g_name, "node names cannot contain /");
  }
  if (g_name.find("~") != std::string::npos)
  {
    throw InvalidNodeNameException(g_name, "node names cannot contain ~");
  }

  g_name = names::resolve(g_namespace, g_name); //resolve名字,全名/+ns+name

  if (options & init_options::AnonymousName && !disable_anon)   //init_options::AnonymousName:  匿名节点,在你的节点名后面加一些数字使它变得unique
  {
    char buf[200];
    snprintf(buf, sizeof(buf), "_%llu", (unsigned long long)WallTime::now().toNSec());
    g_name += buf;
  }

  ros::console::setFixedFilterToken("node", g_name);    //把节点和名字联系起来
}

} // namespace this_node

} // namespace ros

下午写了一下午程序,快五点了不想继续写了,就发this node出来,又惊叹一番,回去看C++ 了bye~
上一篇:iOS 再谈 代理传值,block反向传值


下一篇:iOS Block界面反向传值