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

191 lines
5.0 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-21 19:16:00 +01:00
#include "enum.h"
2008-02-04 23:13:54 +01:00
#include "fatal-error.h"
#include "log.h"
2020-04-10 16:09:51 -07:00
#include <algorithm> // find_if
2008-02-04 23:13:54 +01:00
#include <sstream>
#include <numeric> // std::accumulate
2008-02-04 23:13:54 +01:00
/**
* \file
* \ingroup attribute_Enum
2017-09-18 00:51:28 -07:00
* ns3::EnumValue attribute value implementation.
*/
2008-02-04 23:13:54 +01:00
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("Enum");
EnumValue::EnumValue ()
2014-10-21 16:14:35 -07:00
: m_value ()
2011-05-13 14:52:27 -04:00
{
NS_LOG_FUNCTION (this);
2011-05-13 14:52:27 -04:00
}
2014-10-21 16:14:35 -07:00
EnumValue::EnumValue (int value)
: m_value (value)
2011-05-13 14:52:27 -04:00
{
2014-10-21 16:14:35 -07:00
NS_LOG_FUNCTION (this << value);
2011-05-13 14:52:27 -04:00
}
void
2014-10-21 16:14:35 -07:00
EnumValue::Set (int value)
2008-02-04 23:13:54 +01:00
{
2014-10-21 16:14:35 -07:00
NS_LOG_FUNCTION (this << value);
m_value = value;
2008-02-04 23:13:54 +01:00
}
2011-05-13 14:52:27 -04:00
int
EnumValue::Get (void) const
2008-02-04 23:13:54 +01:00
{
NS_LOG_FUNCTION (this);
2014-10-21 16:14:35 -07:00
return m_value;
2008-02-04 23:13:54 +01:00
}
Ptr<AttributeValue>
EnumValue::Copy (void) const
2008-02-04 23:13:54 +01:00
{
NS_LOG_FUNCTION (this);
return ns3::Create<EnumValue> (*this);
2008-02-04 23:13:54 +01:00
}
2020-03-25 15:12:18 -07:00
std::string
EnumValue::SerializeToString (Ptr<const AttributeChecker> checker) const
2008-02-04 23:13:54 +01:00
{
NS_LOG_FUNCTION (this << checker);
2008-02-18 00:18:45 +01:00
const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
2008-02-04 23:13:54 +01:00
NS_ASSERT (p != 0);
2020-04-10 16:09:51 -07:00
std::string name = p->GetName (m_value);
return name;
2008-02-04 23:13:54 +01:00
}
2020-03-25 15:12:18 -07:00
bool
EnumValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
2008-02-04 23:13:54 +01:00
{
NS_LOG_FUNCTION (this << value << checker);
2008-02-18 00:18:45 +01:00
const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
2008-02-04 23:13:54 +01:00
NS_ASSERT (p != 0);
2020-04-10 16:09:51 -07:00
m_value = p->GetValue (value);
return true;
2008-02-04 23:13:54 +01:00
}
2008-02-18 00:18:45 +01:00
EnumChecker::EnumChecker ()
2011-05-13 14:52:27 -04:00
{
NS_LOG_FUNCTION (this);
2011-05-13 14:52:27 -04:00
}
2008-02-04 23:13:54 +01:00
2011-05-13 14:52:27 -04:00
void
2014-10-21 16:14:35 -07:00
EnumChecker::AddDefault (int value, std::string name)
2008-02-04 23:13:54 +01:00
{
2014-10-21 16:14:35 -07:00
NS_LOG_FUNCTION (this << value << name);
m_valueSet.push_front (std::make_pair (value, name));
2008-02-04 23:13:54 +01:00
}
2011-05-13 14:52:27 -04:00
void
2014-10-21 16:14:35 -07:00
EnumChecker::Add (int value, std::string name)
2008-02-04 23:13:54 +01:00
{
2014-10-21 16:14:35 -07:00
NS_LOG_FUNCTION (this << value << name);
m_valueSet.push_back (std::make_pair (value, name));
2008-02-04 23:13:54 +01:00
}
2020-04-10 16:09:51 -07:00
std::string
EnumChecker::GetName (int value) const
{
auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
[value] (Value v) { return v.first == value; } );
NS_ASSERT_MSG (it != m_valueSet.end (),
"invalid enum value " << value << "! Missed entry in MakeEnumChecker?");
return it->second;
}
int
EnumChecker::GetValue (const std::string name) const
{
auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
[name] (Value v) { return v.second == name; } );
NS_ASSERT_MSG (it != m_valueSet.end (),
"name " << name << " is not a valid enum value. Missed entry in MakeEnumChecker?\nAvailable values: " <<
std::accumulate (m_valueSet.begin (), m_valueSet.end (), std::string{}, [](std::string a, Value v) {
if (a.empty ())
{
return v.second;
}
else
{
return std::move (a) + ", " + v.second;
}
}));
2020-04-10 16:09:51 -07:00
return it->first;
}
2011-05-13 14:52:27 -04:00
bool
EnumChecker::Check (const AttributeValue &value) const
2008-02-04 23:13:54 +01:00
{
NS_LOG_FUNCTION (this << &value);
const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
2008-02-04 23:13:54 +01:00
if (p == 0)
{
return false;
}
2020-04-10 16:09:51 -07:00
auto pvalue = p->Get ();
auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
[pvalue] (Value v) { return v.first == pvalue; } );
return (it != m_valueSet.end ()) ? true : false;
2008-02-04 23:13:54 +01:00
}
2020-03-25 15:12:18 -07:00
std::string
EnumChecker::GetValueTypeName (void) const
{
NS_LOG_FUNCTION (this);
return "ns3::EnumValue";
}
2020-03-25 15:12:18 -07:00
bool
EnumChecker::HasUnderlyingTypeInformation (void) const
{
NS_LOG_FUNCTION (this);
return true;
}
2020-03-25 15:12:18 -07:00
std::string
EnumChecker::GetUnderlyingTypeInformation (void) const
{
NS_LOG_FUNCTION (this);
std::ostringstream oss;
2020-04-10 16:09:51 -07:00
bool moreValues = false;
2020-06-08 17:28:07 -07:00
for (const auto &i : m_valueSet)
{
2020-04-10 16:09:51 -07:00
oss << (moreValues ? "|" : "") << i.second;
moreValues = true;
}
return oss.str ();
}
Ptr<AttributeValue>
EnumChecker::Create (void) const
{
NS_LOG_FUNCTION (this);
return ns3::Create<EnumValue> ();
}
2020-03-25 15:12:18 -07:00
bool
EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
{
NS_LOG_FUNCTION (this << &source << &destination);
const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
if (src == 0 || dst == 0)
{
return false;
}
*dst = *src;
return true;
}
2008-02-04 23:13:54 +01:00
} // namespace ns3