| 1 |
/* Definition of the generic obj class module. |
| 2 |
* |
| 3 |
* This module relies heavily on preprocessor macros in order to |
| 4 |
* provide fast execution time AND ease of use. |
| 5 |
* |
| 6 |
* Each object that uses this base class MUST provide a constructor with |
| 7 |
* the following interface: |
| 8 |
* |
| 9 |
* Destruct(pThis); |
| 10 |
* |
| 11 |
* A constructor is not necessary (except for some features, e.g. de-serialization). |
| 12 |
* If it is provided, it is a three-part constructor (to handle all cases with a |
| 13 |
* generic interface): |
| 14 |
* |
| 15 |
* Construct(&pThis); |
| 16 |
* SetProperty(pThis, property_t *); |
| 17 |
* ConstructFinalize(pThis); |
| 18 |
* |
| 19 |
* SetProperty() and ConstructFinalize() may also be called on an object |
| 20 |
* instance which has been Construct()'ed outside of this module. |
| 21 |
* |
| 22 |
* pThis always references to a pointer of the object. |
| 23 |
* |
| 24 |
* Copyright 2008-2025 Adiscon GmbH. |
| 25 |
* |
| 26 |
* This file is part of the rsyslog runtime library. |
| 27 |
* |
| 28 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 29 |
* you may not use this file except in compliance with the License. |
| 30 |
* You may obtain a copy of the License at |
| 31 |
* |
| 32 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 33 |
* -or- |
| 34 |
* see COPYING.ASL20 in the source distribution |
| 35 |
* |
| 36 |
* Unless required by applicable law or agreed to in writing, software |
| 37 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 38 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 |
* See the License for the specific language governing permissions and |
| 40 |
* limitations under the License. |
| 41 |
*/ |
| 42 |
|
| 43 |
#ifndef OBJ_H_INCLUDED |
| 44 |
#define OBJ_H_INCLUDED |
| 45 |
|
| 46 |
#include "obj-types.h" |
| 47 |
#include "var.h" |
| 48 |
#include "stream.h" |
| 49 |
|
| 50 |
/* macros */ |
| 51 |
/* the following one is a helper that prevents us from writing the |
| 52 |
* ever-same code at the end of Construct() |
| 53 |
*/ |
| 54 |
#define OBJCONSTRUCT_CHECK_SUCCESS_AND_CLEANUP \ |
| 55 |
if (iRet == RS_RET_OK) { \ |
| 56 |
*ppThis = pThis; \ |
| 57 |
} else { \ |
| 58 |
if (pThis != NULL) free(pThis); \ |
| 59 |
} |
| 60 |
|
| 61 |
#define objSerializeSCALAR_VAR(strm, propName, propType, var) \ |
| 62 |
CHKiRet(obj.SerializeProp(strm, (uchar *)#propName, PROPTYPE_##propType, (void *)&var)) |
| 63 |
#define objSerializeSCALAR(strm, propName, propType) \ |
| 64 |
CHKiRet(obj.SerializeProp(strm, (uchar *)#propName, PROPTYPE_##propType, (void *)&pThis->propName)) |
| 65 |
#define objSerializePTR(strm, propName, propType) \ |
| 66 |
CHKiRet(obj.SerializeProp(strm, (uchar *)#propName, PROPTYPE_##propType, (void *)pThis->propName)) |
| 67 |
#define DEFobjStaticHelpers \ |
| 68 |
static objInfo_t __attribute__((unused)) *pObjInfoOBJ = NULL; \ |
| 69 |
DEFobjCurrIf(obj) |
| 70 |
|
| 71 |
|
| 72 |
#define objGetClassName(pThis) (((obj_t *)(pThis))->pObjInfo->pszID) |
| 73 |
#define objGetVersion(pThis) (((obj_t *)(pThis))->pObjInfo->iObjVers) |
| 74 |
/* the next macro MUST be called in Constructors: */ |
| 75 |
#ifndef NDEBUG /* this means if debug... */ |
| 76 |
#define objConstructSetObjInfo(pThis) \ |
| 77 |
((obj_t *)(pThis))->pObjInfo = pObjInfoOBJ; \ |
| 78 |
((obj_t *)(pThis))->pszName = NULL; \ |
| 79 |
((obj_t *)(pThis))->iObjCooCKiE = 0xBADEFEE |
| 80 |
#else |
| 81 |
#define objConstructSetObjInfo(pThis) \ |
| 82 |
((obj_t *)(pThis))->pObjInfo = pObjInfoOBJ; \ |
| 83 |
((obj_t *)(pThis))->pszName = NULL |
| 84 |
#endif |
| 85 |
#define objSerialize(pThis) (((obj_t *)(pThis))->pObjInfo->objMethods[objMethod_SERIALIZE]) |
| 86 |
|
| 87 |
#define OBJSetMethodHandler(methodID, pHdlr) CHKiRet(obj.InfoSetMethod(pObjInfoOBJ, methodID, (rsRetVal(*)())pHdlr)) |
| 88 |
|
| 89 |
/* interfaces */ |
| 90 |
BEGINinterface(obj) /* name must also be changed in ENDinterface macro! */ |
| 91 |
rsRetVal (*UseObj)(const char *srcFile, uchar *pObjName, uchar *pObjFile, interface_t *pIf); |
| 92 |
rsRetVal (*ReleaseObj)(const char *srcFile, uchar *pObjName, uchar *pObjFile, interface_t *pIf); |
| 93 |
rsRetVal (*InfoConstruct)(objInfo_t **ppThis, uchar *pszID, int iObjVers, rsRetVal (*pConstruct)(void *), |
| 94 |
rsRetVal (*pDestruct)(void *), rsRetVal (*pQueryIF)(interface_t *), modInfo_t *); |
| 95 |
rsRetVal (*DestructObjSelf)(obj_t *pThis); |
| 96 |
rsRetVal (*BeginSerializePropBag)(strm_t *pStrm, obj_t *pObj); |
| 97 |
rsRetVal (*InfoSetMethod)(objInfo_t *pThis, objMethod_t objMethod, rsRetVal (*pHandler)(void *)); |
| 98 |
rsRetVal (*BeginSerialize)(strm_t *pStrm, obj_t *pObj); |
| 99 |
rsRetVal (*SerializeProp)(strm_t *pStrm, uchar *pszPropName, propType_t propType, void *pUsr); |
| 100 |
rsRetVal (*EndSerialize)(strm_t *pStrm); |
| 101 |
rsRetVal (*RegisterObj)(uchar *pszObjName, objInfo_t *pInfo); |
| 102 |
rsRetVal (*UnregisterObj)(uchar *pszObjName); |
| 103 |
rsRetVal (*Deserialize)(void *ppObj, uchar *pszTypeExpected, strm_t *pStrm, rsRetVal (*fFixup)(obj_t *, void *), |
| 104 |
void *pUsr); |
| 105 |
rsRetVal (*DeserializePropBag)(obj_t *pObj, strm_t *pStrm); |
| 106 |
rsRetVal (*SetName)(obj_t *pThis, uchar *pszName); |
| 107 |
uchar *(*GetName)(obj_t *pThis); |
| 108 |
ENDinterface(obj) |
| 109 |
#define objCURR_IF_VERSION 2 /* increment whenever you change the interface structure! */ |
| 110 |
|
| 111 |
|
| 112 |
/* prototypes */ |
| 113 |
/* the following define *is* necessary, because it provides the root way of obtaining |
| 114 |
* interfaces (at some place we need to start our query... |
| 115 |
*/ |
| 116 |
rsRetVal objGetObjInterface(obj_if_t *pIf); |
| 117 |
PROTOTYPEObjClassInit(obj); |
| 118 |
PROTOTYPEObjClassExit(obj); |
| 119 |
rsRetVal objDeserializeWithMethods(void *ppObj, |
| 120 |
uchar *pszTypeExpected, |
| 121 |
int lenTypeExpected, |
| 122 |
strm_t *pStrm, |
| 123 |
rsRetVal (*fFixup)(obj_t *, void *), |
| 124 |
void *pUsr, |
| 125 |
rsRetVal (*objConstruct)(void **), |
| 126 |
rsRetVal (*objConstructFinalize)(void *), |
| 127 |
rsRetVal (*objDeserialize)(void *, strm_t *)); |
| 128 |
rsRetVal objDeserializeProperty(var_t *pProp, strm_t *pStrm); |
| 129 |
uchar *objGetName(obj_t *pThis); |
| 130 |
|
| 131 |
|
| 132 |
/* the following definition is only for "friends" */ |
| 133 |
extern pthread_mutex_t mutObjGlobalOp; /* mutex to guard global operations of the object system */ |
| 134 |
|
| 135 |
#endif /* #ifndef OBJ_H_INCLUDED */ |