summaryrefslogtreecommitdiff
path: root/trunk/TAO/tao/CodecFactory/CodecFactory_impl.cpp
blob: 1e27c3affe988a8c6ceda1edd78320ebb487a636 (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
// -*- C++ -*-
//
// $Id$

#include "tao/CodecFactory/CodecFactory_impl.h"
#include "tao/CodecFactory/CDR_Encaps_Codec.h"
#include "tao/SystemException.h"
#include "tao/ORB_Constants.h"
#include "tao/ORB_Core.h"
#include "tao/Codeset_Manager.h"
#include "ace/Codeset_Symbols.h"
#include "ace/CORBA_macros.h"

ACE_RCSID (CodecFactory_impl,
           CodecFactory,
           "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_CodecFactory::TAO_CodecFactory (TAO_ORB_Core * orb_core)
  : orb_core_ (orb_core)
{
}

IOP::Codec_ptr
TAO_CodecFactory::create_codec_with_codesets (const IOP::Encoding_1_2 & enc)
{
  TAO_Codeset_Translator_Base *char_trans = 0;
  TAO_Codeset_Translator_Base *wchar_trans = 0;
  CONV_FRAME::CodeSetId ncsc;
  CONV_FRAME::CodeSetId ncsw;

  TAO_Codeset_Manager *csm = this->orb_core_->codeset_manager ();

  if (csm)
    {
      char_trans = csm->get_char_trans (enc.char_codeset);
      wchar_trans = csm->get_wchar_trans (enc.wchar_codeset);
      csm->get_ncs (ncsc,ncsw); // pass by reference
    }
  else
    {
      // No codeset manager, so also raise an unsupported codeset
      throw IOP::CodecFactory::UnsupportedCodeset (enc.wchar_codeset);
    }

  if (wchar_trans == 0 &&
      enc.wchar_codeset != ACE_CODESET_ID_ISO_UTF_16 &&
      enc.wchar_codeset != ncsw)
    {
      throw IOP::CodecFactory::UnsupportedCodeset (enc.wchar_codeset);
    }

  if (char_trans == 0 &&
      enc.char_codeset != ncsc)
    {
      throw IOP::CodecFactory::UnsupportedCodeset (enc.char_codeset);
    }

  return this->create_codec_i (enc.major_version,
                               enc.minor_version,
                               enc.format,
                               char_trans,
                               wchar_trans);
}

IOP::Codec_ptr
TAO_CodecFactory::create_codec (const IOP::Encoding & enc)
{
  return this->create_codec_i (enc.major_version,
                               enc.minor_version,
                               enc.format,
                               0,
                               0);
}

IOP::Codec_ptr
TAO_CodecFactory::create_codec_i (CORBA::Octet major,
                                  CORBA::Octet minor,
                                  IOP::EncodingFormat encoding_format,
                                  TAO_Codeset_Translator_Base * char_trans,
                                  TAO_Codeset_Translator_Base * wchar_trans)
{
  // @todo: Ideally we should have some sort of CodecFactory
  //        registry to make it possible to add factories
  //        dynamically.  However, there currently isn't a need to
  //        support anything other than CDR encapsulations yet so we
  //        hardcode its Codec.  This may change once TAO starts to
  //        support messaging formats other than GIOP.

  IOP::Codec_ptr codec = IOP::Codec::_nil ();

  switch (encoding_format)
    {

      // @@ MSVC 6 gets confused and thinks that
      //    IOP::ENCODING_CDR_ENCAPS is not a constant, so its actual
      //    value (0) is used instead.
    case 0 /* IOP::ENCODING_CDR_ENCAPS */:
      if (major < 1)
        {
          // There is no such thing as a "0.x" CDR encapsulation.
          throw ::CORBA::BAD_PARAM (
            CORBA::SystemException::_tao_minor_code (
              0,
              EINVAL),
            CORBA::COMPLETED_NO);
        }

      ACE_NEW_THROW_EX (codec,
                        TAO_CDR_Encaps_Codec (major,
                                              minor,
                                              this->orb_core_,
                                              char_trans,
                                              wchar_trans),
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            0,
                            ENOMEM),
                          CORBA::COMPLETED_MAYBE));
      break;

    default:
      throw IOP::CodecFactory::UnknownEncoding ();

    }

  return codec;
}

TAO_END_VERSIONED_NAMESPACE_DECL