[omniORB] struct constructor

David Bellette david.bellette@nec.com.au
Tue Feb 11 23:53:01 2003


Hi Matthew,

structs are a hand-me-down from C and don't have constructors, so you can't implicitly define default values for members
of a struct. You can explicitly define the values everytime you define a struct, but this can be a pain, as you've
realised.

You can create a C++ wrapper class that allows extraction of a struct and assignment. All of your code that would
normally use the struct would then use the class, allowing use of initialisation constructors, then assigning back to
the compatible struct type will provide a struct with the initialised variables.
You can also use the class as a parameter of function calls that expect the struct, forcing an implicit conversion to
the struct.
There is the overhead of a copy in this whenever the stuct is required, but it is safer as the class constructor and
public access functions can ensure only valid values are contained by the members.

Hope this helps.

David



// IDL
enum MaskTypes
{
    MCEL_MASKTYPE_INVALID,
    MCEL_MASKTYPE_CONST,
     MCEL_MASKTYPE_VAR,
    MCEL_MASKTYPE_LAST
};

struct tGridStruct {
    short       first;
    MaskTypes   mask_type;
    short       last;
};


// Wrapper class
class CGridStruct
{
public:
    CGridStruct()
    {
        first = 1;
        mask_type = MCEL_MASKTYPE_INVALID;
        last = 99;
    }

    CGridStruct(const tGridStruct& pGrid)
    {
        first = pGrid.first;
        mask_type = pGrid.mask_type;
        last = pGrid.last;
    }

    CGridStruct& operator=(const tGridStruct& pGrid)
    {
        first = pGrid.first;
        mask_type = pGrid.mask_type;
        last = pGrid.last;

        return *this;
    }

    operator tGridStruct() const
    {
        tGridStruct lGrid;

        lGrid.first = first;
        lGrid.mask_type = mask_type;
        lGrid.last = last;

        return lGrid;
    }

private:
    short       first;
    MaskTypes   mask_type;
    short       last;
};


// Usage:
    CGridStruct lGrid1;                    // default construction, using default initialised values
    tGridStruct lGrid2 = lGrid1;        // Conversion from CGridStruct to a tGridStruct

    lGrid2.first = 2;
    CGridStruct lGrid3 = lGrid2;        // Copy constructor

    lGrid2.last = 98;
    lGrid3 = lGrid2;                    // Assignment



------------------------------------------------------------------------------------------------------------------------
---------------------------------


----- Original Message -----
From: "Matthew Bettencourt" <matt@ssc.usm.edu>
To: <omniorb-list@realvnc.com>
Sent: Wednesday, February 12, 2003 7:06 AM
Subject: [omniORB] struct constructor


> I just figured out that omniORB is critical of invalid enum's.  So, I
> have a structure in an idl file, is there a way to create a constructor
> for this struct so I can set all the val's to values??
>
> Thanks
> Matt
> Grid.idl
> ---------------------
> enum MaskTypes {MCEL_MASKTYPE_INVALID,MCEL_MASKTYPE_CONST,MCEL_MASKTYPE_VAR,
> MCEL_MASKTYPE_LAST};
> struct GridStruct {
> ...
>    MaskTypes mask_type;
> ...
> }
>
>
> what I want to define is
> GridStruct::GridStruct() {
> mask_type = MCEL_MASKTYPE_INVALID;
> }
>
> Thanks
> Matt
>
> _______________________________________________
> omniORB-list mailing list
> omniORB-list@omniorb-support.com
> http://www.omniorb-support.com/mailman/listinfo/omniorb-list
>