INTF-CHANGES-FAQ-0.99.12.txt 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. Frequently Asked Questions about the changes, introduced in version 0.99.12 of
  2. PTCPas
  3. Q: My code uses only the ptcgraph and ptccrt (TP7 compatibility) units. Is it
  4. affected by the changes?
  5. A: No. Only code that uses the ptc unit is affected.
  6. Q: Were the changes necessary?
  7. A: Yes, I believe so. Using certain objects like events, areas, and formats was
  8. tedious because of the need to manually manage and free them. You had to be
  9. aware of their lifecycle, e.g. when getting an event from the console you had
  10. to free it once you were done with it, but, when getting the format of the
  11. console, you had to know that you must not free it and that you must not change
  12. it with .Assign (otherwise things would break in the console, etc.) With the new
  13. interfaces, suddenly you don't have to worry about all that. The original C++
  14. library OpenPTC was much easier to work with, thanks to C++'s automatic
  15. destructors, copy constructors and operator overloading. However, when PTC was
  16. ported to Pascal for the first time, FPC was still at version 1.0.x and didn't
  17. support interfaces, so it wasn't possible to do at the time. I was planning this
  18. change for a long time but I kept postponing it until now :)
  19. Q: Are you planning any more changes that break backward compatibility?
  20. A: No. I promise :)
  21. Q: Was it possible to introduce the changes, without breaking backward
  22. compatibility with existing code?
  23. A: Basically, it was hard to do without having to maintain a full set of wrapper
  24. classes with the old interface for compatibility and that is a lot of extra work
  25. I wouldn't like to do. I'd rather help people migrate their code to the new
  26. version. :)
  27. I documented the necessary changes, most of them can be done with just a series
  28. of search & replace and you can also email me your code if you have any
  29. trouble - I'll do my best to help.
  30. Q: How about keeping the old methods around and adding new overloaded ones?
  31. A: I considered doing that for a while, but I figured it wasn't an option,
  32. because it would make a huge mess out of PTCPas' interface. Also mixing
  33. reference counted interfaces and ordinary classes is dangerous and prone to
  34. errors (see next question for details). If you really cannot justify the effort
  35. needed to upgrade your code, old PTCPas versions are available on sourceforge
  36. forever. :)
  37. Q: Why were constructors replaced with factory methods?
  38. A: I wanted to hide the old classes completely and force the use of interfaces
  39. everywhere. One of the reason for this was that if I kept the old classes, in
  40. certain cases, that would make old code compile without errors, but crash at
  41. runtime. Here's an example:
  42. var
  43. format: TPTCFormat; // bug: this should be changed to IPTCFormat
  44. ...
  45. begin
  46. format := TPTCFormat.Create(8); // object created, ref count = 0
  47. surface := TPTCSurface.Create(320, 200, format); // the reference count of
  48. // 'format' is increased (i.e. becomes 1) before the constructor call and
  49. // decreased afterwards back to 0 and then the object is freed.
  50. // If format was declared of type IPTCFormat, it would keep the reference
  51. // count one higher until the variable goes out of scope.
  52. console := TPTCConsole.Create;
  53. console.Open(320, 200, format); // here we try to use format for the second
  54. // time, but it has been freed and we get a crash at runtime :(
  55. ...
  56. end.
  57. The problem occurs when mixing direct references to the object with access to it
  58. via interfaces. To prevent this type of error, I made the TPTCFormat type
  59. private (i.e. hidden in the implementation part of the ptc unit) and only
  60. IPTCFormat public. This way, when trying to compile this code, you'll get a
  61. compile error in the declaration of 'format: TPTCFormat;' and you'll know that
  62. you need to fix it (by changing the type to IPTCFormat). However, this has the
  63. side effect of making the call to TPTCFormat.Create impossible (because
  64. TPTCFormat isn't public). IPTCFormat.Create sounds like the logical alternative,
  65. but it's also not possible, as IPTCFormat is an interface and interfaces cannot
  66. have constructors. That's why I introduced factory class methods (in this case:
  67. TPTCFormatFactory.CreateNew)