src/core/enum.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@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2381
    20
#ifndef ENUM_VALUE_H
mathieu@2381
    21
#define ENUM_VALUE_H
mathieu@2381
    22
mathieu@2438
    23
#include "attribute.h"
mathieu@2449
    24
#include "attribute-accessor-helper.h"
mathieu@2381
    25
#include <list>
mathieu@2381
    26
mathieu@2381
    27
namespace ns3 {
mathieu@2381
    28
mathieu@2584
    29
/**
tomh@3182
    30
 * \ingroup attribute
tomh@3182
    31
 *
mathieu@2584
    32
 * \brief hold variables of type 'enum'
mathieu@2584
    33
 *
mathieu@2584
    34
 * This class can be used to hold variables of any kind
mathieu@2584
    35
 * of enum.
mathieu@2584
    36
 */
mathieu@2965
    37
class EnumValue : public AttributeValue
mathieu@2381
    38
{
mathieu@2381
    39
public:
mathieu@2965
    40
  EnumValue ();
mathieu@2965
    41
  EnumValue (int v);
mathieu@2381
    42
  void Set (int v);
mathieu@2381
    43
  int Get (void) const;
mathieu@3763
    44
  template <typename T>
mathieu@3763
    45
  bool GetAccessor (T &v) const;
mathieu@2381
    46
mathieu@2965
    47
  virtual Ptr<AttributeValue> Copy (void) const;
mathieu@2427
    48
  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
mathieu@2427
    49
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
mathieu@2381
    50
mathieu@2381
    51
private:
mathieu@2381
    52
  int m_v;
mathieu@2381
    53
};
mathieu@2381
    54
mathieu@3763
    55
template <typename T>
mathieu@3763
    56
bool EnumValue::GetAccessor (T &v) const
mathieu@3763
    57
{
mathieu@3763
    58
  v = T (m_v);
mathieu@3763
    59
  return true;
mathieu@3763
    60
}
mathieu@3763
    61
mathieu@2427
    62
class EnumChecker : public AttributeChecker
mathieu@2381
    63
{
mathieu@2381
    64
public:
mathieu@2427
    65
  EnumChecker ();
mathieu@2381
    66
mathieu@2381
    67
  void AddDefault (int v, std::string name);
mathieu@2381
    68
  void Add (int v, std::string name);
mathieu@2381
    69
mathieu@2965
    70
  virtual bool Check (const AttributeValue &value) const;
mathieu@2969
    71
  virtual std::string GetValueTypeName (void) const;
mathieu@2969
    72
  virtual bool HasUnderlyingTypeInformation (void) const;
mathieu@2969
    73
  virtual std::string GetUnderlyingTypeInformation (void) const;
mathieu@2965
    74
  virtual Ptr<AttributeValue> Create (void) const;
mathieu@2965
    75
  virtual bool Copy (const AttributeValue &src, AttributeValue &dst) const;
mathieu@2381
    76
mathieu@2381
    77
private:
mathieu@2965
    78
  friend class EnumValue;
mathieu@2381
    79
  typedef std::list<std::pair<int,std::string> > ValueSet;
mathieu@2381
    80
  ValueSet m_valueSet;
mathieu@2381
    81
};
mathieu@2381
    82
mathieu@2427
    83
template <typename T1>
mathieu@2436
    84
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1);
mathieu@2427
    85
mathieu@2427
    86
template <typename T1, typename T2>
mathieu@2436
    87
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1, T2 a2);
mathieu@2427
    88
mathieu@2436
    89
Ptr<const AttributeChecker> MakeEnumChecker (int v1, std::string n1,
mathieu@2436
    90
					     int v2 = 0, std::string n2 = "",
mathieu@2436
    91
					     int v3 = 0, std::string n3 = "",
mathieu@2436
    92
					     int v4 = 0, std::string n4 = "",
mathieu@2436
    93
					     int v5 = 0, std::string n5 = "",
mathieu@2436
    94
					     int v6 = 0, std::string n6 = "",
mathieu@2436
    95
					     int v7 = 0, std::string n7 = "",
mathieu@2436
    96
					     int v8 = 0, std::string n8 = "",
mathieu@2436
    97
					     int v9 = 0, std::string n9 = "",
mathieu@2436
    98
					     int v10 = 0, std::string n10 = "",
mathieu@2436
    99
					     int v11 = 0, std::string n11 = "",
mathieu@2436
   100
					     int v12 = 0, std::string n12 = "");
mathieu@2381
   101
mathieu@2381
   102
mathieu@2381
   103
} // namespace ns3
mathieu@2381
   104
mathieu@2381
   105
namespace ns3 {
mathieu@2381
   106
mathieu@2427
   107
template <typename T1>
mathieu@2436
   108
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1)
mathieu@2381
   109
{
mathieu@2965
   110
  return MakeAccessorHelper<EnumValue> (a1);
mathieu@2427
   111
}
mathieu@2427
   112
mathieu@2427
   113
template <typename T1, typename T2>
mathieu@2436
   114
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1, T2 a2)
mathieu@2427
   115
{
mathieu@2965
   116
  return MakeAccessorHelper<EnumValue> (a1, a2);
mathieu@2381
   117
}
mathieu@2381
   118
mathieu@2381
   119
} // namespace ns3
mathieu@2381
   120
mathieu@2381
   121
#endif /* ENUM_VALUE_H */