summaryrefslogtreecommitdiff
path: root/ace/Auto_Functor.h
blob: b9bcf8342b14057f8bb0bee3021f6ae4fb5ace3c (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// -*- C++ -*-
//=============================================================================
/**
 * @file Auto_Functor.h
 *
 * $Id$
 *
 * @author Carlos O'Ryan <coryan@atdesk.com>
 */
//=============================================================================
#ifndef ACE_AUTO_FUNCTOR_H
#define ACE_AUTO_FUNCTOR_H
#include /**/ "ace/pre.h"

#include "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Global_Macros.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE_Utils
{
/**
 * @class Auto_Functor_Ref
 *
 * @brief Helper class to implement assignment and copy-construction
 *        as expected
 */
template<typename X, typename Functor>
struct Auto_Functor_Ref
{
  X * p_;
  Functor f_;

  Auto_Functor_Ref(X * p, Functor f);
};

/**
 * @class Auto_Functor
 *
 * @brief Helper template to implement auto_ptr<>-like classes, but
 *    executing a functor in the destructor, instead of always
 *    deleting things.
 *
 * The functor is called in the destructor, and it must implement:
 *
 * Functor() throw();<BR>
 * Functor(Functor const &) throw();<BR>
 * Functor & operator=(Functor const &) throw();<BR>
 * void operator()(X * p) throw();<BR>
 *
 */
template<typename X, typename Functor>
class Auto_Functor
{
public:
  typedef X element_type;
  typedef Functor functor_type;

  /// Constructor
  explicit Auto_Functor (X * p = 0,
      Functor functor = Functor())
    ACE_THROW_SPEC (());

  Auto_Functor (Auto_Functor & rhs)
    ACE_THROW_SPEC (());

  Auto_Functor<X,Functor>& operator= (Auto_Functor & rhs)
    ACE_THROW_SPEC (());

#if !defined(ACE_LACKS_MEMBER_TEMPLATES)
  template<typename Y>
  Auto_Functor(Auto_Functor<Y,Functor>& rhs)
    ACE_THROW_SPEC (());

  template<typename Y>
  Auto_Functor<X,Functor>& operator= (Auto_Functor<Y,Functor>& rhs)
    ACE_THROW_SPEC (());
#endif /* ACE_LACKS_MEMBER_TEMPLATES */

  ~Auto_Functor()
    ACE_THROW_SPEC (());

  X & operator*() const
    ACE_THROW_SPEC (());

  X * operator->() const
    ACE_THROW_SPEC (());

  X * get()
    ACE_THROW_SPEC (());

  X * release()
    ACE_THROW_SPEC (());

  void reset (X * p = 0)
    ACE_THROW_SPEC (());

  void reset (X * p, Functor f)
    ACE_THROW_SPEC (());

  Functor const & functor() const
    ACE_THROW_SPEC (());

  Auto_Functor(Auto_Functor_Ref<X,Functor> rhs)
    ACE_THROW_SPEC (());

  Auto_Functor<X,Functor> & operator=(Auto_Functor_Ref<X,Functor> rhs)
    ACE_THROW_SPEC (());

#if !defined(ACE_LACKS_MEMBER_TEMPLATES)
  template<typename Y> operator Auto_Functor_Ref<Y,Functor>()
    ACE_THROW_SPEC (());

  template<typename Y> operator Auto_Functor<Y,Functor>()
    ACE_THROW_SPEC (());
#else
  operator Auto_Functor_Ref<X,Functor>()
    ACE_THROW_SPEC (());
#endif /* ACE_LACKS_MEMBER_TEMPLATES */

private:
  X * p_;

  Functor f_;
};

} // namespace ACE_Utils

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined(__ACE_INLINE__)
# include "ace/Auto_Functor.inl"
#endif /* __ACE_INLINE__ */

#if defined(ACE_TEMPLATES_REQUIRE_SOURCE)
# include "ace/Auto_Functor.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#include /**/ "ace/post.h"
#endif /* ACE_AUTO_FUNCTOR_H*/