| 1 | /*! |
|---|
| 2 | * @file synchronizeable_var.h |
|---|
| 3 | * @brief Definition of SynchronizeableVar |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _SYNCHRONIZEABLE_VAR_H |
|---|
| 7 | #define _SYNCHRONIZEABLE_VAR_H |
|---|
| 8 | |
|---|
| 9 | #include "nettypes.h" |
|---|
| 10 | #include <string> |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | class SynchronizeableVar { |
|---|
| 15 | |
|---|
| 16 | public: |
|---|
| 17 | SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_MASTER_SERVER, int priority = 0 ); |
|---|
| 18 | virtual ~SynchronizeableVar(); |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * check if synchronizeable wants to be informed on changes |
|---|
| 22 | * @return true if synchronizeable wants to be informed on changes |
|---|
| 23 | */ |
|---|
| 24 | inline bool beWatched(){ return this->bWatched; } |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * set flag if synchronizeable wants to be informed on changes |
|---|
| 28 | */ |
|---|
| 29 | inline void setWatched( bool watched ){ this->bWatched = watched; } |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * write var data to byte buffer |
|---|
| 33 | * @param buf pointer to write to |
|---|
| 34 | * @param maxLength writeToBuf will not write more than maxLength bytes |
|---|
| 35 | * @return number bytes written |
|---|
| 36 | */ |
|---|
| 37 | virtual int writeToBuf( byte * buf, int maxLength ) = 0; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * read var data from byte buffer |
|---|
| 41 | * @param buf pointer to read from |
|---|
| 42 | * @param maxLength readFromBuf will not read more than maxLength bytes |
|---|
| 43 | * @return number bytes read |
|---|
| 44 | */ |
|---|
| 45 | virtual int readFromBuf( byte * buf, int maxLength ) = 0; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * check if writeToBuf will return the same size every time |
|---|
| 49 | * @return true if same size every time |
|---|
| 50 | */ |
|---|
| 51 | virtual bool hasStaticSize() = 0; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * get size writeToBuf needs |
|---|
| 55 | * @return size in bytes |
|---|
| 56 | */ |
|---|
| 57 | virtual int getSize(){ return length; } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * check for permission to write |
|---|
| 61 | * @return true if you can write |
|---|
| 62 | */ |
|---|
| 63 | inline bool checkPermission( int permission ){ return (permission & this->permission) != 0; } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * get variable name |
|---|
| 67 | * @return name |
|---|
| 68 | */ |
|---|
| 69 | inline std::string getName(){ return name; } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * set variable name |
|---|
| 73 | * @param name new name |
|---|
| 74 | */ |
|---|
| 75 | inline void setName( std::string name ) { this->name = name; } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * get priority |
|---|
| 79 | * @return priority |
|---|
| 80 | */ |
|---|
| 81 | inline int getPriority() { return this->priority; } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * set priority |
|---|
| 85 | * @param p priority |
|---|
| 86 | */ |
|---|
| 87 | inline void setPriority( int p ) { this->priority = p; } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * reset priority to variable specific default value |
|---|
| 91 | */ |
|---|
| 92 | inline void resetPriority() { this->priority = this->realPriority; } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * reads actual size from buffer. this is used when size is not constant |
|---|
| 96 | * @param buf pointer to data |
|---|
| 97 | * @param maxLength maximal size of data |
|---|
| 98 | * @return same as readFromBuf would return |
|---|
| 99 | */ |
|---|
| 100 | virtual inline int getSizeFromBuf( byte * buf, int maxLength ){ return this->getSize(); } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * set variable id |
|---|
| 104 | * @param id |
|---|
| 105 | */ |
|---|
| 106 | inline void setVarId( int id ){ this->varId = id; } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * get variable id |
|---|
| 110 | * @return variable id |
|---|
| 111 | */ |
|---|
| 112 | inline int getVarId(){ return varId; } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * set hasChanged |
|---|
| 116 | * @param changed |
|---|
| 117 | */ |
|---|
| 118 | inline void setHasChanged( bool changed ){ this->changed = changed; } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * get hasChanged |
|---|
| 122 | * @return variable id |
|---|
| 123 | */ |
|---|
| 124 | inline bool getHasChanged(){ return changed; } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * print out variable value |
|---|
| 128 | */ |
|---|
| 129 | virtual void debug() = 0; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | private: |
|---|
| 133 | bool bWatched; //!< true if synchronizeable wants to be informed on changes |
|---|
| 134 | |
|---|
| 135 | int permission; //!< who is allowed to change this var |
|---|
| 136 | int priority; //!< priority assigned to var |
|---|
| 137 | int realPriority; //!< priority assigned to var, increased every time not sent |
|---|
| 138 | int varId; //!< id to identify varables |
|---|
| 139 | |
|---|
| 140 | bool changed; //!< true if last readFromBuf changed anything |
|---|
| 141 | |
|---|
| 142 | protected: |
|---|
| 143 | void * ptrIn; //!< pointer to data (read) |
|---|
| 144 | void * ptrOut; //!< pointer to data (write) |
|---|
| 145 | int length; //!< data length |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | std::string name; //!< variable name (for debugging) |
|---|
| 149 | |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | #endif /* _PROTO_CLASS_H */ |
|---|