mbxmlutils 1.3.0 Multi-Body XML Utils

thislinelocation.h
1 /*
2 * Author: Markus Friedrich
3 *
4 * This file is free and unencumbered software released into the public domain.
5 *
6 * Anyone is free to copy, modify, publish, use, compile, sell, or
7 * distribute this software, either in source code form or as a compiled
8 * binary, for any purpose, commercial or non-commercial, and by any
9 * means.
10 *
11 * In jurisdictions that recognize copyright laws, the author or authors
12 * of this software dedicate any and all copyright interest in the
13 * software to the public domain. We make this dedication for the benefit
14 * of the public at large and to the detriment of our heirs and
15 * successors. We intend this dedication to be an overt act of
16 * relinquishment in perpetuity of all present and future rights to this
17 * software under copyright law.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * For more information, please refer to <http://unlicense.org/>
28 */
29
30 #ifndef _MBXMLUTILSHELPER_THISLINELOCATION_H_
31 #define _MBXMLUTILSHELPER_THISLINELOCATION_H_
32
33 #include <string>
34
35 #ifdef _WIN32
36 # define WIN32_LEAN_AND_MEAN
37 # include <windows.h>
38 #else
39 # ifndef _GNU_SOURCE
40 # define _GNU_SOURCE // dladdr requires _GNU_SOURCE to be defined
41 # endif
42 # include <dlfcn.h>
43 # include <unistd.h>
44 #endif
45
46 namespace {
47 #ifdef _WIN32
48 extern "C" void *__ImageBase;
49 #endif
50 }
51
52 namespace MBXMLUtils {
53
58 class ThisLineLocation {
59 public:
60 ThisLineLocation() {
61 #ifdef _WIN32
62 char moduleName[2048];
63 GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), moduleName, sizeof(moduleName));
64 p=moduleName;
65 #else
66 char buffer[2048];
67 std::string curDir=getcwd(buffer, sizeof(buffer));
68
69 Dl_info info;
70 dladdr(reinterpret_cast<void*>(&dummyFunc), &info);
71 // convert to absolute path and return
72 std::string name(info.dli_fname);
73 p=name[0]=='/'?name:curDir+"/"+name;
74 #endif
75 }
76 std::string operator()() { return p; }
77 private:
78 std::string p;
79 #ifndef _WIN32
80 static void dummyFunc() {}
81 #endif
82 };
83
84 }
85
86 #endif
MBXMLUtils::ThisLineLocation
Definition: thislinelocation.h:58