// Generated on 2011 June Mon, 20 at 05:30:35 by the CERTI message generator
#include <vector>
#include <string>
#include "addressbook.hh"
// The generated file will be put in 
// the MsgTutorial package
namespace MsgTutorial {

   nmsg::nmsg() {
      //type= <no default value in message spec using builtin>
      //messageName= <no default value in message spec using builtin>
   }

   nmsg::~nmsg() {
   }

   void nmsg::serialize(libhla::MessageBuffer& msgBuffer) {
      //Specific serialization code
      msgBuffer.write_uint32(type);
      msgBuffer.write_string(messageName);
   }

   void nmsg::deserialize(libhla::MessageBuffer& msgBuffer) {
      //Specific deserialization code
      type = msgBuffer.read_uint32();
      msgBuffer.read_string(messageName);
   }

   std::ostream& nmsg::show(std::ostream& out) {
      out << "[nmsg -Begin]" << std::endl;      //Specific show code
      out << " type = " << type << " "       << std::endl;
      out << " messageName = " << messageName << " "       << std::endl;
      out << "[nmsg -End]" << std::endl;
      return out;
   }

   PhoneNumber::PhoneNumber() {
      //number= <no default value in message spec using builtin>
      _hasType=false;
      //type= <no default value in message spec using builtin>
   }

   PhoneNumber::~PhoneNumber() {
   }

   void PhoneNumber::serialize(libhla::MessageBuffer& msgBuffer) {
      //Specific serialization code
      msgBuffer.write_string(number);
      msgBuffer.write_bool(_hasType);
      if (_hasType) {
         msgBuffer.write_uint32(type);
      }
   }

   void PhoneNumber::deserialize(libhla::MessageBuffer& msgBuffer) {
      //Specific deserialization code
      msgBuffer.read_string(number);
      _hasType = msgBuffer.read_bool();
      if (_hasType) {
         type = static_cast<addressbook::PhoneType>(msgBuffer.read_uint32 ());
      }
   }

   std::ostream& PhoneNumber::show(std::ostream& out) {
      out << "[PhoneNumber -Begin]" << std::endl;      //Specific show code
      out << " number = " << number << " "       << std::endl;
      out << "(opt) type =" << type << " "       << std::endl;
      out << "[PhoneNumber -End]" << std::endl;
      return out;
   }

   Person::Person() {
      this->messageName = "Person";
      this->type = PERSON;
      //name= <no default value in message spec using builtin>
      //id= <no default value in message spec using builtin>
      _hasEmail=false;
      //email= <no default value in message spec using builtin>
      //phone= <no default value in message spec using builtin>
   }

   Person::~Person() {
   }

   void Person::serialize(libhla::MessageBuffer& msgBuffer) {
      //Call mother class
      Super::serialize(msgBuffer);
      //Specific serialization code
      msgBuffer.write_string(name);
      msgBuffer.write_int32(id);
      msgBuffer.write_bool(_hasEmail);
      if (_hasEmail) {
         msgBuffer.write_string(email);
      }
      uint32_t phoneSize = phone.size();
      msgBuffer.write_uint32(phoneSize);
      for (uint32_t i = 0; i < phoneSize; ++i) {
         phone[i].serialize(msgBuffer);
      }
   }

   void Person::deserialize(libhla::MessageBuffer& msgBuffer) {
      //Call mother class
      Super::deserialize(msgBuffer);
      //Specific deserialization code
      msgBuffer.read_string(name);
      id = msgBuffer.read_int32();
      _hasEmail = msgBuffer.read_bool();
      if (_hasEmail) {
         msgBuffer.read_string(email);
      }
      uint32_t phoneSize = msgBuffer.read_uint32();
      phone.resize(phoneSize);
      for (uint32_t i = 0; i < phoneSize; ++i) {
         phone[i].deserialize(msgBuffer);
      }
   }

   std::ostream& Person::show(std::ostream& out) {
      out << "[Person -Begin]" << std::endl;      //Call mother class
      Super::show(out);
      //Specific show code
      out << " name = " << name << " "       << std::endl;
      out << " id = " << id << " "       << std::endl;
      out << "(opt) email =" << email << " "       << std::endl;
      out << "    phone [] =" << std::endl;
      for (uint32_t i = 0; i < getPhoneSize(); ++i) {
         out << phone[i].show(out);
      }
      out << std::endl;
      out << "[Person -End]" << std::endl;
      return out;
   }

   AddressBook::AddressBook() {
      this->messageName = "AddressBook";
      this->type = ADDRESSBOOK;
      //person= <no default value in message spec using builtin>
   }

   AddressBook::~AddressBook() {
   }

   void AddressBook::serialize(libhla::MessageBuffer& msgBuffer) {
      //Call mother class
      Super::serialize(msgBuffer);
      //Specific serialization code
      uint32_t personSize = person.size();
      msgBuffer.write_uint32(personSize);
      for (uint32_t i = 0; i < personSize; ++i) {
         person[i].serialize(msgBuffer);
      }
   }

   void AddressBook::deserialize(libhla::MessageBuffer& msgBuffer) {
      //Call mother class
      Super::deserialize(msgBuffer);
      //Specific deserialization code
      uint32_t personSize = msgBuffer.read_uint32();
      person.resize(personSize);
      for (uint32_t i = 0; i < personSize; ++i) {
         person[i].deserialize(msgBuffer);
      }
   }

   std::ostream& AddressBook::show(std::ostream& out) {
      out << "[AddressBook -Begin]" << std::endl;      //Call mother class
      Super::show(out);
      //Specific show code
      out << "    person [] =" << std::endl;
      for (uint32_t i = 0; i < getPersonSize(); ++i) {
         out << person[i].show(out);
      }
      out << std::endl;
      out << "[AddressBook -End]" << std::endl;
      return out;
   }

   nmsg* M_Factory::create(M_Type type) throw (std::string) { 
      nmsg* msg = NULL;

      switch (type) {
         case nmsg::NOT_USED:
            throw std::string("NOT_USED message type should not be used!!");
            break;
         case nmsg::PERSON:
            msg = new Person();
            break;
         case nmsg::ADDRESSBOOK:
            msg = new AddressBook();
            break;
         case nmsg::LAST:
            throw std::string("LAST message type should not be used!!");
            break;
      } // end if switch (type)
      return msg;
   } /* end of M_Factory::create */

} // end of namespace MsgTutorial 
