1 /*
2  * Copyright (c) 2011-2014, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #pragma once
31 
32 #include "SubsystemObject.h"
33 
34 class CFormattedSubsystemObject : public CSubsystemObject
35 {
36 public:
37     /**
38      * Builds a new CFormattedSubsystemObject instance, without any mapping information.
39      *
40      * @param[in] pInstanceConfigurableElement Instance of the element linked to the SubsytemObject.
41      */
42     CFormattedSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement);
43 
44     /**
45      * Builds a new CFormattedSubsystemObject instance, using a simple mapping value without Amends.
46      *
47      * @param[in] pInstanceConfigurableElement Instance of the element linked to the SubsytemObject.
48      * @param[in] strFormattedMapping A std::string corresponding to the mapping of the element. The
49      * std::string does not contain any Amend (%) and does not need to be formatted.
50      */
51     CFormattedSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement,
52                               const std::string& strFormattedMapping);
53 
54     /**
55      * Builds a new CFormattedSubsystemObject instance, using a mapping value containing Amends.
56      *
57      * @param[in] pInstanceConfigurableElement Instance of the element linked to the SubsytemObject.
58      * @param[in] strMappingValue A std::string corresponding to the mapping of the element. The
59      * std::string contains Amend (%) and needs to be formatted with information from the context.
60      * @param[in] uiFirstAmendKey Index of the first Amend key
61      * @param[in] uiNbAmendKeys Number of Amends
62      * @param[in] context Contains values associated to Amend keys
63      */
64     CFormattedSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement,
65                               const std::string& strMappingValue,
66                               uint32_t uiFirstAmendKey,
67                               uint32_t uiNbAmendKeys,
68                               const CMappingContext& context);
69     virtual ~CFormattedSubsystemObject();
70 
71     /**
72      * Returns the formatted mapping value associated to the element.
73      *
74      * @return A std::string containing the mapping
75      */
76     virtual std::string getFormattedMappingValue() const;
77 
78 private:
79 
80     /**
81      * Check if the index of Amend key is valid.
82      *
83      * @param uiAmendKey Index of the Amend key
84      *
85      * @return true if the index of the Amend key is > 0 and <= 9.
86      */
87     static bool isAmendKeyValid(uint32_t uiAmendKey);
88 
89     /**
90      * Generic mapping formatting
91      *
92      * Format a std::string from mapping data and its context, replacing amendments by their value
93      *
94      * @param[in] strMappingValue The input mapping std::string containing amendments
95      * @param[in] context uiFirstAmendKey The index of the first Amend key in the key list of the
96      * context
97      * @param[in] uiNbAmendKeys Number of Amend keys in the context
98      * @param[in] context The context containing Amend values
99      *
100      * @return The formatted std::string, corresponding to the input strMappingValue where %n have been
101      * replaced by their value
102      */
103     static std::string formatMappingValue(const std::string& strMappingValue,
104                                      uint32_t uiFirstAmendKey,
105                                      uint32_t uiNbAmendKeys,
106                                      const CMappingContext& context);
107 
108     /**
109      * std::string containing the formatted mapping value
110      */
111     std::string _strFormattedMappingValue;
112 };
113