eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
UnionType.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _EMBEDDED_RPC__UNIONTYPE_H_
37 #define _EMBEDDED_RPC__UNIONTYPE_H_
38 
39 #include "DataType.h"
40 #include "StructType.h"
41 #include "UnionCase.h"
42 #include <map>
43 
45 // Classes
47 
48 namespace erpcgen {
49 
53 class UnionType : public DataType
54 {
55 public:
56  typedef std::vector<UnionCase *> case_vector_t;
67  UnionType(const std::string &name, const std::string &discriminatorName)
68  : DataType(name, kUnionType, kUnionTypeSymbol)
69  , m_discriminator(discriminatorName)
70  , m_members("(union)")
71  , m_parentStruct(nullptr)
72  {
73  }
74 
84  UnionType(const Token &tok, const std::string &discriminatorName)
85  : DataType(tok, kUnionType, kUnionTypeSymbol)
86  , m_discriminator(discriminatorName)
87  , m_members("(union)")
88  , m_parentStruct(nullptr)
89  {
90  }
91 
95  StructType *getParentStruct() { return m_parentStruct; }
96 
100  void setParentStruct(StructType *parent) { m_parentStruct = parent; }
101 
107  void addCase(UnionCase *unionCase);
108 
122  virtual std::string getDescription() const;
123 
129  virtual bool isUnion() const { return true; }
130 
137  bool isNonEncapsulatedUnion() const { return m_discriminator.compare("") == 0; }
138 
144  case_vector_t &getCases() { return m_unionCases; }
145 
151  case_vector_t getUniqueCases();
152 
159  bool addUnionMemberDeclaration(const std::string &name, DataType *dataType);
160 
166  StructType &getUnionMembers() { return m_members; }
167 
175  StructMember *getUnionMemberDeclaration(const std::string &name);
176 
182  std::string getDiscriminatorName() { return m_discriminator; }
183 
187  void printUnionMembers();
188 
197  bool declarationExists(const std::string &name);
198 
199 private:
200  std::string m_discriminator;
201  case_vector_t m_unionCases;
202  StructType m_members;
203  StructType *m_parentStruct;
214  bool casesAreTheSame(UnionCase *a, UnionCase *b);
215 };
216 
217 } // namespace erpcgen
218 
219 #endif // _EMBEDDED_RPC__UNIONTYPE_H_
case_vector_t getUniqueCases()
This function returns unique union cases vector.
Definition: Type.cpp:618
virtual bool isUnion() const
This function return "true" value for identify union type.
Definition: UnionType.h:129
Member of a struct.
Definition: StructMember.h:63
void printUnionMembers()
This function prints information about union members.
Definition: Type.cpp:693
std::vector< UnionCase * > case_vector_t
Definition: UnionType.h:56
StructMember * getUnionMemberDeclaration(const std::string &name)
This function returns union member declaration.
Definition: Type.cpp:681
Discriminated union case type.
Definition: UnionCase.h:52
Structure data type.
Definition: StructType.h:54
void setParentStruct(StructType *parent)
Sets the struct owning this union.
Definition: UnionType.h:100
void addCase(UnionCase *unionCase)
This function will add union case to current union.
Definition: Type.cpp:602
virtual std::string getDescription() const
This function returns description about the union.
Definition: Type.cpp:608
StructType & getUnionMembers()
This function returns struct of union members.
Definition: UnionType.h:166
UnionType(const Token &tok, const std::string &discriminatorName)
Constructor.
Definition: UnionType.h:84
std::string getDiscriminatorName()
This function returns union discriminator name.
Definition: UnionType.h:182
Base class for data types.
Definition: DataType.h:51
Encapsulates all information about a token.
Definition: Token.h:86
UnionType(const std::string &name, const std::string &discriminatorName)
Constructor.
Definition: UnionType.h:67
bool isNonEncapsulatedUnion() const
This function return "true" value for identify non-encapsulated discriminated union type...
Definition: UnionType.h:137
Discriminated union data type.
Definition: UnionType.h:53
StructType * getParentStruct()
Returns the struct owning this union.
Definition: UnionType.h:95
bool addUnionMemberDeclaration(const std::string &name, DataType *dataType)
This function adds unique union member declaration.
Definition: Type.cpp:658
case_vector_t & getCases()
This function returns union cases vector.
Definition: UnionType.h:144
Definition: AstNode.h:51
bool declarationExists(const std::string &name)
This function is looking for member declaration.
Definition: Type.cpp:669