utils/print-introspected-doxygen.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 11 Apr 2008 11:25:44 -0700
changeset 2951 2a3db8645225
parent 2945 8e9f4dc59d8b
child 2964 4e8cb1577144
permissions -rw-r--r--
use HasSetter/HasGetter to improve doxygen output.
     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 << "    <li>Type: \\ref " <<  checker->GetType ();
    23       if (checker->HasTypeConstraints ())
    24 	{
    25 	  os << " -> " << checker->GetTypeConstraints ();
    26 	}
    27       os << "</li>" << std::endl;
    28       uint32_t flags = tid.GetAttributeFlags (j);
    29       Ptr<const AttributeAccessor> accessor = tid.GetAttributeAccessor (j);
    30       os << "    <li>Flags: ";
    31       if (flags & TypeId::ATTR_SET && accessor->HasSetter ())
    32 	{
    33 	  os << "write ";
    34 	}
    35       if (flags & TypeId::ATTR_GET && accessor->HasGetter ())
    36 	{
    37 	  os << "read ";
    38 	}
    39       if (flags & TypeId::ATTR_CONSTRUCT && accessor->HasSetter ())
    40 	{
    41 	  os << "construct ";
    42 	}
    43       os << "</li>" << std::endl;
    44       os << "  </ul> " << std::endl;
    45       
    46     }
    47   os << "</ul>" << std::endl;
    48 }
    49 
    50 void
    51 PrintTraceSources (TypeId tid, std::ostream &os)
    52 {
    53   os << "<ul>"<<std::endl;
    54   for (uint32_t i = 0; i < tid.GetTraceSourceN (); ++i)
    55     {
    56       os << "<li><b>" << tid.GetTraceSourceName (i) << "</b>: "
    57 	 << tid.GetTraceSourceHelp (i)
    58 	 << std::endl;
    59       os << "</li>" << std::endl;
    60     }
    61   os << "</ul>"<<std::endl;
    62 }
    63 
    64 
    65 class StaticInformation
    66 {
    67 public:
    68   void RecordAggregationInfo (std::string a, std::string b);
    69   void Gather (TypeId tid);
    70   void Print (void) const;
    71 
    72   std::vector<std::string> Get (TypeId tid);
    73 
    74 private:
    75   std::string GetCurrentPath (void) const;
    76   void DoGather (TypeId tid);
    77   void RecordOutput (TypeId tid);
    78   bool HasAlreadyBeenProcessed (TypeId tid) const;
    79   std::vector<std::pair<TypeId,std::string> > m_output;
    80   std::vector<std::string> m_currentPath;
    81   std::vector<TypeId> m_alreadyProcessed;
    82   std::vector<std::pair<TypeId,TypeId> > m_aggregates;
    83 };
    84 
    85 void 
    86 StaticInformation::RecordAggregationInfo (std::string a, std::string b)
    87 {
    88   m_aggregates.push_back (std::make_pair (TypeId::LookupByName (a), TypeId::LookupByName (b)));
    89 }
    90 
    91 void 
    92 StaticInformation::Print (void) const
    93 {
    94   for (std::vector<std::pair<TypeId,std::string> >::const_iterator i = m_output.begin (); i != m_output.end (); ++i)
    95     {
    96       std::pair<TypeId,std::string> item = *i;
    97       std::cout << item.first.GetName () << " -> " << item.second << std::endl;
    98     }
    99 }
   100 
   101 std::string
   102 StaticInformation::GetCurrentPath (void) const
   103 {
   104   std::ostringstream oss;
   105   for (std::vector<std::string>::const_iterator i = m_currentPath.begin (); i != m_currentPath.end (); ++i)
   106     {
   107       std::string item = *i;
   108       oss << "/" << item;
   109     }
   110   return oss.str ();
   111 }
   112 
   113 void
   114 StaticInformation::RecordOutput (TypeId tid)
   115 {
   116   m_output.push_back (std::make_pair (tid, GetCurrentPath ()));
   117 }
   118 
   119 bool
   120 StaticInformation::HasAlreadyBeenProcessed (TypeId tid) const
   121 {
   122   for (uint32_t i = 0; i < m_alreadyProcessed.size (); ++i)
   123     {
   124       if (m_alreadyProcessed[i] == tid)
   125 	{
   126 	  return true;
   127 	}
   128     }
   129   return false;
   130 }
   131 
   132 std::vector<std::string> 
   133 StaticInformation::Get (TypeId tid)
   134 {
   135   std::vector<std::string> paths;
   136   for (uint32_t i = 0; i < m_output.size (); ++i)
   137     {
   138       std::pair<TypeId,std::string> tmp = m_output[i];
   139       if (tmp.first == tid)
   140 	{
   141 	  paths.push_back (tmp.second);
   142 	}
   143     }
   144   return paths;
   145 }
   146 
   147 void
   148 StaticInformation::Gather (TypeId tid)
   149 {
   150   DoGather (tid);
   151 
   152   std::sort (m_output.begin (), m_output.end ());
   153   m_output.erase (std::unique (m_output.begin (), m_output.end ()), m_output.end ());
   154 }
   155 
   156 void 
   157 StaticInformation::DoGather (TypeId tid)
   158 {
   159   NS_LOG_FUNCTION;
   160   if (HasAlreadyBeenProcessed (tid))
   161     {
   162       return;
   163     }
   164   RecordOutput (tid);
   165   for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
   166     {
   167       Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
   168       const PointerChecker *ptrChecker = dynamic_cast<const PointerChecker *> (PeekPointer (checker));
   169       if (ptrChecker != 0)
   170 	{
   171 	  TypeId pointee = ptrChecker->GetPointeeTypeId ();
   172 	  m_currentPath.push_back (tid.GetAttributeName (i));
   173 	  m_alreadyProcessed.push_back (tid);
   174 	  DoGather (pointee);
   175 	  m_alreadyProcessed.pop_back ();
   176 	  m_currentPath.pop_back ();
   177 	  continue;
   178 	}
   179       // attempt to cast to an object vector.
   180       const ObjectVectorChecker *vectorChecker = dynamic_cast<const ObjectVectorChecker *> (PeekPointer (checker));
   181       if (vectorChecker != 0)
   182 	{
   183 	  TypeId item = vectorChecker->GetItemTypeId ();
   184 	  m_currentPath.push_back (tid.GetAttributeName (i) + "/[i]");
   185 	  m_alreadyProcessed.push_back (tid);
   186 	  DoGather (item);
   187 	  m_alreadyProcessed.pop_back ();
   188 	  m_currentPath.pop_back ();
   189 	  continue;
   190 	}
   191     }
   192   for (uint32_t j = 0; j < TypeId::GetRegisteredN (); j++)
   193     {
   194       TypeId child = TypeId::GetRegistered (j);
   195       if (child.IsChildOf (tid))
   196 	{
   197 	  m_currentPath.push_back ("$%" + child.GetName ());
   198 	  m_alreadyProcessed.push_back (tid);
   199 	  DoGather (child);
   200 	  m_alreadyProcessed.pop_back ();
   201 	  m_currentPath.pop_back ();
   202 	}
   203     }
   204   for (uint32_t k = 0; k < m_aggregates.size (); ++k)
   205     {
   206       std::pair<TypeId,TypeId> tmp = m_aggregates[k];
   207       if (tmp.first == tid || tmp.second == tid)
   208 	{
   209 	  TypeId other;
   210 	  if (tmp.first == tid)
   211 	    {
   212 	      other = tmp.second;
   213 	    }
   214 	  if (tmp.second == tid)
   215 	    {
   216 	      other = tmp.first;
   217 	    }
   218 	  // Note: we insert a % in the path below to ensure that doxygen does not
   219 	  // attempt to resolve the typeid names included in the string.
   220 	  m_currentPath.push_back ("$%" + other.GetName ());
   221 	  m_alreadyProcessed.push_back (tid);
   222 	  DoGather (other);
   223 	  m_alreadyProcessed.pop_back ();
   224 	  m_currentPath.pop_back ();	  
   225 	}
   226     }
   227 }
   228 
   229 int main (int argc, char *argv[])
   230 {
   231   NodeContainer c; c.Create (1);
   232 
   233   StaticInformation info;
   234   info.RecordAggregationInfo ("ns3::Node", "ns3::Tcp");
   235   info.RecordAggregationInfo ("ns3::Node", "ns3::Udp");
   236   info.RecordAggregationInfo ("ns3::Node", "ns3::PacketSocketFactory");
   237   info.RecordAggregationInfo ("ns3::Node", "ns3::olsr::Agent");
   238   info.RecordAggregationInfo ("ns3::Node", "ns3::MobilityModel");
   239   info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L4Demux");
   240   info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L3Protocol");
   241   info.RecordAggregationInfo ("ns3::Node", "ns3::ArpL3Protocol");
   242 
   243   for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN (); ++i)
   244     {
   245       Ptr<Object> object = Config::GetRootNamespaceObject (i);
   246       info.Gather (object->GetInstanceTypeId ());
   247     }
   248 
   249   for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
   250     {
   251       std::cout << "/*!" << std::endl;
   252       TypeId tid = TypeId::GetRegistered (i);
   253       if (tid.MustHideFromDocumentation ())
   254 	{
   255 	  continue;
   256 	}
   257       std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << std::endl;
   258       std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () 
   259 		<< std::endl << std::endl;
   260       std::vector<std::string> paths = info.Get (tid);
   261       if (!paths.empty ())
   262 	{
   263 	  std::cout << "This object is accessible through the following paths with Config::Set and Config::Connect:" 
   264 		    << std::endl;
   265 	  std::cout << "<ul>" << std::endl;
   266 	  for (uint32_t k = 0; k < paths.size (); ++k)
   267 	    {
   268 	      std::string path = paths[k];
   269 	      std::cout << "<li>" << path << "</li>" << std::endl;
   270 	    }
   271 	  std::cout << "</ul>" << std::endl;
   272 	}
   273       if (tid.GetAttributeN () == 0)
   274 	{
   275 	  std::cout << "No Attributes defined for this type." << std::endl;
   276 	}
   277       else
   278 	{
   279 	  std::cout << "Attributes defined for this type:" << std::endl;
   280 	  PrintAttributes (tid, std::cout);
   281 	}
   282       {
   283 	TypeId tmp = tid.GetParent ();
   284 	while (tmp.GetParent () != tmp)
   285 	  {
   286 	    if (tmp.GetAttributeN () != 0)
   287 	      {
   288 		std::cout << "Attributes defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   289 		PrintAttributes (tmp, std::cout);
   290 	      }
   291 	    tmp = tmp.GetParent ();
   292 	  }
   293       }
   294       if (tid.GetTraceSourceN () == 0)
   295 	{
   296 	  std::cout << "No TraceSources defined for this type." << std::endl;
   297 	}
   298       else
   299 	{
   300 	  std::cout << "TraceSources defined for this type:" << std::endl;
   301 	  PrintTraceSources (tid, std::cout);
   302 	}
   303       {
   304 	TypeId tmp = tid.GetParent ();
   305 	while (tmp.GetParent () != tmp)
   306 	  {
   307 	    if (tmp.GetTraceSourceN () != 0)
   308 	      {
   309 		std::cout << "TraceSources defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   310 		PrintTraceSources (tmp, std::cout);
   311 	      }
   312 	    tmp = tmp.GetParent ();
   313 	  }
   314       }
   315       std::cout << "*/" << std::endl;
   316     }
   317 
   318 
   319   return 0;
   320 }