summaryrefslogtreecommitdiff
path: root/src/components/formatters/include/formatters/CSmartFactory.h
blob: 9fed89253b1b3fd8b95aa47aa32bbcd180a0b1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
 * @file CSmartFactory.hpp
 * @brief CSmartFactory header file.
 */
// Copyright (c) 2013, Ford Motor Company
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Ford Motor Company nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef SRC_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_CSMARTFACTORY_H_
#define SRC_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_CSMARTFACTORY_H_

#include "smart_objects/smart_object.h"
#include "smart_objects/smart_schema.h"
#include <map>
#include <string>

namespace NsSmartDeviceLink {
namespace NsJSONHandler {
/**
 * @brief String constants used by SmartFactory.
 */
namespace strings {
/**
 * @brief String constant for MSG_PARAMS.
 */
extern const std::string S_MSG_PARAMS;

/**
 * @brief String constant for PARAMS.
 */
extern const std::string S_PARAMS;

/**
 * @brief String constant for FUNCTION_ID.
 */
extern const std::string S_FUNCTION_ID;

/**
 * @brief String constant for MESSAGE_TYPE.
 */
extern const std::string S_MESSAGE_TYPE;

/**
 * @brief String constant for PROTOCOL_VERSION.
 */
extern const std::string S_PROTOCOL_VERSION;

/**
 * @brief String constant for PROTOCOL_TYPE.
 */
extern const std::string S_PROTOCOL_TYPE;

/**
 * @brief String constant for CORRELATION_ID.
 */
extern const std::string S_CORRELATION_ID;

/**
 * @brief String constant for "code" param name.
 */
extern const std::string kCode;

/**
 * @brief String constant for "message" param name.
 */
extern const std::string kMessage;
}

/**
 * @brief Smart Schema key.
 *
 * @tparam FunctionIdEnum Type of function ID enum.
 * @tparam MessageTypeEnum Type of messageType enum.
 */
template <class FunctionIdEnum, class MessageTypeEnum>
struct SmartSchemaKey {
  /**
   * @brief Value of function ID for the key.
   */
  FunctionIdEnum functionId;

  /**
   * @brief Value of messageType for the key.
   */
  MessageTypeEnum messageType;

  /**
   * @brief Constructor.
   *
   * @param functionIdParam Value of function ID.
   * @param messageTypeParam Value of message type.
   */
  SmartSchemaKey(FunctionIdEnum functionIdParam,
                 MessageTypeEnum messageTypeParam);
};

/**
 * @brief Smart Factory.
 *
 * This class is used as base class for generated factories.
 * Clients should use methods of this class to access all
 * SmartSchema validation features.
 *
 * @tparam FunctionIdEnum Type of function ID enum.
 * @tparam MessageTypeEnum Type of messageType enum.
 * @tparam StructIdEnum Type of StructId enum.
 */
template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
class CSmartFactory {
 public:
  /**
   * @brief Constructor.
   */
  CSmartFactory(void);

  /**
   * @brief Attach schema to the function SmartObject.
   *
   * @param object SmartObject to attach schema for.
   *
   * @param RemoveFakeParameters contains true if need
   * to remove fake parameters from smart object otherwise contains false.
   *
   * @return True if operation was successful or false otherwise.
   */
  bool attachSchema(NsSmartDeviceLink::NsSmartObjects::SmartObject& object,
                    const bool RemoveFakeParameters);

  /**
   * @brief Attach schema to the struct SmartObject.
   *
   * @param struct_id Identifier of the struct.
   * @param object SmartObject to attach schema for.
   *
   * @return True if operation was successful of false otherwise.
   */
  bool AttachSchema(const StructIdEnum struct_id,
                    NsSmartDeviceLink::NsSmartObjects::SmartObject& object);

  /**
   * @brief Create new SmartObject with attached function SmartSchema.
   *
   * @param function_id FunctionID of the function.
   * @param message_type messageType of the function.
   *
   * @return If function succeeded it returns new SmartObject with
   *         map type and attached SmartSchema. Client can use such
   *         object to store specific function and perform validation.
   *         Otherwise (if SmartSchema was not attached to the
   *         SmartObject) function returns empty SmartObject with
   *         null type.
   */
  NsSmartDeviceLink::NsSmartObjects::SmartObject CreateSmartObject(
      const FunctionIdEnum function_id, const MessageTypeEnum message_type);

  /**
   * @brief Create new SmartObject with attached struct SmartSchema.
   *
   * @param struct_id Identifier of the struct.
   *
   * @return If function succeeded it returns new SmartObject with
   *         map type and attached SmartSchema. Client can use such
   *         object to store specific struct and perform validation.
   *         Otherwise (if SmartSchema was not attached to the
   *         SmartObject) function returns empty SmartObject with
   *         null type.
   */
  NsSmartDeviceLink::NsSmartObjects::SmartObject CreateSmartObject(
      const StructIdEnum struct_id);

  /**
   * @brief Get SmartSchema for specific function.
   *
   * @param function_id FunctionID of the function.
   * @param message_type messageType of the function.
   * @param[out] result This value will be copy of the desired
   *                    function SmartSchema if it found (this
   *                    function returns true) or unmodified if
   *                    SmartSchema is not found (this function
   *                    returns false).
   *
   * @return True if function schema for specified input parameters
   *         is found or false otherwise.
   */
  bool GetSchema(const FunctionIdEnum function_id,
                 const MessageTypeEnum message_type,
                 NsSmartDeviceLink::NsSmartObjects::CSmartSchema& result);

  /**
   * @brief Get SmartSchema for specific struct.
   *
   * @param struct_id Identifier of the struct.
   *
   * @param[out] result This value will be copy of the desired
   *                    struct SmartSchema if it found (this
   *                    function returns true) or unmodified if
   *                    SmartSchema is not found (this function
   *                    returns false).
   *
   * @return True if struct schema for specified input parameter is
   *         found or false otherwise.
   */
  bool GetSchema(const StructIdEnum struct_id,
                 NsSmartDeviceLink::NsSmartObjects::CSmartSchema& result);

 protected:
  /**
   * @brief Defines map of SmartSchemaKeys to the SmartSchemes.
   *
   * This container type should be used to store SmartSchemes of
   * functions.
   */
  typedef std::map<SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>,
                   NsSmartDeviceLink::NsSmartObjects::CSmartSchema>
      FuncionsSchemesMap;

  /**
   * @brief Defines map of StructIdEnum to the SmartSchemes.
   *
   * This container type should be used to store SmartSchemes of
   * structs.
   */
  typedef std::map<StructIdEnum,
                   NsSmartDeviceLink::NsSmartObjects::CSmartSchema>
      StructsSchemesMap;

  /**
   * @brief Map of all function schemes for this factory.
   */
  FuncionsSchemesMap functions_schemes_;

  /**
   * @brief Map of all struct shemes for this factory.
   */
  StructsSchemesMap structs_schemes_;
};

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::CSmartFactory(
    void)
    : functions_schemes_(), structs_schemes_() {}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::attachSchema(
    NsSmartDeviceLink::NsSmartObjects::SmartObject& object,
    const bool RemoveFakeParameters) {
  if (false == object.keyExists(strings::S_PARAMS))
    return false;
  if (false == object[strings::S_PARAMS].keyExists(strings::S_MESSAGE_TYPE))
    return false;
  if (false == object[strings::S_PARAMS].keyExists(strings::S_FUNCTION_ID))
    return false;

  MessageTypeEnum msgtype(
      (MessageTypeEnum)
          object[strings::S_PARAMS][strings::S_MESSAGE_TYPE].asInt());
  FunctionIdEnum fid(
      (FunctionIdEnum)
          object[strings::S_PARAMS][strings::S_FUNCTION_ID].asInt());

  SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(fid, msgtype);

  typename FuncionsSchemesMap::iterator schemaIterator =
      functions_schemes_.find(key);

  if (schemaIterator == functions_schemes_.end()) {
    // Schema was not found
    return false;
  }

  object.setSchema(schemaIterator->second);
  schemaIterator->second.applySchema(object, RemoveFakeParameters);

  return true;
}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::AttachSchema(
    const StructIdEnum struct_id,
    NsSmartDeviceLink::NsSmartObjects::SmartObject& object) {
  typename StructsSchemesMap::iterator structs_iterator =
      structs_schemes_.find(struct_id);

  if (structs_iterator == structs_schemes_.end()) {
    return false;
  }

  object.setSchema(structs_iterator->second);
  structs_iterator->second.applySchema(object, false);

  return true;
}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
NsSmartDeviceLink::NsSmartObjects::SmartObject
CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::CreateSmartObject(
    const FunctionIdEnum function_id, const MessageTypeEnum message_type) {
  SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(function_id,
                                                      message_type);

  typename FuncionsSchemesMap::iterator schema_iterator =
      functions_schemes_.find(key);

  if (schema_iterator != functions_schemes_.end()) {
    NsSmartDeviceLink::NsSmartObjects::SmartObject function_object(
        NsSmartDeviceLink::NsSmartObjects::SmartType_Map);
    function_object.setSchema(schema_iterator->second);
    schema_iterator->second.applySchema(function_object, false);
    return function_object;
  }

  return NsSmartDeviceLink::NsSmartObjects::SmartObject();
}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
NsSmartDeviceLink::NsSmartObjects::SmartObject
CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::CreateSmartObject(
    const StructIdEnum struct_id) {
  NsSmartDeviceLink::NsSmartObjects::SmartObject struct_object(
      NsSmartDeviceLink::NsSmartObjects::SmartType_Map);
  if (AttachSchema(struct_id, struct_object)) {
    return struct_object;
  }

  return NsSmartDeviceLink::NsSmartObjects::SmartObject();
}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::GetSchema(
    const FunctionIdEnum function_id,
    const MessageTypeEnum message_type,
    NsSmartDeviceLink::NsSmartObjects::CSmartSchema& result) {
  SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(function_id,
                                                      message_type);

  typename FuncionsSchemesMap::iterator schema_iterator =
      functions_schemes_.find(key);

  if (schema_iterator != functions_schemes_.end()) {
    result = schema_iterator->second;
    return true;
  }

  return false;
}

template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum>
bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::GetSchema(
    const StructIdEnum struct_id,
    NsSmartDeviceLink::NsSmartObjects::CSmartSchema& result) {
  typename StructsSchemesMap::iterator structs_iterator =
      structs_schemes_.find(struct_id);

  if (structs_iterator != structs_schemes_.end()) {
    result = structs_iterator->second;
    return true;
  }

  return false;
}

template <class FunctionIdEnum, class MessageTypeEnum>
SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>::SmartSchemaKey(
    FunctionIdEnum functionIdParam, MessageTypeEnum messageTypeParam)
    : functionId(functionIdParam), messageType(messageTypeParam) {}

template <class FunctionIdEnum, class MessageTypeEnum>
bool operator<(const SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>& l,
               const SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>& r) {
  if (l.functionId < r.functionId)
    return true;
  if (l.functionId > r.functionId)
    return false;

  if (l.messageType < r.messageType)
    return true;
  if (l.messageType > r.messageType)
    return false;

  return false;
}
}
}
#endif  // SRC_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_CSMARTFACTORY_H_