File: make_rc.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (82 lines) | stat: -rw-r--r-- 3,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2024- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//


#pragma once

#include <openvpn/common/rc.hpp>

namespace openvpn {

/**
 * @brief A class template that enables reference counting for a given type.
 * @details This class inherits from both the TypeT and RcT (Reference Counting)
 *          classes. It provides a convenient way to create reference-counted
 *          objects of TypeT.
 * @tparam TypeT The base type to be reference-counted.
 * @tparam RcT The reference counting class, defaulting to RC<thread_unsafe_refcount>.
 */
template <typename TypeT, typename RcT = RC<thread_unsafe_refcount>>
class RcEnable : public TypeT, public RcT
{
  public:
    using Ptr = RCPtr<RcEnable>; ///< Alias for the pointer type used by the reference counting class.

    /**
     * @brief Creates a new instance of RcEnable with the given arguments.
     * @details This function creates a new RcEnable object using the provided arguments
     *          and returns a smart pointer (Ptr) to the created object.
     * @tparam ArgsT The parameter pack types for the arguments to be forwarded
     *               to the RcEnable constructor.
     * @param args The arguments to be forwarded to the RcEnable constructor.
     * @return A smart pointer (Ptr) to the newly created RcEnable object with intrusive ref count.
     */
    template <typename... ArgsT>
    [[nodiscard]] static Ptr Create(ArgsT &&...args)
    {
        return Ptr(new RcEnable(std::forward<ArgsT>(args)...));
    }

  private:
    /**
     * @brief Private constructor for RcEnable.
     * @details This constructor is used to create a new instance of RcEnable with the
     *          provided arguments. It initializes the base classes TypeT and RcT with
     *          the forwarded arguments.
     * @tparam ArgsT The parameter pack types for the arguments to be forwarded
     *               to the base class constructors.
     * @param args The arguments to be forwarded to the base class constructors.
     * @note This constructor is private and should not be called directly.
     *       Use the Create() function to create instances of RcEnable.
     */
    template <typename... ArgsT>
    RcEnable(ArgsT &&...args)
        : TypeT(std::forward<ArgsT>(args)...),
          RcT(){};
};

/**
 * @brief Helper function to create a reference-counted object with the default thread-unsafe reference counting policy.
 * @tparam TypeT The type of the object to be created.
 * @tparam RcT The RC type that shall be used, defaults to RC<thread_unsafe_refcount>
 * @tparam ArgsT The types of the arguments to be forwarded to the constructor of TypeT.
 * @param args The arguments to be forwarded to the constructor of TypeT.
 * @return A reference-counted object of type TypeT, using the default thread-unsafe reference counting policy.
 * @note This function is a convenience wrapper around make_rc_impl, using the default RC<thread_unsafe_refcount>
 *       as the reference counting policy.
 */
template <typename TypeT, typename RcT = RC<thread_unsafe_refcount>, typename... ArgsT>
auto make_rc(ArgsT &&...args)
{
    return RcEnable<TypeT, RcT>::Create(std::forward<ArgsT>(args)...);
}

} // namespace openvpn