utils/print-introspected-doxygen.cc
changeset 2989 b7eb3929096c
parent 2971 75780f899be3
child 3125 d2d8a36cfd23
equal deleted inserted replaced
2988:8774a8c9526f 2989:b7eb3929096c
     1 #include <iostream>
     1 #include <iostream>
     2 #include "ns3/object.h"
     2 #include "ns3/object.h"
       
     3 #include "ns3/pointer.h"
       
     4 #include "ns3/object-vector.h"
       
     5 #include "ns3/config.h"
       
     6 #include "ns3/log.h"
       
     7 #include "ns3/helper-module.h"
     3 
     8 
     4 using namespace ns3;
     9 using namespace ns3;
       
    10 
       
    11 NS_LOG_COMPONENT_DEFINE ("Main");
     5 
    12 
     6 void
    13 void
     7 PrintAttributes (TypeId tid, std::ostream &os)
    14 PrintAttributes (TypeId tid, std::ostream &os)
     8 {
    15 {
     9   os << "<ul>"<<std::endl;
    16   os << "<ul>"<<std::endl;
    10   for (uint32_t j = 0; j < tid.GetAttributeN (); j++)
    17   for (uint32_t j = 0; j < tid.GetAttributeN (); j++)
    11     {
    18     {
    12       os << "<li><b>" << tid.GetAttributeName (j) << "</b>: "
    19       os << "<li><b>" << tid.GetAttributeName (j) << "</b>: "
    13 		<< tid.GetAttributeHelp (j) << std::endl;
    20 		<< tid.GetAttributeHelp (j) << std::endl;
    14       Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (j);
    21       Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (j);
    15       os << "  <ul>" << std::endl << "    <li>Type: " <<  checker->GetType ();
    22       os << "  <ul>" << std::endl 
    16       if (checker->HasTypeConstraints ())
    23 	 << "    <li>Set with class: \\ref " <<  checker->GetValueTypeName () << "</li>" << std::endl;
    17 	{
    24       if (checker->HasUnderlyingTypeInformation ())
    18 	  os << " -> " << checker->GetTypeConstraints ();
    25 	{
       
    26 	  os << "    <li>Underlying type: \\ref " << checker->GetUnderlyingTypeInformation () << "</li>" << std::endl;
       
    27 	}
       
    28       uint32_t flags = tid.GetAttributeFlags (j);
       
    29       Ptr<const AttributeAccessor> accessor = tid.GetAttributeAccessor (j);
       
    30       if (flags & TypeId::ATTR_CONSTRUCT && accessor->HasSetter ())
       
    31 	{
       
    32 	  Ptr<const AttributeValue> initial = tid.GetAttributeInitialValue (j);
       
    33 	  os << "    <li>Initial value: " << initial->SerializeToString (checker) << "</li>" << std::endl;
       
    34 	}
       
    35       os << "    <li>Flags: ";
       
    36       if (flags & TypeId::ATTR_CONSTRUCT && accessor->HasSetter ())
       
    37 	{
       
    38 	  os << "construct ";
       
    39 	}
       
    40       if (flags & TypeId::ATTR_SET && accessor->HasSetter ())
       
    41 	{
       
    42 	  os << "write ";
       
    43 	}
       
    44       if (flags & TypeId::ATTR_GET && accessor->HasGetter ())
       
    45 	{
       
    46 	  os << "read ";
    19 	}
    47 	}
    20       os << "</li>" << std::endl;
    48       os << "</li>" << std::endl;
    21       uint32_t flags = tid.GetAttributeFlags (j);
       
    22       os << "<li>Flags: ";
       
    23       if (flags & TypeId::ATTR_SET)
       
    24 	{
       
    25 	  os << "write ";
       
    26 	}
       
    27       if (flags & TypeId::ATTR_GET)
       
    28 	{
       
    29 	  os << "read ";
       
    30 	}
       
    31       if (flags & TypeId::ATTR_CONSTRUCT)
       
    32 	{
       
    33 	  os << "construct ";
       
    34 	}
       
    35       os << "  </ul> " << std::endl;
    49       os << "  </ul> " << std::endl;
    36       
    50       
    37     }
    51     }
    38   os << "</ul>" << std::endl;
    52   os << "</ul>" << std::endl;
    39 }
    53 }
    40 
    54 
       
    55 void
       
    56 PrintTraceSources (TypeId tid, std::ostream &os)
       
    57 {
       
    58   os << "<ul>"<<std::endl;
       
    59   for (uint32_t i = 0; i < tid.GetTraceSourceN (); ++i)
       
    60     {
       
    61       os << "<li><b>" << tid.GetTraceSourceName (i) << "</b>: "
       
    62 	 << tid.GetTraceSourceHelp (i)
       
    63 	 << std::endl;
       
    64       os << "</li>" << std::endl;
       
    65     }
       
    66   os << "</ul>"<<std::endl;
       
    67 }
       
    68 
       
    69 
       
    70 class StaticInformation
       
    71 {
       
    72 public:
       
    73   void RecordAggregationInfo (std::string a, std::string b);
       
    74   void Gather (TypeId tid);
       
    75   void Print (void) const;
       
    76 
       
    77   std::vector<std::string> Get (TypeId tid);
       
    78 
       
    79 private:
       
    80   std::string GetCurrentPath (void) const;
       
    81   void DoGather (TypeId tid);
       
    82   void RecordOutput (TypeId tid);
       
    83   bool HasAlreadyBeenProcessed (TypeId tid) const;
       
    84   std::vector<std::pair<TypeId,std::string> > m_output;
       
    85   std::vector<std::string> m_currentPath;
       
    86   std::vector<TypeId> m_alreadyProcessed;
       
    87   std::vector<std::pair<TypeId,TypeId> > m_aggregates;
       
    88 };
       
    89 
       
    90 void 
       
    91 StaticInformation::RecordAggregationInfo (std::string a, std::string b)
       
    92 {
       
    93   m_aggregates.push_back (std::make_pair (TypeId::LookupByName (a), TypeId::LookupByName (b)));
       
    94 }
       
    95 
       
    96 void 
       
    97 StaticInformation::Print (void) const
       
    98 {
       
    99   for (std::vector<std::pair<TypeId,std::string> >::const_iterator i = m_output.begin (); i != m_output.end (); ++i)
       
   100     {
       
   101       std::pair<TypeId,std::string> item = *i;
       
   102       std::cout << item.first.GetName () << " -> " << item.second << std::endl;
       
   103     }
       
   104 }
       
   105 
       
   106 std::string
       
   107 StaticInformation::GetCurrentPath (void) const
       
   108 {
       
   109   std::ostringstream oss;
       
   110   for (std::vector<std::string>::const_iterator i = m_currentPath.begin (); i != m_currentPath.end (); ++i)
       
   111     {
       
   112       std::string item = *i;
       
   113       oss << "/" << item;
       
   114     }
       
   115   return oss.str ();
       
   116 }
       
   117 
       
   118 void
       
   119 StaticInformation::RecordOutput (TypeId tid)
       
   120 {
       
   121   m_output.push_back (std::make_pair (tid, GetCurrentPath ()));
       
   122 }
       
   123 
       
   124 bool
       
   125 StaticInformation::HasAlreadyBeenProcessed (TypeId tid) const
       
   126 {
       
   127   for (uint32_t i = 0; i < m_alreadyProcessed.size (); ++i)
       
   128     {
       
   129       if (m_alreadyProcessed[i] == tid)
       
   130 	{
       
   131 	  return true;
       
   132 	}
       
   133     }
       
   134   return false;
       
   135 }
       
   136 
       
   137 std::vector<std::string> 
       
   138 StaticInformation::Get (TypeId tid)
       
   139 {
       
   140   std::vector<std::string> paths;
       
   141   for (uint32_t i = 0; i < m_output.size (); ++i)
       
   142     {
       
   143       std::pair<TypeId,std::string> tmp = m_output[i];
       
   144       if (tmp.first == tid)
       
   145 	{
       
   146 	  paths.push_back (tmp.second);
       
   147 	}
       
   148     }
       
   149   return paths;
       
   150 }
       
   151 
       
   152 void
       
   153 StaticInformation::Gather (TypeId tid)
       
   154 {
       
   155   DoGather (tid);
       
   156 
       
   157   std::sort (m_output.begin (), m_output.end ());
       
   158   m_output.erase (std::unique (m_output.begin (), m_output.end ()), m_output.end ());
       
   159 }
       
   160 
       
   161 void 
       
   162 StaticInformation::DoGather (TypeId tid)
       
   163 {
       
   164   NS_LOG_FUNCTION (this);
       
   165   if (HasAlreadyBeenProcessed (tid))
       
   166     {
       
   167       return;
       
   168     }
       
   169   RecordOutput (tid);
       
   170   for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
       
   171     {
       
   172       Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
       
   173       const PointerChecker *ptrChecker = dynamic_cast<const PointerChecker *> (PeekPointer (checker));
       
   174       if (ptrChecker != 0)
       
   175 	{
       
   176 	  TypeId pointee = ptrChecker->GetPointeeTypeId ();
       
   177 	  m_currentPath.push_back (tid.GetAttributeName (i));
       
   178 	  m_alreadyProcessed.push_back (tid);
       
   179 	  DoGather (pointee);
       
   180 	  m_alreadyProcessed.pop_back ();
       
   181 	  m_currentPath.pop_back ();
       
   182 	  continue;
       
   183 	}
       
   184       // attempt to cast to an object vector.
       
   185       const ObjectVectorChecker *vectorChecker = dynamic_cast<const ObjectVectorChecker *> (PeekPointer (checker));
       
   186       if (vectorChecker != 0)
       
   187 	{
       
   188 	  TypeId item = vectorChecker->GetItemTypeId ();
       
   189 	  m_currentPath.push_back (tid.GetAttributeName (i) + "/[i]");
       
   190 	  m_alreadyProcessed.push_back (tid);
       
   191 	  DoGather (item);
       
   192 	  m_alreadyProcessed.pop_back ();
       
   193 	  m_currentPath.pop_back ();
       
   194 	  continue;
       
   195 	}
       
   196     }
       
   197   for (uint32_t j = 0; j < TypeId::GetRegisteredN (); j++)
       
   198     {
       
   199       TypeId child = TypeId::GetRegistered (j);
       
   200       if (child.IsChildOf (tid))
       
   201 	{
       
   202 	  m_currentPath.push_back ("$%" + child.GetName ());
       
   203 	  m_alreadyProcessed.push_back (tid);
       
   204 	  DoGather (child);
       
   205 	  m_alreadyProcessed.pop_back ();
       
   206 	  m_currentPath.pop_back ();
       
   207 	}
       
   208     }
       
   209   for (uint32_t k = 0; k < m_aggregates.size (); ++k)
       
   210     {
       
   211       std::pair<TypeId,TypeId> tmp = m_aggregates[k];
       
   212       if (tmp.first == tid || tmp.second == tid)
       
   213 	{
       
   214 	  TypeId other;
       
   215 	  if (tmp.first == tid)
       
   216 	    {
       
   217 	      other = tmp.second;
       
   218 	    }
       
   219 	  if (tmp.second == tid)
       
   220 	    {
       
   221 	      other = tmp.first;
       
   222 	    }
       
   223 	  // Note: we insert a % in the path below to ensure that doxygen does not
       
   224 	  // attempt to resolve the typeid names included in the string.
       
   225 	  m_currentPath.push_back ("$%" + other.GetName ());
       
   226 	  m_alreadyProcessed.push_back (tid);
       
   227 	  DoGather (other);
       
   228 	  m_alreadyProcessed.pop_back ();
       
   229 	  m_currentPath.pop_back ();	  
       
   230 	}
       
   231     }
       
   232 }
    41 
   233 
    42 int main (int argc, char *argv[])
   234 int main (int argc, char *argv[])
    43 {
   235 {
       
   236   NodeContainer c; c.Create (1);
       
   237 
       
   238   StaticInformation info;
       
   239   info.RecordAggregationInfo ("ns3::Node", "ns3::Tcp");
       
   240   info.RecordAggregationInfo ("ns3::Node", "ns3::Udp");
       
   241   info.RecordAggregationInfo ("ns3::Node", "ns3::PacketSocketFactory");
       
   242   info.RecordAggregationInfo ("ns3::Node", "ns3::olsr::Agent");
       
   243   info.RecordAggregationInfo ("ns3::Node", "ns3::MobilityModel");
       
   244   info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L4Demux");
       
   245   info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L3Protocol");
       
   246   info.RecordAggregationInfo ("ns3::Node", "ns3::ArpL3Protocol");
       
   247 
       
   248   for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN (); ++i)
       
   249     {
       
   250       Ptr<Object> object = Config::GetRootNamespaceObject (i);
       
   251       info.Gather (object->GetInstanceTypeId ());
       
   252     }
    44 
   253 
    45   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
   254   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
    46     {
   255     {
    47       std::cout << "/*!" << std::endl;
   256       std::cout << "/*!" << std::endl;
    48       TypeId tid = TypeId::GetRegistered (i);
   257       TypeId tid = TypeId::GetRegistered (i);
    49       if (tid.MustHideFromDocumentation ())
   258       if (tid.MustHideFromDocumentation ())
    50 	{
   259 	{
    51 	  continue;
   260 	  continue;
    52 	}
   261 	}
    53       std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << std::endl;
   262       std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << std::endl;
    54       std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () << std::endl << std::endl;
   263       std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () 
       
   264 		<< std::endl << std::endl;
       
   265       std::vector<std::string> paths = info.Get (tid);
       
   266       if (!paths.empty ())
       
   267 	{
       
   268 	  std::cout << "This object is accessible through the following paths with Config::Set and Config::Connect:" 
       
   269 		    << std::endl;
       
   270 	  std::cout << "<ul>" << std::endl;
       
   271 	  for (uint32_t k = 0; k < paths.size (); ++k)
       
   272 	    {
       
   273 	      std::string path = paths[k];
       
   274 	      std::cout << "<li>" << path << "</li>" << std::endl;
       
   275 	    }
       
   276 	  std::cout << "</ul>" << std::endl;
       
   277 	}
    55       if (tid.GetAttributeN () == 0)
   278       if (tid.GetAttributeN () == 0)
    56 	{
   279 	{
    57 	  std::cout << "No Attributes defined for this type." << std::endl;
   280 	  std::cout << "No Attributes defined for this type.<br>" << std::endl;
    58 	}
   281 	}
    59       else
   282       else
    60 	{
   283 	{
    61 	  std::cout << "Attributes defined for this type:" << std::endl;
   284 	  std::cout << "Attributes defined for this type:<br>" << std::endl;
    62 	  PrintAttributes (tid, std::cout);
   285 	  PrintAttributes (tid, std::cout);
    63 	}
   286 	}
    64       bool hasAttributesInParent = false;
   287       {
    65       TypeId tmp = tid.GetParent ();
   288 	TypeId tmp = tid.GetParent ();
    66       while (tmp.GetParent () != tmp)
   289 	while (tmp.GetParent () != tmp)
    67 	{
   290 	  {
    68 	  if (tmp.GetAttributeN () != 0)
   291 	    if (tmp.GetAttributeN () != 0)
    69 	    {
   292 	      {
    70 	      hasAttributesInParent = true;
   293 		std::cout << "Attributes defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
    71 	    }
   294 		PrintAttributes (tmp, std::cout);
    72 	  tmp = tmp.GetParent ();
   295 	      }
    73 	}
   296 	    tmp = tmp.GetParent ();
    74       if (hasAttributesInParent)
   297 	  }
    75 	{
   298       }
    76 	  std::cout << "Attributes defined in parent classes:<br>" << std::endl;
   299       if (tid.GetTraceSourceN () == 0)
    77 	  tmp = tid.GetParent ();
   300 	{
    78 	  while (tmp.GetParent () != tmp)
   301 	  std::cout << "No TraceSources defined for this type.<br>" << std::endl;
    79 	    {
   302 	}
    80 	      if (tmp.GetAttributeN () != 0)
   303       else
    81 		{
   304 	{
    82 		  std::cout << tmp.GetName () << std::endl;
   305 	  std::cout << "TraceSources defined for this type:<br>" << std::endl;
    83 		  PrintAttributes (tmp, std::cout);
   306 	  PrintTraceSources (tid, std::cout);
    84 		}
   307 	}
    85 	      tmp = tmp.GetParent ();
   308       {
    86 	    }
   309 	TypeId tmp = tid.GetParent ();
    87 	}
   310 	while (tmp.GetParent () != tmp)
       
   311 	  {
       
   312 	    if (tmp.GetTraceSourceN () != 0)
       
   313 	      {
       
   314 		std::cout << "TraceSources defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
       
   315 		PrintTraceSources (tmp, std::cout);
       
   316 	      }
       
   317 	    tmp = tmp.GetParent ();
       
   318 	  }
       
   319       }
    88       std::cout << "*/" << std::endl;
   320       std::cout << "*/" << std::endl;
    89     }
   321     }
    90 
   322 
    91 
   323 
       
   324   std::cout << "/*!" << std::endl
       
   325 	    << "\\ingroup core" << std::endl
       
   326 	    << "\\defgroup TraceSourceList The list of all trace sources." << std::endl;
       
   327   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
       
   328     {
       
   329       TypeId tid = TypeId::GetRegistered (i);
       
   330       if (tid.GetTraceSourceN () == 0 ||
       
   331 	  tid.MustHideFromDocumentation ())
       
   332 	{
       
   333 	  continue;
       
   334 	}
       
   335       std::cout << "<b>" << tid.GetName () << "</b><br>" << std::endl
       
   336 		<< "<ul>" << std::endl;
       
   337       for (uint32_t j = 0; j < tid.GetTraceSourceN (); ++j)
       
   338 	{
       
   339 	  std::cout << "<li>" << tid.GetTraceSourceName (j) << ": " << tid.GetTraceSourceHelp (j) << "</li>" << std::endl;
       
   340 	}
       
   341       std::cout << "</ul>" << std::endl;
       
   342     }
       
   343   std::cout << "*/" << std::endl;
       
   344 
       
   345 
       
   346   std::cout << "/*!" << std::endl
       
   347 	    << "\\ingroup core" << std::endl
       
   348 	    << "\\defgroup AttributeList The list of all attributes." << std::endl;
       
   349   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
       
   350     {
       
   351       TypeId tid = TypeId::GetRegistered (i);
       
   352       if (tid.GetAttributeN () == 0 ||
       
   353 	  tid.MustHideFromDocumentation ())
       
   354 	{
       
   355 	  continue;
       
   356 	}
       
   357       std::cout << "<b>" << tid.GetName () << "</b><br>" << std::endl
       
   358 		<< "<ul>" << std::endl;
       
   359       for (uint32_t j = 0; j < tid.GetAttributeN (); ++j)
       
   360 	{
       
   361 	  std::cout << "<li>" << tid.GetAttributeName (j) << ": " << tid.GetAttributeHelp (j) << "</li>" << std::endl;
       
   362 	}
       
   363       std::cout << "</ul>" << std::endl;
       
   364     }
       
   365   std::cout << "*/" << std::endl;
       
   366 
       
   367 
       
   368 
    92   return 0;
   369   return 0;
    93 }
   370 }