VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Space Entity Parameter Class

This subclass of the DtMovingObjectParameters adds a new parameter (maximum hull temperature) to our new entity


Creating a New Object Parameters Class

Now that we have a new state repository class for the space entity (DtSpaceEntityStateRepository ), we need to create an class to hold parameters specific to space entities. An instance of this object parameters class will be created by the state repository and used to initialize itself. This enables the entity to be configured with the values specified in the object parameters database.

Each state repository class creates a new instance of a DtVrfObjectParameters object in its createParameters() method. The derivation of the "parameters" class must parallel the derivation of the "state repository" class. For instance, in this case, DtSpaceEntityStateRepository is derived from DtVrfMovingObjectStateRepository. Since DtVrfMovingObjectStateRepository::createParameters() instantiates a DtMovingObjectParameters object, DtSpaceEntityParameters must be derived from DtMovingObjectParameters. DtSpaceEntityStateRepostiory overrides createParameters() to create an instance of this new class.

Deriving a Parameters Class

As in the new state repository class, the new parameters class needs to override the constructor, type(), and creator() member functions from its parent.

The Constructor

The parameter class constructor sets up the object as read/ write-capable. The parameters for the object can then be read in from the object parameters database.

  DtSpaceEntityParameters::DtSpaceEntityParameters(const DtString& name,
       DtReaderWriterRegistry* parentRegistry) :
       DtMovingObjectParameters(name, parentRegistry),
       myMaxHullTemperature("max-hull-temperature", 1260.0, &myRegistry)
  {
  }
  

The type() function

The type() function returns a string that uniquely identifies the parameter class. It is used as a key in the object parameters factory. Object parameter types are defined in vrfParamTypes.h. All strings must be unique, so care must be taken to ensure that strings returned by new type() functions do not conflict with existing types.

  const char DtSpaceVehicleParamType[] = "space-param";
  DtString DtSpaceEntityParameters::type() const
  {
     return DtSpaceVehicleParamType;
  }
  

The creator() function

The static creator() function is registered with the object parameters factory. It should return a newly created instance of your derived class.

  DtVrfObjectParameters* DtSpaceEntityParameters::creator()
  {
     return new DtSpaceEntityParameters;
  }
  

The creator() function must be registered with the DtSpaceEntityParameters type with the state repository factory.

Specifying the Parameter Data Type

We are adding one readable, writable member variable to the class – myMaxHullTemperature. To make the variable readable and writable, we must choose a type of member variable derived from DtReaderWriter and register the data member with our parameters class. Since it conceptually represents a continuous real quantity, we chose to make myMaxHullTemperature a DtRwReal, which is a readable, writable double precision floating point number (there are numerous readable, writable classes for representing basic data types).

To make the myMaxHullTemperature data member capable of being read from a file, we need to register it with the parameters class. This is done in the constructor:

  DtSpaceEntityParameters::DtSpaceEntityParameters(const DtString& name,
      DtReaderWriterRegistry* parentRegistry) :
      DtMovingObjectParameters(name, parentRegistry),
      myMaxHullTemperature("max-hull-temperature", 1260.0, &myRegistry)
  {
  }
  

Initialization of myMaxHullTemperature in this manner ensures that when the parameter "max-hull-temperature" is encountered in the object parameters database for this entity, the corresponding value can be stored in the myMaxHullTemperature member variable.

For example, a new space entity parameter entry might look like the following:

  (new-space-entity
     (parameter-type "space-param")
     (object-type  1 (1 5 0 0 0 0 0))
     (max-hull-temperature 1500)
     . . .
  )
  

The parameter-type variable indicates that the kind of parameters class to be created for this object is of type DtSpaceEntityParameters (the same type as returned by its type() member function). The max-hull-temperature parameter is used to initialize the myMaxHullTemperature member variable to 1500 C.

Initializing the State Repository with Parameter Data

In the DtSpaceEntityStateRepository class, we need to override the createParameters()member to instantiate DtSpaceEntityParameters.

  DtBoolean DtSpaceEntityStateRepository::createParameters()
  {
     if (myParameters)
     {
        delete myParameters;
        myParameters = NULL;
     }
     myParameters = new DtSpaceEntityParameters("parameters");
     if (myParameters == NULL)
     {
        return DtFalse;
     }
     return DtTrue;
  }
  

Override initializeFromParameters() to initialize our new data member, myMaxHullTemperature from our new parameters object. Make sure to call down to the base class to ensure that the rest of the object is initialized. This initializes the myMaxHullTemperature member with any values specified for the parameter in the object parameters database.

  void DtSpaceEntityStateRepository::initializeFromParameters()
  {
     DtVrfMovingObjectStateRepository::initializeFromParameters();
     myMaxHullTemperature = spaceEntityParameters()->maxHullTemperature();
  }
  

Header file:

/*******************************************************************************
** Copyright (c) 2013 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
//
//File: spacePar.h
//
//Class: DtSpaceEntityParameters
//
//Description: This parameter class holds all of the parameters required by
//space entities (entities having a state repository class of type
//DtSpaceEntityStateRepository). It contains a single the readable/writeable
//parameter data members specific to space entities. In this example, we add a
//single new readable/writeable parameter, myMaxHullTemperature.
#pragma once
class DtString;
{
public:
//constructor with parameters to set up read/writability correctly
DtSpaceEntityParameters(const DtString& name = DtString::nullString(),
DtReaderWriterRegistry* parentRegistry = NULL, DtEnvironmentSP env = DtEnvironmentSP(), DtEnvValueSP* objectNode = NULL);
//copy constructor
const DtString& name,
DtReaderWriterRegistry* parentRegistry = NULL);
//destructor
//Returns new copy of this object
const DtString& name = DtString::nullString(),
DtReaderWriterRegistry* parentRegistry = 0) const;
//should return the type of entity this is a parameter block for.
//Use the strings in paramTypes.h
virtual DtString type() const;
//Accessor/mutator for our new parameter, myMaxHullTemperature.
//Set/get maximum hull temperature this entity can sustain.
//In degrees Celsius.
virtual void setMaxHullTemperature(DtReal temp);
virtual DtReal maxHullTemperature() const;
static DtVrfObjectParameters* creator(DtEnvironmentSP, DtEnvValueSP* root);
protected:
};

Implementation:

/*******************************************************************************
** Copyright (c) 2013 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
#include "spacePar.h"
#include <vlutil/vlString.h>
#include <matrix/vlVector.h>
#include "spaceTypes.h"
//constructor with parameters to set up read/writability correctly
DtReaderWriterRegistry* parentRegistry, DtEnvironmentSP env, DtEnvValueSP* objectNode) :
DtFixedWingEntityParameters(name, parentRegistry, env, objectNode),
myMaxHullTemperature("max-hull-temperature", &myRegistry)
{
myMaxHullTemperature = 1260.0;
//Initialize to false so that parameter inheritance will work correctly
myMaxHullTemperature.setValueSpecified(false);
}
//copy constructor
const DtSpaceEntityParameters& orig, const DtString& name,
DtReaderWriterRegistry* parentRegistry) :
DtFixedWingEntityParameters(orig, name, parentRegistry),
myMaxHullTemperature(orig.myMaxHullTemperature.name(), &myRegistry)
{
myMaxHullTemperature = orig.maxHullTemperature();
}
{
if (this == &orig)
{
return *this;
}
return *this;
}
//destructor
{
}
const DtString& name, DtReaderWriterRegistry* parentRegistry) const
{
parentRegistry);
if (!newPars)
{
DtWarn ("Out of memory !?! in DtSpaceEntityParameters::clone()\n");
return NULL;
}
if (! newPars->init())
{
return NULL;
}
*newPars = *this; //invokes copy constructor
return newPars;
}
//should return the type of entity this is a parameter block for.
//Use the strings in paramTypes.h
{
}
{
return new DtSpaceEntityParameters("", 0, env, root);
}
{
}
{
}

Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)