// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. #ifndef XPLRA_SETDATAREFTASK_H #define XPLRA_SETDATAREFTASK_H //------------------------------------------------------------------------------ #include "DataRefTask.h" #include //------------------------------------------------------------------------------ namespace xplra { //------------------------------------------------------------------------------ /** * Base class for dataref tasks that set the value of some data * referenced by a dataref. * * Since XPLM provides no feedback on whether the setting is * successful, we detect only if the dataref is valid or not. So the * isValid() function provides the only feedback if the task executed * successfully or not. (We could use XPLMCanWriteDataRef, but for * efficiency reasons we refrain from it. A separate task may be * created for this purpose.) */ class SetDataRefTask : public DataRefTask { public: /** * Read a dataref setting specification and create the appropriate * SetDataRefTask instance. * * @param result will contain the result of the operation. It * signifies an error only, if some wrong data is read. If the * stream fails, it will indicate RESULT_OK. * @param stream the stream to read from * * @return the new instance or 0 on error. */ static SetDataRefTask* create(uint8_t& result, hu::varadiistvan::scpl::io::DataStream& stream); /** * Construct the task for the dataref with the given name. */ SetDataRefTask(const std::string& name); /** * Construct the task for the given dataref. */ SetDataRefTask(XPLMDataRef dataRef); /** * Read and set the value to update from the given stream */ virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream) = 0; }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * Base class for dataref setting tasks that set a single scalar * value. * * ConcreteClass is the actual child class. It should have a function * name setData() which performs the actual setting. */ template class SetScalarDataRefTask : public SetDataRefTask { protected: /** * The value to set. */ T value; public: /** * Construct the task for setting the data referenced by the * dataref with the given name. */ SetScalarDataRefTask(const std::string& name); /** * Construct the task for setting the data referenced by the given * dataref. */ SetScalarDataRefTask(XPLMDataRef dataRef); protected: /** * Perform the actual operation. */ virtual void process(); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * Task to set the value of an integer data. */ class SetIntDataRefTask : public SetScalarDataRefTask { public: /** * Set the given dataref's value to the given value. */ static void setData(XPLMDataRef dataRef, int value); /** * Construct the task for the dataref with the given name. */ SetIntDataRefTask(const std::string& name); /** * Construct the task for the given dataref. */ SetIntDataRefTask(XPLMDataRef dataRef); /** * Read and set the value to update from the given stream */ virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * Task to set the value of a single-precision floating point data. */ class SetFloatDataRefTask : public SetScalarDataRefTask { public: /** * Set the given dataref's value to the given value. */ static void setData(XPLMDataRef dataRef, float value); /** * Construct the task for the dataref with the given name. */ SetFloatDataRefTask(const std::string& name); /** * Construct the task for the given dataref. */ SetFloatDataRefTask(XPLMDataRef dataRef); /** * Read and set the value to update from the given stream */ virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * Task to set the value of a double-precision floating point data. */ class SetDoubleDataRefTask : public SetScalarDataRefTask { public: /** * Set the given dataref's value to the given value. */ static void setData(XPLMDataRef dataRef, double value); /** * Construct the task for the dataref with the given name. */ SetDoubleDataRefTask(const std::string& name); /** * Construct the task for the given dataref. */ SetDoubleDataRefTask(XPLMDataRef dataRef); /** * Read and set the value to update from the given stream */ virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * Base class for dataref setting tasks that set the values of an * array. * * ConcreteClass is the actual child class. It should have a function * name setData() which performs the actual setting. */ template class SetArrayDataRefTask : public SetDataRefTask { private: /** * The number of data items in the value. */ int count; /** * The offset starting with which the data should be set. */ int offset; /** * The value to set. */ T* value; public: /** * Construct the data setting task for the dataref with the given * name. * * The value array will be allocated, but not filled with * data. Thus count should be positive! */ SetArrayDataRefTask(const std::string& name, int count, int offset = 0); /** * Construct the data setting task for the given dataref. * * The value array will be allocated, but not filled with * data. Thus count should be positive! */ SetArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0); /** * Destroy the task. */ virtual ~SetArrayDataRefTask(); /** * Read and set the value to update from the given stream */ virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream); protected: /** * Perform the actual operation. */ virtual void process(); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * A dataref setting task which sets an array of integers. */ class SetIntArrayDataRefTask : public SetArrayDataRefTask { public: /** * Perform the actual setting. */ static void setData(XPLMDataRef dataRef, int* value, int count, int offset); /** * Construct the task for the dataref with the given name. */ SetIntArrayDataRefTask(const std::string& name, int count, int offset = 0); /** * Construct the task for the given dataref. */ SetIntArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * A dataref setting task which sets an array of single-precision * floating-point values. */ class SetFloatArrayDataRefTask : public SetArrayDataRefTask { public: /** * Perform the actual setting. */ static void setData(XPLMDataRef dataRef, float* value, int count, int offset); /** * Construct the task for the dataref with the given name. */ SetFloatArrayDataRefTask(const std::string& name, int count, int offset = 0); /** * Construct the task for the given dataref. */ SetFloatArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0); }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ /** * A dataref setting task which sets an array of bytes. */ class SetByteArrayDataRefTask : public SetArrayDataRefTask { public: /** * Perform the actual setting. */ static void setData(XPLMDataRef dataRef, unsigned char* value, int count, int offset); /** * Construct the task for the dataref with the given name. */ SetByteArrayDataRefTask(const std::string& name, int count, int offset = 0); /** * Construct the task for the given dataref. */ SetByteArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0); }; //------------------------------------------------------------------------------ // Template definitions //------------------------------------------------------------------------------ template inline SetScalarDataRefTask:: SetScalarDataRefTask(const std::string& name) : SetDataRefTask(name) { } //------------------------------------------------------------------------------ template inline SetScalarDataRefTask:: SetScalarDataRefTask(XPLMDataRef dataRef) : SetDataRefTask(dataRef) { } //------------------------------------------------------------------------------ template void SetScalarDataRefTask::process() { ConcreteClass::setData(getDataRef(), value); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ template inline SetArrayDataRefTask:: SetArrayDataRefTask(const std::string& name, int count, int offset) : SetDataRefTask(name), count(count), offset(offset), value(new T[count]) { } //------------------------------------------------------------------------------ template inline SetArrayDataRefTask:: SetArrayDataRefTask(XPLMDataRef dataRef, int count, int offset) : SetDataRefTask(dataRef), count(count), offset(offset), value(new T[count]) { } //------------------------------------------------------------------------------ template inline SetArrayDataRefTask::~SetArrayDataRefTask() { delete [] value; } //------------------------------------------------------------------------------ template void SetArrayDataRefTask:: readValue(hu::varadiistvan::scpl::io::DataStream& stream) { stream.read(value, count * sizeof(T)); } //------------------------------------------------------------------------------ template void SetArrayDataRefTask::process() { ConcreteClass::setData(getDataRef(), value, count, offset); } //------------------------------------------------------------------------------ // Inline definitions //------------------------------------------------------------------------------ inline SetDataRefTask::SetDataRefTask(const std::string& name) : DataRefTask(name) { } //------------------------------------------------------------------------------ inline SetDataRefTask::SetDataRefTask(XPLMDataRef dataRef) : DataRefTask(dataRef) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetIntDataRefTask::setData(XPLMDataRef dataRef, int value) { XPLMSetDatai(dataRef, value); } //------------------------------------------------------------------------------ inline SetIntDataRefTask::SetIntDataRefTask(const std::string& name) : SetScalarDataRefTask(name) { } //------------------------------------------------------------------------------ inline SetIntDataRefTask::SetIntDataRefTask(XPLMDataRef dataRef) : SetScalarDataRefTask(dataRef) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetFloatDataRefTask::setData(XPLMDataRef dataRef, float value) { XPLMSetDatai(dataRef, value); } //------------------------------------------------------------------------------ inline SetFloatDataRefTask::SetFloatDataRefTask(const std::string& name) : SetScalarDataRefTask(name) { } //------------------------------------------------------------------------------ inline SetFloatDataRefTask::SetFloatDataRefTask(XPLMDataRef dataRef) : SetScalarDataRefTask(dataRef) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetDoubleDataRefTask::setData(XPLMDataRef dataRef, double value) { XPLMSetDatai(dataRef, value); } //------------------------------------------------------------------------------ inline SetDoubleDataRefTask::SetDoubleDataRefTask(const std::string& name) : SetScalarDataRefTask(name) { } //------------------------------------------------------------------------------ inline SetDoubleDataRefTask::SetDoubleDataRefTask(XPLMDataRef dataRef) : SetScalarDataRefTask(dataRef) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetIntArrayDataRefTask::setData(XPLMDataRef dataRef, int* value, int count, int offset) { XPLMSetDatavi(dataRef, value, offset, count); } //------------------------------------------------------------------------------ inline SetIntArrayDataRefTask::SetIntArrayDataRefTask(const std::string& name, int count, int offset) : SetArrayDataRefTask(name, count, offset) { } //------------------------------------------------------------------------------ inline SetIntArrayDataRefTask::SetIntArrayDataRefTask(XPLMDataRef dataRef, int count, int offset) : SetArrayDataRefTask(dataRef, count, offset) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetFloatArrayDataRefTask::setData(XPLMDataRef dataRef, float* value, int count, int offset) { XPLMSetDatavf(dataRef, value, offset, count); } //------------------------------------------------------------------------------ inline SetFloatArrayDataRefTask::SetFloatArrayDataRefTask(const std::string& name, int count, int offset) : SetArrayDataRefTask(name, count, offset) { } //------------------------------------------------------------------------------ inline SetFloatArrayDataRefTask::SetFloatArrayDataRefTask(XPLMDataRef dataRef, int count, int offset) : SetArrayDataRefTask(dataRef, count, offset) { } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ inline void SetByteArrayDataRefTask::setData(XPLMDataRef dataRef, unsigned char* value, int count, int offset) { XPLMSetDatab(dataRef, value, offset, count); } //------------------------------------------------------------------------------ inline SetByteArrayDataRefTask::SetByteArrayDataRefTask(const std::string& name, int count, int offset) : SetArrayDataRefTask(name, count, offset) { } //------------------------------------------------------------------------------ inline SetByteArrayDataRefTask::SetByteArrayDataRefTask(XPLMDataRef dataRef, int count, int offset) : SetArrayDataRefTask(dataRef, count, offset) { } //------------------------------------------------------------------------------ } /* namespace xplra */ //------------------------------------------------------------------------------ #endif // XPLRA_SETDATAREFTASK_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: