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

126 lines
3.5 KiB
C++
Raw Normal View History

2008-03-10 00:38:48 +01:00
/*
* 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:04:18 +01:00
#include "integer.h"
2022-10-07 20:08:35 +00:00
2008-02-04 22:32:13 +01:00
#include "fatal-error.h"
#include "log.h"
2022-10-07 20:08:35 +00:00
2008-02-04 22:18:07 +01:00
#include <sstream>
/**
* \file
* \ingroup attribute_Integer
2017-09-18 00:51:28 -07:00
* ns3::MakeIntegerChecker implementation.
*/
2022-10-07 20:08:35 +00:00
namespace ns3
{
2008-02-04 22:18:07 +01:00
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("Integer");
2022-10-07 20:08:35 +00:00
ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(int64_t, Integer);
2022-10-07 20:08:35 +00:00
namespace internal
{
/**
* \ingroup attribute_Integer
* Make an Integer attribute checker with embedded numeric type name.
*
* \param [in] min The minimum allowed value.
* \param [in] max The maximum allowed value.
* \param [in] name The original type name ("int8_t", "int16_t", _etc_.).
* \returns The AttributeChecker.
*/
2008-02-20 20:57:59 +01:00
Ptr<const AttributeChecker>
2022-10-07 20:08:35 +00:00
MakeIntegerChecker(int64_t min, int64_t max, std::string name)
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(min << max << name);
struct IntegerChecker : public AttributeChecker
2020-03-25 15:12:18 -07:00
{
2022-10-07 20:08:35 +00:00
IntegerChecker(int64_t minValue, int64_t maxValue, std::string name)
: m_minValue(minValue),
m_maxValue(maxValue),
m_name(name)
2011-05-13 14:52:27 -04:00
{
}
2022-10-07 20:08:35 +00:00
bool Check(const AttributeValue& value) const override
{
NS_LOG_FUNCTION(&value);
const IntegerValue* v = dynamic_cast<const IntegerValue*>(&value);
if (v == nullptr)
{
return false;
}
return v->Get() >= m_minValue && v->Get() <= m_maxValue;
}
std::string GetValueTypeName() const override
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
return "ns3::IntegerValue";
}
2022-10-07 20:08:35 +00:00
bool HasUnderlyingTypeInformation() const override
{
NS_LOG_FUNCTION_NOARGS();
return true;
}
std::string GetUnderlyingTypeInformation() const override
{
NS_LOG_FUNCTION_NOARGS();
std::ostringstream oss;
oss << m_name << " " << m_minValue << ":" << m_maxValue;
return oss.str();
}
Ptr<AttributeValue> Create() const override
{
NS_LOG_FUNCTION_NOARGS();
return ns3::Create<IntegerValue>();
}
bool Copy(const AttributeValue& src, AttributeValue& dst) const override
{
NS_LOG_FUNCTION(&src << &dst);
const IntegerValue* source = dynamic_cast<const IntegerValue*>(&src);
IntegerValue* destination = dynamic_cast<IntegerValue*>(&dst);
if (source == nullptr || destination == nullptr)
{
return false;
}
*destination = *source;
return true;
}
int64_t m_minValue;
int64_t m_maxValue;
std::string m_name;
}* checker = new IntegerChecker(min, max, name);
return Ptr<AttributeChecker>(checker, false);
}
} // namespace internal
2008-02-04 22:18:07 +01:00
} // namespace ns3