home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16087 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  3.4 KB

  1. Path: sparky!uunet!airgun!airgun.wg.waii.com!bab
  2. From: bab@se39.wg2.waii.com (Brian Button)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Discriminating Variant Records Without switch() ??
  5. Message-ID: <BAB.92Nov11075454@se39.wg2.waii.com>
  6. Date: 11 Nov 92 12:54:54 GMT
  7. References: <BAB.92Nov10073324@se39.wg2.waii.com> <1992Nov10.181529.2711@bmerh85.bnr.ca>
  8. Sender: news@airgun.wg.waii.com
  9. Organization: Western Geophysical Exploration Products
  10. Lines: 70
  11. Nntp-Posting-Host: se39.wg2.waii.com
  12. In-reply-to: glenm@bnr.ca's message of 10 Nov 92 18:15:29 GMT
  13.  
  14.  
  15. In article <BAB.92Nov10073324@se39.wg2.waii.com>, bab@se39.wg2.waii.com (Brian Button) writes:
  16. ...
  17. |> As an example:
  18. |> 
  19. |> We're reading structures sent to us over a socket. Since these
  20. |> structures are being sent and received on the same machine, we can
  21. |> just memcpy the byte stream read from the socket into a big union, and
  22. |> then switch based on the first field of the union to determine the type.
  23. |> 
  24. |> As a trivial example:
  25. |> 
  26. |> {
  27. |>     read( sd, buf, 16 );
  28. |>     memcpy(( char * )&union_of_structs, buf, 16 );
  29. |>     switch( union_of_structs.type )
  30. |>     {
  31. |>         case A:
  32. |> 
  33. |>             .
  34. |>             .
  35. |>             .
  36. |>     }
  37. |> }
  38.  
  39.    Glen> Well, this is a bit of a can of worms.
  40.  
  41.    Glen> It would seem to me that a solution to this problem would be a
  42.    Glen> class dispatcher (or whatever you'd like to call it). Basically,
  43.    Glen> the central entity is responsible for creating objects based on
  44.    Glen> the data stream, having been given a list of tags and constructors
  45.    Glen> to use for this purpose.
  46.  
  47. Basically, what you've described is a factory object. This object is
  48. responsible for creating new instances of any arbitrary object, as
  49. long as it is descended from some base class. In RogueWave, this class
  50. is the RWCollectable, and the factory is an RWFactory.
  51.  
  52. Our problem, however, is not in the object creation, since the factory
  53. can do that for us. Our problem is how to discriminate the type of the
  54. object and process it accordingly after it is already created.
  55.  
  56. As an example, we're dealing with gyro-compasses, inclinometers, and
  57. streamers. The first two objects are just data gathering devices, but
  58. a streamer is a collection of instances of each of the previous
  59. objects. Once we've created a streamer or a data gathering device
  60. through this factory, they obviously need to be handled in different
  61. ways. We may need to get the reading from a compass, reset its bias,
  62. reposition it on the streamer, or get a list of all devices on the
  63. streamer, find all compasses on the streamer, add a device to it, etc.
  64. The point I'm trying to make is that there is no common set of
  65. operations which may be performed on these arbitrary objects.
  66.  
  67. Since the set of operations is not common, we need to know the type of the
  68. object before proceeding with processing. Have we painted ourselves
  69. into a corner? It seems that we need a switch() to do this, but in a
  70. proper OOP design, it shouldn't be necessary.
  71.  
  72. Advice??
  73.  
  74. bab
  75.  
  76. --
  77. |-----------------------|----------------------------------------------------|
  78. | Brian Button          | email : button@wg2.waii.com                        |
  79. | Design Engineer       |         71023.276@compuserve.com                   |
  80. | Western Geophysical   | voice : (713)964-6221                              |
  81. | 3600 Briarpark        |----------------------------------------------------|
  82. | Houston, Texas  77042 |                  Opinions Be Mine!!                |
  83. |-----------------------|----------------------------------------------------|
  84.