1,353c1,314
< // Zone.cpp: implementation of the Zone class.
< //
< ////////////////////////////////////
< 
< #include <iostream>
< //#include <fstream>
< #include <string>
< #include <cstring> // strcpy
< 
< #include "Zone.h"
< #include "Serializer.h"
< #include "Au.h"
< #include "AxsDef.h"
< 
< #include "init.h"
< 
< using namespace std;
< using namespace tsb; // timber supply branch namespace
< 
< ////////////////////////////////////
< // Construction/Destruction
< ////////////////////////////////////
< 
< // ZONE struct has built-in index (m_pStruct->id)
< //map<string, int> Zone::m_labelIndex = map<string, int>();
< 
< vector<Zone*> Zone::m_objectMemoryAddresses = vector<Zone*>();
< 
< /* inlined for speed
< Zone::Zone(ZONE* pZoneStruct)
<     : m_pStruct(pZoneStruct)
< {
< }
< */
< 
< Zone::~Zone()
< {
<     // do nothing yet, but may need to clean up m_pStruct
< }
< 
< // write contents to binary file
< void Zone::serialize( Serializer& binaryArchive ) //throw (FileException)
< {
<     if (binaryArchive.isWriting())
<     {
<         // write zone index
<         binaryArchive.write( &m_pStruct->id, sizeof(m_pStruct->id) ); // data, size
< 
<         // write zone label length & text
<         size_t labelLength = 0;
<         labelLength = strlen( m_pStruct->label ) + 1; // write with terminating null
<         binaryArchive.write( &labelLength, sizeof(labelLength) ); // data, size
<         binaryArchive.write( &(m_pStruct->label), labelLength ); // data, size
< 
<         // write zone area
<         //binaryArchive.write( &(m_pStruct->area), sizeof(m_pStruct->area) ); // data, size
< 
<         // write last zone AUID
<         //binaryArchive.write( &(m_pStruct->LastAUID), sizeof(m_pStruct->LastAUID) ); // data, size
< 
<         // target and reportFlags are not serialized (always read from file)
< 
<         // zone achieved struct has default values and is
<         // therefore not serialized, but must be re-allocated on read
<     }
<     else // reading archive
<     {
<         /*// allocate space to read in values
<         ZONE tempStruct = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
<         m_pStruct = &tempStruct;*/
< 
<         // read zone index
<         int id = 0;
<         binaryArchive.read( &id, sizeof(id) ); // data, size
< 
<         // read zone label length & text
<         size_t labelLength = 0; // already includes terminating null
<         binaryArchive.read( &labelLength, sizeof(labelLength) ); // data, size
<         char* label = new char[labelLength];
<         binaryArchive.read( label, labelLength ); // data, size
< 
<         // re-create & insert the C-struct into the hashed FssimApp::m_zoneList
<         this->recreateStruct(label, id);
< 
<         //m_labelIndex[m_pStruct->label] = m_pStruct->id; // create & insert key-value pair
<         delete[] label;
<         label = 0;
< 
<         // read zone area
<         //binaryArchive.read( &(m_pStruct->area), sizeof(m_pStruct->area) ); // data, size
< 
<         // read last zone AUID
<         //binaryArchive.read( &(m_pStruct->LastAUID), sizeof(m_pStruct->LastAUID) ); // data, size
< 
<         // target and reportFlags are not serialized (always read from file)
< 
<         // zone achieved struct has default values and is
<         // therefore not serialized, but must be re-allocated on read
<     }
<     // priority is not serialized (always read from file)
< 
<     this->serializeAuList(binaryArchive);
<     this->serializeAxsList(binaryArchive);
< }
< 
< void Zone::recreateStruct(char* label, int id)
< {
<     /*if (0 != m_pStruct) // clean up memory if overwriting existing struct
<     {
<         init_delete_zone(m_pStruct);
<     }*/
<     ZONE* pZone = init_make_zone(label, id);
<     if (0 == pZone)
<     {
<         string message = "ERROR: init_make_zone() failed in Zone::serialize.\n";
<         throw (message);
<     }
<     else
<     {
<         m_pStruct = pZone;
<         m_objectMemoryAddresses.push_back(this);
<     }
< }
< 
< // zone has custody of its AUs, called after serialization of zone
< void Zone::serializeAuList(Serializer& binaryArchive)
< {
<     if (binaryArchive.isWriting())
<     {
<         if (this->hasAUs())
<         {
< #ifdef _TSB_SERIALIZER_DEBUG
<             cout << "Zone " << m_pStruct->id << " has "
<                  << m_pStruct->aulist->size << " AUs" << endl;
< #endif
<             // write number of zone's Aus
<             int numberOfAus = m_pStruct->aulist->size;
<             binaryArchive.write( &numberOfAus, sizeof(numberOfAus) ); // data, size
< 
<             m_pStruct->aulist = (LIST_PTR *)List::reset(m_pStruct->aulist);
<             AU* pAU = 0;
<             do
<             {
<                 pAU = static_cast<AU*>(List::next(m_pStruct->aulist));
<                 if (0 != pAU)
<                 {
<                     Au auObject(pAU, this);
<                     auObject.serialize(binaryArchive);
<                 }
<             } while (0 != pAU);
<         }
<         else // no au's, write a zero
<         {
< #ifdef _TSB_SERIALIZER_DEBUG
<             cout << "Zone " << m_pStruct->id << " has no AU's" << endl;
< #endif
<             // write number of zone's Aus
<             int numberOfAus = 0;
<             binaryArchive.write( &numberOfAus, sizeof(numberOfAus) ); // data, size
<         }
< 
<     }
<     else // we are reading from binary file
<     {
<         vector<Au> auList;
<         if (0 != m_pStruct) // does the ZONE exist?
<         {
<             // read number of zone's Aus
<             int numberOfAus = 0;
<             binaryArchive.read( &numberOfAus, sizeof(numberOfAus) ); // data, size
< #ifdef _TSB_SERIALIZER_DEBUG
<             if (0 < numberOfAus) // does the ZONE have AUs?
<             {
<                 cout << "Zone " << m_pStruct->id << " has "
<                  << numberOfAus << " AUs" << endl;
<             }
<             else
<             {
<                 cout << "Zone " << m_pStruct->id << " has no AU's" << endl;
<             }
< #endif
<             // populate list of AUs
<             for( int i = 0; i < numberOfAus; ++i )
<             {
<                 // insert into list of AUs
<                 Au auObject(0, this);
<                 auObject.serialize(binaryArchive);
<                 auList.push_back(auObject); // shallow copy into list
<                 // new au is added to aulist by init_make_au
<                 //m_pStruct->aulist =
<                 //        static_cast<LIST_PTR*>(List::insert(m_pStruct->aulist, auObject.getStruct(), 0));
<             }
<         }
<         // once all au's are re-created, iterate through & read their regen lists
<         for(int j = 0 ; j < auList.size() ; ++j) // warning signed/unsigned compare
<         {
<             auList[j].lookupRegenListAUs();
<             //iter++;
<         }
<         // destruct C++ block objects (do not delete associated structs)
<         auList.clear();
< 
<     }
< }
< 
< // zone has custody of its AXSDEFs, called after serialization of zone
< void Zone::serializeAxsList(Serializer& binaryArchive)
< {
<     if (binaryArchive.isWriting())
<     {
<         if (this->hasAXSDEFs())
<         {
< #ifdef _TSB_SERIALIZER_DEBUG
<             cout << "Zone " << m_pStruct->id
<                  << ", label " << m_pStruct->label << " has "
<                  << m_pStruct->axslist->size << " AXSDEFs" << endl;
< #endif
<             // write number of zone's AxsDefs
<             int numberOfAxsDefs = m_pStruct->axslist->size;
<             binaryArchive.write( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
< 
<             m_pStruct->axslist = (LIST_PTR *)List::reset(m_pStruct->axslist);
<             AXSDEF* pAXSDEF = 0;
<             do
<             {
<                 pAXSDEF = static_cast<AXSDEF*>(List::next(m_pStruct->axslist));
<                 if (0 != pAXSDEF)
<                 {
<                     AxsDef axsDefObject(pAXSDEF, this->m_pStruct, 0);
<                     axsDefObject.serialize(binaryArchive);
<                 }
<             } while (0 != pAXSDEF);
<         }
<         else // no axsdefs, write a zero
<         {
<             // write number of zone's numberOfAxsDefs
<             int numberOfAxsDefs = 0;
<             binaryArchive.write( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
<         }
<     }
<     else // we are reading from binary file
<     {
<         if (0 != m_pStruct) // does the ZONE exist?
<         {
<             // read number of zone's AxsDefs
<             int numberOfAxsDefs = 0;
<             binaryArchive.read( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
< 
<             // populate list of AXSDEFs
<             for( int i = 0; i < numberOfAxsDefs; ++i )
<             {
<                 // insert into list of AUs
<                 AxsDef axsDefObject(0, this->m_pStruct, 0);
<                 //axsDefObject.recreateStruct(utype, start, end, percent, sign, age);
<                 axsDefObject.serialize(binaryArchive);
<                 // new axsdef is added to axslist by init_make_axsdef
<                 //m_pStruct->axslist =
<                 //        static_cast<LIST_PTR*>(List::insert(m_pStruct->axslist, axsDefObject.getStruct(), 0));
<             }
< #ifdef _TSB_SERIALIZER_DEBUG
<             if (this->hasAXSDEFs()) // does the ZONE have AXSs?
<             {
<                 cout << "Zone " << m_pStruct->id << " has "
<                  << m_pStruct->axslist->size << " AXSDEFs" << endl;
<             }
< #endif
<         }
<     }
< }
< 
< void Zone::serializeZoneList(Serializer& binaryArchive)
< {
<     FssimApp* pApp = FssimApp::getApp();
<     if (binaryArchive.isWriting())
<     {
<         // write size of m_zoneList
<         int zoneListLength = (pApp->m_zoneList->size - 1); //skip zone zero
<         binaryArchive.write( &zoneListLength, sizeof(zoneListLength) ); // data, size
< #ifdef _TSB_SERIALIZER_DEBUG
<             cout << endl << "Writing " << zoneListLength
<                  << " Zones to binary file..." << endl;
< #endif
<         ZONE* pZone = 0;
<         pApp->m_zoneList = static_cast<LIST_PTR*>(List::reset(pApp->m_zoneList));
<         // skip zone zero
<         pZone = static_cast<ZONE*>(List::next(pApp->m_zoneList));
<         do
<         {
<             pZone = static_cast<ZONE*>(List::next(pApp->m_zoneList));
<             if (0 != pZone)
<             {
<                 Zone newZoneObject( pZone ); // m_zoneList is not a hashed list
<             	newZoneObject.serialize(binaryArchive); // serialize zone
<             }
<         } while (0 != pZone);
<     }
<     else // we are reading from binary file
<     {
<         // read size of m_zoneList
<         int zoneListLength = 0;
<         binaryArchive.read( &zoneListLength, sizeof(zoneListLength) ); // data, size
< #ifdef _TSB_SERIALIZER_DEBUG
<             cout << endl << "Reading " << zoneListLength
<                  << " zones from binary file..." << endl;
< #endif
<         // populate m_zoneList
<         for( int i = 0; i < zoneListLength; ++i )
<         {
<             Zone newZoneObject; // create default Zone object
<             try
<             {
<                 Zone::m_objectMemoryAddresses.push_back(&newZoneObject);
<                 newZoneObject.serialize(binaryArchive);
< #ifdef _TSB_SERIALIZER_DEBUG
<                 cout << "finished serializing zone: " << newZoneObject.m_pStruct->id << endl << endl;
< #endif
<                 // new zone is added to m_zoneList by init_make_zone
<                 /*pApp->m_zoneList =
<                        static_cast<LIST_PTR*>( List::insert(pApp->m_zoneList,
<                                                             newZoneObject.getStruct(), 0) );*/
<             }
<             catch(FileException& e)
<             {
< #ifdef _TSB_SERIALIZER_DEBUG
<                 cout << "File exception: " << e.what() << endl;
< #endif
<                 if (e.isEndOfFile())
<                 {
<                     //endOfFile = true;
<                     break;
<                 }
<                 else
<                 {
<                     throw e;
<                 }
<             }
<             catch(exception& e)
<             {
<                 cout << "Binary read exception: " << e.what() << endl;
<                 throw e;
<             }
<         }
< #ifdef _TSB_SERIALIZER_DEBUG
<         if (0 != pApp->m_zoneList) // does the Zone list have zones?
<         {
<             cout << "m_zoneList " << "has "
<              << zoneListLength << " Zones" << endl;
<         }
< #endif
<     }
< }
< 
< 
---
> // Zone.cpp: implementation of the Zone class.
> 
> #include <iostream>
> #include <string>
> #include <cstring> // strcpy
> 
> #include "Zone.h"
> 
> #include "Au.h"
> #include "AxsDef.h"
> 
> #include "FileException.h"
> 
> #include "init.h"
> 
> using namespace std;
> using namespace tsb; // timber supply branch namespace
> 
> 
> // write contents to binary file
> void Zone::serialize( Serializer& binaryArchive ) //throw (FileException)
> {
>     if (binaryArchive.isWriting())
>     {
>         // write zone index
>         binaryArchive.write( &m_pStruct->id, sizeof(m_pStruct->id) ); // data, size
> 
>         // write zone label length & text
>         size_t labelLength = 0;
>         labelLength = strlen( m_pStruct->label ) + 1; // write with terminating null
>         binaryArchive.write( &labelLength, sizeof(labelLength) ); // data, size
>         binaryArchive.write( &(m_pStruct->label), labelLength ); // data, size
> 
>         // target and reportFlags are not serialized (always read from file)
> 
>         // zone achieved struct has default values and is
>         // therefore not serialized, but must be re-allocated on read
>     }
>     else // reading archive
>     {
>         // read zone index
>         int id = 0;
>         binaryArchive.read( &id, sizeof(id) ); // data, size
> 
>         // read zone label length & text
>         size_t labelLength = 0; // already includes terminating null
>         binaryArchive.read( &labelLength, sizeof(labelLength) ); // data, size
>         char* label = new char[labelLength];
>         binaryArchive.read( label, labelLength ); // data, size
> 
>         // re-create & insert the C-struct into the hashed FssimApp::m_zoneList
>         this->recreateStruct(label, id);
> 
>         //m_labelIndex[m_pStruct->label] = m_pStruct->id; // create & insert key-value pair
>         delete[] label;
>         label = 0;
> 
>         // target and reportFlags are not serialized (always read from file)
> 
>         // zone achieved struct has default values and is
>         // therefore not serialized, but must be re-allocated on read
>     }
>     // priority is not serialized (always read from file)
> 
>     this->serializeAuList(binaryArchive);
>     this->serializeAxsList(binaryArchive);
> }
> 
> void Zone::recreateStruct(char* label, int id)
> {
>     ZoneStruct* pZone = init_make_zone(label, id);
>     if (0 == pZone)
>     {
>         string message = "ERROR: init_make_zone() failed in Zone::serialize.\n";
>         throw (message);
>     }
>     else
>     {
>         m_pStruct = pZone;
>     }
> }
> 
> // zone has custody of its AUs, called after serialization of zone
> void Zone::serializeAuList(Serializer& binaryArchive)
> {
>     if (binaryArchive.isWriting())
>     {
>         if (this->hasAUs())
>         {
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>             cout << "Zone " << m_pStruct->id << " has "
>                  << m_pStruct->aulist.size() << " AUs" << endl;
> #endif
> 
>             // write number of zone's Aus
>             int numberOfAus = m_pStruct->aulist.size();
>             binaryArchive.write( &numberOfAus, sizeof(numberOfAus) ); // data, size
> 
>             typedef vector<AUStruct*>::iterator AI;
>             for (AI auIter = m_pStruct->aulist.begin(); auIter != m_pStruct->aulist.end(); ++auIter)
>             {
>                 if (0 != (*auIter))
>                 {
>                     Au auObject(this, (*auIter));
>                     auObject.serialize(binaryArchive);
>                 }
> 
>             }
>         }
>         else // no au's, write a zero
>         {
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>             cout << "Zone " << m_pStruct->id << " has no AUStruct's" << endl;
> #endif
> 
>             // write number of zone's Aus
>             int numberOfAus = 0;
>             binaryArchive.write( &numberOfAus, sizeof(numberOfAus) ); // data, size
>         }
> 
>     }
>     else // we are reading from binary file
>     {
>         if (0 != m_pStruct) // does the ZoneStruct exist?
>         {
>             // read number of zone's Aus
>             int numberOfAus = 0;
>             binaryArchive.read( &numberOfAus, sizeof(numberOfAus) ); // data, size
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>             if (0 < numberOfAus) // does the ZoneStruct have AUs?
>             {
>                 cout << "Zone " << m_pStruct->id << " has "
>                  << numberOfAus << " AUs" << endl;
>             }
>             else
>             {
>                 cout << "Zone " << m_pStruct->id << " has no AUStruct's" << endl;
>             }
> #endif
> 
>             // populate list of AUs
>             for( int i = 0; i < numberOfAus; ++i )
>             {
>                 // insert into list of AUs
>                 Au auObject(this, 0);
>                 auObject.serialize(binaryArchive);
> 
>                 // new au is added to aulist by init_make_au
>             }
>         }
>     }
> }
> 
> // zone has custody of its AXSDEFs, called after serialization of zone
> void Zone::serializeAxsList(Serializer& binaryArchive)
> {
>     if (binaryArchive.isWriting())
>     {
>         if (this->hasAXSDEFs())
>         {
> 
> #ifdef _TSB_AXSLIST_DEBUG
>             cout << "Zone " << m_pStruct->id
>                  << ", label " << m_pStruct->label << " has "
>                  << m_pStruct->axslist.size() << " AXSDEFs" << endl;
> #endif
> 
>             // write number of zone's AxsDefs
>             int numberOfAxsDefs = m_pStruct->axslist.size();
>             binaryArchive.write( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
> 
>             vector<AxsDefStruct*>::iterator axsIter = m_pStruct->axslist.begin();
>             while (axsIter != m_pStruct->axslist.end())
>             {
>                 if (0 != *axsIter)
>                 {
>                     AxsDef axsDefObject(*axsIter, this->m_pStruct);
>                     axsDefObject.serialize(binaryArchive);
>                 }
>                 axsIter++;
>             }
>         }
>         else // no axsdefs, write a zero
>         {
>             // write number of zone's numberOfAxsDefs
>             int numberOfAxsDefs = 0;
>             binaryArchive.write( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
>         }
>     }
>     else // we are reading from binary file
>     {
>         if (0 != m_pStruct) // does the ZoneStruct exist?
>         {
>             // read number of zone's AxsDefs
>             int numberOfAxsDefs = 0;
>             binaryArchive.read( &numberOfAxsDefs, sizeof(numberOfAxsDefs) ); // data, size
> 
>             // populate list of AXSDEFs
>             for( int i = 0; i < numberOfAxsDefs; ++i )
>             {
>                 // insert into list of AUs
>                 AxsDef axsDefObject(0, this->m_pStruct);
>                 //axsDefObject.recreateStruct(utype, start, end, percent, sign, age);
>                 axsDefObject.serialize(binaryArchive);
>                 // new axsdef is added to axslist by init_make_axsdef
>             }
> 
> #ifdef _TSB_AXSLIST_DEBUG
>             if (this->hasAXSDEFs()) // does the ZoneStruct have AXSs?
>             {
>                 cout << "Zone " << m_pStruct->id << " has "
>                  << m_pStruct->axslist.size() << " AXSDEFs" << endl;
>             }
> #endif
>         }
>     }
> }
> 
> void Zone::serializeZoneList(Serializer& binaryArchive)
> {
>     FssimApp* pApp = FssimApp::getApp();
>     if (binaryArchive.isWriting())
>     {
>         // write size of m_zoneList
>         int zoneListLength = (pApp->m_zoneList.size() - 1); //skip zone zero
>         binaryArchive.write( &zoneListLength, sizeof(zoneListLength) ); // data, size
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>             cout << endl << "Writing " << zoneListLength << " Zones to binary file..." << endl;
> #endif
> 
>         typedef vector<ZoneStruct*>::iterator ZI;
>         ZI zoneIter = pApp->m_zoneList.begin();
>         ++zoneIter; // skip zone zero
>         for ( /* zoneIter already initialized */; zoneIter != pApp->m_zoneList.end(); ++zoneIter)
>         {
>             if (0 != *zoneIter)
>             {
>                 Zone newZoneObject( *zoneIter ); // m_zoneList is not a hashed list
>             	newZoneObject.serialize(binaryArchive); // serialize zone
>             }
> 
>         }
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>         if ( !pApp->m_zoneList.empty() ) // does the Zone list have zones?
>         {
>             cout << "m_zoneList has " << zoneListLength << " Zones" << endl;
>         }
> #endif
> 
>     }
>     else // we are reading from binary file
>     {
>         // read size of m_zoneList
>         int zoneListLength = 0;
>         binaryArchive.read( &zoneListLength, sizeof(zoneListLength) ); // data, size
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>             cout << endl << "Reading " << zoneListLength << " zones from binary file..." << endl;
> #endif
> 
>         // populate m_zoneList
>         for( int i = 0; i < zoneListLength; ++i )
>         {
>             Zone newZoneObject; // create default Zone object
>             try
>             {
>                 newZoneObject.serialize(binaryArchive);
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>                 cout << "finished serializing zone: " << newZoneObject.m_pStruct->id << endl << endl;
> #endif
> 
>                 // new zone is added to m_zoneList by init_make_zone
>             }
>             catch(FileException& e)
>             {
> 
> #ifdef _TSB_DEBUG
>                 cerr << "File exception: " << e.what() << endl;
> #endif
> 
>                 if (e.isEndOfFile())
>                 {
>                     //endOfFile = true;
>                     break;
>                 }
>                 else
>                 {
>                     throw e;
>                 }
>             }
>             catch(exception& e)
>             {
>                 cerr << "Binary read exception: " << e.what() << endl;
>                 throw e;
>             }
>         }
> 
> #ifdef _TSB_SERIALIZER_DEBUG
>         if ( !pApp->m_zoneList.empty() ) // does the Zone list have zones?
>         {
>             cout << "m_zoneList has " << zoneListLength << " Zones" << endl;
>         }
> #endif
> 
>     }
> }
> 
> 
