Apache Log4cxx  Version 1.3.1
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 #if defined(__cpp_concepts) && __cpp_concepts >= 201500
23 #include <concepts>
24 #endif
25 
26 namespace LOG4CXX_NS
27 {
28 namespace helpers
29 {
30 
31 
35 template <class T>
36 class WideLife
37 {
38 public:
40  {
41  new(&storage) T();
42  }
43  template <class Arg0, class... Args>
44 #if defined(__cpp_concepts) && __cpp_concepts >= 201500
45  requires (!std::same_as<WideLife, Arg0>)
46 #endif
47  WideLife(Arg0&& arg0, Args&&... args)
48  {
49  new(&storage) T(std::forward<Arg0>(arg0), std::forward<Args>(args)...);
50  }
51 
53  {
54 #if LOG4CXX_EVENTS_AT_EXIT
55  // keep the holded value alive
56 #else
57  value().~T();
58 #endif
59  }
60 
61  T& value()
62  {
63  return *reinterpret_cast<T*>(&storage);
64  }
65 
66  const T& value() const
67  {
68  return *reinterpret_cast<const T*>(&storage);
69  }
70 
71  operator T&()
72  {
73  return value();
74  }
75 
76  operator const T&() const
77  {
78  return value();
79  }
80 
81 private:
82  alignas(T) char storage[sizeof(T)];
83  // Non-copyable
84  WideLife(const WideLife& other) = delete;
85  WideLife(const WideLife&& other) = delete;
86  // Non-assignable
87  WideLife& operator=(const WideLife& other) = delete;
88  WideLife& operator=(const WideLife&& other) = delete;
89 }; // class WideLife
90 } // namespace helpers
91 } // namespace log4cx
92 
93 #endif //_LOG4CXX_HELPERS_WIDELIFE_H
The WideLife wrapper is destined to prolongate the runtime logger state lifetime from static duration...
Definition: widelife.h:37
WideLife(Arg0 &&arg0, Args &&... args)
Definition: widelife.h:47
WideLife()
Definition: widelife.h:39
~WideLife()
Definition: widelife.h:52
const T & value() const
Definition: widelife.h:66
T & value()
Definition: widelife.h:61