ATLAS Offline Software
Loading...
Searching...
No Matches
xmlUtilities.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4//
5// xmlUtilities.h
6// HDef
7//
8// Created by sroe on 15/03/2016.
9//
10
11#ifndef xmlUtilities_h
12#define xmlUtilities_h
13#include <memory>
14#include <string>
15#include <xercesc/util/PlatformUtils.hpp> // Initialize, Terminate
16#include <xercesc/util/XMLString.hpp> // transcode
17#include <xercesc/dom/DOM.hpp> // DOMxxx
18
20ATLAS_NO_CHECK_FILE_THREAD_SAFETY; // Not sure if usage of Xerces-C++ here is thread safe.
21// The following warning message is given if checked:
22// warning: Use of static expression 'xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
23// of type 'xercesc_3_1::MemoryManager*' within function
24// 'toNative(const XMLCh*)::<lambda(char*)>' may not be thread-safe.
25//
26// https://xerces.apache.org/xerces-c/faq-parse-3.html#faq-6
27// Is Xerces-C++ thread-safe?
28// The answer is yes if you observe the following rules for using Xerces-C++
29// in a multi-threaded environment:
30// ... ...
31
32typedef std::basic_string<XMLCh>XercesString;
33inline XercesString
34fromNative(const char* str) {
35 auto xDeleter = [&](XMLCh buf[]) {
36 xercesc::XMLString::release(&buf);
37 };
38
39 std::unique_ptr<XMLCh[], decltype(xDeleter)> ptr(xercesc::XMLString::transcode(str), xDeleter);
40
41 return XercesString(ptr.get());
42}
43
44inline XercesString
45fromNative(const std::string& str) {
46 return fromNative(str.c_str());
47}
48
49inline std::string
50toNative(const XMLCh* str) {
51 auto cDeleter = [&](char buf[]) {
52 xercesc::XMLString::release(&buf);
53 };
54
55 std::unique_ptr<char[], decltype(cDeleter)> ptr(xercesc::XMLString::transcode(str), cDeleter);
56
57 return std::string(ptr.get());
58}
59
60inline std::string
62 return toNative(str.c_str());
63}
64
65namespace myXerces {
66 struct Lib {
67 Lib() {
68 xercesc::XMLPlatformUtils::Initialize();
69 }
70
71 ~Lib() {
72 xercesc::XMLPlatformUtils::Terminate();
73 }
74 };
75} // namespace myXerces
76
77
78
79#endif /* xmlUtilities_h */
Define macros for attributes used to control the static checker.
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
XercesString fromNative(const char *str)
std::basic_string< XMLCh > XercesString
std::string toNative(const XMLCh *str)