1 #ifndef ENUMTYPE_HPP
2 #define ENUMTYPE_HPP
3 
4 /*  $Id: enumtype.hpp 546704 2017-09-20 18:15:56Z gouriano $
5 * ===========================================================================
6 *
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's official duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 *   Enumerated type definition
33 *
34 */
35 
36 #include "type.hpp"
37 #include <list>
38 
39 BEGIN_NCBI_SCOPE
40 
41 class CEnumDataTypeValue
42 {
43 public:
CEnumDataTypeValue(const string & name,TEnumValueType value)44     CEnumDataTypeValue(const string& name, TEnumValueType value)
45         : m_SourceLine(0), m_Name(name), m_Value(value), m_Flags(eNone), m_IdName(name)
46         {
47         }
48 
GetName(void) const49     const string& GetName(void) const
50         {
51             return m_Name;
52         }
GetValue(void) const53     TEnumValueType GetValue(void) const
54         {
55             return m_Value;
56         }
57 
GetComments(void)58     CComments& GetComments(void)
59         {
60             return m_Comments;
61         }
GetComments(void) const62     const CComments& GetComments(void) const
63         {
64             return m_Comments;
65         }
SetSourceLine(int line)66     void SetSourceLine(int line)
67         {
68             m_SourceLine = line;
69         }
GetSourceLine(void) const70     int GetSourceLine(void) const
71         {
72             return m_SourceLine;
73         }
74 
GetEnumId(void) const75     const string& GetEnumId(void) const {
76         return m_IdName;
77     }
SetEnumId(const string & id) const78     void SetEnumId(const string& id) const {
79         m_IdName = id;
80     }
81 
82     enum EValueFlags {
83         eNone        = 0,
84         eHideName = 1
85     };
86     typedef unsigned int TValueFlags;
87 
SetFlag(EValueFlags flag) const88     void SetFlag(EValueFlags flag) const {
89         m_Flags |= flag;
90     }
GetFlags(void) const91     TValueFlags GetFlags(void) const {
92         return m_Flags;
93     }
94 private:
95     int m_SourceLine;
96     string m_Name;
97     TEnumValueType m_Value;
98     CComments m_Comments;
99     mutable TValueFlags m_Flags;
100     mutable string m_IdName;
101 };
102 
103 class CEnumDataType : public CDataType
104 {
105     typedef CDataType CParent;
106 public:
107     typedef CEnumDataTypeValue TValue;
108     typedef list<TValue> TValues;
109 
110     CEnumDataType(void);
111     virtual bool IsInteger(void) const;
112     virtual const char* GetASNKeyword(void) const override;
113     virtual const char* GetDEFKeyword(void) const override;
114     virtual string GetXMLContents(void) const;
115 
116     TValue& AddValue(const string& name, TEnumValueType value);
GetValues(void) const117     const TValues& GetValues(void) const
118         {
119             return m_Values;
120         }
121 
122     virtual void PrintASN(CNcbiOstream& out, int indent) const override;
123     virtual void PrintSpecDumpExtra(CNcbiOstream& out, int indent) const override;
124     virtual void PrintJSONSchema(CNcbiOstream& out, int indent, list<string>& required, bool contents_only=false) const override;
125     virtual void PrintXMLSchema(CNcbiOstream& out, int indent, bool contents_only=false) const override;
126     virtual void PrintDTDElement(CNcbiOstream& out, bool contents_only=false) const override;
127     virtual void PrintDTDExtra(CNcbiOstream& out) const override;
128 
129     virtual bool CheckValue(const CDataValue& value) const override;
130     virtual TObjectPtr CreateDefault(const CDataValue& value) const override;
131     virtual string GetDefaultString(const CDataValue& value) const override;
132     virtual string GetXmlValueName(const string& value) const;
133 
134     struct SEnumCInfo {
135         string enumName;
136         string cType;
137         string valuePrefix;
138 
SEnumCInfoCEnumDataType::SEnumCInfo139         SEnumCInfo(const string& name, const string& type,
140                    const string& prefix)
141             : enumName(name), cType(type), valuePrefix(prefix)
142             {
143             }
144     };
145 
146     string DefaultEnumName(void) const;
147     SEnumCInfo GetEnumCInfo(void) const;
148 
LastComments(void)149     CComments& LastComments(void)
150         {
151             return m_LastComments;
152         }
153 
154 public:
155 
156     virtual CTypeInfo* CreateTypeInfo(void) override;
157     virtual AutoPtr<CTypeStrings> GetRefCType(void) const override;
158     virtual AutoPtr<CTypeStrings> GetFullCType(void) const override;
159     virtual AutoPtr<CTypeStrings> GenerateCode(void) const override;
160 
161 private:
162     TValues m_Values;
163     CComments m_LastComments;
164 };
165 
166 class CIntEnumDataType : public CEnumDataType {
167     typedef CEnumDataType CParent;
168 public:
169     virtual bool IsInteger(void) const override;
170     virtual const char* GetASNKeyword(void) const override;
171     virtual const char* GetDEFKeyword(void) const override;
172     virtual string GetXmlValueName(const string& value) const override;
173 };
174 
175 class CBigIntEnumDataType : public CIntEnumDataType {
176     typedef CIntEnumDataType CParent;
177 public:
178     virtual const char* GetASNKeyword(void) const override;
179     virtual const char* GetDEFKeyword(void) const override;
180 };
181 
182 END_NCBI_SCOPE
183 
184 #endif
185