![]() |
VR-Forces 5.0.1 Developer's Guide
|
The DtSetSpeedAndHeadingRequest is a new class that encapsulates the setting of both the speed and the heading for a particular entity. This message is handled by the subclasses of the appropriate back-end set controllers.
Header file:
/*******************************************************************************
** Copyright (c) 2006 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: setSpeedAndHeading.h,v $ $Revision: 1.3 $ $State: Exp $
*******************************************************************************/
#pragma once
#include "vrftasks/setDataRequest.h"
#include <vlpi/netStructs.h>
#include <readerWriter/rwReal.h>
#include "vrftasks/radioMessageTypes.h"
namespace vrfAddSetExample
{
//class DtSetSpeedAndHeadingRequest:
//
//Instances of DtSetSpeedAndHeading are an example of a new kind of set that tells
//an entity to set their current speed and heading.
//DtSetSpeedAndHeadingString must be a unique string representing the set
//(existing string types are defined in msgTypes.h).
const DtSetMessageType DtSetSpeedAndHeadingType("set-speed-and-heading");
//DtSetSpeedAndHeadingMenuItemIdName must be a unique string identifying the
//DtSetSpeedAndHeadingMenuItem object, which inherits from DtVrfTaskSetMenuItem
const char DtSetSpeedAndHeadingMenuItemIdName[] = "DtSetSpeedAndHeadingMenuItem";
class DtSetSpeedAndHeadingRequest : public DtSetDataRequest
{
public:
//default constructor
DtSetSpeedAndHeadingRequest();
//constructor;
//If you have a request 'put' itself, the writeName will preface it.
DtSetSpeedAndHeadingRequest(const DtString & writeName,
DtReaderWriterRegistry * parentRegistry = 0);
//copy constructor
DtSetSpeedAndHeadingRequest(const DtString & writeName, const DtSetSpeedAndHeadingRequest &
orig, DtReaderWriterRegistry * parentRegistry = 0);
//assignment operator
const DtSetSpeedAndHeadingRequest & operator=(const DtSetSpeedAndHeadingRequest & orig);
virtual ~DtSetSpeedAndHeadingRequest();
//Returns DtSetSpeedAndHeadingRequestType.
virtual const DtSetMessageType& type() const;
//Creates a copy of the request.
virtual DtSetDataRequest * clone(const DtString & name,
DtReaderWriterRegistry * parentRegistry = 0) const;
//Get a readable string representation of this setDataRequest.
virtual DtString stringRep() const;
//In meters/second.
virtual DtReal speed() const;
virtual void setSpeed(DtReal speed);
//In radians
virtual DtReal heading() const;
virtual void setHeading(DtReal heading);
//Returns the size of the full network representation
//padded to an 8-byte boundary.
virtual int netRepSize() const;
//Fills in the task from the network buffer.
virtual bool setFromNet(const char* contentBuffer,
unsigned contentSize);
//Fills in the network buffer (created by base class setDataRequest) with
//the set data request contents.
virtual bool setToNet();
static DtSetDataRequest * creator();
protected:
DtRwReal mySpeed;
DtRwReal myHeading;
};
}
Implementation:
/*******************************************************************************
** Copyright (c) 2006 MaK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
#include <math.h>
#include <readerWriter/bufferSerialize.h>
#include "../addSetSim/setSpeedAndHeading.h"
#include <tdbutil/unitFormatter.h>
namespace vrfAddSetExample
{
//default constructor
DtSetSpeedAndHeadingRequest::DtSetSpeedAndHeadingRequest() :
DtSetDataRequest(),
mySpeed("speed", & myRegistry),
myHeading("heading", & myRegistry)
{
mySpeed = 0.;
myHeading = 0.;
}
//constructor
//if you have a task 'put' itself, the writeName will preface the task.
DtSetSpeedAndHeadingRequest::DtSetSpeedAndHeadingRequest(const DtString & writeName,
DtReaderWriterRegistry * parentRegistry) :
DtSetDataRequest(writeName, parentRegistry),
mySpeed("speed", & myRegistry),
myHeading("heading", & myRegistry)
{
mySpeed = 0.;
myHeading = 0.;
}
//copy constructor
DtSetSpeedAndHeadingRequest::DtSetSpeedAndHeadingRequest(const DtString & writeName, const
DtSetSpeedAndHeadingRequest & orig, DtReaderWriterRegistry * parentRegistry) :
DtSetDataRequest(writeName, orig, parentRegistry),
mySpeed(orig.mySpeed.name(), & myRegistry),
myHeading(orig.myHeading.name(), & myRegistry)
{
mySpeed = orig.mySpeed;
myHeading = orig.myHeading;
}
//assignment operator
const DtSetSpeedAndHeadingRequest & DtSetSpeedAndHeadingRequest::operator=(const
DtSetSpeedAndHeadingRequest & orig)
{
if (this == & orig)
{
return *this;
}
mySpeed = orig.mySpeed;
myHeading = orig.myHeading;
return *this;
}
DtSetSpeedAndHeadingRequest::~DtSetSpeedAndHeadingRequest()
{
}
const DtSetMessageType& DtSetSpeedAndHeadingRequest::type() const
{
return DtSetSpeedAndHeadingType;
}
DtSetDataRequest * DtSetSpeedAndHeadingRequest::clone(const DtString & name,
DtReaderWriterRegistry * parentRegistry) const
{
return new DtSetSpeedAndHeadingRequest(name, *this, parentRegistry);
}
//Get a readable string representation of this setDataRequest.
DtString DtSetSpeedAndHeadingRequest::stringRep() const
{
DtString str = trUtf8("Set Speed=", "DtSetSpeedRequest");
char valueBuff[128];
sprintf(valueBuff, "%g Heading (deg)=%g", mySpeed.value(), DtRad2Deg(myHeading.value()));
str += valueBuff;
return str;
}
DtReal DtSetSpeedAndHeadingRequest::speed() const
{
return mySpeed;
}
void DtSetSpeedAndHeadingRequest::setSpeed(DtReal speed)
{
mySpeed = speed;
}
DtReal DtSetSpeedAndHeadingRequest::heading() const
{
return myHeading;
}
void DtSetSpeedAndHeadingRequest::setHeading(DtReal heading)
{
myHeading = heading;
}
DtSetDataRequest * DtSetSpeedAndHeadingRequest::creator()
{
return new DtSetSpeedAndHeadingRequest();
}
//returns the size of the full network representation (control object name
//included) padded to an 8-byte boundary.
int DtSetSpeedAndHeadingRequest::netRepSize() const
{
int size = 0;
size += DtBufferSerialize::getSize(mySpeed);
size += DtBufferSerialize::getSize(myHeading);
return size;
}
//Fills in the task from the network buffer
bool DtSetSpeedAndHeadingRequest::setFromNet(const char* contentBuffer,
unsigned contentSize)
{
contentBuffer = DtBufferSerialize::decode(mySpeed, contentBuffer);
contentBuffer = DtBufferSerialize::decode(myHeading, contentBuffer);
return true;
}
//Fills in the network buffer (created by base class simTask) with the
//task contents.
bool DtSetSpeedAndHeadingRequest::setToNet()
{
if (!netRep())
{
return false;
}
char* contentBuffer = netRep();
contentBuffer = DtBufferSerialize::encode(mySpeed, contentBuffer);
contentBuffer = DtBufferSerialize::encode(myHeading, contentBuffer);
return true;
}
}