From GridLAB-D Wiki
Jump to: navigation, search

Creating a class - Procedure to create a new class in a GridLAB-D module

Synopsis

module-name/class-name.h
module-name/class-name.cpp
module-name/Makefile.am
module-name/module-name.vcproj

Header file

module-name/class-name.h
// $Id$
// Copyright (c) <<<YEAR>>> <<<COMPANY>>>
// Template valid as of Hassayampa (Version 3.0)
#ifndef _<<<CLASS>>>_H
#define _<<<CLASS>>>_H
#include "gridlabd.h"
class <<<CLASS>>> : public gld_object {
public: // published variables
  // TODO add public typedefs
  // TODO declare published variables using GL_* macros
private: // unpublished variables
  // TODO add private typedefs
  // TODO add unpublished variables
public: // required functions
  <<<CLASS>>>(MODULE *module);
  int create(void);
  int init(OBJECT *parent);
  // TODO add optional class functions
public: // optional/user-defined functions
  // TODO add published class functions
private: // internal functions
  // TODO add desired internal functions
public: // required members
  static CLASS *oclass;
  static <<<CLASS>>> *defaults;
};
#endif // _<<<CLASS>>>_H

Source file

module-name/class-name.cpp
// $Id$
// Copyright (c) <<<YEAR>>> <<<COMPANY>>>
// Template valid as of Hassayampa (Version 3.0)
#include "<<<CLASS>>>.h"
EXPORT_CREATE(<<<CLASS>>>);
EXPORT_INIT(<<<CLASS>>>);
// TODO add optional functions declarations
CLASS <<<CLASS>>>::oclass = NULL;
<<<CLASS>>> *<<<CLASS>>>::defaults = NULL;
// TODO add declaration of class globals
<<<CLASS>>>::<<<CLASS>>>(MODULE *module)
{
  if ( oclass!=NULL )
    exception("cannot register class more than once");
  oclass = gld_class::create(module,"<<<CLASS>>>",sizeof(<<<CLASS>>>),<<<OPTIONS>>>);
  if ( oclass==NULL )
    exception("class registration failed");
  oclass->trl = <<<TRL>>>;
  if ( gl_publish_variable(oclass, <<<VARIABLESPECS>>>, NULL)<1 )
    exception("unable to publish properties");
  memset(defaults=this,0,sizeof(*this));
  // TODO set defaults
}
<<<CLASS>>>::create(void)
{
  memcpy(this,defaults,sizeof(*this));
  // TODO set defaults
  return SUCCESS; // return FAILED on create error
}
<<<CLASS>>>::init(OBJECT *parent)
{
  // TODO initialize object
  return SUCCESS; // return FAILED on create error
}
// TODO add implementations of optional class functions

Autoconf file

module-name/Makefile.am
pkglib_LTLIBRARIES = <<<MODULE>>>.la
<<<MODULE>>>_la_SOURCES = main.cpp \
	<<<CLASS>>>.cpp <<<CLASS>>>.h \
       # TODO add new classes before this line
<<<MODULE>>>_la_LDFLAGS = -module -no-undefined -avoid-version -version-info 1:0:0

uninstall-hook:
	-rmdir $(DESTDIR)$(pkglibdir)

Project file

Additional include directories
..\core
Debug information format
Program Database (/Zi)

 TODO:  add other VS project options

Pass control

 TODO: 

Forced naming

 TODO: 

Automatic locking

 TODO: 

Observers

 TODO: 

Parent classes

 TODO: 

Abstract classes

 TODO: 

Parent overrides

 TODO: 

See also