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