ppSubroutine.cxx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Filename: ppSubroutine.cxx
  2. // Created by: drose (10Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "ppSubroutine.h"
  6. PPSubroutine::Subroutines PPSubroutine::_subroutines;
  7. PPSubroutine::Subroutines PPSubroutine::_functions;
  8. ////////////////////////////////////////////////////////////////////
  9. // Function: PPSubroutine::define_sub
  10. // Access: Public, Static
  11. // Description: Adds a subroutine to the global list with the
  12. // indicated name. The subroutine pointer must have
  13. // been recently allocated, and ownership of the pointer
  14. // will be passed to the global list; it may later
  15. // delete it if another subroutine is defined with the
  16. // same name.
  17. ////////////////////////////////////////////////////////////////////
  18. void PPSubroutine::
  19. define_sub(const string &name, PPSubroutine *sub) {
  20. Subroutines::iterator si;
  21. si = _subroutines.find(name);
  22. if (si == _subroutines.end()) {
  23. _subroutines.insert(Subroutines::value_type(name, sub));
  24. } else {
  25. delete (*si).second;
  26. (*si).second = sub;
  27. }
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: PPSubroutine::get_sub
  31. // Access: Public, Static
  32. // Description: Returns the previously-defined subroutine with the
  33. // given name, or NULL if there is no such subroutine
  34. // with that name.
  35. ////////////////////////////////////////////////////////////////////
  36. const PPSubroutine *PPSubroutine::
  37. get_sub(const string &name) {
  38. Subroutines::const_iterator si;
  39. si = _subroutines.find(name);
  40. if (si == _subroutines.end()) {
  41. return NULL;
  42. } else {
  43. return (*si).second;
  44. }
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. // Function: PPSubroutine::define_func
  48. // Access: Public, Static
  49. // Description: Adds a function to the global list with the
  50. // indicated name. This is similar to a subroutine
  51. // except it is to be invoked via a variable reference,
  52. // instead of by a #call function. It cannot be
  53. // shadowed by a local variable; it will always override
  54. // any variable definition.
  55. //
  56. // The subroutine pointer must have been recently
  57. // allocated, and ownership of the pointer will be
  58. // passed to the global list; it may later delete it if
  59. // another subroutine is defined with the same name.
  60. ////////////////////////////////////////////////////////////////////
  61. void PPSubroutine::
  62. define_func(const string &name, PPSubroutine *sub) {
  63. Subroutines::iterator si;
  64. si = _functions.find(name);
  65. if (si == _functions.end()) {
  66. _functions.insert(Subroutines::value_type(name, sub));
  67. } else {
  68. delete (*si).second;
  69. (*si).second = sub;
  70. }
  71. }
  72. ////////////////////////////////////////////////////////////////////
  73. // Function: PPSubroutine::get_func
  74. // Access: Public, Static
  75. // Description: Returns the previously-defined function with the
  76. // given name, or NULL if there is no such function
  77. // with that name.
  78. ////////////////////////////////////////////////////////////////////
  79. const PPSubroutine *PPSubroutine::
  80. get_func(const string &name) {
  81. Subroutines::const_iterator si;
  82. si = _functions.find(name);
  83. if (si == _functions.end()) {
  84. return NULL;
  85. } else {
  86. return (*si).second;
  87. }
  88. }