samples/main-query-interface.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 06 Nov 2007 15:05:56 +0100
changeset 2081 434742b27b1e
parent 1504 36ecc970ba96
child 2230 9f13ac3291e0
permissions -rw-r--r--
use the new style support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
/*
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
 * Copyright (c) 2007 University of Washington
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
 * Authors:  Tom Henderson, Craig Dowell
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
 *
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     9
 *
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    13
 * GNU General Public License for more details.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    14
 *
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
 */
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
    20
#include "ns3/log.h"
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
#include "ns3/object.h"
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
#include "ns3/component-manager.h"
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
using namespace ns3;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    25
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    26
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    27
// This sample file shows examples of how to use QueryInterface.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    29
// QueryInterface is a templated method of class Object, defined in 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    30
// src/core/object.h.  ns-3 objects that derive from class Object
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    31
// can have QueryInterface invoked on them.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    32
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    33
// QueryInterface is a type-safe way to ask an object, at run-time, 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    34
// "Do you support the interface identified by the given InterfaceId?"  
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    35
// It avoids deprecated techniques of having to downcast pointers to 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    36
// an object to ask questions about its type.  One or more interfaces
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
// may be associated with a given object.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    38
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
// QueryInterface is of most use when working with base class
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
// pointers of objects that may be subclassed.  For instance,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
// one may have a pointer to a Node, but not know whether it has
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
// an IPv4 stack.  Another example might be to determine whether
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
// a Node has an EnergyModel, to which calls to decrement energy
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
// from the node's battery might be made.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    46
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    48
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    49
// Object is the base class for ns-3 node-related objects used at 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    50
// the public API.  Object provides reference counting implementations
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    51
// and the QueryInterface.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    52
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    53
// A common design paradigm for an ns-3 node object, such as a Queue,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    54
// is that we provide an abstract base class that inherits from
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    55
// Object.  This class is assigned an interface ID (iid) and 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    56
// contains the basic API for objects in this class and subclasses.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    57
// This base class is specialized to provide implementations of
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    58
// the object in question (such as a DropTailQueue).
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    59
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    60
// The design pattern commonly used is known as the "non-virtual
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    61
// public interface" pattern, whereby the public API for this
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    62
// object is a set of public non-virtual functions that forward
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    63
// to private virtual functions.  The forwarding functions can
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    64
// impose pre- and post-conditions on the forwarding call at
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    65
// the base class level.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    66
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    67
// We'll call this base class "AnInterface" in the example below.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    68
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    69
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    70
class AnInterface : public Object
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    71
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    72
public:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    73
  static const InterfaceId iid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    74
  void methodA (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    75
private:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    76
  virtual void domethodA (void) = 0;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    77
};
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    78
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    79
void
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    80
AnInterface::methodA (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    81
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
    82
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    83
  // pre-dispatch asserts
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
    84
  NS_LOG_LOGIC ("pre-condition");
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    85
  domethodA ();
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
    86
  NS_LOG_LOGIC ("post-condition");
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    87
  // post-dispatch asserts
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    88
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    89
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    90
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    91
// The below assignment assigns the InterfaceId of the class AnInterface,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    92
// and declares that the parent iid is that of class Object.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    93
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    94
const InterfaceId AnInterface::iid = MakeInterfaceId ("AnInterface", Object::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    95
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    97
// AnImplementation is an implementation of the abstract base class
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    98
// defined above. It provides implementation for the virtual functions 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    99
// in the base class.  It defines one ClassId for each constructor, 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   100
// and can also provide an interface itself (in this example, 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   101
// a methodImpl is available)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   102
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   103
class AnImplementation : public AnInterface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   104
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   105
public:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   106
  static const InterfaceId iid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
  static const ClassId cid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   108
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   109
  AnImplementation ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
  void methodImpl (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
private:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   112
  virtual void domethodA (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
};
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   115
void
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   116
AnImplementation::methodImpl (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   117
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
   118
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   119
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   120
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   121
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
AnImplementation::AnImplementation (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
   124
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
  // enable our interface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
  SetInterfaceId (AnImplementation::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   127
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   128
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   129
void
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   130
AnImplementation::domethodA () 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   131
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
   132
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   133
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   134
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
// The below assignment assigns the InterfaceId of the class AnImplementation,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   137
// and declares that the parent iid is that of class Object.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   138
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   139
const InterfaceId AnImplementation::iid = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   140
  MakeInterfaceId ("AnImplementation", AnInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   141
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   142
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   143
// The next few lines are used by the component manager.  They
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   144
// state that the component manager can create a new object 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   145
// AnImplementation and return an interface corresponding to 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   146
// the AnImplementation iid.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   147
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   148
const ClassId AnImplementation::cid = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   149
  MakeClassId<AnImplementation> 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   150
  ("AnImplementation", AnImplementation::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   151
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   152
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   153
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   154
// Extending interfaces
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   155
// ==================
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   156
// What if AnInterface doesn't provide enough API for your
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   157
// object type?
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   158
// - if you aren't concerned about backward compatibility and
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   159
//   don't mind recompiling, you just add new methods to AnInterface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   160
//   and recompile.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   161
// - if you want to address backward compatibiliy, or allow part
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   162
//   of the system to use the old interface, you have to do more.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   163
//   You have to declare a new interface with the new functionality.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   164
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   165
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   166
class AnExtendedInterface : public AnInterface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   167
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   168
public:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   169
  static const InterfaceId iid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   170
  void methodB (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   171
private:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   172
  virtual void domethodB (void) = 0;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   173
};
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   174
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   175
const InterfaceId AnExtendedInterface::iid = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   176
  MakeInterfaceId ("AnExtendedInterface", AnInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   177
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   178
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   179
// Then you need provide an implementation for the virtual 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   180
// methods.  If you are providing a new implementation for 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   181
// everything, the answer is straightforward
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   182
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   183
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   184
class ANewImplementation : public AnExtendedInterface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   185
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   186
public:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   187
  static const InterfaceId iid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   188
  static const ClassId cid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   189
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   190
  ANewImplementation ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   191
  void methodImpl (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   192
private:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   193
  virtual void domethodA (void) { /* new-implementation-behavior ();  */}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   194
  virtual void domethodB (void) { /* new-implementation-behavior (); */}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   195
};
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   196
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   197
ANewImplementation::ANewImplementation (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   198
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   199
  // enable our interface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   200
  SetInterfaceId (ANewImplementation::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   201
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   202
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   203
void
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   204
ANewImplementation::methodImpl (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   205
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
   206
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   207
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   208
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   209
const InterfaceId ANewImplementation::iid = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   210
  MakeInterfaceId ("ANewImplementation", AnExtendedInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   211
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   212
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   213
// If you want to extend an existing implementation, you can use 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   214
// the existing class to instantiate an implementation of its 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   215
// methods (hasa) and do the following if you can use stuff from 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   216
// the existing class.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   217
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   218
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   219
class AnExtendedImplementation : public AnExtendedInterface
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   220
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   221
public:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   222
  static const InterfaceId iid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   223
  static const ClassId cid;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   224
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   225
  AnExtendedImplementation ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   226
  void methodImpl (void) { /* pImpl->methodImpl (); */ }
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   227
  void methodExtendedImpl (void);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   228
private:
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   229
  virtual void domethodA (void) { /* new-implementation-behavior (); */}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   230
  virtual void domethodB (void) { /* new-implementation-behavior (); */}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   231
  Ptr<AnImplementation> pImpl;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   232
};
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   233
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   234
AnExtendedImplementation::AnExtendedImplementation (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   235
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   236
  pImpl = Create<AnImplementation> (); 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   237
  SetInterfaceId (AnExtendedImplementation::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   238
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   239
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   240
void
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   241
AnExtendedImplementation::methodExtendedImpl (void)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   242
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 766
diff changeset
   243
  NS_LOG_FUNCTION;
766
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   244
}
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   245
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   246
const InterfaceId AnExtendedImplementation::iid = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   247
  MakeInterfaceId ("AnExtendedImplementation", AnExtendedInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   248
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   249
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   250
// Inheriting from an existing implementation (isa) and an extended
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   251
// interface is tricky, because of the diamond multiple inheritance
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   252
// problem.  If the pImpl method above is not desirable, it may
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   253
// be that the implementation extension could be aggregated.
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   254
// 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   255
// The extension will not have access to the base implementation,
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   256
// so this design pattern may be more appropriate if the extension
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   257
// is very modular (e.g., add an EnergyModel to a wireless interface)
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   258
//
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   259
// EXAMPLE NOT YET PROVIDED
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   260
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   261
int main (int argc, char *argv[])
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   262
{
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   263
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   264
  Ptr<AnInterface> aBase = ComponentManager::Create<AnImplementation> 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   265
    (AnImplementation::cid, AnInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   266
  NS_ASSERT (aBase != 0);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   267
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   268
  aBase->methodA ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   269
  //aBase->methodImpl (); // XXX won't compile, aBase not right ptr type
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   270
  
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   271
  Ptr<AnImplementation> aBaseImplPtr = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   272
    aBase-> QueryInterface<AnImplementation> (AnImplementation::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   273
  aBaseImplPtr->methodImpl ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   274
  aBaseImplPtr->methodA();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   275
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   276
  // Test symmetric property of QueryInterface 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   277
  Ptr<AnInterface> aBase2 = 
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   278
    aBaseImplPtr-> QueryInterface<AnInterface> (AnInterface::iid);
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   279
  aBase2->methodA ();
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   280
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   281
  return 0;
aa1dff7879c8 Add main-query-interface.cc sample file
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   282
}