/////////////////////////////////////////////////////////////////////////////
// object.cc
//
// SIMLIB version: 2.18
// Date: 2004-01-25
//
// Copyright (c) 1991-2004 Petr Peringer 
//
// This library is licensed under GNU Library GPL. See the file COPYING.
//

//
// this module contains implementation of base class SimObject
//

#include "simlib.h"
#include "internal.h"

////////////////////////////////////////////////////////////////////////////
SIMLIB_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////

// static flag for IsAllocated()
static bool SimObject_allocated = false;

////////////////////////////////////////////////////////////////////////////
// operator new --- allocate memory for object
//
void *SimObject::operator new(unsigned size) {
//  dprintf(("SimObject::operator new(%u) = %p ", size, ptr));
//  try {
  void *ptr = ::new char[size]; // global operator new
//  }catch(...) { SIMLIB_error(MemoryError); }
  if(!ptr) // we expect old behavior of new
      SIMLIB_error(MemoryError);
  SimObject_allocated = true; // update flag
  return ptr;
}

////////////////////////////////////////////////////////////////////////////
//  delete --- deallocate memory
//
void SimObject::operator delete(void *ptr) {
//  dprintf(("SimObject::operator delete(%p) ", ptr));
  if ((static_cast<SimObject*>(ptr))->IsAllocated()) 
    ::operator delete(ptr);  // free memory
}


////////////////////////////////////////////////////////////////////////////
// constructor --- initialize name
//
SimObject::SimObject() :
  _name(0),
  _flags(0) 
{
//  dprintf(("SimObject::SimObject() this=%p ", this));
  if(SimObject_allocated) {
    SimObject_allocated = false;
    _flags |= _ALLOCATED;
  }
}

////////////////////////////////////////////////////////////////////////////
// destructor --- cleanup, virtual
//
SimObject::~SimObject()
{
//  dprintf(("SimObject::~SimObject() this=%p ", this));
}

////////////////////////////////////////////////////////////////////////////
//  SimObject::SetName
//
void SimObject::SetName(const char *name)
{
  _name = name;
}


////////////////////////////////////////////////////////////////////////////
//  SimObject::Name --- get the string name of object
//
const char *SimObject::Name() 
{
  if(_name==0) return ""; // no name
  return _name;
}


////////////////////////////////////////////////////////////////////////////
//  SimObject::Output() --- print object info
//
void SimObject::Output() 
{
  Print("SimObject: this=%p, name=%s\n", this, Name());  // default
}


// end

