home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / BST_SystemDocs.lha / BeastV1 / Examples / BST_System / BST_Example3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-03  |  12.4 KB  |  415 lines

  1. /****h* Beast/BST_Example3.c [1.0] **************
  2. *
  3. *    NAME
  4. *      BST_Example3 --
  5. *
  6. *    COPYRIGHT
  7. *      Maverick Software Development
  8. *
  9. *    FUNCTION
  10. *
  11. *    AUTHOR
  12. *      Jacco van Weert
  13. *
  14. *    CREATION DATE
  15. *      17-Mar-96
  16. *
  17. *    MODIFICATION HISTORY
  18. *
  19. *    NOTES
  20. *
  21. ****************************************************
  22. */
  23. #include <BEAST:Include/proto/beast.h>
  24. #include <proto/exec.h>
  25. #include <stdio.h>
  26.  
  27. struct Library *BeastBase;
  28.  
  29. struct TagItem EmptyList[] =
  30. {
  31.   {TAG_DONE, 0}
  32. };
  33.  
  34.  
  35. /*************************************************************
  36.  ***** math class instance definitions, only used for sizeof()!!
  37.  *****/
  38.  
  39. struct math_x2_Instance
  40. {
  41. #include "math_a.instance"
  42. };
  43. struct math_x_Instance
  44. {
  45. #include "math_a.instance"
  46. };
  47. struct math_a_Instance
  48. {
  49. #include "math_a.instance"
  50. };
  51. struct math_xy_Instance
  52. {
  53. #include "math_a.instance"
  54.     LONG    Y;
  55. };
  56.  
  57.  
  58.  
  59.  
  60. /***************************************************************************
  61.  ===========================================================================
  62.  **** MATH_aClass methods
  63.  ************************/
  64. __geta4 rfcall (mth_a_Init, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  65. {
  66.   struct math_a_Instance *Instance = Macro_GetInstance;
  67.  
  68.   Instance->A = 1;
  69.   return( MethodFlags );
  70. }
  71.  
  72.  
  73.  
  74. /**********************************************
  75.  ----------------------------------------------
  76.  **** Method called when a new X number arrives
  77.  ****/
  78. __geta4 rfcall (mth_a_InputX, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  79. {
  80.   struct math_a_Instance *Instance = Macro_GetInstance;
  81.  
  82.   struct TagItem TL_Output[] =
  83.     { {BTA_Y,    Instance->A},
  84.       {TAG_DONE, 0}
  85.     };
  86.   OBJ_ToOutput( Object,TL_Output, OBM_OUTPUT, 0 );
  87.  
  88.   return( MethodFlags );
  89. }
  90.  
  91.  
  92.  
  93. /***************************************************************
  94.  ---------------------------------------------------------------
  95.  **** Method called the object connected to this method requests
  96.  **** for new input.
  97.  ****/
  98. __geta4 rfcall (mth_a_OutputX, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  99. {
  100.  
  101.   /***********************************
  102.    **** Just call our OBM_INPUT method
  103.    ****/
  104.   OBJ_DoMethod( Object, OBM_INPUT, EmptyList, 0 );
  105.   return( MethodFlags );
  106. }
  107.  
  108.  
  109.  
  110. /*****************************************************************
  111.  -----------------------------------------------------------------
  112.  **** Method call to set some attributes
  113.  **** Note: This routine is a little bit overdone, but it's a demo
  114.  ****/
  115. __geta4 rfcall (mth_a_SetAttr, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  116. {
  117.   struct math_a_Instance *Instance = Macro_GetInstance;
  118.  
  119.   struct TagItem *cur_ti, *numberof_ti = NULL;
  120.   ULONG  the_tag, nr_attr = 0xFFFFFFFF;
  121.  
  122.   /*******************************
  123.    **** Check for the BTA_NumberOf
  124.    ****/
  125.   if (numberof_ti = BST_FindTagItem( BTA_NumberOf, TagList ))
  126.       {    nr_attr = numberof_ti->ti_Data; }
  127.  
  128.  
  129.   /************************
  130.    **** Set every attribute
  131.    ****/
  132.   for (cur_ti = TagList; ( cur_ti->ti_Tag != TAG_DONE ) &
  133.                ( nr_attr >  0  )       ; cur_ti = BST_NextTagItem( cur_ti ))
  134.     {
  135.       the_tag         = cur_ti->ti_Tag;
  136.       cur_ti->ti_Tag |= BTF_Ignore;
  137.       nr_attr--;
  138.       switch( the_tag )
  139.         {
  140.       case BTA_LongNumber:     Instance->A = cur_ti->ti_Data;  break;
  141.           default           :     nr_attr++;
  142.                       cur_ti->ti_Tag  &= ~BTF_Ignore; break;
  143.         }
  144.     }
  145.  
  146.   /****************************************************
  147.    **** Reset the MethodFlags if all attributes are set
  148.    ****/
  149.   if (  numberof_ti )
  150.     if (!(numberof_ti->ti_Data = nr_attr))
  151.     MethodFlags &= ~( MTHF_DOPARENTS | MTHF_DOCHILDREN | MTHF_PASSTOCHILD );
  152.  
  153.   return( MethodFlags );
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. /***************************************************************************
  161.  ===========================================================================
  162.  **** MATH_x2_Class methods
  163.  **************************/
  164.  
  165. /**********************************************
  166.  ----------------------------------------------
  167.  **** Method called when a new X number arrives
  168.  ****/
  169. __geta4 rfcall (mth_x2_InputX, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  170. {
  171.   struct math_x2_Instance *Instance = Macro_GetInstance;
  172.   struct TagItem       *cur_ti;
  173.  
  174.   /******************************
  175.    **** Search for the BTA_X tag.
  176.    ****/
  177.   if ( cur_ti = BST_FindTagItem( BTA_X, TagList ))
  178.     {
  179.       LONG X = cur_ti->ti_Data;
  180.  
  181.       struct TagItem TL_Output[] =
  182.       { {BTA_Y, (Instance->A * X*X)},        /**** Calculate A * X^2  ****/
  183.       {TAG_DONE, 0}
  184.       };
  185.       OBJ_ToOutput( Object,TL_Output, OBM_OUTPUT, 0 );
  186.     }
  187.  
  188.   return( MethodFlags | MTHF_BREAK );
  189. }
  190.  
  191.  
  192.  
  193. /***************************************************************************
  194.  ===========================================================================
  195.  **** MATH_x_Class methods
  196.  *************************/
  197.  
  198.  
  199. /**********************************************
  200.  ----------------------------------------------
  201.  **** Method called when a new X number arrives
  202.  ****/
  203. __geta4 rfcall (mth_x_InputX, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  204. {
  205.   struct math_x_Instance *Instance = Macro_GetInstance;
  206.   struct TagItem       *cur_ti;
  207.  
  208.   /******************************
  209.    **** Search for the BTA_X tag.
  210.    ****/
  211.   if ( cur_ti = BST_FindTagItem( BTA_X, TagList ))
  212.     {
  213.       LONG X = cur_ti->ti_Data;
  214.  
  215.       struct TagItem TL_Output[] =
  216.       { {BTA_Y, (Instance->A * X)},        /**** Calculate A * X   ****/
  217.       {TAG_DONE, 0}
  218.       };
  219.       OBJ_ToOutput( Object,TL_Output, OBM_OUTPUT, 0 );
  220.     }
  221.  
  222.   return( MethodFlags | MTHF_BREAK );
  223. }
  224.  
  225.  
  226.  
  227. /***************************************************************************
  228.  ===========================================================================
  229.  **** MATH_xy_Class methods
  230.  **************************/
  231. __geta4 rfcall (mth_xy_Init, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  232. {
  233.   struct math_xy_Instance *Instance = Macro_GetInstance;
  234.  
  235.   Instance->Y = 0;
  236.   return( MethodFlags );
  237. }
  238.  
  239.  
  240.  
  241. /**********************************************************
  242.  ----------------------------------------------------------
  243.  **** Method called when a new _part_ of the result arrives
  244.  ****/
  245. __geta4 rfcall (mth_xy_InputY, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  246. {
  247.   struct math_xy_Instance *Instance = Macro_GetInstance;
  248.   struct TagItem      *cur_ti;
  249.  
  250.   /******************************
  251.    **** Search for the BTA_Y tag.
  252.    ****/
  253.   if ( cur_ti = BST_FindTagItem( BTA_Y, TagList ))
  254.     {
  255.       printf("----- add %ld\n",cur_ti->ti_Data);
  256.       Instance->Y = Instance->Y + cur_ti->ti_Data;
  257.     }
  258.  
  259.   return( MethodFlags | MTHF_BREAK );
  260. }
  261.  
  262.  
  263.  
  264. /***************************************************************
  265.  ---------------------------------------------------------------
  266.  **** Method called the object connected to this method requests
  267.  **** for new input.
  268.  ****/
  269. __geta4 rfcall (mth_xy_OutputX, BST_MethodFlags MethodFlags, struct BST_Object *Object, struct TagItem *TagList)
  270. {
  271.   struct math_xy_Instance *Instance = Macro_GetInstance;
  272.  
  273.   Instance->Y = 0.0;    /**** Reset the result ****/
  274.  
  275.   struct TagItem TL_Output[] =
  276.     { {BTA_X, Instance->A},
  277.       {TAG_DONE, 0}
  278.     };
  279.   OBJ_ToOutput( Object, TL_Output, OBM_OUTPUT, 0 );
  280.  
  281.   printf("**** Y result = %ld \n", Instance->Y );
  282.  
  283.   return( MethodFlags | MTHF_BREAK );
  284. }
  285.  
  286.  
  287.  
  288.  
  289. /*********************************/
  290. /**** M A I N - P R O G R A M ****/
  291. /*********************************/
  292. main()
  293. {
  294.   struct BST_Class  *math_x2_class, *math_x_class, *math_a_class, *math_xy_class;
  295.   struct BST_Object *math_x2_obj,   *math_x_obj,   *math_a_obj,   *math_xy_obj;
  296.   
  297.   printf("*** Beast OO Example3 START ***\n");
  298.   if (BeastBase = (struct Library *)OpenLibrary("beast.library",0))
  299.     {
  300.  
  301.       math_a_class   = BST_MakeClass( "MATH_aClass",   sizeof( struct math_a_Instance   ));
  302.       CLSS_AddMethod( math_a_class,   &mth_a_Init,      OBM_INIT    );
  303.       CLSS_AddMethod( math_a_class,   &mth_a_InputX,    OBM_INPUT   );
  304.       CLSS_AddMethod( math_a_class,   &mth_a_OutputX,   OBM_OUTPUT  );
  305.       CLSS_AddMethod( math_a_class,   &mth_a_SetAttr,     OBM_SETATTR );
  306.       BST_AddClass(   math_a_class   );
  307.  
  308.       math_x2_class = BST_MakeSubClass( "MATH_x2Class", sizeof( struct math_x2_Instance ), "MATH_aClass" );
  309.       math_x_class  = BST_MakeSubClass( "MATH_xClass",  sizeof( struct math_x_Instance  ), "MATH_aClass" );
  310.       math_xy_class = BST_MakeSubClass( "MATH_xyClass", sizeof( struct math_xy_Instance ), "MATH_aClass" );
  311.  
  312.       CLSS_AddMethod( math_x2_class, &mth_x2_InputX,  OBM_INPUT   );
  313.       CLSS_AddMethod( math_x_class,  &mth_x_InputX,   OBM_INPUT   );
  314.       CLSS_AddMethod( math_xy_class, &mth_xy_Init,    OBM_INIT    );
  315.       CLSS_AddMethod( math_xy_class, &mth_xy_InputY,  OBM_INPUT   );
  316.       CLSS_AddMethod( math_xy_class, &mth_xy_OutputX, OBM_OUTPUT  );
  317.  
  318.       BST_AddClass(   math_x2_class );
  319.       BST_AddClass(   math_x_class  );
  320.       BST_AddClass(   math_xy_class );
  321.  
  322.       /***************************
  323.        **** Now create the objects
  324.        ****/
  325.       math_x2_obj = OBJ_NewObject( NULL, "MATH_x2Class", NULL );
  326.       if (math_x2_obj != NULL)
  327.       {
  328.     math_x_obj = OBJ_NewObject( NULL, "MATH_xClass", NULL );
  329.     if (math_x_obj != NULL)
  330.     {
  331.       math_a_obj = OBJ_NewObject( NULL, "MATH_aClass", NULL );
  332.       if (math_a_obj != NULL)
  333.       {
  334.         math_xy_obj = OBJ_NewObject( NULL, "MATH_xyClass", NULL );
  335.         if (math_xy_obj != NULL)
  336.         {
  337.           OBJ_DoMethod( math_x2_obj, OBM_INIT, EmptyList, 0 );
  338.           OBJ_DoMethod( math_x_obj,  OBM_INIT, EmptyList, 0 );
  339.           OBJ_DoMethod( math_a_obj,  OBM_INIT, EmptyList, 0 );
  340.  
  341.           /**** Create the connection (the actual program) ****
  342.            *
  343.            * The object connection will look like;
  344.            *
  345.            *
  346.            *     OBM_INPUT  ,--------------, OBM_OUTPUT
  347.            *  ,------------>| math_x2_obj  |------------,
  348.            *  |        `--------------'            |
  349.            *  |                            | OBM_INPUT
  350.            *  |  OBM_INPUT  ,--------------, OBM_OUTPUT \    ,-------------, OBM_OUTPUT
  351.            *  |------------>| math_x_obj   |--------------->| math_xy_obj |---,
  352.            *  |        `--------------'        /    `-------------'      |
  353.            *  |                        |              |
  354.            *  |  OBM_INPUT    ,--------------, OBM_OUTPUT |              |
  355.            *  |------------>| math_a_obj   |------------'              |
  356.            *  |        `--------------'                  |
  357.            *  |                                  |
  358.            *  `---------------------------------------------------------------'
  359.            *
  360.            *******************************************************/
  361.  
  362.           OBJ_CreateConnection( math_x2_obj, math_xy_obj, OBM_OUTPUT, OBM_INPUT );
  363.           OBJ_CreateConnection( math_x_obj,  math_xy_obj, OBM_OUTPUT, OBM_INPUT );
  364.           OBJ_CreateConnection( math_a_obj,  math_xy_obj, OBM_OUTPUT, OBM_INPUT );
  365.           OBJ_CreateConnection( math_xy_obj, math_x2_obj, OBM_OUTPUT, OBM_INPUT );
  366.           OBJ_CreateConnection( math_xy_obj, math_x_obj,  OBM_OUTPUT, OBM_INPUT );
  367.           OBJ_CreateConnection( math_xy_obj, math_a_obj,  OBM_OUTPUT, OBM_INPUT );
  368.  
  369.  
  370.           /**** Setting our values ****/
  371.           { struct TagItem TL_SetAttr[] = { {BTA_NumberOf, 1}, {BTA_LongNumber, 1}, {TAG_DONE, 0} };
  372.             OBJ_DoMethod( math_x2_obj, OBM_SETATTR, TL_SetAttr, 0 ); }
  373.           { struct TagItem TL_SetAttr[] = { {BTA_NumberOf, 1}, {BTA_LongNumber, 1}, {TAG_DONE, 0} };
  374.             OBJ_DoMethod( math_x_obj,  OBM_SETATTR, TL_SetAttr, 0 ); }
  375.           { struct TagItem TL_SetAttr[] = { {BTA_NumberOf, 1}, {BTA_LongNumber, 4}, {TAG_DONE, 0} };
  376.             OBJ_DoMethod( math_a_obj,   OBM_SETATTR, TL_SetAttr, 0 ); }
  377.  
  378.           { struct TagItem TL_SetAttr[] = { {BTA_NumberOf, 1}, {BTA_LongNumber, 2}, {TAG_DONE, 0} };
  379.             OBJ_DoMethod( math_xy_obj,  OBM_SETATTR, TL_SetAttr, 0 ); }
  380.  
  381.           /**** Play around ****/
  382.           { struct TagItem TL_Start[] = { {TAG_DONE, 0} };
  383.             OBJ_DoMethod( math_xy_obj, OBM_OUTPUT, TL_Start, 0 ); }
  384.  
  385.         }
  386.       }
  387.     }
  388.       }
  389.  
  390.       /**************************
  391.        **** Get rid of our object
  392.        ****/
  393.       OBJ_DisposeObject( math_x2_obj );
  394.       OBJ_DisposeObject( math_x_obj  );
  395.       OBJ_DisposeObject( math_a_obj  );
  396.       OBJ_DisposeObject( math_xy_obj );
  397.  
  398.       /***********************
  399.        **** Remove the classes
  400.        ****/
  401.       BST_RemoveClass( math_xy_class );
  402.       BST_RemoveClass( math_a_class  );
  403.       BST_RemoveClass( math_x_class  );
  404.       BST_RemoveClass( math_x2_class );
  405.  
  406.       BST_FreeClass(   math_xy_class );
  407.       BST_FreeClass(   math_a_class  );
  408.       BST_FreeClass(   math_x_class  );
  409.       BST_FreeClass(   math_x2_class );
  410.  
  411.       CloseLibrary( BeastBase );
  412.     }
  413.   printf("*** Beast OO Example3 END ***\n");
  414. }
  415.