/tags/dev-20000116/FreeSpeech/data-flow/src/Iterator.cc

# · C++ · 157 lines · 79 code · 30 blank · 48 comment · 18 complexity · 7465931d8ca2d97e3f1a80fa4e727263 MD5 · raw file

  1. // Copyright (C) 1999 Jean-Marc Valin & Dominic Letourneau
  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. #ifndef _ITERATOR_CC_
  17. #define _ITERATOR_CC_
  18. #include "Iterator.h"
  19. #include "Node.h"
  20. #include "ObjectRef.h"
  21. /***************************************************************************/
  22. /*
  23. Iterator::Iterator(...)
  24. Dominic Letourneau
  25. */
  26. /***************************************************************************/
  27. Iterator::Iterator (string nodeName, ParameterSet params)
  28. : Network(nodeName, params), output(new Object(Object::nil)) {
  29. translator = NULL;
  30. }
  31. /***************************************************************************/
  32. /*
  33. Iterator::getOutput(...)
  34. Dominic Letourneau
  35. */
  36. /***************************************************************************/
  37. ObjectRef Iterator::getOutput (int output_id, int count) {
  38. if (!hasOutput(output_id)) throw NodeException (this, "Cannot getOutput id",__FILE__,__LINE__);
  39. lock();
  40. if (processCount != count) {
  41. try {
  42. //Reinitialization of all the processCount in our Network
  43. map<string,Node*>::iterator iter;
  44. if (processCount != -1 ) {
  45. for (iter = nodeDictionary.begin(); iter != nodeDictionary.end(); iter++) {
  46. if (debugMode) {
  47. cout<<"DEBUG : Iterator is now resetting node "<<(*iter).second->getName()<<endl;
  48. }
  49. (*iter).second->reset();
  50. }
  51. }
  52. //We are doing a little trick for the translator (real inputNode)
  53. //if it exists
  54. if (translator) {
  55. translator->setProcessCount(count);
  56. }
  57. int conditionID = conditionNode->translateOutput("OUTPUT");
  58. int pc = 0;
  59. while(1)
  60. {
  61. if (doWhile)
  62. {
  63. output = sinkNode->getOutput(output_id,pc);
  64. }
  65. ObjectRef condition = conditionNode->getOutput(conditionID,pc);
  66. if (dereference_cast<bool>(condition)==false) break;
  67. if (!doWhile)
  68. {
  69. output = sinkNode->getOutput(output_id,pc);
  70. }
  71. pc++;
  72. }
  73. }
  74. catch (GenericCastException &e) {
  75. //We had a problem casting, our inputs are invalid?
  76. e.print();
  77. output = ObjectRef(new Object(Object::nil));
  78. }
  79. catch (BaseException &e) {
  80. //Something weird happened
  81. e.print();
  82. throw NodeException (this,string("Error in Iterator::getOutput"), __FILE__,__LINE__);
  83. }
  84. processCount = count;
  85. }
  86. unlock();
  87. return output;
  88. }
  89. /***************************************************************************/
  90. /*
  91. Iterator::connectToNode(...)
  92. Dominic Letourneau
  93. */
  94. /***************************************************************************/
  95. void Iterator::connectToNode(unsigned int in, Node *inNode, unsigned int out) {
  96. if (!inputNode)
  97. throw NodeException(this,"Trying to connect without input node",__FILE__,__LINE__);
  98. if (!translator) {
  99. //let's add our translator node
  100. translator = new InputTranslator("ITERATOR_TRANSLATOR",ParameterSet());
  101. addNode (*translator);
  102. }
  103. int translator_out = translator->addInput(this->getInputs()[in].name);
  104. // Connecting the inputNode
  105. inputNode->connectToNode(in,translator,translator_out);
  106. // We are connecting the translator
  107. translator->connectToNode(translator_out,inNode,out);
  108. }
  109. /***************************************************************************/
  110. /*
  111. Iterator::specificInitialize(...)
  112. Dominic Letourneau
  113. */
  114. /***************************************************************************/
  115. void Iterator::specificInitialize() {
  116. if (!conditionNode) {
  117. throw NodeException(this,"No condition Node specified in Iterator",__FILE__,__LINE__);
  118. }
  119. // We must initialize the conditionNode before
  120. conditionNode->initialize();
  121. this->Network::specificInitialize();
  122. if (parameters.exist("DOWHILE"))
  123. doWhile = true;
  124. else
  125. doWhile = false;
  126. }
  127. #endif