123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "restvar.h"
- #define GET_BOOL_VAL(p, m) (!!(*p & m))
- #define SET_BIT(p, m) (*p |= m)
- #define CLR_BIT(p, m) (*p &= ~m)
- #define STORE_BIT(p, m, b) (b) ? SET_BIT(p, m) : CLR_BIT(p, m)
- /////////////////////////////////////////////////////////////////////////////
- CRestBitVariable::CRestBitVariable(void *pData, size_t nOffset, int nBitNr, HSHM hShm, const char *pszName, CRest *pParent)
- : m_name(pszName), m_pszPath(NULL), m_hShm(hShm), m_pParent(pParent), m_nCbVarpath(0)
- {
- if(!pData || !hShm || (nBitNr > 7))
- {
- ASSERT(false);
- return;
- }
- m_mask = (1 << nBitNr);
- m_pShmByte = (uint8_t*)pData + nOffset;
- }
- CRestBitVariable::~CRestBitVariable(void)
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- void CRestBitVariable::CreateMembersTable(CRestVarTable &vt)
- {
- vt.AddVar(static_cast<CRest*>(this));
- }
- void CRestBitVariable::InitPath(CRest *pParent, const char *pszMemberName, int nIndex)
- {
- CRest::CreatePath(pParent, pszMemberName, nIndex, m_path);
- m_pszPath = m_path.c_str();
- m_nCbVarpath = m_path.length();
- }
- void CRestBitVariable::GetValue(int nReqIndex, json_t *pjtMap)
- {
- json_array_append_new(pjtMap, GetValue(nReqIndex));
- }
- json_t* CRestBitVariable::GetValue(int nReqIndex)
- {
- json_t *pjtVal = NULL;
- Lock();
- bool bVal = GET_BOOL_VAL(m_pShmByte, m_mask);
- Unlock();
- if((pjtVal = json_boolean(bVal)))
- return CreateValueObject(nReqIndex, m_pszPath, -1, json_typeof(pjtVal), m_name.c_str(), pjtVal);
- else
- return CreateStatusObject(nReqIndex, -1, "Unexpected Error!", m_pszPath);
- }
- json_t* CRestBitVariable::SetValue(int nReqIndex, json_t *pjtVal)
- {
- if(!pjtVal)
- return CreateStatusObject(nReqIndex, -1, "Unexpected error!", GetPath());
- json_int_t jInt;
- int nCode = 0;
- const char *pszMsg = NULL;
- int nType = json_typeof(pjtVal);
-
- switch(nType)
- {
- case JSON_INTEGER:
- jInt = json_integer_value(pjtVal);
- STORE_BIT(m_pShmByte, m_mask, !!jInt);
- break;
- case JSON_TRUE:
- SET_BIT(m_pShmByte, m_mask);
- break;
- case JSON_FALSE:
- CLR_BIT(m_pShmByte, m_mask);
- break;
- default:
- nCode = 4;
- pszMsg = "Cannot assign incompatible JSON type!";
- break;
- }
- return CreateStatusObject(nReqIndex, nCode, pszMsg, GetPath());
- }
- /////////////////////////////////////////////////////////////////////////////
- void CRestBitVariable::Lock(void)
- {
- ::GfaIpcLockSHM(m_hShm);
- }
- void CRestBitVariable::Unlock(void)
- {
- ::GfaIpcUnlockSHM(m_hShm);
- }
|