Consider the following reflected model defined in C++
// C++ code
namespace ns
{
$model+ Point
{
int32 x;
int32 y;
};
}
The following Python code illustrates various ways of creating an instance of a Point object which is associated with a python object referenced by a python variable:
# python code
# Default constructed point, same as Point(0,0)
p = ns.Point()
print 'p = ' + `p`
# Members of model can be accessed using dot notation
p.x = 10
p.y = 20
print 'p = ' + `p`
# Constructor which initialises the members in the same order as they appear in the model
p = ns.Point(4,-5)
print 'p = ' + `p`
# Provide values of any subset of the members in any order using a comma separate list of name=value
# pairs, using the same of the member of the model.
p = ns.Point(X=3) # Y will have default value of 0
p = ns.Point(Y=3,X=2) # Order doesn't need to correspond to the order in which the members appear in the model
p = ns.Point(Y=1) # X will have default value of 0
// C++ code
namespace dt
{
$enum+ class TMonth
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
$model+ TDate
{
int32 Year;
TMonth Month;
int32 Day;
};
$model+ TTime
{
int32 Hour;
int32 Minute;
int32 Second;
};
$model+ TDateTime
{
TDate Date;
TTime Time;
};
}
namespace geom
{
$enum+ class EColour
{
red,
green,
blue,
magenta,
cyan,
yellow
};
$model+ TPoint
{
float32 X;
float32 Y;
};
$model+ TLine
{
TPoint P1;
TPoint P2;
};
$model+ TRect
{
TPoint P1;
TPoint P2;
};
$model+ TCircle
{
TPoint C;
float32 R;
};
$model+ TPolygon
{
xvector<TPoint> V;
};
$variant+ TShape
{
default TCircle(TPoint(0,0),1);
TPoint Point;
TLine Line;
TRect Rect;
TCircle Circle;
TPolygon Polygon;
};
$model+ TPicture <<multiline>>
{
string8 Name;
string8 Author;
dt::TDateTime CreationDate;
EColour Colour;
TRect BoundingBox;
xvector<TShape> Shapes;
};
}
The following Python code:
polygon = geom.TPolygon( [ geom.TPoint(X=1,Y=2), geom.TPoint(X=7,Y=-2) ] )
polygon.V.append( geom.TPoint(X=5,Y=1) )
polygon.V[0].X = 1000
polygon.V.append( polygon.V[1] )
picture = geom.TPicture(
Name = "my picture",
Author = "P. Hunt",
Colour = geom.EColour.magenta,
CreationDate = dt.TDateTime( Date = dt.TDate(2006,dt.TMonth.Jun,23) ),
BoundingBox = geom.TRect( geom.TPoint(0,0), geom.TPoint(800,600) ),
Shapes =
[
geom.TShape(),
geom.TShape.Polygon(polygon),
geom.TShape.Rect(),
geom.TShape.Rect( geom.TPoint(X=5,Y=8), geom.TPoint(15,12) ),
geom.TShape.Rect( geom.TPoint(Y=5,X=8), geom.TPoint(10,17) ),
geom.TShape.Circle( geom.TPoint(), 5 ),
geom.TShape.Circle( R=2 ),
geom.TShape.Circle( C=geom.TPoint(X=100) )
]
)
print('picture = {}'.format(picture))
produces the following output:
picture = TPicture
{
Name = my picture
Author = P. Hunt
CreationDate = Jun 23 2006 00:00
Colour = magenta
BoundingBox = TRect(TPoint(0,0),TPoint(800,600))
Shapes = [
Circle(TCircle(TPoint(0,0),1)),
Polygon(TPolygon([TPoint(1000,2),TPoint(7,-2),TPoint(5,1),TPoint(7,-2)])),
Rect(TRect(TPoint(0,0),TPoint(0,0))),
Rect(TRect(TPoint(5,8),TPoint(15,12))),
Rect(TRect(TPoint(8,5),TPoint(10,17))),
Circle(TCircle(TPoint(0,0),5)),
Circle(TCircle(TPoint(0,0),2)),
Circle(TCircle(TPoint(100,0),0))]
}