SourceInfo.h
1 /*
2  * Copyright (C) 2004-2008 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef Pt_SourceInfo_h
29 #define Pt_SourceInfo_h
30 
31 #include <Pt/Api.h>
32 #include <string>
33 
34 // GNU C++ compiler
35 #ifdef __GNUC__
36  #define PT_FUNCTION __PRETTY_FUNCTION__
37 // Borland C++
38 #elif defined(__BORLANDC__)
39  #define PT_FUNCTION __FUNC__
40 // Microsoft C++ compiler
41 #elif defined(_MSC_VER)
42  // .NET 2003 support's demangled function names
43  #if _MSC_VER >= 1300
44  #define PT_FUNCTION __FUNCDNAME__
45  #else
46  #define PT_FUNCTION __FUNCTION__
47  #endif
48 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
49  #define PT_FUNCTION __func__
50 // otherwise use standard macro
51 #else
52  #define PT_FUNCTION "unknown symbol"
53 #endif
54 
55 #define PT_STRINGIFY(x) #x
56 #define PT_TOSTRING(x) PT_STRINGIFY(x)
57 
62 #define PT_SOURCEINFO_STR __FILE__ ":" PT_TOSTRING(__LINE__)
63 
68 #define PT_ERROR_MSG(msg) __FILE__ ":" PT_TOSTRING(__LINE__) ": " #msg
69 
74 #define PT_SOURCEINFO Pt::SourceInfo(__FILE__, PT_TOSTRING(__LINE__), PT_FUNCTION)
75 
76 namespace Pt {
77 
101 {
102  public:
109  inline SourceInfo(const char* f, const char* ln, const char* fn)
110  : _file(f), _line(ln), _func(fn)
111  { }
112 
115  inline const char* file() const
116  { return _file; }
117 
120  inline const char* line() const
121  { return _line; }
122 
125  inline const char* func() const
126  { return _func; }
127 
128  private:
129  const char* _file;
130  const char* _line;
131  const char* _func;
132 };
133 
138 inline std::string operator+(const std::string& what, const SourceInfo& info)
139 {
140  return std::string( info.file() ) + ':' + info.line() + ": " += what;
141 }
142 
147 inline std::string operator+(const char* what, const SourceInfo& info)
148 {
149  return std::string( info.file() ) + ':' + info.line() + ": " += what;
150 }
151 
156 inline std::string operator+( const SourceInfo& info, const std::string& what)
157 {
158  return std::string( info.file() ) + ':' + info.line() + ": " += what;
159 }
160 
165 inline std::string operator+(const SourceInfo& info, const char* what)
166 {
167  return std::string( info.file() ) + ':' + info.line() + ": " += what;
168 }
169 
170 } // namespace Pt
171 
172 #endif
std::string operator+(const char *what, const SourceInfo &info)
Concatenates a string with a SourceInfo.
Definition: SourceInfo.h:147
const char * func() const
Returns the function signature.
Definition: SourceInfo.h:125
std::string operator+(const SourceInfo &info, const std::string &what)
Concatenates a string with a SourceInfo.
Definition: SourceInfo.h:156
Source code info class.
Definition: SourceInfo.h:100
SourceInfo(const char *f, const char *ln, const char *fn)
Constructor.
Definition: SourceInfo.h:109
const char * file() const
Returns the filename.
Definition: SourceInfo.h:115
std::string operator+(const std::string &what, const SourceInfo &info)
Concatenates a string with a SourceInfo.
Definition: SourceInfo.h:138
const char * line() const
Returns the line number.
Definition: SourceInfo.h:120
std::string operator+(const SourceInfo &info, const char *what)
Concatenates a string with a SourceInfo.
Definition: SourceInfo.h:165