Apache Log4cxx Version 1.2.0
Loading...
Searching...
No Matches
widelife.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _LOG4CXX_HELPERS_WIDELIFE_H
19#define _LOG4CXX_HELPERS_WIDELIFE_H
20
21#include <log4cxx/log4cxx.h>
22
23namespace log4cxx
24{
25namespace helpers
26{
27
28
32template <class T>
34{
35public:
36 template <class... Args>
37 WideLife(Args&&... args)
38 {
39 new(&storage) T(std::forward<Args>(args)...);
40 }
41
43 {
44#if LOG4CXX_EVENTS_AT_EXIT
45 // keep the holded value alive
46#else
47 value().~T();
48#endif
49 }
50
51 T& value()
52 {
53 return *reinterpret_cast<T*>(&storage);
54 }
55
56 const T& value() const
57 {
58 return *reinterpret_cast<const T*>(&storage);
59 }
60
61 operator T&()
62 {
63 return value();
64 }
65
66 operator const T&() const
67 {
68 return value();
69 }
70
71private:
72 alignas(T) char storage[sizeof(T)];
73}; // class WideLife
74} // namespace helpers
75} // namespace log4cx
76
77#endif //_LOG4CXX_HELPERS_WIDELIFE_H
The WideLife wrapper is destined to prolongate the runtime logger state lifetime from static duration...
Definition: widelife.h:34
const T & value() const
Definition: widelife.h:56
~WideLife()
Definition: widelife.h:42
WideLife(Args &&... args)
Definition: widelife.h:37
T & value()
Definition: widelife.h:51
Definition: configuration.h:25