1,358c1,161
< // TimberClass.cpp: implementation of the TimberClass class.
< //
< ////////////////////////////////////
< 
< #include <iostream>
< //#include <fstream>
< #include <string>
< #include <cstring> // strcpy
< #include <list>
< 
< #include "TimberClass.h"
< #include "Serializer.h"
< #include "Block.h"
< 
< #include "init.h"
< #include "func.h"
< 
< using namespace std;
< using namespace tsb; // timber supply branch namespace
< 
< ////////////////////////////////////
< // Construction/Destruction
< ////////////////////////////////////
< 
< // counter for number of C++ timberClass objects
<     int TimberClass::m_timberClassCount = 0;
< //    map<string, int> TimberClass::m_labelIndex = map<string, int>();
< 
< /*
< TimberClass::TimberClass(TIMBER_CLASS* pTimberClassStruct,
<                          ZONE* pParentZone, BLOCK* pParentBlock,
<                          AU* pParentAu, ZONE* pZoneOfParentAu)
<     : m_pStruct(pTimberClassStruct), m_pParentZone(pParentZone),
<       m_pParentAU(pParentAu), m_pZoneOfParentAU(pZoneOfParentAu),
<       m_pParentBLOCK(pParentBlock), m_index(++m_timberClassCount)
< {
<     if (0 != m_pStruct)
<     {
<         // initialize null members from struct
<         if(0 != m_pStruct->parent_zone && 0 == m_pParentZone)
<         {
<             m_pParentZone = m_pStruct->parent_zone;
<         }
<         if(0 != m_pStruct->parent_au && 0 == m_pParentAU)
<         {
<             m_pParentAU = m_pStruct->parent_au;
<         }
<         if(0 != m_pStruct->parent_block && 0 == m_pParentBLOCK)
<         {
<             m_pParentBLOCK = m_pStruct->parent_block;
<         }
<     }
< }
< */
< 
< TimberClass::~TimberClass()
< {
<     // do nothing yet, but may need to clean up m_pStruct
< }
< 
< // write contents to binary file
< void TimberClass::serialize( Serializer& binaryArchive ) //throw (FileException)
< {
<     if (binaryArchive.isWriting())
<     {
<         // write timberClass age
<         binaryArchive.write( &(m_pStruct->age), sizeof(m_pStruct->age) ); // data, size
< 
<         // write timberClass width
<         binaryArchive.write( &(m_pStruct->width), sizeof(m_pStruct->width) ); // data, size
< 
<         // write timberClass class_type
<         binaryArchive.write( &(m_pStruct->class_type), sizeof(m_pStruct->class_type) ); // data, size
<     	if (1 == m_pParentZone->getStruct()->id && 16 == m_pParentAU->id)
< 		{
< 		    printf("\ntimber_class class_type (serialize write): %d", m_pStruct->class_type);
< 		}
< 
<         // write timberClass reversion_age
<         binaryArchive.write( &(m_pStruct->reversion_age), sizeof(m_pStruct->reversion_age) ); // data, size
< 
<         // write timberClass area
<         binaryArchive.write( &(m_pStruct->area), sizeof(m_pStruct->area) ); // data, size
< 
<         // write timberClass MerchArea
<         //binaryArchive.write( &(m_pStruct->MerchArea), sizeof(m_pStruct->MerchArea) ); // data, size
< 
<         // write timberClass parent_zone ID
<         int parentZoneID = m_pParentZone->getStruct()->id;
<         binaryArchive.write( &(parentZoneID), sizeof(parentZoneID) ); // data, size
< 
<         // write timberClass parent_au ID
<         //int parentAuID = m_pParentAU->id;
<         //binaryArchive.write( &(parentAuID), sizeof(parentAuID) ); // data, size
< 
<         // write timberClass parent_block ID
<         int parentBlockID = Block::lookupBlockID(m_pParentBLOCK->label);
<         binaryArchive.write( &(parentBlockID), sizeof(parentBlockID) ); // data, size
< 
<         // write timberClass roaded
<         binaryArchive.write( &(m_pStruct->roaded), sizeof(m_pStruct->roaded) ); // data, size
< 
<         // write timberClass transferred
<         //binaryArchive.write( &(m_pStruct->transferred), sizeof(m_pStruct->transferred) ); // data, size
< 
<         // write timberClass RandValue
<         // pseudo-random, unchanged after binary load unless Randomizer Seed is altered in config.dat
<         binaryArchive.write( &(m_pStruct->RandValue), sizeof(m_pStruct->RandValue) ); // data, size
< 
<         // write timberClass ReEntryLag
<         binaryArchive.write( &(m_pStruct->ReEntryLag), sizeof(m_pStruct->ReEntryLag) ); // data, size
< 
<         // static & float groups are not serialized,
<         // since they are populated within the simulation loop
<         /*
<         if (this->hasStaticGroups()) // populate vectors of AU indices
<         {
<             this->createStaticGroupIndexList();
<         }
<         if (this->hasFloatGroups()) // populate lists of AXS indices
<         {
<             this->createFloatGroupIndexList();
<         }
<         */
<     }
<     else // reading archive
<     {
<         // allocate space to read in values
<         TIMBER_CLASS tempStruct;
<         m_pStruct = &tempStruct;
< 
<         // read timberClass age
<         binaryArchive.read( &(m_pStruct->age), sizeof(m_pStruct->age) ); // data, size
< 
<         // read timberClass width
<         binaryArchive.read( &(m_pStruct->width), sizeof(m_pStruct->width) ); // data, size
< 
<         // read timberClass class_type
<         binaryArchive.read( &(m_pStruct->class_type), sizeof(m_pStruct->class_type) ); // data, size
<     	if (1 == m_pParentZone->getStruct()->id && 16 == m_pParentAU->id)
< 		{
< 		    printf("\ntimber_class class_type (serialize read): %d", m_pStruct->class_type);
< 		}
< 
<         // read timberClass reversion_age
<         binaryArchive.read( &(m_pStruct->reversion_age), sizeof(m_pStruct->reversion_age) ); // data, size
< 
<         // read timberClass area
<         binaryArchive.read( &(m_pStruct->area), sizeof(m_pStruct->area) ); // data, size
< 
<         // read timberClass MerchArea
<         //binaryArchive.read( &(m_pStruct->MerchArea), sizeof(m_pStruct->MerchArea) ); // data, size
< 
<         // read timberClass parent_zone ID
<         int parentZoneID = 0;
<         binaryArchive.read( &(parentZoneID), sizeof(parentZoneID) ); // data, size
<         m_pParentZone = Zone::lookupZoneAddress(parentZoneID); // lookup parent zone
< 
<         // read timberClass parent_au ID
<         //int parentAuID = 0;
<         //binaryArchive.read( &(parentAuID), sizeof(parentAuID) ); // data, size
<         //m_pParentAU = m_pParentZone.lookupAUAddress(parentAuID); // lookup parent au
< 
<         // read timberClass parent_block ID
<         int parentBlockID = 0;
<         binaryArchive.read( &(parentBlockID), sizeof(parentBlockID) ); // data, size
<         char blockLabel[FssimApp::MAX_LABEL_LENGTH];
<         strcpy( blockLabel, (Block::lookupBlockLabel(parentBlockID)).c_str() ); // lookup parent block
<         m_pParentBLOCK = func_find_block(blockLabel);
<         // func_find_block(int) won't work due to blocklist hashing
< 
<         // read timberClass roaded
<         binaryArchive.read( &(m_pStruct->roaded), sizeof(m_pStruct->roaded) ); // data, size
< 
<         // read timberClass transferred
<         //binaryArchive.read( &(m_pStruct->transferred), sizeof(m_pStruct->transferred) ); // data, size
< 
<         // read timberClass RandValue
<         // pseudo-random, unchanged after binary load unless Randomizer Seed is altered in config.dat
<         binaryArchive.read( &(m_pStruct->RandValue), sizeof(m_pStruct->RandValue) ); // data, size
< 
<         // read timberClass ReEntryLag
<         binaryArchive.read( &(m_pStruct->ReEntryLag), sizeof(m_pStruct->ReEntryLag) ); // data, size
< 
<         if (0 != m_pParentZone && 0 != m_pParentAU && 0 != m_pParentBLOCK)
<         {
<         this->recreateStruct(m_pStruct->age, m_pStruct->width, m_pStruct->area, m_pStruct->class_type,
<                              m_pStruct->reversion_age, m_pStruct->ReEntryLag,
<                              0,	/* float_group_list */  0, /* static_group_list */
<                              m_pStruct->roaded, m_pStruct->RandValue);
<         }
<     }
<     //this->serializeIDList(binaryArchive, m_staticGroupIndices);
<     //this->serializeIDList(binaryArchive, m_floatGroupIndices);
< }
< 
< // static & float groups are not serialized,
< // since they are populated within the simulation loop
< /*
< void TimberClass::serializeIDList(Serializer& binaryArchive, list<int>& idList)
< {
<     if (binaryArchive.isWriting())
<     {
<         int numberOfItems = idList.size();
< 
<         // write number of timberClass's AUs
<         binaryArchive.write( &numberOfItems, sizeof(numberOfItems) ); // data, size
< 
<         if (0 != numberOfItems)
<         {
<             // write AUs' timberClass indices
<             int index = 0;
<             list<int>::iterator iter = idList.begin( );
<             while(iter != idList.end( ))
<             {
<             	index = (*iter);
<                 binaryArchive.write( &index, sizeof(index) ); // data, size
<                 ++iter;
<             }
<         }
<     }
<     else // reading archive
<     {
<         // clear list of AUs' indices, in case not already empty
<         idList.clear();
<         // read number of timberClass's AUs
<         int numberOfItems = 0;
<         binaryArchive.read( &numberOfItems, sizeof(numberOfItems) ); // data, size
< #ifdef _TSB_SERIALIZER_DEBUG
<         if (0 != numberOfItems) // does the TIMBER_CLASS have AUs?
<         {
<             cout << "TimberClass " << m_index << " has "
<                  << numberOfItems << " associated items" << endl;
<         }
< #endif // _TSB_SERIALIZER_DEBUG
<         // read timberClass's associated items' indices
<         int currentID = 0;
<         for( int i = 0; i < numberOfItems; ++i )
<         {
<             binaryArchive.read( &currentID, sizeof(currentID) ); // data, size
<             idList.push_back(currentID);
<         }
<     }
< }
< */
< 
< void TimberClass::recreateStruct(int age, int width, double area, int class_type,
<                                  int reversion_age, int ReEntryLag,
<                                  LIST_PTR *float_group_list, LIST_PTR *static_group_list,
<                                  int roaded, int RandValue)
< {
<     /*if (0 != m_pStruct) // clean up memory if overwriting existing struct
<     {
<         init_delete_class(m_pStruct);
<     }*/
<     TIMBER_CLASS* pTimberClass = init_make_class(m_pParentZone->getStruct(), m_pParentAU, m_pParentBLOCK,
<                                                  age, width, area, class_type, reversion_age,
<                                                  ReEntryLag, float_group_list, static_group_list,
<                                                  roaded, RandValue);
<     if (0 == pTimberClass)
<     {
<         string message = "ERROR: init_make_class() failed in TimberClass::serialize.\n";
<         throw (message);
<     }
<     else
<     {
<         m_pStruct = pTimberClass;
<     }
< }
< 
< /*int TimberClass::lookupTimberClassID(const string& label)
< {
<     // lookup AU's index in map by label
<     AConstIndexIterator item = m_labelIndex.find(label);
<     if ( item != m_labelIndex.end() )
<     {
<         return ( (item->second) );
<     }
<     else
<     {
<         throw ("AU not found in labelIndex map");
<     }
< }*/
< 
< // static & float groups are not serialized,
< // since they are populated within the simulation loop
< /* inlined for speed
< void TimberClass::createFloatGroupIndexList() // called before binary write
< {
< #ifdef _TSB_SERIALIZER_DEBUG
<     cout << "TimberClass " << m_index << " has "
<          << m_pStruct->float_group_list->size << " float groups" << endl;
< #endif
<     m_pStruct->float_group_list = (LIST_PTR *)List::reset(m_pStruct->float_group_list);
<     GROUP* pGROUP = 0;
<     do
<     {
<         pGROUP = static_cast<GROUP*>(List::next(m_pStruct->float_group_list));
<         if (0 != pGROUP)
<         {
<             m_floatGroupIndices.push_back(pGROUP->id);
<         }
< 
<     } while (0 != pGROUP);
< }
< */
< 
< /* inlined for speed
< void TimberClass::createStaticGroupIndexList() // called before binary write
< {
< #ifdef _TSB_SERIALIZER_DEBUG
<     cout << "TimberClass " << m_index << " has "
<          << m_pStruct->static_group_list->size << " static groups" << endl;
< #endif
<     m_pStruct->static_group_list = (LIST_PTR *)List::reset(m_pStruct->static_group_list);
<     GROUP* pGROUP = 0;
<     do
<     {
<         pGROUP = static_cast<GROUP*>(List::next(m_pStruct->static_group_list));
<         if (0 != pGROUP)
<         {
<             m_staticGroupIndices.push_back(pGROUP->id);
<         }
< 
<     } while (0 != pGROUP);
< }
< */
< 
< // timberClass should not have custody of its AUs or its AXSDEFs
< /*    void TimberClass::recreateAUStructureList(vector<TimberClass>& timberClassList) // called after binary read
< {
<     if (0 != m_pStruct) // does the TIMBER_CLASS exist?
<     {
<         // populate list of AUing timberClass indices
<         list<int>::iterator iter = m_AUIndices.begin();
<         while(iter != m_AUIndices.end())
<         {
<             // insert into list of AUs
<             TIMBER_CLASS* pAU = timberClassList[(*iter)-1].m_pStruct; // get address of indexed TimberClass's struct
<             // new class is added to classlist by init_make_class
<             //m_pStruct->aulist =
<             //        static_cast<LIST_PTR*>(List::insert(m_pStruct->aulist, pAU, 0));
<             ++iter;
<         }
< #ifdef _TSB_SERIALIZER_DEBUG
<         if (0 != m_pStruct->aulist) // does the TIMBER_CLASS have AUs?
<         {
<             cout << "TimberClass " << m_index << " has "
<              << m_pStruct->aulist->size << " AUs" << endl;
<         }
< #endif
<     }
< }
< */
< 
< 
< 
< 
---
> // TimberClass.cpp: implementation of the TimberClass class.
> //
> ////////////////////////////////////
> 
> #include <iostream>
> //#include <fstream>
> #include <string>
> #include <cstring> // strcpy
> #include <list>
> 
> #include "TimberClass.h"
> 
> //#include "Serializer.h"
> #include "Block.h"
> 
> #include "init.h"
> #include "func.h"
> 
> using namespace std;
> using namespace tsb; // timber supply branch namespace
> 
> ////////////////////////////////////
> // Construction/Destruction
> ////////////////////////////////////
> 
> // counter for number of C++ timberClass objects
>     int TimberClass::m_timberClassCount = 0;
> 
> TimberClass::~TimberClass()
> {
>     // do nothing yet, but may need to clean up m_pStruct
> }
> 
> // write contents to binary file
> void TimberClass::serialize( Serializer& binaryArchive ) //throw (FileException)
> {
>     if (binaryArchive.isWriting())
>     {
>         // write timberClass age
>         binaryArchive.write( &(m_pStruct->age), sizeof(m_pStruct->age) ); // data, size
> #ifdef _TSB_AGE_DEBUG
>         cout << endl << "writing timber_class with age: " << m_pStruct->age << endl;
> #endif
>         // write timberClass width
>         binaryArchive.write( &(m_pStruct->width), sizeof(m_pStruct->width) ); // data, size
> 
>         // write timberClass class_type
>         binaryArchive.write( &(m_pStruct->class_type), sizeof(m_pStruct->class_type) ); // data, size
> 
>         // write timberClass reversion_age
>         binaryArchive.write( &(m_pStruct->reversion_age), sizeof(m_pStruct->reversion_age) ); // data, size
> 
>         // write timberClass area
>         binaryArchive.write( &(m_pStruct->area), sizeof(m_pStruct->area) ); // data, size
> 
>         // parent zone is passed to TimberClass constructor (zone->au has custody of timber classes)
>         // parent au must be passed to TimberClass constructor (au has custody of timber classes)
> 
>         // write timberClass parent_block ID
>         int parentBlockID = m_pParentBlock->id;
>         binaryArchive.write( &(parentBlockID), sizeof(parentBlockID) ); // data, size
> 
>         // write timberClass roaded
>         binaryArchive.write( &(m_pStruct->roaded), sizeof(m_pStruct->roaded) ); // data, size
> 
>         // write timberClass RandValue
>         // pseudo-random, unchanged after binary load unless Randomizer Seed is altered in config.dat
>         binaryArchive.write( &(m_pStruct->RandValue), sizeof(m_pStruct->RandValue) ); // data, size
> 
>         // write timberClass ReEntryLag
>         binaryArchive.write( &(m_pStruct->ReEntryLag), sizeof(m_pStruct->ReEntryLag) ); // data, size
> 
>         // static & float groups are not serialized,
>         // since they are populated within the simulation loop
>     }
>     else // reading archive
>     {
>         // read timberClass age
>         int age = 0;
>         binaryArchive.read( &(age), sizeof(age) ); // data, size
> #ifdef _TSB_AGE_DEBUG
>         cout << endl << "reading timber_class with age: " << age << endl;
> #endif
> 
>         // read timberClass width
>         int width = 0;
>         binaryArchive.read( &(width), sizeof(width) ); // data, size
> 
>         // read timberClass class_type
>         int classType = 0;
>         binaryArchive.read( &(classType), sizeof(classType) ); // data, size
> 
>         // read timberClass reversion_age
>         int reversionAge = 0;
>         binaryArchive.read( &(reversionAge), sizeof(reversionAge) ); // data, size
> 
>         // read timberClass area
>         double area = 0;
>         binaryArchive.read( &(area), sizeof(area) ); // data, size
> 
>         // parent zone is passed to TimberClass constructor (zone->au has custody of timber classes)
>         // parent au must be passed to TimberClass constructor (au has custody of timber classes)
> 
> #ifdef _TSB_TIMBER_CLASS_PARENT_DEBUG
>         cout << "serializing timber class with parent id: " << m_pParentAU->id << endl;
> #endif
> 
>         // read timberClass parent_block ID
>         int parentBlockID = 0;
>         binaryArchive.read( &(parentBlockID), sizeof(parentBlockID) ); // data, size
>         m_pParentBlock = BlockStruct::find(parentBlockID); // lookup parent block
> 
>         // read timberClass roaded
>         bool roaded = false;
>         binaryArchive.read( &(roaded), sizeof(roaded) ); // data, size
> 
>         // read timberClass RandValue
>         // pseudo-random, unchanged after binary load unless Randomizer Seed is altered in config.dat
>         int randomValue = 0;
>         binaryArchive.read( &(randomValue), sizeof(randomValue) ); // data, size
> 
>         // read timberClass ReEntryLag
>         int reEntryLag = 0;
>         binaryArchive.read( &(reEntryLag), sizeof(reEntryLag) ); // data, size
> 
> 
>         this->recreateStruct(age, width, area, classType, reversionAge, reEntryLag, roaded, randomValue);
>     }
> }
> 
> void TimberClass::recreateStruct(const int age, const int width, const double area, const int classType,
>                                  const int reversionAge, const int reEntryLag,
>                                  const bool roaded, const int randomValue)
> {
>     if (0 == m_pParentZone)
>     {
>         throw InvalidDataException(0, "Cannot recreate struct with null parent zone");
>     }
>     if (0 == m_pParentAU)
>     {
>         throw InvalidDataException(0, "Cannot recreate struct with null parent au");
>     }
>     if(0 == m_pParentBlock)
>     {
>         throw InvalidDataException(0, "Cannot recreate struct with null parent block");
>     }
> 
>     TimberClassStruct* pTimberClass = init_make_class(m_pParentZone->getStruct(), m_pParentAU, m_pParentBlock,
>                                                  age, width, area, classType, reversionAge,
>                                                  reEntryLag, 0,	/* pFloatGroupList */  0, /* pStaticGroupList */
>                                                  roaded, randomValue);
>     if (0 == pTimberClass)
>     {
>         string message = "ERROR: init_make_class() failed in TimberClass::serialize.\n";
>         throw (message);
>     }
>     else
>     {
>         m_pStruct = pTimberClass;
>     }
> }
