13.1.25.4 Boolean、Octet、Char、および制限付き文字列の識別

OMG IDLの型のBoolean、octet、およびcharは、特定のC++型にマッピングする必要はありません。そのため、型保障Anyインタフェースで使用できるようにするために、これらの型を互いに識別する方法が別に必要となります。同様に、制限付き文字列と無制限文字列は共にchar*にマッピングされるため、両者を識別する方法が別に必要となります。ここでは、識別を行うために、Anyクラス・インタフェース内でネストされた新しいヘルパー型をいくつか導入します。たとえば、これは下記に示されるように達成されます:

// C++
class Any
{
  public:
    // special helper types needed for boolean, octet,
   // char, and bounded string insertion
   struct from_boolean {
       from_boolean(Boolean b) : val(b) {}
       Boolean val;
   };
   struct from_octet {
        from_octet(Octet o) : val(o) {}
        Octet val;
   };
   struct from_char {
   from_char(Char c) : val(c) {}
   Char val;
   };
   struct from_string {
     from_string(char* s, ULong b,
               Boolean nocopy = FALSE) :
            val(s), bound(b) {}
     char *val;
     ULong bound;
   };
   void operator<<=(from_boolean);
   void operator<<=(from_char);
   void operator<<=(from_octet);
   void operator<<=(from_string);
  // special helper types needed for boolean, octet,
 // char, and bounded string extraction
 struct to_boolean {
    to_boolean(Boolean &b) : ref(b) {}
    Boolean &ref;
  };
  struct to_char {
     to_char(Char &c) : ref(c) {}
     Char &ref;
  };
  struct to_octet {
     to_octet(Octet &o) : ref(o) {}
     Octet &ref;
  };
  struct to_string {
     to_string(char *&s, ULong b) : val(s), bound(b) {}
     char *&val;
     ULong bound;
  };
  Boolean operator>>=(to_boolean) const;
  Boolean operator>>=(to_char) const;
  Boolean operator>>=(to_octet) const;
  Boolean operator>>=(to_string) const;
  // other public Any details omitted
private:
   // these functions are private and not implemented
  // hiding these causes compile-time errors for
  // unsigned char
 void operator<<=(unsigned char);
 Boolean operator>>=(unsigned char &) const;
};

ORBでは、これらの特別なヘルパー型用に、オーバーロードのoperator<<=およびoperator>>=関数を提供します。これらのヘルパー型は、次のように使用します。

// C++
Boolean b = TRUE;
Any any;
any <<= Any::from_boolean(b);
// ...
if (any >>= Any::to_boolean(b)) {
     // ...any contained a Boolean...
}
char* p = "bounded";
any <<= Any::from_string(p, 8);
// ...
if (any >>= Any::to_string(p, 8)) {
    // ...any contained a string<8>...
}

制限値が0(ゼロ)の場合、無制限文字列を示します。

Anyへの制限付き文字列または無制限文字列の挿入をコピーせずに行う場合、from_stringコンストラクタでnocopyフラグをTRUEに設定する必要があります:

// C++
char* p = string_alloc(8);
// ...initialize string p...
any <<= Any::from_string(p, 8, 1);      // any consumes p

boolean、charおよびoctetがすべてC++型のunsigned charにマッピングされる場合、boolean、charおよびoctet型のいずれかを直接挿入または抽出しようとすると、unsigned charのプライベートで未実装のoperator<<=関数とoperator>>=関数によってコンパイル時エラーが発生します:

// C++
Octet oct = 040;
Any any;
any <<= oct;                       // this line will not compile
any <<= Any::from_octet(oct);     // but this one will