// SwitchP.C: Switch nodes (private source).
// Automatically generated with nodegen 0.5a - do not edit.

#include "Switch.H"
#include "xrmlP.H"

namespace xrml {
int Switch::major_version(void) { return 0; }
int Switch::minor_version(void) { return 5; }
bool Switch::is_leaf_class(void) { return true; }

bool IsSwitch(SFNode* node)
{
  return (dynamic_cast<Switch*>(node) != 0);
}

void Switch::synchronize_choice(void)
{
  if (_choice.size > choice.size) choice.grow(_choice.size - choice.size);
  for (int i=choice.size=0; i<_choice.size; i++)
    choice.append(dynamic_cast<Children*>(_choice[i]->firstnode()));
}

bool Switch::assign_choice(const MFNode& value)
{
  MFNode oldval = _choice;
  _choice = value;
  if (checkValue(_choice, IsChildren, "Switch.choice", "Children")) {
    synchronize_choice();
    status |= choice_updated;
    timestamp = interface[0]->timestamp = world ? world->time : 0;
    return true;
  } else {
    _choice = oldval;
    return false;
  }
}

static bool assign_Switch_choice(SFNode* node, const fieldValue& value)
{
  Switch* Switch_node = dynamic_cast<Switch*>(node);
  if (!Switch_node)
    Fatal(-1, "assign_Switch_choice", "called for a non-Switch node");
  return Switch_node->assign_choice((const MFNode&)value);
}

void Switch::set_choice(const MFNode& val)
{
  MFNode tmpval(val);
  fieldValue v(&tmpval);
  interface[0]->assign(this, v);
}

bool Switch::assign_whichChoice(const SFInt32& value)
{
  // TODO: value range check!
  whichChoice = value;
  status |= whichChoice_updated;
  timestamp = interface[1]->timestamp = world ? world->time : 0;
  return true;
}

static bool assign_Switch_whichChoice(SFNode* node, const fieldValue& value)
{
  Switch* Switch_node = dynamic_cast<Switch*>(node);
  if (!Switch_node)
    Fatal(-1, "assign_Switch_whichChoice", "called for a non-Switch node");
  return Switch_node->assign_whichChoice((const SFInt32&)value);
}

void Switch::set_whichChoice(const SFInt32& val)
{
  SFInt32 tmpval(val);
  fieldValue v(&tmpval);
  interface[1]->assign(this, v);
}

Switch::Switch()
{
  interfaceMember* the_interface[2] = {
    new exposedField("choice", fieldValue(&_choice), assign_Switch_choice),
    new exposedField("whichChoice", fieldValue(&whichChoice), assign_Switch_whichChoice),
  };
  SFNode::construct(SwitchTypeId, 2, the_interface);

  choice = array<Children*>(0); 	_choice = MFNode(0);
  whichChoice = -1;
  
  Grouping::util_init();
  Children::util_init();
  Switch::util_init();
}

void Switch::destruct(void)
{
  Switch::util_destruct();
  Grouping::util_destruct();
  Children::util_destruct();
  SFNode::destruct();
}

Switch::~Switch()
{
  destruct();
}

void Switch::clone_here(const Switch& src)
{
  SFNode::clone(src);
  
  interface[0]->value = fieldValue(&_choice);
  interface[1]->value = fieldValue(&whichChoice);
  
  choice = src.choice;	  _choice = src._choice;
  whichChoice = src.whichChoice;
  
  Grouping::util_clone(src);
  Children::util_clone(src);
  Switch::util_clone(src);
}

Switch::Switch(const Switch& src)
{
  clone_here(src);
}

Switch& Switch::operator=(const Switch& src)
{
  if (this != &src) {
    destruct();
    clone_here(src);
  }
  return *this;
}

SFNode* Switch::instantiate(Proto *enclosing_proto)
{
  if (nameIndex>=0)
    // named node: return pointer to pre-made instance
    return enclosing_proto ? enclosing_proto->namedNodes[nameIndex] : this;
  
  Switch* instance = new Switch(*this);
  instance->instantiate_interface(enclosing_proto);
  instance->synchronize_choice();
  return instance;
}

}  // namespace xrml
