/// TKS_CachedObject.cpp
///
/// (c) 1999-2009 by Bastian Spiegel <bs@tkscript.de>
///     - published under terms of the GNU general public license (GPL).
///
/// created: xxxx1999
///

#include "tks.h"
#include "TKS_CachedObject.h"

#include "Pool.h"
#include "ObjectPool.h"

#ifdef TKS_CO_OBJECTCOUNTER
sSI TKS_CachedObject::object_counter = 0;
sSI TKS_CachedObject::refname_counter = 0;
sSI TKS_CachedObject::copyname_counter = 0;
#endif // TKS_CO_OBJECTCOUNTER


TKS_CachedObject::TKS_CachedObject(void) {
#ifdef TKS_CO_OBJECTCOUNTER
   TKS_CachedObject::object_counter++;
#endif // TKS_CO_OBJECTCOUNTER

   flags       = 0;
   type        = 0;
   value.any   = 0;
   deleteme    = 0;
   local_index = 0;
   //name        = NULL;
   b_own_name  = 0;
}

TKS_CachedObject::~TKS_CachedObject() {
#ifdef TKS_CO_OBJECTCOUNTER
   TKS_CachedObject::object_counter--;
#endif // TKS_CO_OBJECTCOUNTER

   unset();
   if(b_own_name)
   {
      //YAC_DELETE(name);
#ifdef TKS_CO_OBJECTCOUNTER
      TKS_CachedObject::copyname_counter--;
#endif // TKS_CO_OBJECTCOUNTER
   }
   else
   {
#ifdef TKS_CO_OBJECTCOUNTER
      TKS_CachedObject::refname_counter--;
#endif // TKS_CO_OBJECTCOUNTER
   }
   //name = NULL;
}

void TKS_CachedObject::initScriptValCO(YAC_Value *_v) {
   copySafe(_v);
   flags = 0;
}

void TKS_CachedObject::setName(YAC_String *_name, sBool _bCopyName) {
   if(_bCopyName)
   {
      /*if(ObjectPool::b_initialized)
      {
         name = (YAC_String*) YAC_NEW_CORE_POOLED(YAC_CLID_STRING);
      }
      else
      {
         name = YAC_New_String();
      }
      name->yacCopy(_name);*/
      name.yacCopy(_name);
      name.getKey();
      b_own_name = 1;
#ifdef TKS_CO_OBJECTCOUNTER
      TKS_CachedObject::copyname_counter++;
#endif // TKS_CO_OBJECTCOUNTER
   }
   else
   {
      //name = _name;
      name.visit(_name);
      name.getKey();
#ifdef TKS_CO_OBJECTCOUNTER
      TKS_CachedObject::refname_counter++;
#endif // TKS_CO_OBJECTCOUNTER
   }
}
