src/core/interface.h
changeset 578 e305018537c9
parent 573 0da9201a048e
child 579 7e31a7f75fee
equal deleted inserted replaced
577:a700b8761829 578:e305018537c9
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2007 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 #ifndef INTERFACE_H
       
    22 #define INTERFACE_H
       
    23 
       
    24 #include <string>
       
    25 #include "ptr.h"
       
    26 
       
    27 namespace ns3 {
       
    28 
       
    29 class NsUnknownImpl;
       
    30 
       
    31 class Iid
       
    32 {
       
    33 public:
       
    34   Iid (std::string name);
       
    35 private:
       
    36   friend bool operator == (const Iid &a, const Iid &b);
       
    37   uint32_t m_iid;
       
    38 };
       
    39 
       
    40 /**
       
    41  * \brief COM-like IUnknown
       
    42  *
       
    43  * This class should be used as a base class for every object which
       
    44  * wishes to provide a COM-like QueryInterface API. Multiple 
       
    45  * inheritance where this base class is at the top of the dreaded 
       
    46  * "diamond" shape is not allowed.
       
    47  */
       
    48 class NsUnknown
       
    49 {
       
    50 public:
       
    51   virtual ~NsUnknown ();
       
    52   void Ref (void) const;
       
    53   void Unref (void) const;
       
    54 
       
    55   /**
       
    56    * \param iid the NsUnknown id of the requested interface
       
    57    */
       
    58   template <typename T>
       
    59   Ptr<T> QueryInterface (Iid iid) const;
       
    60 
       
    61   /**
       
    62    * \param interface another interface
       
    63    * 
       
    64    * Aggregate together the two interfaces. After this call,
       
    65    * the two interface objects are tied together: each of them
       
    66    * will be able to perform QI on each other and their lifetimes
       
    67    * will be found by the same reference count.
       
    68    */
       
    69   void AddInterface (Ptr<NsUnknown> interface);
       
    70 
       
    71   void Dispose (void);
       
    72 protected:
       
    73   /**
       
    74    * \param iid the Interface Id of the interface defined by a direct subclass
       
    75    * of this base class
       
    76    *
       
    77    * If you are a direct subclass of this class, you _must_ register
       
    78    * the name of your interface with this constructor.
       
    79    */
       
    80   NsUnknown (Iid iid);
       
    81   /**
       
    82    * \param iid the Interface id of the interface
       
    83    * \param a pointer to the interface object
       
    84    *
       
    85    * If you are not a direct subclass of the ns3::NsUnknown base class,
       
    86    * and if you want to register yourself as another accessible interface
       
    87    * (typically, your subclass has added API), you need to call
       
    88    * this method to associate an interface id to your interface.
       
    89    */
       
    90   void AddSelfInterface (Iid iid, Ptr<NsUnknown> interface);
       
    91 protected:
       
    92   /**
       
    93    * Subclasses who want to handle the "dispose" event should
       
    94    * override this method. They are also responsible for
       
    95    * "chaining up" to their parent class' DoDispose method
       
    96    * once they have done their own "dispose".
       
    97    */
       
    98   virtual void DoDispose (void);
       
    99 private:
       
   100   friend class NsUnknownImpl;
       
   101   NsUnknown ();
       
   102   Ptr<NsUnknown> DoQueryInterface (Iid iid) const;
       
   103   void RefInternal (void);
       
   104   void UnrefInternal (void);
       
   105   NsUnknownImpl *m_impl;
       
   106   uint32_t m_ref;
       
   107 };
       
   108 
       
   109 }//namespace ns3
       
   110 
       
   111 namespace ns3 {
       
   112 
       
   113 template <typename T>
       
   114 Ptr<T>
       
   115 NsUnknown::QueryInterface (Iid iid) const
       
   116 {
       
   117   Ptr<NsUnknown> found = DoQueryInterface (iid);
       
   118   if (found != 0)
       
   119     {
       
   120       return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
       
   121     }
       
   122   return 0;
       
   123 }
       
   124 
       
   125 
       
   126 }//namespace ns3
       
   127 
       
   128 #endif /* INTERFACE_H */