Go to the documentation of this file.00001
00006 #ifndef DATAELEMENT_H_
00007 #define DATAELEMENT_H_
00008
00009 #include <string>
00010 #include <stdexcept>
00011 #include <boost/variant.hpp>
00012 #include <boost/lexical_cast.hpp>
00013 #include <stdint.h>
00014 #include "nasa_common_logging/Logger.h"
00015 #include "shared_memory_transport/resource.hpp"
00016
00019 class DataElement
00020 {
00021 public:
00022
00025 typedef boost::variant<bool, int16_t, int32_t, uint16_t, uint32_t, float> ValueType;
00026
00027
00030 enum ElementSize
00031 {
00032 EightBit,
00033 SixteenBit,
00034 ThirtyTwoBit
00035 };
00036
00039 enum ReadWrite
00040 {
00041 Read,
00042 Write
00043 };
00044
00045
00046 ValueType value;
00047 ReadWrite readWrite;
00048 std::shared_ptr<SMT::Resource> smtResource;
00050
00051 DataElement();
00052 DataElement(const std::string& type, ReadWrite readWrite, std::shared_ptr<SMT::Resource> resource = nullptr);
00053 virtual ~DataElement();
00054 template <typename T> T getValue() const;
00055 template <typename T> void setValue(const T v);
00056 const std::type_info* getType();
00057 ElementSize getSize();
00058 ReadWrite getReadWrite();
00059 void setReadWrite(ReadWrite readWrite);
00060 virtual std::string toString();
00061 };
00062
00063
00064
00070 template <typename T>
00071 T DataElement::getValue() const
00072 {
00073
00074 if (value.type() == typeid(T))
00075 {
00076
00077 return boost::get<T>(value);
00078 }
00079 else
00080 {
00081 std::stringstream err;
00082 err << "DataElement::getValue() - Template type must match the underlying variant type.";
00083 NasaCommonLogging::Logger::log("gov.nasa.robonet.DataElement", log4cpp::Priority::ERROR, err.str());
00084 throw std::invalid_argument(err.str());
00085 }
00086 }
00087
00093 template <typename T>
00094 void DataElement::setValue(const T v)
00095 {
00096
00097 value = v;
00098
00099
00100 if (smtResource){
00101 smtResource->set<T>(v);
00102 }
00103 }
00104
00105 #endif