/tags/rel-0-4-3/FreeSpeech/data-flow/src/Catch.cc

# · C++ · 123 lines · 72 code · 12 blank · 39 comment · 9 complexity · 9574876bdbbdb3a35b82c0fd3abb3e26 MD5 · raw file

  1. // Copyright (C) 1999 Jean-Marc Valin
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "Node.h"
  17. #include "FlowException.h"
  18. class Catch;
  19. DECLARE_NODE(Catch)
  20. /*Node
  21. *
  22. * @name Catch
  23. * @category Flow
  24. * @description Catches an exception
  25. *
  26. * @input_name INPUT
  27. * @input_description Normal flow
  28. *
  29. * @input_name CATCH
  30. * @input_description Flow to follow is an exception is caught
  31. *
  32. * @output_name OUTPUT
  33. * @output_description Flow output
  34. *
  35. * @output_name EXCEPTION
  36. * @output_description The exception caught (use only as feedback link)
  37. *
  38. END*/
  39. class Catch : public Node {
  40. protected:
  41. int inputID;
  42. int catchID;
  43. int outputID;
  44. int exceptionID;
  45. bool isInside;
  46. ObjectRef currentException;
  47. public:
  48. Catch(string nodeName, ParameterSet params)
  49. : Node(nodeName, params)
  50. , isInside(false)
  51. {
  52. try {
  53. inputID=addInput("INPUT");
  54. catchID=addInput("CATCH");
  55. outputID=addOutput("OUTPUT");
  56. exceptionID=addOutput("EXCEPTION");
  57. } catch (BaseException *e)
  58. {
  59. throw e->add(new NodeException (NULL, "Exception caught in Catch constructor", __FILE__, __LINE__));
  60. }
  61. }
  62. /*WARNING: Do not try this at home. Overriding the registerOutput() method should not be
  63. done unless you REALLY know what you're doing... and I'm not even sure
  64. I know what I'm doing here*/
  65. void registerOutput (int out)
  66. {
  67. if (out == outputID)
  68. incrementOutputInitialize();
  69. }
  70. ObjectRef getOutput(int output_id, int count)
  71. {
  72. if (output_id == outputID)
  73. {
  74. if (isInside)
  75. {
  76. cerr << "What the heck is going on??? " << endl;
  77. throw new NodeException (this, "I don't know what I'm doing", __FILE__, __LINE__);
  78. }
  79. try
  80. {
  81. ObjectRef inputValue = getInput(inputID, count);
  82. return inputValue;
  83. } catch (RCPtr<FlowException> e)
  84. {
  85. isInside = true;
  86. currentException = e->getObject();
  87. ObjectRef catchValue(NULL);
  88. try
  89. {
  90. catchValue = getInput(catchID, count);
  91. } catch (...)
  92. {
  93. throw;
  94. //cerr << "The catch flow is throwing an exception... I'm confused" << endl;
  95. //throw NodeException (this, "The catch flow is throwing an exception", __FILE__, __LINE__);
  96. }
  97. isInside = false;
  98. return catchValue;
  99. }
  100. } else if (output_id==exceptionID)
  101. {
  102. if (!isInside)
  103. {
  104. throw new NodeException (this, "The EXCEPTION output is only for the catch flow", __FILE__, __LINE__);
  105. }
  106. return currentException;
  107. } else {
  108. throw new NodeException (this, "Output not found", __FILE__, __LINE__);
  109. }
  110. }
  111. };