vtkObjectIdMap.cxx 4.27 KB
Newer Older
Zach's avatar
Zach committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkObjectIdMap.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkObjectIdMap.h"

#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkWeakPointer.h"

#include <map>
#include <string>
#include <set>

namespace
{
struct ObjectId
{
  ObjectId(vtkTypeUInt32 id)
  {
    this->GlobalId = id;
    this->Object = NULL;
  }

  ObjectId(vtkObject* obj, vtkTypeUInt32 id = 0)
  {
    this->GlobalId = id;
    this->Object = obj;
  }

41 42 43 44 45 46
  ObjectId(const ObjectId& other)
  {
    this->GlobalId = other.GlobalId;
    this->Object = other.Object;
  }

Zach's avatar
Zach committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  ObjectId& operator=(const ObjectId &other)
  {
    if (this == &other)
      {
      // Same object?
      return *this;
      }

    this->GlobalId = other.GlobalId;
    this->Object = other.Object;
    return *this;
  }

   bool operator<(const ObjectId &other) const
   {
     if (this == &other)
       {
       // Same object?
       return false;
       }

     if( this->GlobalId != 0 && other.GlobalId != 0
         && this->GlobalId != other.GlobalId)
       {
       return (this->GlobalId < other.GlobalId);
       }

     if( this->Object.GetPointer() != NULL && other.Object.GetPointer() != NULL
         && this->Object.GetPointer() != other.Object.GetPointer())
       {
       return (this->Object.GetPointer() < other.Object.GetPointer());
       }

     return false;
   }

  vtkSmartPointer<vtkObject> Object;
  vtkTypeUInt32 GlobalId;
};
}

struct vtkObjectIdMap::vtkInternals
{
  std::set<ObjectId> RegisteredObjects;
  std::map<std::string, vtkWeakPointer<vtkObject> > ActiveObjects;
  vtkTypeUInt32 NextAvailableId;

  vtkInternals() : NextAvailableId(1)
  {
  }

};

vtkStandardNewMacro(vtkObjectIdMap);
//----------------------------------------------------------------------------
vtkObjectIdMap::vtkObjectIdMap() :
  Internals(new vtkInternals())
{
}

//----------------------------------------------------------------------------
vtkObjectIdMap::~vtkObjectIdMap()
{
  delete this->Internals;
  this->Internals = 0;
}

//----------------------------------------------------------------------------
void vtkObjectIdMap::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
vtkTypeUInt32 vtkObjectIdMap::GetGlobalId(vtkObject* obj)
{
  if(obj == NULL)
    {
    return 0;
    }

  ObjectId key(obj, 0);
  std::set<ObjectId>::iterator iter = this->Internals->RegisteredObjects.find(key);
  if(iter == this->Internals->RegisteredObjects.end())
    {
    key.GlobalId = this->Internals->NextAvailableId++;
    this->Internals->RegisteredObjects.insert(key);
    return key.GlobalId;
    }
  return iter->GlobalId;
}

//----------------------------------------------------------------------------
140
vtkObject* vtkObjectIdMap::GetVTKObject(vtkTypeUInt32 globalId)
Zach's avatar
Zach committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
{
  ObjectId key(globalId);
  std::set<ObjectId>::iterator iter = this->Internals->RegisteredObjects.find(key);
  if(iter == this->Internals->RegisteredObjects.end())
    {
    return NULL;
    }
  return iter->Object.GetPointer();
}

//----------------------------------------------------------------------------
vtkTypeUInt32 vtkObjectIdMap::SetActiveObject(const char* objectType, vtkObject* obj)
{
  if(objectType)
    {
    this->Internals->ActiveObjects[objectType] = obj;
    return this->GetGlobalId(obj);
    }
  return 0;
}

//----------------------------------------------------------------------------
vtkObject* vtkObjectIdMap::GetActiveObject(const char* objectType)
{
  if(objectType)
    {
    return this->Internals->ActiveObjects[objectType].GetPointer();
    }
  return NULL;
}

//----------------------------------------------------------------------------
void vtkObjectIdMap::FreeObject(vtkObject* obj)
{
  this->Internals->RegisteredObjects.erase(obj);
}