generate path information for each type.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 10 Apr 2008 11:50:47 -0700
changeset 2944d8806baadedb
parent 2943 cdbcd43a019c
child 2945 8e9f4dc59d8b
generate path information for each type.
utils/print-introspected-doxygen.cc
     1.1 --- a/utils/print-introspected-doxygen.cc	Thu Apr 10 11:50:08 2008 -0700
     1.2 +++ b/utils/print-introspected-doxygen.cc	Thu Apr 10 11:50:47 2008 -0700
     1.3 @@ -1,8 +1,15 @@
     1.4  #include <iostream>
     1.5  #include "ns3/object.h"
     1.6 +#include "ns3/pointer.h"
     1.7 +#include "ns3/object-vector.h"
     1.8 +#include "ns3/config.h"
     1.9 +#include "ns3/log.h"
    1.10 +#include "ns3/helper-module.h"
    1.11  
    1.12  using namespace ns3;
    1.13  
    1.14 +NS_LOG_COMPONENT_DEFINE ("Main");
    1.15 +
    1.16  void
    1.17  PrintAttributes (TypeId tid, std::ostream &os)
    1.18  {
    1.19 @@ -19,7 +26,7 @@
    1.20  	}
    1.21        os << "</li>" << std::endl;
    1.22        uint32_t flags = tid.GetAttributeFlags (j);
    1.23 -      os << "<li>Flags: ";
    1.24 +      os << "    <li>Flags: ";
    1.25        if (flags & TypeId::ATTR_SET)
    1.26  	{
    1.27  	  os << "write ";
    1.28 @@ -32,15 +39,196 @@
    1.29  	{
    1.30  	  os << "construct ";
    1.31  	}
    1.32 +      os << "</li>" << std::endl;
    1.33        os << "  </ul> " << std::endl;
    1.34        
    1.35      }
    1.36    os << "</ul>" << std::endl;
    1.37  }
    1.38  
    1.39 +class StaticInformation
    1.40 +{
    1.41 +public:
    1.42 +  void RecordAggregationInfo (std::string a, std::string b);
    1.43 +  void Gather (TypeId tid);
    1.44 +  void Print (void) const;
    1.45 +
    1.46 +  std::vector<std::string> Get (TypeId tid);
    1.47 +
    1.48 +private:
    1.49 +  std::string GetCurrentPath (void) const;
    1.50 +  void DoGather (TypeId tid);
    1.51 +  void RecordOutput (TypeId tid);
    1.52 +  bool HasAlreadyBeenProcessed (TypeId tid) const;
    1.53 +  std::vector<std::pair<TypeId,std::string> > m_output;
    1.54 +  std::vector<std::string> m_currentPath;
    1.55 +  std::vector<TypeId> m_alreadyProcessed;
    1.56 +  std::vector<std::pair<TypeId,TypeId> > m_aggregates;
    1.57 +};
    1.58 +
    1.59 +void 
    1.60 +StaticInformation::RecordAggregationInfo (std::string a, std::string b)
    1.61 +{
    1.62 +  m_aggregates.push_back (std::make_pair (TypeId::LookupByName (a), TypeId::LookupByName (b)));
    1.63 +}
    1.64 +
    1.65 +void 
    1.66 +StaticInformation::Print (void) const
    1.67 +{
    1.68 +  for (std::vector<std::pair<TypeId,std::string> >::const_iterator i = m_output.begin (); i != m_output.end (); ++i)
    1.69 +    {
    1.70 +      std::pair<TypeId,std::string> item = *i;
    1.71 +      std::cout << item.first.GetName () << " -> " << item.second << std::endl;
    1.72 +    }
    1.73 +}
    1.74 +
    1.75 +std::string
    1.76 +StaticInformation::GetCurrentPath (void) const
    1.77 +{
    1.78 +  std::ostringstream oss;
    1.79 +  for (std::vector<std::string>::const_iterator i = m_currentPath.begin (); i != m_currentPath.end (); ++i)
    1.80 +    {
    1.81 +      std::string item = *i;
    1.82 +      oss << "/" << item;
    1.83 +    }
    1.84 +  return oss.str ();
    1.85 +}
    1.86 +
    1.87 +void
    1.88 +StaticInformation::RecordOutput (TypeId tid)
    1.89 +{
    1.90 +  m_output.push_back (std::make_pair (tid, GetCurrentPath ()));
    1.91 +}
    1.92 +
    1.93 +bool
    1.94 +StaticInformation::HasAlreadyBeenProcessed (TypeId tid) const
    1.95 +{
    1.96 +  for (uint32_t i = 0; i < m_alreadyProcessed.size (); ++i)
    1.97 +    {
    1.98 +      if (m_alreadyProcessed[i] == tid)
    1.99 +	{
   1.100 +	  return true;
   1.101 +	}
   1.102 +    }
   1.103 +  return false;
   1.104 +}
   1.105 +
   1.106 +std::vector<std::string> 
   1.107 +StaticInformation::Get (TypeId tid)
   1.108 +{
   1.109 +  std::vector<std::string> paths;
   1.110 +  for (uint32_t i = 0; i < m_output.size (); ++i)
   1.111 +    {
   1.112 +      std::pair<TypeId,std::string> tmp = m_output[i];
   1.113 +      if (tmp.first == tid)
   1.114 +	{
   1.115 +	  paths.push_back (tmp.second);
   1.116 +	}
   1.117 +    }
   1.118 +  return paths;
   1.119 +}
   1.120 +
   1.121 +void
   1.122 +StaticInformation::Gather (TypeId tid)
   1.123 +{
   1.124 +  DoGather (tid);
   1.125 +
   1.126 +  std::sort (m_output.begin (), m_output.end ());
   1.127 +  m_output.erase (std::unique (m_output.begin (), m_output.end ()), m_output.end ());
   1.128 +}
   1.129 +
   1.130 +void 
   1.131 +StaticInformation::DoGather (TypeId tid)
   1.132 +{
   1.133 +  NS_LOG_FUNCTION;
   1.134 +  if (HasAlreadyBeenProcessed (tid))
   1.135 +    {
   1.136 +      return;
   1.137 +    }
   1.138 +  RecordOutput (tid);
   1.139 +  for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
   1.140 +    {
   1.141 +      Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
   1.142 +      const PointerChecker *ptrChecker = dynamic_cast<const PointerChecker *> (PeekPointer (checker));
   1.143 +      if (ptrChecker != 0)
   1.144 +	{
   1.145 +	  TypeId pointee = ptrChecker->GetPointeeTypeId ();
   1.146 +	  m_currentPath.push_back (tid.GetAttributeName (i));
   1.147 +	  m_alreadyProcessed.push_back (tid);
   1.148 +	  DoGather (pointee);
   1.149 +	  m_alreadyProcessed.pop_back ();
   1.150 +	  m_currentPath.pop_back ();
   1.151 +	  continue;
   1.152 +	}
   1.153 +      // attempt to cast to an object vector.
   1.154 +      const ObjectVectorChecker *vectorChecker = dynamic_cast<const ObjectVectorChecker *> (PeekPointer (checker));
   1.155 +      if (vectorChecker != 0)
   1.156 +	{
   1.157 +	  TypeId item = vectorChecker->GetItemTypeId ();
   1.158 +	  m_currentPath.push_back (tid.GetAttributeName (i) + "/[i]");
   1.159 +	  m_alreadyProcessed.push_back (tid);
   1.160 +	  DoGather (item);
   1.161 +	  m_alreadyProcessed.pop_back ();
   1.162 +	  m_currentPath.pop_back ();
   1.163 +	  continue;
   1.164 +	}
   1.165 +    }
   1.166 +  for (uint32_t j = 0; j < TypeId::GetRegisteredN (); j++)
   1.167 +    {
   1.168 +      TypeId child = TypeId::GetRegistered (j);
   1.169 +      if (child.IsChildOf (tid))
   1.170 +	{
   1.171 +	  m_currentPath.push_back ("$%" + child.GetName ());
   1.172 +	  m_alreadyProcessed.push_back (tid);
   1.173 +	  DoGather (child);
   1.174 +	  m_alreadyProcessed.pop_back ();
   1.175 +	  m_currentPath.pop_back ();
   1.176 +	}
   1.177 +    }
   1.178 +  for (uint32_t k = 0; k < m_aggregates.size (); ++k)
   1.179 +    {
   1.180 +      std::pair<TypeId,TypeId> tmp = m_aggregates[k];
   1.181 +      if (tmp.first == tid || tmp.second == tid)
   1.182 +	{
   1.183 +	  TypeId other;
   1.184 +	  if (tmp.first == tid)
   1.185 +	    {
   1.186 +	      other = tmp.second;
   1.187 +	    }
   1.188 +	  if (tmp.second == tid)
   1.189 +	    {
   1.190 +	      other = tmp.first;
   1.191 +	    }
   1.192 +	  // Note: we insert a % in the path below to ensure that doxygen does not
   1.193 +	  // attempt to resolve the typeid names included in the string.
   1.194 +	  m_currentPath.push_back ("$%" + other.GetName ());
   1.195 +	  m_alreadyProcessed.push_back (tid);
   1.196 +	  DoGather (other);
   1.197 +	  m_alreadyProcessed.pop_back ();
   1.198 +	  m_currentPath.pop_back ();	  
   1.199 +	}
   1.200 +    }
   1.201 +}
   1.202  
   1.203  int main (int argc, char *argv[])
   1.204  {
   1.205 +  NodeContainer c; c.Create (1);
   1.206 +
   1.207 +  StaticInformation info;
   1.208 +  info.RecordAggregationInfo ("ns3::Node", "ns3::Tcp");
   1.209 +  info.RecordAggregationInfo ("ns3::Node", "ns3::Udp");
   1.210 +  info.RecordAggregationInfo ("ns3::Node", "ns3::PacketSocketFactory");
   1.211 +  info.RecordAggregationInfo ("ns3::Node", "ns3::olsr::Agent");
   1.212 +  info.RecordAggregationInfo ("ns3::Node", "ns3::MobilityModel");
   1.213 +  info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L4Demux");
   1.214 +  info.RecordAggregationInfo ("ns3::Node", "ns3::Ipv4L3Protocol");
   1.215 +  info.RecordAggregationInfo ("ns3::Node", "ns3::ArpL3Protocol");
   1.216 +
   1.217 +  for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN (); ++i)
   1.218 +    {
   1.219 +      Ptr<Object> object = Config::GetRootNamespaceObject (i);
   1.220 +      info.Gather (object->GetInstanceTypeId ());
   1.221 +    }
   1.222  
   1.223    for (uint32_t i = 0; i < TypeId::GetRegisteredN (); i++)
   1.224      {
   1.225 @@ -51,7 +239,21 @@
   1.226  	  continue;
   1.227  	}
   1.228        std::cout << "\\fn static TypeId " << tid.GetName () << "::GetTypeId (void)" << std::endl;
   1.229 -      std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () << std::endl << std::endl;
   1.230 +      std::cout << "\\brief This method returns the TypeId associated to \\ref " << tid.GetName () 
   1.231 +		<< std::endl << std::endl;
   1.232 +      std::vector<std::string> paths = info.Get (tid);
   1.233 +      if (!paths.empty ())
   1.234 +	{
   1.235 +	  std::cout << "This object is accessible through the following paths with Config::Set and Config::Connect:" 
   1.236 +		    << std::endl;
   1.237 +	  std::cout << "<ul>" << std::endl;
   1.238 +	  for (uint32_t k = 0; k < paths.size (); ++k)
   1.239 +	    {
   1.240 +	      std::string path = paths[k];
   1.241 +	      std::cout << "<li>" << path << "</li>" << std::endl;
   1.242 +	    }
   1.243 +	  std::cout << "</ul>" << std::endl;
   1.244 +	}
   1.245        if (tid.GetAttributeN () == 0)
   1.246  	{
   1.247  	  std::cout << "No Attributes defined for this type." << std::endl;
   1.248 @@ -73,13 +275,12 @@
   1.249  	}
   1.250        if (hasAttributesInParent)
   1.251  	{
   1.252 -	  std::cout << "Attributes defined in parent classes:<br>" << std::endl;
   1.253  	  tmp = tid.GetParent ();
   1.254  	  while (tmp.GetParent () != tmp)
   1.255  	    {
   1.256  	      if (tmp.GetAttributeN () != 0)
   1.257  		{
   1.258 -		  std::cout << tmp.GetName () << std::endl;
   1.259 +		  std::cout << "Attributes defined in parent class " << tmp.GetName () << ":<br>" << std::endl;
   1.260  		  PrintAttributes (tmp, std::cout);
   1.261  		}
   1.262  	      tmp = tmp.GetParent ();