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