
/*

  KLayout Layout Viewer
  Copyright (C) 2006-2026 Matthias Koefferlein

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


#ifndef HDR_dbLibraryProxy
#define HDR_dbLibraryProxy

#include "dbCommon.h"

#include "dbTypes.h"
#include "dbCell.h"

namespace db
{

/**
 *  @brief A cell specialization: a proxy for a library cell.
 *
 *  This cell serves as a proxy (or copy) of a cell contained in another library.
 *  This cell thus is a reference and a cached copy. 
 *  The library reference is through the library id and the cell index inside this
 *  library.
 */
class DB_PUBLIC LibraryProxy 
  : public Cell
{
public:
  /** 
   *  @brief The constructor
   *
   *  The constructor gets the parameters that are unique for this variant.
   */
  LibraryProxy (db::cell_index_type ci, db::Layout &layout, lib_id_type lib_id, cell_index_type cell_index);

  /**
   *  @brief The destructor
   */
  ~LibraryProxy ();

  /**
   *  @brief Cloning 
   */
  virtual Cell *clone (Layout &layout) const;

  /**
   *  @brief Get the library id 
   */
  lib_id_type lib_id () const
  {
    return m_lib_id;
  }

  /**
   *  @brief Get the cell index inside the library
   */
  cell_index_type library_cell_index () const
  {
    return m_library_cell_index;
  }

  /**
   *  @brief Update the layout
   */
  virtual void update (ImportLayerMapping *layer_mapping = 0);

  /**
   *  @brief Tell, if this cell is a proxy cell
   *
   *  Proxy cells are such whose layout represents a snapshot of another entity.
   *  Such cells can be PCell variants or library references for example.
   */
  virtual bool is_proxy () const 
  { 
    return true; 
  }

  /**
   *  @brief Gets a value indicating that this cell is a replica that can be skipped
   *
   *  This attribute is evaluated by file writers to skip cell replicas for
   *  library cells that do not want to replicated.
   */
  virtual bool can_skip_replica () const;

  /**
   *  @brief Gets the basic name
   *
   *  This returns the basic name of the proxy target
   */
  virtual std::string get_basic_name () const;

  /**
   *  @brief Gets the variant name
   *
   *  This returns the basic name of the proxy target
   */
  virtual std::string get_variant_name () const;

  /**
   *  @brief Gets the display name
   *
   *  This returns the basic name of the proxy target
   */
  virtual std::string get_display_name () const;

  /**
   *  @brief Gets the qualified name
   *
   *  Gets a combination of the library name and the target cell's qualified name
   */
  virtual std::string get_qualified_name () const;

  /**
   *  @brief Resets the binding of this proxy
   */
  void remap (lib_id_type lib_id, cell_index_type cell_index);

  /**
   *  @brief Reimplemented from Cell: unregisters the proxy at the layout
   */
  virtual void unregister ();

  /**
   *  @brief Reimplemented from Cell: reregisters the proxy at the layout
   */
  virtual void reregister ();

private:
  lib_id_type m_lib_id;
  cell_index_type m_library_cell_index;

  std::vector<int> get_layer_indices (db::Layout &layout, db::ImportLayerMapping *layer_mapping);
};

}

#endif


