src/core/pointer.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2926
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2926
     2
/*
mathieu@2926
     3
 * Copyright (c) 2008 INRIA
mathieu@2926
     4
 *
mathieu@2926
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2926
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2926
     7
 * published by the Free Software Foundation;
mathieu@2926
     8
 *
mathieu@2926
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2926
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2926
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2926
    12
 * GNU General Public License for more details.
mathieu@2926
    13
 *
mathieu@2926
    14
 * You should have received a copy of the GNU General Public License
mathieu@2926
    15
 * along with this program; if not, write to the Free Software
mathieu@2926
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2926
    17
 *
mathieu@2926
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2926
    19
 */
mathieu@2926
    20
#ifndef NS_POINTER_H
mathieu@2926
    21
#define NS_POINTER_H
mathieu@2926
    22
mathieu@2926
    23
#include "attribute.h"
mathieu@2926
    24
#include "object.h"
mathieu@2926
    25
mathieu@2926
    26
namespace ns3 {
mathieu@2926
    27
mathieu@2969
    28
/**
tomh@3182
    29
 * \ingroup attribute
tomh@3182
    30
 *
mathieu@2969
    31
 * \brief hold objects of type Ptr<T>
mathieu@2969
    32
 */
mathieu@2965
    33
class PointerValue : public AttributeValue
mathieu@2926
    34
{
mathieu@2926
    35
public:
mathieu@2965
    36
  PointerValue ();
mathieu@2926
    37
mathieu@2965
    38
  PointerValue (Ptr<Object> object);
mathieu@2926
    39
mathieu@2926
    40
  void SetObject (Ptr<Object> object);
mathieu@2926
    41
mathieu@2926
    42
  Ptr<Object> GetObject (void) const;
mathieu@2926
    43
mathieu@2926
    44
  template <typename T>
mathieu@2965
    45
  PointerValue (const Ptr<T> &object);
mathieu@2926
    46
mathieu@2926
    47
  template <typename T>
mathieu@2926
    48
  void Set (const Ptr<T> &object);
mathieu@2926
    49
mathieu@2926
    50
  template <typename T>
mathieu@2926
    51
  Ptr<T> Get (void) const;
mathieu@2926
    52
mathieu@2926
    53
  template <typename T>
mathieu@3763
    54
  bool GetAccessor (Ptr<T> &v) const;
mathieu@3763
    55
mathieu@3763
    56
  template <typename T>
mathieu@2926
    57
  operator Ptr<T> () const;
mathieu@2926
    58
mathieu@2965
    59
  virtual Ptr<AttributeValue> Copy (void) const;
mathieu@2965
    60
  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
mathieu@2965
    61
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
mathieu@2965
    62
mathieu@2926
    63
private:
mathieu@2926
    64
  Ptr<Object> m_value;
mathieu@2926
    65
};
mathieu@2926
    66
mathieu@2926
    67
mathieu@2932
    68
class PointerChecker : public AttributeChecker 
mathieu@2932
    69
{
mathieu@2932
    70
public:
mathieu@2932
    71
  virtual TypeId GetPointeeTypeId (void) const = 0;
mathieu@2932
    72
};
mathieu@2926
    73
template <typename T>
mathieu@2926
    74
Ptr<AttributeChecker> MakePointerChecker (void);
mathieu@2926
    75
mathieu@2926
    76
} // namespace ns3
mathieu@2926
    77
mathieu@2926
    78
namespace ns3 {
mathieu@2926
    79
mathieu@2926
    80
mathieu@2926
    81
namespace internal {
mathieu@2926
    82
mathieu@2926
    83
template <typename T>
mathieu@2927
    84
class APointerChecker : public PointerChecker
mathieu@2926
    85
{
mathieu@2965
    86
  virtual bool Check (const AttributeValue &val) const {
mathieu@2965
    87
    const PointerValue *value = dynamic_cast<const PointerValue *> (&val);
mathieu@2926
    88
    if (value == 0)
mathieu@2926
    89
      {
mathieu@2926
    90
	return false;
mathieu@2926
    91
      }
mathieu@2965
    92
    if (value->GetObject () == 0)
mathieu@2926
    93
      {
mathieu@2926
    94
	return true;
mathieu@2926
    95
      }
mathieu@2965
    96
    T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ()));
mathieu@2926
    97
    if (ptr == 0)
mathieu@2926
    98
      {
mathieu@2926
    99
	return false;
mathieu@2926
   100
      }
mathieu@2926
   101
    return true;
mathieu@2926
   102
  }
mathieu@2969
   103
  virtual std::string GetValueTypeName (void) const {
mathieu@2969
   104
    return "ns3::PointerValue";
mathieu@2969
   105
  }
mathieu@2969
   106
  virtual bool HasUnderlyingTypeInformation (void) const {
mathieu@2969
   107
    return true;
mathieu@2969
   108
  }
mathieu@2969
   109
  virtual std::string GetUnderlyingTypeInformation (void) const {
mathieu@2926
   110
    TypeId tid = T::GetTypeId ();
mathieu@2970
   111
    return "ns3::Ptr< " + tid.GetName () + " >";
mathieu@2926
   112
  }
mathieu@2965
   113
  virtual Ptr<AttributeValue> Create (void) const {
mathieu@2965
   114
    return ns3::Create<PointerValue> ();
mathieu@2965
   115
  }
mathieu@2965
   116
  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
mathieu@2965
   117
    const PointerValue *src = dynamic_cast<const PointerValue *> (&source);
mathieu@2965
   118
    PointerValue *dst = dynamic_cast<PointerValue *> (&destination);
mathieu@2965
   119
    if (src == 0 || dst == 0)
mathieu@2965
   120
      {
mathieu@2965
   121
        return false;
mathieu@2965
   122
      }
mathieu@2965
   123
    *dst = *src;
mathieu@2965
   124
    return true;
mathieu@2926
   125
  }
mathieu@2932
   126
  virtual TypeId GetPointeeTypeId (void) const {
mathieu@2932
   127
    return T::GetTypeId ();
mathieu@2932
   128
  }
mathieu@2926
   129
};
mathieu@2926
   130
mathieu@2926
   131
} // namespace internal
mathieu@2926
   132
mathieu@2926
   133
template <typename T>
mathieu@2965
   134
PointerValue::PointerValue (const Ptr<T> &object)
mathieu@2926
   135
{
mathieu@2926
   136
  m_value = object;
mathieu@2926
   137
}
mathieu@2926
   138
mathieu@2926
   139
template <typename T>
mathieu@2926
   140
void 
mathieu@2965
   141
PointerValue::Set (const Ptr<T> &object)
mathieu@2926
   142
{
mathieu@2926
   143
  m_value = object;
mathieu@2926
   144
}
mathieu@2926
   145
mathieu@2926
   146
template <typename T>
mathieu@2926
   147
Ptr<T> 
mathieu@2965
   148
PointerValue::Get (void) const
mathieu@2926
   149
{
mathieu@2926
   150
  T *v = dynamic_cast<T *> (PeekPointer (m_value));
mathieu@2926
   151
  return v;
mathieu@2926
   152
}
mathieu@2926
   153
mathieu@2926
   154
template <typename T>
mathieu@2965
   155
PointerValue::operator Ptr<T> () const
mathieu@2926
   156
{
mathieu@2926
   157
  return Get<T> ();
mathieu@2926
   158
}
mathieu@2926
   159
mathieu@3763
   160
template <typename T>
mathieu@3763
   161
bool 
mathieu@3763
   162
PointerValue::GetAccessor (Ptr<T> &v) const
mathieu@2926
   163
{
mathieu@3763
   164
  Ptr<T> ptr = dynamic_cast<T*> (PeekPointer (m_value));
mathieu@3763
   165
  if (ptr == 0)
mathieu@3763
   166
    {
mathieu@3763
   167
      return false;
mathieu@2926
   168
    }
mathieu@3763
   169
  v = ptr;
mathieu@3763
   170
  return true;
mathieu@2926
   171
}
mathieu@2926
   172
mathieu@2926
   173
mathieu@3763
   174
ATTRIBUTE_ACCESSOR_DEFINE (Pointer);
mathieu@2926
   175
mathieu@2926
   176
template <typename T>
mathieu@2926
   177
Ptr<AttributeChecker>
mathieu@2926
   178
MakePointerChecker (void)
mathieu@2926
   179
{
mathieu@2926
   180
  return Create<internal::APointerChecker<T> > ();
mathieu@2926
   181
}
mathieu@2926
   182
mathieu@2926
   183
mathieu@2926
   184
} // namespace ns3
mathieu@2926
   185
mathieu@2926
   186
#endif /* NS_POINTER_H */