Advertisement
x89codered89x

identification.cxx

Dec 9th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. //PUBLIC - Constructor
  2. Identification::Identification( const std::string typeName_ = std::string("Identification"), const bool active_ = true):
  3. _typeNameID(this->pullTypeNameID(typeName_)),
  4. _instanceID(this->pullInstanceID()),
  5. _active(active_),
  6. _reference(this)
  7. {};
  8.  
  9. //Destructor
  10. Identification::~Identification(){
  11.     this->pushInstanceID();
  12. };
  13.  
  14. //Const Access
  15. const bool& Identification::active() const {
  16.     return _active;
  17. }
  18. const void* const Identification::reference() const {
  19.     return _reference;
  20. };
  21. const std::string& Identification::typeName() const {
  22.     return _typeNames[_typeNameID];
  23. };
  24. const unsigned int& Identification::instanceID() const {
  25.     return _instanceID;
  26. };
  27. const unsigned int& Identification::typeNameID() const {
  28.     return _typeNameID;
  29. }
  30.  
  31. //Debug and Metrics
  32. std::string Identification::display() {//not fleshed
  33.     std::stringstream out( std::stringstream::in | std::stringstream::out | std::stringstream::trunc );
  34.     out.seekp(0, std::ios::beg);
  35.     out << "Identification :\n\n";
  36.     out << "::_typeNames.size() : " << _typeNames.size() << "\n";
  37.     for (unsigned int k=0; k < _typeNames.size(); k++){
  38.         out << "::_typeNames[ " << k << ", " <<_typeNames[k] << " ] : " << _typeNames[k] <<" ("<<Identification::instancesCount()[k]<<")\n";
  39.     }
  40.  
  41.     out << "\n::_nextInstanceIDs.size() : " << _nextInstanceIDs.size() << "\n";
  42.     for (unsigned int k=0; k < _nextInstanceIDs.size(); k++){
  43.         out << "::_nextInstanceIDs[ " << k <<", " <<_typeNames[k] << " ] : " << _nextInstanceIDs[k] << " \n";
  44.     };
  45.  
  46.     out << "\n::_freeInstanceIDs.size() : " << _freeInstanceIDs.size() << "\n";
  47.     for (unsigned int j=0; j <_freeInstanceIDs.size(); j++){
  48.         out << "::_freeInstanceIDs[ " << j<< " ].size() : " << _freeInstanceIDs[j].size() << "\n";
  49.         if (!_freeInstanceIDs[j].empty()){
  50.             std::queue<unsigned int> _freeInstanceIDsTemp= _freeInstanceIDs[j];
  51.             out << "\t::_freeInstanceIDs[ " << j << " ][ " << 0 << ":"<<(_freeInstanceIDsTemp.size()-1)<<" ] : " ;
  52.             while (!_freeInstanceIDsTemp.empty()){
  53.                 out << _freeInstanceIDsTemp.front() << " ";
  54.                 _freeInstanceIDsTemp.pop();
  55.             }
  56.             out << "\n";
  57.         }
  58.     }
  59.     unsigned int length = out.tellp();
  60.     out.seekg(0,std::ios::beg);
  61.     char* buffer = new char[length];
  62.     out.read(buffer,length);
  63.     std::string _buffer(buffer);
  64.     delete [ ] buffer;
  65.     _buffer += "\n\0";
  66.     return _buffer;
  67. };
  68. unsigned int Identification::typeNamesCount() {
  69.     return _typeNames.size();
  70. }
  71. std::vector<unsigned int> Identification::freeIDsCount(){
  72.     std::vector<unsigned int> freeIDsCount_;
  73.     for (unsigned int k=0; k <Identification::typeNamesCount(); k++){
  74.         freeIDsCount_.push_back(_freeInstanceIDs[k].size());
  75.     }
  76.     return freeIDsCount_;
  77. }
  78. std::vector<unsigned int> Identification::instancesCount(){
  79.     std::vector<unsigned int> instancesCount_;
  80.     for (unsigned int k=0; k < Identification::typeNamesCount(); k++){
  81.         instancesCount_.push_back( Identification::nextInstanceIDs()[k] - Identification::freeIDsCount()[k]);
  82.     }
  83.     return instancesCount_;
  84. }
  85. int Identification::findTypeNameID(const std::string& typeName_){
  86.     for(unsigned int k=0; k < _typeNames.size(); k++) {
  87.         if (_typeNames[k] == typeName_) {
  88.             return k;
  89.         };
  90.     };
  91.     return -1;
  92. }
  93.  
  94. //Use
  95. void* const Identification::reference() {
  96.     return _reference;
  97. };
  98.  
  99. //PROTECTED - Activation
  100. void Identification::activate(){
  101.     _active = true;
  102.     _instanceID = pullInstanceID();
  103. };
  104. void Identification::deactivate(){
  105.     _active = false;
  106.     this->pushInstanceID();
  107. };
  108.  
  109. //Mod Stat Access
  110. std::vector<std::string>& Identification::typeNames() {
  111.     return _typeNames;
  112. }
  113. std::vector<unsigned int>& Identification::nextInstanceIDs() {
  114.     return _nextInstanceIDs;
  115. }
  116. std::vector<std::queue<unsigned int> >& Identification::freeInstanceIDs() {
  117.     return _freeInstanceIDs;
  118. }
  119.  
  120. //PRIVATE - Internal Management
  121. void Identification::pushInstanceID() {
  122.     _freeInstanceIDs[_typeNameID].push(_instanceID);
  123. };
  124.  
  125. const unsigned int Identification::pullInstanceID() {
  126.     if (_freeInstanceIDs[_typeNameID].empty() ) {
  127.         return _nextInstanceIDs[_typeNameID]++;
  128.     };
  129.     unsigned int freeInstanceID_ = _freeInstanceIDs[_typeNameID].front();
  130.     _freeInstanceIDs[_typeNameID].pop();
  131.     return freeInstanceID_;
  132. };
  133.  
  134. const unsigned int Identification::pullTypeNameID(const std::string& typeName_) {
  135.     for (unsigned int k=0; k< _typeNames.size(); k++){
  136.         if (_typeNames[k] == typeName_){
  137.             return k;
  138.         }
  139.     }
  140.     _typeNames.push_back(typeName_);
  141.     _nextInstanceIDs.push_back(0);
  142.     _freeInstanceIDs.push_back(std::queue<unsigned int>());
  143.     return (_typeNames.size()-(unsigned int)(1));
  144. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement