utils/print-introspected-doxygen.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 23 Mar 2009 14:37:43 +0100
changeset 4364 579bbfe8bb65
parent 3588 ad0a36bfdb62
child 4513 544c9d637ff1
permissions -rw-r--r--
rename
     1 #include <iostream>
     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"
     8 
     9 using namespace ns3;
    10 
    11 NS_LOG_COMPONENT_DEFINE ("Main");
    12 
    13 void
    14 PrintAttributes (TypeId tid, std::ostream &os)
    15 {
    16   os << "<ul>"<<std::endl;
    17   for (uint32_t j = 0; j < tid.GetAttributeN (); j++)
    18     {
    19       os << "<li><b>" << tid.GetAttributeName (j) << "</b>: "
    20 		<< tid.GetAttributeHelp (j) << std::endl;
    21       Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (j);
    22       os << "  <ul>" << std::endl 
    23 	 << "    <li>Set with class: \\ref " <<  checker->GetValueTypeName () << "</li>" << std::endl;
    24       if (checker->HasUnderlyingTypeInformation ())
    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 ";
    47 	}
    48       os << "</li>" << std::endl;
    49       os << "  </ul> " << std::endl;
    50       
    51     }
    52   os << "</ul>" << std::endl;
    53 }
    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 }
   233 
   234 int main (int argc, char *argv[])
   235 {
   236   NodeContainer c; c.Create (1);
   237 
   238   StaticInformation info;
   239   info.RecordAggregationInfo ("ns3::Node", "ns3::TcpSocketFactory");
   240   info.RecordAggregationInfo ("ns3::Node", "ns3::UdpSocketFactory");
   241   info.RecordAggregationInfo ("ns3::Node", "ns3::PacketSocketFactory");
   242   info.RecordAggregationInfo ("ns3::Node", "ns3::olsr::RoutingProtocol");
   243   info.RecordAggregationInfo ("ns3::Node", "ns3::MobilityModel");
   244   info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L3Protocol");
   245   info.RecordAggregationInfo ("ns3::Node", "ns3::ArpL3Protocol");
   246 
   247   for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN (); ++i)
   248     {
   249       Ptr<Object> object = Config::GetRootNamespaceObject (i);
   250       info.Gather (object->GetInstanceTypeId ());
   251     }
   252 
   253   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
   254     {
   255       std::cout << "/*!" << std::endl;
   256       TypeId tid = TypeId::GetRegistered (i);
   257       if (tid.MustHideFromDocumentation ())
   258 	{
   259 	  continue;
   260 	}
   261       std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << std::endl;
   262       std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () 
   263 		<< std::endl << std::endl;
   264       std::vector<std::string> paths = info.Get (tid);
   265       if (!paths.empty ())
   266 	{
   267 	  std::cout << "This object is accessible through the following paths with Config::Set and Config::Connect:" 
   268 		    << std::endl;
   269 	  std::cout << "<ul>" << std::endl;
   270 	  for (uint32_t k = 0; k < paths.size (); ++k)
   271 	    {
   272 	      std::string path = paths[k];
   273 	      std::cout << "<li>" << path << "</li>" << std::endl;
   274 	    }
   275 	  std::cout << "</ul>" << std::endl;
   276 	}
   277       if (tid.GetAttributeN () == 0)
   278 	{
   279 	  std::cout << "No Attributes defined for this type.<br>" << std::endl;
   280 	}
   281       else
   282 	{
   283 	  std::cout << "Attributes defined for this type:<br>" << std::endl;
   284 	  PrintAttributes (tid, std::cout);
   285 	}
   286       {
   287 	TypeId tmp = tid.GetParent ();
   288 	while (tmp.GetParent () != tmp)
   289 	  {
   290 	    if (tmp.GetAttributeN () != 0)
   291 	      {
   292 		std::cout << "Attributes defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   293 		PrintAttributes (tmp, std::cout);
   294 	      }
   295 	    tmp = tmp.GetParent ();
   296 	  }
   297       }
   298       if (tid.GetTraceSourceN () == 0)
   299 	{
   300 	  std::cout << "No TraceSources defined for this type.<br>" << std::endl;
   301 	}
   302       else
   303 	{
   304 	  std::cout << "TraceSources defined for this type:<br>" << std::endl;
   305 	  PrintTraceSources (tid, std::cout);
   306 	}
   307       {
   308 	TypeId tmp = tid.GetParent ();
   309 	while (tmp.GetParent () != tmp)
   310 	  {
   311 	    if (tmp.GetTraceSourceN () != 0)
   312 	      {
   313 		std::cout << "TraceSources defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   314 		PrintTraceSources (tmp, std::cout);
   315 	      }
   316 	    tmp = tmp.GetParent ();
   317 	  }
   318       }
   319       std::cout << "*/" << std::endl;
   320     }
   321 
   322 
   323   std::cout << "/*!" << std::endl
   324 	    << "\\ingroup core" << std::endl
   325 	    << "\\defgroup TraceSourceList The list of all trace sources." << std::endl;
   326   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
   327     {
   328       TypeId tid = TypeId::GetRegistered (i);
   329       if (tid.GetTraceSourceN () == 0 ||
   330 	  tid.MustHideFromDocumentation ())
   331 	{
   332 	  continue;
   333 	}
   334       std::cout << "<b>" << tid.GetName () << "</b><br>" << std::endl
   335 		<< "<ul>" << std::endl;
   336       for (uint32_t j = 0; j < tid.GetTraceSourceN (); ++j)
   337 	{
   338 	  std::cout << "<li>" << tid.GetTraceSourceName (j) << ": " << tid.GetTraceSourceHelp (j) << "</li>" << std::endl;
   339 	}
   340       std::cout << "</ul>" << std::endl;
   341     }
   342   std::cout << "*/" << std::endl;
   343 
   344 
   345   std::cout << "/*!" << std::endl
   346 	    << "\\ingroup core" << std::endl
   347 	    << "\\defgroup AttributeList The list of all attributes." << std::endl;
   348   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); ++i)
   349     {
   350       TypeId tid = TypeId::GetRegistered (i);
   351       if (tid.GetAttributeN () == 0 ||
   352 	  tid.MustHideFromDocumentation ())
   353 	{
   354 	  continue;
   355 	}
   356       std::cout << "<b>" << tid.GetName () << "</b><br>" << std::endl
   357 		<< "<ul>" << std::endl;
   358       for (uint32_t j = 0; j < tid.GetAttributeN (); ++j)
   359 	{
   360 	  std::cout << "<li>" << tid.GetAttributeName (j) << ": " << tid.GetAttributeHelp (j) << "</li>" << std::endl;
   361 	}
   362       std::cout << "</ul>" << std::endl;
   363     }
   364   std::cout << "*/" << std::endl;
   365 
   366 
   367 
   368   return 0;
   369 }