datareference.h

00001 // Copyright (C) 2008,2009,2010 by Philipp Münzel. All rights reserved.
00002 // Released under the terms of the license described in license.txt
00003 
00004 #ifndef DATAREFERENCE_H
00005 #define DATAREFERENCE_H
00006 
00007 #include "dllexport.h"
00008 
00009 #include <string>
00010 #include <boost/date_time.hpp>
00011 #include <boost/function.hpp>
00012 
00013 #include "canaerometadata.h"
00014 
00015 namespace SCS {
00016 
00027 class DLL_PUBLIC TypelessDataReference {
00028 public:
00029 
00030     virtual ~TypelessDataReference() {}
00031 
00035     virtual void update() = 0;
00036 
00041     virtual void readFromCan(const CanAeroMetadata& meta, const can_t& can) = 0;
00042 
00047     virtual std::vector<can_t> write(const CanAeroMetadata& meta) = 0;
00048 
00052     virtual bool needRequest() const = 0;
00053 
00058     virtual void requested() = 0;
00059 
00063     virtual bool hasChanged() const = 0;
00064 
00068     virtual bool isSelfPublished() const = 0;
00069 };
00070 
00071 
00078 template <typename T>
00079 class DLL_PUBLIC DataReference : public TypelessDataReference
00080 {
00081 public:
00082     typedef boost::function<void (const T&)> func;
00083 
00088     DataReference(bool own, T* ptr);
00089 
00093     DataReference(bool own, func func_ptr);
00094 
00100     DataReference(bool own, func func_ptr, T* ptr);
00101 
00105     void update();
00106 
00110     virtual void readFromCan(const CanAeroMetadata& meta, const can_t& can);
00111 
00115     std::vector<can_t> write(const CanAeroMetadata& meta);
00116 
00120     bool needRequest() const;
00121 
00125     void requested();
00126 
00130     bool hasChanged() const;
00131 
00135     bool isSelfPublished() const { return m_is_self_published; }
00136 
00137 protected:
00138     unsigned long m_number_of_losses;
00139     T m_cached_value;
00140     T m_history_value;
00141 
00142 private:
00143     void checkMessageCode(const CanAeroMetadata& meta, uint8_t msg_code);
00144     void received();
00145     std::vector<can_t> writeCan(const CanAeroMetadata& meta, const T&);
00146     void init();
00147 
00148 private:
00149     T* m_ptr;
00150     func m_func_ptr;
00151     bool m_was_requested;
00152     bool m_has_changed;
00153     bool m_is_self_published;
00154     boost::posix_time::ptime m_last_received;
00155 
00156 };
00157 
00158 
00159 }
00160 #endif // DATAREFERENCE_H