Files
unison/src/core/model/enum.h

219 lines
5.9 KiB
C
Raw Normal View History

2008-03-10 00:38:48 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2008 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2008-02-04 23:13:54 +01:00
#ifndef ENUM_VALUE_H
#define ENUM_VALUE_H
2008-02-20 21:45:42 +01:00
#include "attribute.h"
#include "attribute-accessor-helper.h"
2008-02-04 23:13:54 +01:00
#include <list>
/**
* \file
* \ingroup attribute_Enum
2017-09-18 00:51:28 -07:00
* ns3::EnumValue attribute value declarations.
*/
2008-02-04 23:13:54 +01:00
namespace ns3 {
// Additional docs for class EnumValue:
2008-03-10 14:05:26 -07:00
/**
2014-10-21 16:14:35 -07:00
* Hold variables of type \c enum
2008-03-10 14:05:26 -07:00
*
* This class can be used to hold variables of any kind
* of enum.
2014-10-21 16:14:35 -07:00
*
* This is often used with ObjectFactory and Config to bind
* the value of a particular enum to an Attribute or Config name.
* For example,
* \code
* Ptr<RateErrorModel> model = CreateObjectWithAttributes<RateErrorModel> (
* "ErrorRate", DoubleValue (0.05),
* "ErrorUnit", EnumValue (RateErrorModel::ERROR_UNIT_PACKET));
*
* Config::SetDefault ("ns3::RipNg::SplitHorizon",
* EnumValue (RipNg::NO_SPLIT_HORIZON));
* \endcode
2008-03-10 14:05:26 -07:00
*/
class EnumValue : public AttributeValue
2008-02-04 23:13:54 +01:00
{
public:
EnumValue ();
2014-10-21 16:14:35 -07:00
/**
* Construct from an explicit value.
*
* \param [in] value The value to begin with.
*/
EnumValue (int value);
void Set (int value);
2008-02-04 23:13:54 +01:00
int Get (void) const;
2008-10-17 14:15:52 +02:00
template <typename T>
2014-10-21 16:14:35 -07:00
bool GetAccessor (T & value) const;
2008-02-04 23:13:54 +01:00
virtual Ptr<AttributeValue> Copy (void) const;
2008-02-18 00:18:45 +01:00
virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
2008-02-04 23:13:54 +01:00
private:
2014-10-21 16:14:35 -07:00
int m_value; //!< The stored integer value.
2008-02-04 23:13:54 +01:00
};
2008-10-17 14:15:52 +02:00
template <typename T>
2014-10-21 16:14:35 -07:00
bool
EnumValue::GetAccessor (T &value) const
2008-10-17 14:15:52 +02:00
{
2014-10-21 16:14:35 -07:00
value = T (m_value);
2008-10-17 14:15:52 +02:00
return true;
}
2008-02-18 00:18:45 +01:00
class EnumChecker : public AttributeChecker
2008-02-04 23:13:54 +01:00
{
public:
2008-02-18 00:18:45 +01:00
EnumChecker ();
2008-02-04 23:13:54 +01:00
2014-10-21 16:14:35 -07:00
/**
* Add a default value.
* \param [in] value The value.
* \param [in] name Then enum symbol name.
*/
void AddDefault (int value, std::string name);
/**
* Add a new value.
* \param [in] value The value.
2020-04-10 16:09:51 -07:00
* \param [in] name The enum symbol name.
2014-10-21 16:14:35 -07:00
*/
void Add (int value, std::string name);
2008-02-04 23:13:54 +01:00
2020-04-10 16:09:51 -07:00
/**
* Get the enum symbol name by value.
* \param [in] value The value.
* \return The enum symbol name.
*/
std::string GetName (int value) const;
/**
* Get the enum value by name.
* \param [in] name Then enum symbol name.
* \returns The enum value.
*/
int GetValue (const std::string name) const;
2020-04-10 16:09:51 -07:00
// Inherited
virtual bool Check (const AttributeValue &value) const;
virtual std::string GetValueTypeName (void) const;
virtual bool HasUnderlyingTypeInformation (void) const;
virtual std::string GetUnderlyingTypeInformation (void) const;
virtual Ptr<AttributeValue> Create (void) const;
virtual bool Copy (const AttributeValue &src, AttributeValue &dst) const;
2008-02-04 23:13:54 +01:00
private:
2020-04-10 16:09:51 -07:00
/** Type for the pair value, name */
typedef std::pair<int,std::string> Value;
2014-10-21 16:14:35 -07:00
/** Type of container for storing Enum values and symbol names. */
2020-04-10 16:09:51 -07:00
typedef std::list<Value> ValueSet;
2014-10-21 16:14:35 -07:00
/** The stored Enum values and symbol names. */
2008-02-04 23:13:54 +01:00
ValueSet m_valueSet;
};
2008-02-18 00:18:45 +01:00
template <typename T1>
2008-02-20 20:57:59 +01:00
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1);
2008-02-18 00:18:45 +01:00
template <typename T1, typename T2>
2008-02-20 20:57:59 +01:00
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1, T2 a2);
2014-10-21 16:14:35 -07:00
/**
* Make an EnumChecker pre-configured with a set of allowed
* values by name.
*
* Values are normally given as fully qualified enum symbols
* with matching names. For example,
* \c MakeEnumChecker (RipNg::SPLIT_HORIZON, "ns3::RipNg::SplitHorizon");
*
* As many additional enum value, name pairs as desired can be passed
* as arguments.
*
2014-10-21 16:14:35 -07:00
* \see AttributeChecker
*
* \tparam Ts The type list of additional parameters. Additional parameters
* should be int, string pairs.
2014-10-21 16:14:35 -07:00
* \returns The AttributeChecker
* \param [in] v The default enum value.
* \param [in] n The corresponding name.
2021-12-21 17:06:57 +00:00
* \param [in] args Any additional arguments.
2020-03-25 15:12:18 -07:00
*/
template <typename... Ts>
Ptr<const AttributeChecker>
MakeEnumChecker (int v, std::string n, Ts... args)
{
Ptr<EnumChecker> checker = Create<EnumChecker> ();
checker->AddDefault (v, n);
return MakeEnumChecker (checker, args...);
}
2008-02-04 23:13:54 +01:00
/**
* Handler for enum value, name pairs other than the default.
*
* \tparam Ts The type list of additional parameters. Additional parameters
* should be in int, string pairs.
* \returns The AttributeChecker
* \param [in] checker The AttributeChecker.
* \param [in] v The next enum value.
* \param [in] n The corresponding name.
2021-12-21 17:06:57 +00:00
* \param [in] args Any additional arguments.
*/
2008-02-04 23:13:54 +01:00
template <typename... Ts>
Ptr<const AttributeChecker>
MakeEnumChecker (Ptr<EnumChecker> checker, int v, std::string n, Ts... args)
{
checker->Add (v, n);
return MakeEnumChecker (checker, args...);
}
/**
* Terminate the recursion of variadic arguments.
*
2021-12-21 17:06:57 +00:00
* \returns The \p checker
* \param [in] checker The AttributeChecker.
*/
// inline to allow tail call optimization
inline
Ptr<const AttributeChecker>
MakeEnumChecker (Ptr<EnumChecker> checker)
{
return checker;
}
2008-02-04 23:13:54 +01:00
2008-02-18 00:18:45 +01:00
template <typename T1>
2008-02-20 20:57:59 +01:00
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1)
2008-02-18 00:18:45 +01:00
{
return MakeAccessorHelper<EnumValue> (a1);
2008-02-18 00:18:45 +01:00
}
template <typename T1, typename T2>
2008-02-20 20:57:59 +01:00
Ptr<const AttributeAccessor> MakeEnumAccessor (T1 a1, T2 a2)
2008-02-04 23:13:54 +01:00
{
return MakeAccessorHelper<EnumValue> (a1, a2);
2008-02-04 23:13:54 +01:00
}
} // namespace ns3
#endif /* ENUM_VALUE_H */