| Directory: | ./ |
|---|---|
| File: | src/phoenix_composeType.h |
| Date: | 2024-12-09 11:00:39 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 61 | 63 | 96.8% |
| Branches: | 48 | 71 | 67.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #ifndef __PHOENIX_COMPOSETYPE_H__ | ||
| 8 | #define __PHOENIX_COMPOSETYPE_H__ | ||
| 9 | |||
| 10 | #include <map> | ||
| 11 | |||
| 12 | #include "phoenix_ptype.h" | ||
| 13 | #include "type_stream_get_type.h" | ||
| 14 | #include "PComposeType.h" | ||
| 15 | |||
| 16 | #define TYPE_STREAM_VECTOR 11lu | ||
| 17 | #define TYPE_STREAM_LIST 12lu | ||
| 18 | #define TYPE_STREAM_MAP 13lu | ||
| 19 | |||
| 20 | bool checkValue(const std::string & testName, const PComposeType & value, const PComposeType & reference); | ||
| 21 | bool checkValue(const std::string & testName, const std::vector<PComposeType> & vecValueType, const std::vector<PComposeType> & vecReferenceType); | ||
| 22 | std::string ts_ptypeToStr(PType type); | ||
| 23 | std::string ts_pveccomposeTypeToStr(const std::vector<PComposeType> & vecComposeType); | ||
| 24 | std::string ts_pcomposeTypeToStr(const PComposeType & composeType); | ||
| 25 | std::string ts_pcomposeVarToStr(const PComposeVar & composeVar); | ||
| 26 | std::string ts_protoTypeToStr(const PFunctionPrototype & prototype); | ||
| 27 | std::string ts_pfunctioncallToStr(const PFunctionCall & functionCall); | ||
| 28 | |||
| 29 | |||
| 30 | ///@brief Generic DataStream class | ||
| 31 | template<typename T> | ||
| 32 | struct TypeStream{ | ||
| 33 | ///Get the PComposeType of a generic type | ||
| 34 | /** @return PComposeType of a std::vector | ||
| 35 | */ | ||
| 36 | 164 | static PComposeType getType(){ | |
| 37 | 164 | PComposeType tmpType; | |
| 38 |
2/2✓ Branch 1 taken 112 times.
✓ Branch 4 taken 112 times.
|
164 | tmpType.setType(ts_getType<T>()); |
| 39 | 164 | return tmpType; | |
| 40 | } | ||
| 41 | ///Convert the generic type into a string | ||
| 42 | /** @return corresponding string | ||
| 43 | */ | ||
| 44 | 14 | static std::string getTypeToStr(){ | |
| 45 | 14 | return ts_getTypeToStr<T>(); | |
| 46 | } | ||
| 47 | }; | ||
| 48 | |||
| 49 | ///@brief Generic std::vector type abstraction | ||
| 50 | template<typename T> | ||
| 51 | struct TypeStream<std::vector<T> >{ | ||
| 52 | ///Get the PComposeType of a std::vector | ||
| 53 | /** @return PComposeType of a std::vector | ||
| 54 | */ | ||
| 55 | 1 | static PComposeType getType(){ | |
| 56 | 1 | PComposeType tmpType; | |
| 57 | //The currnet type if a std::vector | ||
| 58 |
1/1✓ Branch 1 taken 1 times.
|
1 | tmpType.setType(TYPE_STREAM_VECTOR); |
| 59 | //Let's get the sub type inside the vector | ||
| 60 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | tmpType.getVecType().push_back(TypeStream<T>::getType()); |
| 61 | 1 | return tmpType; | |
| 62 | } | ||
| 63 | ///Convert the std::vector< T > type into a string | ||
| 64 | /** @return corresponding string | ||
| 65 | */ | ||
| 66 | 1 | static std::string getTypeToStr(){ | |
| 67 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | return "std::vector<" + TypeStream<T>::getTypeToStr() + " >"; |
| 68 | } | ||
| 69 | }; | ||
| 70 | |||
| 71 | ///@brief Generic std::list type abstraction | ||
| 72 | template<typename T> | ||
| 73 | struct TypeStream<std::list<T> >{ | ||
| 74 | ///Get the PComposeType of a std::list | ||
| 75 | /** @return PComposeType of a std::list | ||
| 76 | */ | ||
| 77 | 1 | static PComposeType getType(){ | |
| 78 | 1 | PComposeType tmpType; | |
| 79 | //The currnet type if a std::list | ||
| 80 |
1/1✓ Branch 1 taken 1 times.
|
1 | tmpType.setType(TYPE_STREAM_LIST); |
| 81 | //Let's get the sub type inside the list | ||
| 82 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | tmpType.getVecType().push_back(TypeStream<T>::getType()); |
| 83 | 1 | return tmpType; | |
| 84 | } | ||
| 85 | ///Convert the std::list< T > type into a string | ||
| 86 | /** @return corresponding string | ||
| 87 | */ | ||
| 88 | 1 | static std::string getTypeToStr(){ | |
| 89 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | return "std::list<" + TypeStream<T>::getTypeToStr() + " >"; |
| 90 | } | ||
| 91 | }; | ||
| 92 | |||
| 93 | ///@brief Generic std::list type abstraction | ||
| 94 | template<typename T, typename U> | ||
| 95 | struct TypeStream<std::map<T, U> >{ | ||
| 96 | ///Get the PComposeType of a std::list | ||
| 97 | /** @return PComposeType of a std::list | ||
| 98 | */ | ||
| 99 | 1 | static PComposeType getType(){ | |
| 100 | 1 | PComposeType tmpType; | |
| 101 | //The currnet type if a std::list | ||
| 102 |
1/1✓ Branch 1 taken 1 times.
|
1 | tmpType.setType(TYPE_STREAM_MAP); |
| 103 | //Let's get the sub type inside the list | ||
| 104 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | tmpType.getVecType().push_back(TypeStream<T>::getType()); |
| 105 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | tmpType.getVecType().push_back(TypeStream<U>::getType()); |
| 106 | 1 | return tmpType; | |
| 107 | } | ||
| 108 | ///Convert the std::list< T > type into a string | ||
| 109 | /** @return corresponding string | ||
| 110 | */ | ||
| 111 | 2 | static std::string getTypeToStr(){ | |
| 112 |
5/5✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 14 taken 2 times.
|
2 | return "std::map<" + TypeStream<T>::getTypeToStr() + ", " + TypeStream<U>::getTypeToStr() + " >"; |
| 113 | } | ||
| 114 | }; | ||
| 115 | |||
| 116 | template<typename T> | ||
| 117 | std::string phoenix_getTypeToStr(); | ||
| 118 | template<typename T> | ||
| 119 | std::string phoenix_getTypeToStr(const T & data); | ||
| 120 | |||
| 121 | template<typename T> | ||
| 122 | PComposeType phoenix_getComposeType(); | ||
| 123 | template<typename T> | ||
| 124 | PComposeType phoenix_getComposeType(const T & data); | ||
| 125 | |||
| 126 | template<typename T> | ||
| 127 | PComposeVar phoenix_getComposeVar(const std::string & name); | ||
| 128 | |||
| 129 | ///@brief Serialise a call type into a PComposeType | ||
| 130 | template <int First, int Last> | ||
| 131 | struct PSerialiseCallType{ | ||
| 132 | ///Create the corresponding vector of PComposeType to a function call | ||
| 133 | /** @param[out] vecType : vector of PComposeType of the function call | ||
| 134 | * @param tupleCall : called tuple of function parameters | ||
| 135 | * @return true on success, false otherwise | ||
| 136 | */ | ||
| 137 | template <typename T> | ||
| 138 | 86 | static bool makeType(std::vector<PComposeType> & vecType, const T & tupleCall){ | |
| 139 | 86 | bool b(true); | |
| 140 | if(First < Last){ | ||
| 141 | 86 | auto value = std::get<First>(tupleCall); | |
| 142 |
2/2✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
|
86 | vecType.push_back(phoenix_getComposeType(value)); |
| 143 | 86 | b &= PSerialiseCallType<First+1, Last>::makeType(vecType, tupleCall); | |
| 144 | } | ||
| 145 | 86 | return b; | |
| 146 | } | ||
| 147 | ///Create the corresponding vector of serialised parameters to a function call | ||
| 148 | /** @param[out] vecParam : vector of serialised parameters of the function call | ||
| 149 | * @param tupleCall : called tuple of function parameters | ||
| 150 | * @return true on success, false otherwise | ||
| 151 | */ | ||
| 152 | template <typename T> | ||
| 153 | 80 | static bool makeParam(std::vector<std::vector<char> > & vecParam, const T & tupleCall){ | |
| 154 | 80 | bool b(true); | |
| 155 | if(First < Last){ | ||
| 156 | 80 | auto value = std::get<First>(tupleCall); | |
| 157 |
1/1✓ Branch 1 taken 40 times.
|
80 | size_t dataSize(data_size(value)); |
| 158 | 80 | std::vector<char> vecTmp; | |
| 159 |
1/1✓ Branch 1 taken 40 times.
|
80 | vecTmp.resize(dataSize); |
| 160 | 80 | char* iter = (char*)vecTmp.data(); | |
| 161 |
2/3✓ Branch 1 taken 40 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
|
80 | if(data_message_save(iter, value)){ |
| 162 |
1/1✓ Branch 1 taken 40 times.
|
80 | vecParam.push_back(vecTmp); |
| 163 | 80 | b &= PSerialiseCallType<First+1, Last>::makeParam(vecParam, tupleCall); | |
| 164 | }else{ | ||
| 165 | ✗ | std::cerr << "makeParam<"<<First<<", "<<Last<<"> : cannot serialise value '" << value <<"' of type '"<<phoenix_getTypeToStr(value)<<"' in message" << std::endl; | |
| 166 | ✗ | b = false; | |
| 167 | } | ||
| 168 | 80 | } | |
| 169 | 80 | return b; | |
| 170 | } | ||
| 171 | }; | ||
| 172 | |||
| 173 | ///@brief Serialise a call type into a PComposeType | ||
| 174 | template <int N> | ||
| 175 | struct PSerialiseCallType<N, N>{ | ||
| 176 | ///End function which creates the corresponding vector of PComposeType to a function call | ||
| 177 | /** @param[out] vecType : vector of PComposeType of the function call | ||
| 178 | * @param tupleCall : called tuple of function parameters | ||
| 179 | * @return true on success, false otherwise | ||
| 180 | */ | ||
| 181 | template <typename T> | ||
| 182 | 86 | static bool makeType(std::vector<PComposeType> & vecType, const T & tupleCall){return true;} | |
| 183 | ///End function which creates the corresponding vector of serialised parameters to a function call | ||
| 184 | /** @param[out] vecParam : vector of serialised parameters of the function call | ||
| 185 | * @param tupleCall : called tuple of function parameters | ||
| 186 | * @return true on success, false otherwise | ||
| 187 | */ | ||
| 188 | template <typename T> | ||
| 189 | 80 | static bool makeParam(std::vector<std::vector<char> > & vecParam, const T & tupleCall){return true;} | |
| 190 | }; | ||
| 191 | |||
| 192 | |||
| 193 | ///Get the corresponding prototype of a function | ||
| 194 | /** @param functionName : name of the function | ||
| 195 | * @param _ : the given function | ||
| 196 | * @return corresponding PFunctionPrototype | ||
| 197 | */ | ||
| 198 | template <typename R, typename... T> | ||
| 199 | 3 | PFunctionPrototype phoenix_getPrototype(const std::string & functionName, R (*_)(T...)){ | |
| 200 |
1/1✓ Branch 1 taken 3 times.
|
3 | PFunctionPrototype prototype; |
| 201 |
1/1✓ Branch 1 taken 3 times.
|
3 | prototype.setName(functionName); |
| 202 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | prototype.getVecReturnType().push_back(phoenix_getComposeType<R>()); |
| 203 | 3 | std::tuple<T...> args = std::tuple<T...>(); | |
| 204 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | PSerialiseCallType<0, std::tuple_size<std::tuple<T... > >{} >::makeType(prototype.getVecParam(), args); |
| 205 | 6 | return prototype; | |
| 206 | } | ||
| 207 | |||
| 208 | ///Serialise parameters call | ||
| 209 | /** @param[out] call : function call to be updated | ||
| 210 | * @param[out] functionName : name of the function | ||
| 211 | * @param __args : arguments | ||
| 212 | */ | ||
| 213 | template<typename... _Args> | ||
| 214 | 20 | void phoenix_serialiseCallParam(PFunctionCall & call, const std::string & functionName, _Args&... __args){ | |
| 215 |
1/1✓ Branch 1 taken 20 times.
|
20 | std::tuple<_Args... > vecCallParam = std::make_tuple(__args...); |
| 216 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
|
20 | call.getPrototype().setName(functionName); |
| 217 |
3/3✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
|
20 | PSerialiseCallType<0, std::tuple_size<std::tuple<_Args... > >{} >::makeType(call.getPrototype().getVecParam(), vecCallParam); |
| 218 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
|
20 | PSerialiseCallType<0, std::tuple_size<std::tuple<_Args... > >{} >::makeParam(call.getVecParam(), vecCallParam); |
| 219 | 20 | } | |
| 220 | |||
| 221 | |||
| 222 | #include "phoenix_composeType_impl.h" | ||
| 223 | |||
| 224 | #endif | ||
| 225 |