sourceinfo.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2007 Marc Boris Duerner
3  * Copyright (C) 2006 Aloysius Indrayanto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 #ifndef cxxtools_SourceInfo_h
30 #define cxxtools_SourceInfo_h
31 
32 #include <string>
33 
34 // GNU C++ compiler
35 #ifdef __GNUC__
36  #define CXXTOOLS_FUNCTION __PRETTY_FUNCTION__
37 // Borland C++
38 #elif defined(__BORLANDC__)
39  #define CXXTOOLS_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 CXXTOOLS_FUNCTION __FUNCDNAME__
45  #else
46  #define CXXTOOLS_FUNCTION __FUNCTION__
47  #endif
48 // otherwise use standard macro
49 #else
50  #define CXXTOOLS_FUNCTION "unknown symbol"
51 #endif
52 
53 #define CXXTOOLS_STRINGIFY(x) #x
54 #define CXXTOOLS_TOSTRING(x) CXXTOOLS_STRINGIFY(x)
55 
56 #define CXXTOOLS_SOURCEINFO_STR __FILE__ ":" CXXTOOLS_TOSTRING(__LINE__)
57 
60 #define CXXTOOLS_SOURCEINFO cxxtools::SourceInfo(__FILE__, CXXTOOLS_TOSTRING(__LINE__), CXXTOOLS_FUNCTION)
61 
62 namespace cxxtools
63 {
64 
85 {
86  public:
93  inline SourceInfo(const char* file, const char* line, const char* func)
94  : _file(file), _line(line), _func(func)
95  { }
96 
99  inline const char* file() const
100  { return _file; }
101 
104  inline const char* line() const
105  { return _line; }
106 
109  inline const char* func() const
110  { return _func; }
111 
112  private:
113  const char* _file;
114  const char* _line;
115  const char* _func;
116 };
117 
118 
119 inline std::string operator+(const std::string& what, const SourceInfo& info)
120 {
121  return std::string( info.file() ) + ':' + info.line() + ": " += what;
122 }
123 
124 inline std::string operator+(const char* what, const SourceInfo& info)
125 {
126  return std::string( info.file() ) + ':' + info.line() + ": " += what;
127 }
128 
129 inline std::string operator+( const SourceInfo& info, const std::string& what)
130 {
131  return std::string( info.file() ) + ':' + info.line() + ": " += what;
132 }
133 
134 inline std::string operator+(const SourceInfo& info, const char* what)
135 {
136  return std::string( info.file() ) + ':' + info.line() + ": " += what;
137 }
138 
139 } // namespace cxxtools
140 
141 #endif