core: Separate C++ demangler from CallbackImplBase

This commit is contained in:
Gabriel Ferreira
2024-08-13 11:21:26 +02:00
parent 5781b03ef3
commit 7a55c57c9a
5 changed files with 125 additions and 70 deletions

View File

@@ -183,6 +183,7 @@ set(source_files
model/realtime-simulator-impl.cc
model/wall-clock-synchronizer.cc
model/matrix-array.cc
model/demangle.cc
)
# Define core lib headers
@@ -211,6 +212,7 @@ set(header_files
model/config.h
model/default-deleter.h
model/default-simulator-impl.h
model/demangle.h
model/deprecated.h
model/des-metrics.h
model/double.h

View File

@@ -88,67 +88,3 @@ CallbackValue::DeserializeFromString(std::string value, Ptr<const AttributeCheck
ATTRIBUTE_CHECKER_IMPLEMENT(Callback);
} // namespace ns3
#if (__GNUC__ >= 3)
#include <cstdlib>
#include <cxxabi.h>
namespace ns3
{
std::string
CallbackImplBase::Demangle(const std::string& mangled)
{
NS_LOG_FUNCTION(mangled);
int status;
char* demangled = abi::__cxa_demangle(mangled.c_str(), nullptr, nullptr, &status);
std::string ret;
if (status == 0)
{
NS_ASSERT(demangled);
ret = demangled;
}
else if (status == -1)
{
NS_LOG_UNCOND("Callback demangling failed: Memory allocation failure occurred.");
ret = mangled;
}
else if (status == -2)
{
NS_LOG_UNCOND("Callback demangling failed: Mangled name is not a valid under the C++ ABI "
"mangling rules.");
ret = mangled;
}
else if (status == -3)
{
NS_LOG_UNCOND("Callback demangling failed: One of the arguments is invalid.");
ret = mangled;
}
else
{
NS_LOG_UNCOND("Callback demangling failed: status " << status);
ret = mangled;
}
if (demangled)
{
std::free(demangled);
}
return ret;
}
} // namespace ns3
#else
std::string
ns3::CallbackImplBase::Demangle(const std::string& mangled)
{
NS_LOG_FUNCTION(this << mangled);
return mangled;
}
#endif

View File

@@ -23,6 +23,7 @@
#include "attribute-helper.h"
#include "attribute.h"
#include "demangle.h"
#include "fatal-error.h"
#include "ptr.h"
#include "simple-ref-count.h"
@@ -99,12 +100,6 @@ class CallbackImplBase : public SimpleRefCount<CallbackImplBase>
virtual std::string GetTypeid() const = 0;
protected:
/**
* \param [in] mangled The mangled string
* \return The demangled form of mangled
*/
static std::string Demangle(const std::string& mangled);
/**
* Helper to get the C++ typeid as a string.
*

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2005,2006 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
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
* Modified by: Gabriel Ferreira <gabrielcarvfer@gmail.com>
* Moved the Demangle function out of CallbackImplBase
*/
#include "demangle.h"
#include "assert.h"
#include "log.h"
NS_LOG_COMPONENT_DEFINE("Demangle");
#if (__GNUC__ >= 3)
#include <cstdlib>
#include <cxxabi.h>
std::string
ns3::Demangle(const std::string& mangled)
{
NS_LOG_FUNCTION(mangled);
int status;
char* demangled = abi::__cxa_demangle(mangled.c_str(), nullptr, nullptr, &status);
std::string ret;
if (status == 0)
{
NS_ASSERT(demangled);
ret = demangled;
}
else if (status == -1)
{
NS_LOG_DEBUG("Demangling failed: Memory allocation failure occurred.");
ret = mangled;
}
else if (status == -2)
{
NS_LOG_DEBUG("Demangling failed: Mangled name is not a valid under the C++ ABI "
"mangling rules.");
ret = mangled;
}
else if (status == -3)
{
NS_LOG_DEBUG("Demangling failed: One of the arguments is invalid.");
ret = mangled;
}
else
{
NS_LOG_DEBUG("Demangling failed: status " << status);
ret = mangled;
}
if (demangled)
{
std::free(demangled);
}
return ret;
}
#else
std::string
ns3::Demangle(const std::string& mangled)
{
NS_LOG_FUNCTION(mangled);
return mangled;
}
#endif

36
src/core/model/demangle.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2005,2006 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
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
* Modified by: Gabriel Ferreira <gabrielcarvfer@gmail.com>
* Moved the Demangle function out of CallbackImplBase
*/
#ifndef NS3_DEMANGLE_H
#define NS3_DEMANGLE_H
#include <string>
namespace ns3
{
/**
* \param [in] mangled The mangled string
* \return The demangled form of mangled
*/
std::string Demangle(const std::string& mangled);
}; // namespace ns3
#endif // NS3_DEMANGLE_H