Files
ResInsight/VisualizationModules/LibCore/cvfCodeLocation.cpp
Jacob Støren 5a84a12864 Integrated new version of the Visualization modules:
From change List number 20662.
p4#: 20667
2013-02-28 11:37:32 +01:00

164 lines
5.1 KiB
C++

//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2012 Ceetron AS
//
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library 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 at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
//##################################################################################################
#include "cvfBase.h"
#include "cvfCodeLocation.h"
#include <algorithm>
#include <cstring>
namespace cvf {
//==================================================================================================
///
/// \class cvf::CodeLocation
/// \ingroup Core
///
/// Represents a source code location.
///
/// Typically used with logging, asserts etc. Typically initialized using built-in compiler macros
/// such as __FILE__ and __LINE__.
///
/// Note that the strings parameters for file name and function must be a static strings with a
/// lifetime that's longer than the lifetime of the CodeLocation object
///
//==================================================================================================
static const char* const EMPTY_STRING = "";
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CodeLocation::CodeLocation()
: m_fileName(EMPTY_STRING),
m_functionName(EMPTY_STRING),
m_lineNumber(-1)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CodeLocation::CodeLocation(const char* fileName, const char* functionName, int lineNumber)
: m_fileName(fileName),
m_functionName(functionName),
m_lineNumber(lineNumber)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CodeLocation::CodeLocation(const CodeLocation& other)
: m_fileName(other.m_fileName),
m_functionName(other.m_functionName),
m_lineNumber(other.m_lineNumber)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const CodeLocation& CodeLocation::operator=(CodeLocation rhs)
{
// Copy-and-swap (copy already done since parameter is passed by value)
rhs.swap(*this);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const char* CodeLocation::fileName() const
{
return m_fileName ? m_fileName : EMPTY_STRING;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const char* CodeLocation::shortFileName() const
{
if (m_fileName)
{
const char* ptrToLastSlash = strrchr(m_fileName, '/');
#ifdef WIN32
const char* ptrToLastBwdSlash = strrchr(m_fileName, '\\');
if (ptrToLastBwdSlash > ptrToLastSlash)
{
ptrToLastSlash = ptrToLastBwdSlash;
}
#endif
if (ptrToLastSlash)
{
return ptrToLastSlash + 1;
}
else
{
return m_fileName;
}
}
else
{
return EMPTY_STRING;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const char* CodeLocation::functionName() const
{
return m_functionName ? m_functionName : EMPTY_STRING;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int CodeLocation::lineNumber() const
{
return m_lineNumber;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CodeLocation::swap(CodeLocation& other)
{
std::swap(m_fileName, other.m_fileName);
std::swap(m_functionName, other.m_functionName);
std::swap(m_lineNumber, other.m_lineNumber);
}
} // namespace cvf