utils/print-introspected-doxygen.cc
author Tom Henderson <tomh@tomh.org>
Sat, 17 May 2008 12:08:20 -0700
changeset 3125 d2d8a36cfd23
parent 2989 b7eb3929096c
child 3132 b0b0abb911cd
permissions -rw-r--r--
s/ns3::Udp/ns3::UdpSocketFactory
     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::Tcp");
   240   info.RecordAggregationInfo ("ns3::Node", "ns3::UdpSocketFactory");
   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     }
   253 
   254   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
   255     {
   256       std::cout << "/*!" << std::endl;
   257       TypeId tid = TypeId::GetRegistered (i);
   258       if (tid.MustHideFromDocumentation ())
   259 	{
   260 	  continue;
   261 	}
   262       std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << 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 	}
   278       if (tid.GetAttributeN () == 0)
   279 	{
   280 	  std::cout << "No Attributes defined for this type.<br>" << std::endl;
   281 	}
   282       else
   283 	{
   284 	  std::cout << "Attributes defined for this type:<br>" << std::endl;
   285 	  PrintAttributes (tid, std::cout);
   286 	}
   287       {
   288 	TypeId tmp = tid.GetParent ();
   289 	while (tmp.GetParent () != tmp)
   290 	  {
   291 	    if (tmp.GetAttributeN () != 0)
   292 	      {
   293 		std::cout << "Attributes defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   294 		PrintAttributes (tmp, std::cout);
   295 	      }
   296 	    tmp = tmp.GetParent ();
   297 	  }
   298       }
   299       if (tid.GetTraceSourceN () == 0)
   300 	{
   301 	  std::cout << "No TraceSources defined for this type.<br>" << std::endl;
   302 	}
   303       else
   304 	{
   305 	  std::cout << "TraceSources defined for this type:<br>" << std::endl;
   306 	  PrintTraceSources (tid, std::cout);
   307 	}
   308       {
   309 	TypeId tmp = tid.GetParent ();
   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       }
   320       std::cout << "*/" << std::endl;
   321     }
   322 
   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 
   369   return 0;
   370 }