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