C++ Mapping for _var Types for Fixed-Length Underlying Types

Last updated: 8 February 2000


On 10 January 2000, the OMG C++ Mapping RTF voted to add an example implementation for _var types for fixed-length underlying types to the C++ mapping. (Note that the implementation shown is not binding in the sense that its internals may differ in each ORB; however, the semantics must be the same.) The upshot of this mapping is that, once initialized, a _var variable for a fixed-length underlying type behaves like a value of the underlying type.

The T_var types are also produced for fixed-length structured types for reasons of consistency. These types have the same semantics as T_var types for variable-length types. This allows applications to be coded in terms of T_var types regardless of whether the underlying types are fixed- or variable-length. T_var types for fixed-length structured types have the following general form:

class T_var
{
  public:
    T_var() : m_ptr(0) {}

    T_var(T *t) : m_ptr(t) {}

    T_var(const T& t) : m_ptr(new T(t)) {}

    T_var(const T_var &t) : m_ptr(0) {
	if (t.m_ptr != 0)
	    m_ptr = new T(*t.m_ptr);
    }

    ~T_var() { delete m_ptr; }

    T_var &operator=(T *t) {
	if (t != m_ptr) {
	    delete m_ptr;
	    m_ptr = t;
	}
	return *this;
    }

    T_var &operator=(const T& t) {
	if (&t != m_ptr) {
	    T* old_m_ptr = m_ptr;
	    m_ptr = new T(t);
	    delete old_m_ptr;
	}
	return *this;
    }

    T_var &operator=(const T_var &t) {
	if (this != &t) {
	    T* old_m_ptr = m_ptr;
	    if (t.m_ptr != 0)
		m_ptr = new T(*t.m_ptr);
	    else
		m_ptr = 0;
	    delete old_m_ptr;
	}
	return *this;
    }

    T* operator->() { return m_ptr; }

    const T* operator->() const { return m_ptr; }

    const T& in() const { return *m_ptr; }

    T& inout() { return *m_ptr; }

    T& out() {
	if (m_ptr == 0)
	    m_ptr = new T;
	return *m_ptr;
    }

    T _retn() { return *m_ptr; }

  private:
    T* m_ptr;
};

Valid HTML 3.2!