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