summaryrefslogtreecommitdiff
path: root/TAO/tao/Load_Protocol_Factory_T.h
blob: 29a2519aa0536bc39efbc24a7bb91f4b384fbc48 (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file Load_Protocol_Factory_T.h
 *
 *  Function templates to load a protocol factory.
 *
 *  @author  Johnny Willemsen
 */
//=============================================================================

#ifndef TAO_LOAD_PROTOCOL_FACTORY_T_H
#define TAO_LOAD_PROTOCOL_FACTORY_T_H

#include /**/ "ace/pre.h"

#include "tao/Protocol_Factory.h"
#include "tao/Resource_Factory.h"
#include "tao/debug.h"
#include "ace/Auto_Ptr.h"
#include "ace/Dynamic_Service.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  namespace details
  {
    template<typename T>
    int
    load_protocol_factory (TAO_ProtocolFactorySet &protocol_set,
                           const char *name)
    {
      TAO_Protocol_Factory *protocol_factory = 0;
#if defined (ACE_HAS_CPP11)
      std::unique_ptr<TAO_Protocol_Factory> safe_protocol_factory;
#else
      auto_ptr<TAO_Protocol_Factory> safe_protocol_factory;
#endif /* ACE_HAS_CPP11 */

      TAO_Protocol_Item *item = 0;

      // If a protocol factory is obtained from the Service
      // Configurator then do not transfer ownership to the
      // TAO_Protocol_Item.
      bool transfer_ownership = false;

      protocol_factory =
        ACE_Dynamic_Service<TAO_Protocol_Factory>::instance (
          ACE_TEXT_CHAR_TO_TCHAR (name));

      if (protocol_factory == 0)
        {
          if (TAO_debug_level > 0)
            TAOLIB_ERROR ((LM_WARNING,
                        ACE_TEXT("(%P|%t) WARNING - No <%C> found in Service")
                        ACE_TEXT(" Repository. Using default instance.\n"),
                        name));

          ACE_NEW_RETURN (protocol_factory,
                          T,
                          -1);

          ACE_auto_ptr_reset (safe_protocol_factory,
                              protocol_factory);

          transfer_ownership = true;
        }
      else
        {
          transfer_ownership = false;
        }

      ACE_NEW_RETURN (item, TAO_Protocol_Item (name), -1);
      // If the TAO_Protocol_Item retains ownership of the
      // TAO_Protocol_Factory then we used an auto_ptr<> above, so
      // release the TAO_Protocol_Factory from it.  Otherwise, we
      // obtained the TAO_Protocol_Factory from the Service
      // Configurator so an auto_ptr<> wasn't used since the Service
      // Configurator retains ownership, hence there was no need to
      // use an auto_ptr<> in this method.
      item->factory ((transfer_ownership ?
                      safe_protocol_factory.release () :
                      protocol_factory),
                      transfer_ownership);

      if (protocol_set.insert (item) == -1)
        {
          TAOLIB_ERROR ((LM_ERROR,
                      ACE_TEXT("TAO (%P|%t) Unable to add ")
                      ACE_TEXT("<%C> to protocol factory set.\n"),
                      item->protocol_name ().c_str ()));

          delete item;

          if (transfer_ownership == false)
            delete protocol_factory;

          return -1;
        }

      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG ((LM_DEBUG,
                      ACE_TEXT("TAO (%P|%t) - Loaded default ")
                      ACE_TEXT("protocol <%C>\n"),
                      name));
        }

      return 0;
    }
  }
}

TAO_END_VERSIONED_NAMESPACE_DECL

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

#endif  /* TAO_LOAD_PROTOCOL_FACTORY_T_H*/