// This may look like C code, but it is really -*- C++ -*-

/***********************************************************
Copyright 1991 by the Massachusetts Institute of Technology

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.  M.I.T. makes no representations about the
suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.


Author: Roman J. Budzianowski - Project Athena MIT

******************************************************************/

#include "Objects.h"
#include "ObjectEditor.h"
#include <iostream.h>

Constant::Constant(String name)  : BaseObject("constant", name)
{
   value = new Integer(100);		// XXX needs way to set value by user
   workspace->AddObjectView(ClassName(),this,1,1);
}

void Constant::SetupIO(ObjectView* v)
{
   v->AddInput(trigger =new ObjectInput("trigger", this, Data::BooleanType));
   v->AddOutput(output=new ObjectOutput("output", this, Data::IntegerType));
}

Boolean Constant::AcceptInput(IO*,Data*)
{
   output->Send(value);
   return True;
}

Splitter::Splitter(String name)  : BaseObject("splitter", name)
{
   workspace->AddObjectView(ClassName(),this,1,2);
}

void Splitter::SetupIO(ObjectView* v)
{
   v->AddInput(input =new ObjectInput("input", this, Data::AnyType));
   v->AddOutput(output1 =new ObjectOutput("output1", this, Data::AnyType));
   v->AddOutput(output2 =new ObjectOutput("output2", this, Data::AnyType));
}

Boolean Splitter::AcceptInput(IO*,Data* data)
{
//   cerr << "Splitter: got input: " << (int)*data << "\n";
   output1->Send(data);
   output2->Send(data);
   return True;
}

Multiplier::Multiplier(String name)  
: BaseObject("multiplier", name), input_data1(0), input_data2(0)
{
   workspace->AddObjectView(ClassName(),this,2,1);
}

void Multiplier::SetupIO(ObjectView* v)
{
   v->AddInput(input1 =new ObjectInput("input1", this, Data::NumberType));
   v->AddInput(input2 =new ObjectInput("input2", this, Data::NumberType));
   v->AddOutput(output=new ObjectOutput("output", this, Data::NumberType));
}

Boolean Multiplier::AcceptInput(IO* io,Data* data)
{
//   cerr << "Multiplier: got input: " << (float)*data << "\n";
// temporary hack
   if(io==input1){
      if(input_data1) delete input_data1;
      input_data1 = new Float((float)*data);
   }
   if(io==input2){
      if(input_data2) delete input_data2;
      input_data2 = new Float((float)*data);
   }

   if(input_data1 && input_data2) {
      Float new_output(((float)*input_data1) * ((float)*input_data2));
      output->Send(&new_output);	// XXX remember to copy or use reference
   }
   return True;
}

Amplifier::Amplifier(String name)  : BaseObject("amplifier", name)
{
   workspace->AddObjectView(ClassName(),this,1,1);
}

void Amplifier::SetupIO(ObjectView* v)
{
   v->AddInput(input =new ObjectInput("input", this, Data::IntegerType));
   v->AddOutput(output=new ObjectOutput("output", this, Data::IntegerType));
}

Boolean Amplifier::AcceptInput(IO*,Data* data)
{
   cerr << "Amplifier: got input: " << (int)*data << "\n";
   Integer new_output(((int)*data)*((int)*data));
   output->Send(&new_output);
   return True;
}

