summaryrefslogtreecommitdiff
path: root/TAO/tao/Valuetype/ValueFactory_Map.cpp
blob: 674c1764babfb59bd5f67ec1fa147df990bdde25 (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
#include "tao/Valuetype/ValueFactory_Map.h"
#include "tao/Valuetype/ValueFactory.h"
#include "tao/CORBA_String.h"
#include "tao/TAO_Singleton.h"


ACE_RCSID (Valuetype,
           ValueFactory_Map,
           "$Id$")


TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_ValueFactory_Map::TAO_ValueFactory_Map (void)
  : map_ (TAO_DEFAULT_VALUE_FACTORY_TABLE_SIZE)
{
}

TAO_ValueFactory_Map::~TAO_ValueFactory_Map (void)
{
  // Initialize an iterator.  We need to go thru each entry and free
  // up storage allocated to hold the external ids and invoke
  // _remove_ref () on the internal ids.
  FACTORY_MAP_MANAGER::ITERATOR iterator (this->map_);

  for (FACTORY_MAP_MANAGER::ENTRY *entry = 0;
       iterator.next (entry) != 0;
       iterator.advance ())
    {
      // We had allocated memory and stored the string. So we free the
      // memory.
      CORBA::string_free ((char *) entry->ext_id_);
      entry->ext_id_ = 0;
      entry->int_id_->_remove_ref ();
      entry->int_id_ = 0;
    }
}

// %! Thread issues

int
TAO_ValueFactory_Map::rebind (const char *repo_id,
                              CORBA::ValueFactory &factory)
{
//  ACE_READ_GUARD_RETURN (TAO_SYNCH_RW_MUTEX, guard, map_->mutex(),-1);
//   --- but must be recursive
  const char *prev_repo_id = 0;
  CORBA::ValueFactory prev_factory = 0;
  int ret = 0;
  ret = this->map_.rebind (CORBA::string_dup (repo_id),
                           factory,
                           prev_repo_id,
                           prev_factory);

  if (ret > -1)   // ok, no error
    {
      factory->_add_ref ();    // The map owns one reference.

      if (ret == 1)    // there was a previous factory
        {
          factory = prev_factory;
          CORBA::string_free (const_cast<char*> (prev_repo_id));
        }
    }

  return ret;
}

int
TAO_ValueFactory_Map::unbind (const char *repo_id,
                              CORBA::ValueFactory &factory)
{
  FACTORY_MAP_MANAGER::ENTRY *prev_entry = 0;
  int ret = 0;
  ret = this->map_.find (repo_id,
                         prev_entry);
  if (ret == 0)    // there was a matching factory
    {
      // set factory to the previous factory,
      factory = prev_entry->int_id_;
      char *temp = const_cast<char *> (prev_entry->ext_id_);
      ret = this->map_.unbind (prev_entry);

      if (ret == 0)
        {
          CORBA::string_free (temp);
        }
    }

  return ret;
}

// %! perhaps inline
int
TAO_ValueFactory_Map::find (const char *repo_id,
                            CORBA::ValueFactory &factory)
{
  int ret = 0;
  ret = this->map_.find (repo_id,
                         factory);
  // %! this must be guarded to be atomic  !!!!!!!!!!!!!!!!!!
  if (ret > -1)
    {
      factory->_add_ref ();    // The caller gets one reference as gift.
    }

  return ret;
}

TAO_ValueFactory_Map *
TAO_ValueFactory_Map::instance (void)
{
  // Hide the template instantiation to prevent multiple instances
  // from being created.

  return
    TAO_Singleton<TAO_ValueFactory_Map, TAO_SYNCH_MUTEX>::instance ();
}

TAO_END_VERSIONED_NAMESPACE_DECL

#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
template TAO_Singleton<TAO_ValueFactory_Map, TAO_SYNCH_MUTEX> * TAO_Singleton<TAO_ValueFactory_Map, TAO_SYNCH_MUTEX>::singleton_;
#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */