NCBI C++ ToolKit
enumstr.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: enumstr.cpp 67925 2015-06-25 12:36:15Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * Type info for class generation: includes, used classes, C code etc.
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include "enumstr.hpp"
34 #include "classctx.hpp"
35 #include "srcutil.hpp"
36 #include "enumtype.hpp"
37 #include "code.hpp"
38 #include <corelib/ncbiutil.hpp>
39 
41 
42 CEnumTypeStrings::CEnumTypeStrings(const string& externalName,
43  const string& enumName,
44  const string& packedType,
45  const string& cType, bool isInteger,
46  const TValues& values,
47  const string& valuePrefix,
48  const string& namespaceName,
49  const CDataType* dataType,
50  const CComments& comments)
51  : CParent(namespaceName, dataType, comments),
52  m_ExternalName(externalName), m_EnumName(enumName),
53  m_PackedType(packedType),
54  m_CType(cType), m_IsInteger(isInteger), m_IsBitset(false),
55  m_Values(values), m_ValuesPrefix(valuePrefix)
56 {
57 }
58 
60 {
61  return eKindEnum;
62 }
63 
64 const string& CEnumTypeStrings::GetEnumName(void) const
65 {
66  return m_EnumName;
67 }
68 
69 string CEnumTypeStrings::GetCType(const CNamespace& /*ns*/) const
70 {
71  return m_CType;
72 }
73 
75  const string& methodPrefix) const
76 {
77  string s;
78  if (!m_IsInteger) {
79  s += methodPrefix;
80  }
81  return s + GetCType(ns);
82 }
83 
84 string CEnumTypeStrings::GetRef(const CNamespace& /*ns*/) const
85 {
86  return "ENUM, ("+m_CType+", "+m_EnumName+')';
87 }
88 
90 {
91  return "(" + m_EnumName + ")(0)";
92 }
93 
95 {
96  string methodPrefix = ctx.GetMethodPrefix();
97  bool inClass = !methodPrefix.empty();
98  {
99  // alignments
100  size_t maxlen = 0, maxwidth=0;
101  ITERATE ( TValues, i, m_Values ) {
102  maxlen = max(maxlen,i->GetName().size());
103  size_t w = 0;
104  TEnumValueType val = i->GetValue();
105  if (val < 0) {
106  ++w; val = -val;
107  }
108  while (val > 0) {
109  ++w; val /= 10;
110  }
111  maxwidth = max(maxwidth,w);
112  }
113  // generated enum
114  CNcbiOstrstream hpp;
115  PrintHPPComments(hpp);
116  hpp << "enum " << m_EnumName;
117  if (!m_PackedType.empty()) {
118  hpp << " NCBI_PACKED_ENUM_TYPE( " << m_PackedType << " )";
119  }
120  hpp << " {";
121  ITERATE ( TValues, i, m_Values ) {
122  string id = Identifier( i->GetEnumId(), false );
123  hpp << "\n " << m_ValuesPrefix << id;
124  hpp << string( maxlen - i->GetName().size(),' ' ) << " = ";
125  hpp.width( maxwidth );
126  hpp << i->GetValue();
128  if ( ++next != m_Values.end() ) {
129  hpp.put(',');
130  }
131  i->GetComments().PrintHPPEnum(hpp);
132  }
133  hpp << "\n}";
134  if (!m_PackedType.empty()) {
135  hpp << " NCBI_PACKED_ENUM_END()";
136  }
137  hpp << ";\n\n";
138  // prototype of GetTypeInfo_enum_* function
139 
140 #if 0
141  if ( inClass )
142  hpp << "DECLARE_INTERNAL_ENUM_INFO";
143  else
144  hpp << CClassCode::GetExportSpecifier() << " DECLARE_ENUM_INFO";
145  hpp << '('<<m_EnumName<<");\n\n";
146 #else
147  hpp << "/// Access to " << m_EnumName
148  << "'s attributes (values, names) as defined in spec\n";
149  if ( inClass ) {
150  hpp << "static";
151  } else {
153  }
154  hpp << " const NCBI_NS_NCBI::CEnumeratedTypeValues* ENUM_METHOD_NAME";
155  hpp << '('<<m_EnumName<<")(void);\n\n";
156 #endif
157  ctx.AddHPPCode(hpp);
158  }
159  {
160  // definition of GetTypeInfo_enum_ function
161  CNcbiOstrstream cpp;
162  if ( methodPrefix.empty() ) {
163  cpp <<
164  "BEGIN_NAMED_ENUM_INFO(\""<<GetExternalName()<<'\"';
165  }
166  else {
167  cpp <<
168  "BEGIN_NAMED_ENUM_IN_INFO(\""<<GetExternalName()<<"\", "<<
169  methodPrefix;
170  }
171  cpp <<", "<<m_EnumName<<", "<<(m_IsInteger?"true":"false")<<")\n"
172  "{\n";
174  string module_name = GetModuleName(&names);
175  if ( GetExternalName().empty() && !names.m_OwnerName.empty() ) {
176  cpp <<
177  " SET_ENUM_INTERNAL_NAME(\""<<names.m_OwnerName<<"\", ";
178  if ( !names.m_MemberName.empty() )
179  cpp << "\""<<names.m_MemberName<<"\"";
180  else
181  cpp << "0";
182  cpp << ");\n";
183  }
184  if ( !module_name.empty() ) {
185  cpp <<
186  " SET_ENUM_MODULE(\""<<module_name<<"\");\n";
187  }
188  if (IsBitset()) {
189  cpp <<
190  " SET_ENUM_BITSET;\n";
191  }
192  ITERATE ( TValues, i, m_Values ) {
193  string id = Identifier(i->GetEnumId(), false);
195  if (flags) {
196  cpp <<
197  " ADD_ENUM_VALUE_FLAGS(\""<<i->GetName()<<"\", "<<m_ValuesPrefix<<id<<", "<<flags<<");\n";
198  } else {
199  cpp <<
200  " ADD_ENUM_VALUE(\""<<i->GetName()<<"\", "<<m_ValuesPrefix<<id<<");\n";
201  }
202  }
203  cpp <<
204  "}\n"
205  "END_ENUM_INFO\n"
206  "\n";
207  ctx.AddCPPCode(cpp);
208  }
209 }
210 
212  const string& cType,
213  const CNamespace& ns,
214  const string& fileName,
215  const CComments& comments)
216  : CParent(comments),
217  m_EnumName(enumName),
218  m_CType(cType), m_Namespace(ns),
219  m_FileName(fileName)
220 {
221 }
222 
224 {
225  return eKindEnum;
226 }
227 
229 {
230  return m_Namespace;
231 }
232 
233 const string& CEnumRefTypeStrings::GetEnumName(void) const
234 {
235  return m_EnumName;
236 }
237 
239 {
240  if ( !m_CType.empty() && m_CType != m_EnumName )
241  return m_CType;
242 
244 }
245 
247  const string& /*methodPrefix*/) const
248 {
249  return GetCType(ns);
250 }
251 
253 {
254  string ref = "ENUM";
255  bool haveNamespace = !m_Namespace.IsEmpty() && m_Namespace != ns;
256  if ( haveNamespace )
257  ref += "_IN";
258  ref += ", (";
259  if ( m_CType.empty() )
260  ref += m_EnumName;
261  else
262  ref += m_CType;
263  if ( haveNamespace ) {
264  ref += ", ";
265  ref += m_Namespace.ToString();
266  }
267  return ref+", "+m_EnumName+')';
268 }
269 
271 {
272  return "(" + GetCType(CNamespace::KEmptyNamespace) + ")(0)";
273 }
274 
276 {
277  ctx.HPPIncludes().insert(m_FileName);
278 }
279 
static const string & GetExportSpecifier(void)
Definition: code.cpp:88
unsigned int TValueFlags
Definition: enumtype.hpp:86
virtual void GenerateTypeCode(CClassContext &ctx) const override
Definition: enumstr.cpp:275
virtual string GetInitializer(void) const override
Definition: enumstr.cpp:270
virtual const CNamespace & GetNamespace(void) const override
Definition: enumstr.cpp:228
virtual string GetCType(const CNamespace &ns) const override
Definition: enumstr.cpp:238
CEnumRefTypeStrings(const string &enumName, const string &cName, const CNamespace &ns, const string &fileName, const CComments &comments)
Definition: enumstr.cpp:211
virtual const string & GetEnumName(void) const override
Definition: enumstr.cpp:233
virtual string GetRef(const CNamespace &ns) const override
Definition: enumstr.cpp:252
virtual string GetPrefixedCType(const CNamespace &ns, const string &methodPrefix) const override
Definition: enumstr.cpp:246
CNamespace m_Namespace
Definition: enumstr.hpp:117
virtual EKind GetKind(void) const override
Definition: enumstr.cpp:223
virtual string GetPrefixedCType(const CNamespace &ns, const string &methodPrefix) const override
Definition: enumstr.cpp:74
string m_PackedType
Definition: enumstr.hpp:84
string m_EnumName
Definition: enumstr.hpp:83
CEnumTypeStrings(const string &externalName, const string &enumName, const string &packedType, const string &cType, bool isInteger, const TValues &values, const string &valuesPrefix, const string &namespaceName, const CDataType *dataType, const CComments &comments)
Definition: enumstr.cpp:42
bool IsBitset(void) const
Definition: enumstr.hpp:78
virtual string GetRef(const CNamespace &ns) const override
Definition: enumstr.cpp:84
virtual void GenerateTypeCode(CClassContext &ctx) const override
Definition: enumstr.cpp:94
list< CEnumDataTypeValue > TValues
Definition: enumstr.hpp:49
virtual string GetCType(const CNamespace &ns) const override
Definition: enumstr.cpp:69
const TValues & m_Values
Definition: enumstr.hpp:88
const string & GetExternalName(void) const
Definition: enumstr.hpp:57
virtual string GetInitializer(void) const override
Definition: enumstr.cpp:89
virtual const string & GetEnumName(void) const override
Definition: enumstr.cpp:64
string m_CType
Definition: enumstr.hpp:85
virtual EKind GetKind(void) const override
Definition: enumstr.cpp:59
string m_ValuesPrefix
Definition: enumstr.hpp:89
string GetNamespaceRef(const CNamespace &ns) const
Definition: namespace.cpp:97
string ToString(void) const
Definition: namespace.hpp:80
static const CNamespace KEmptyNamespace
Definition: namespace.hpp:104
bool IsEmpty(void) const
Definition: namespace.hpp:87
void PrintHPPComments(CNcbiOstream &out) const
Definition: typestr.hpp:127
const string & GetModuleName(void) const
Definition: typestr.hpp:55
static uch flags
CS_CONTEXT * ctx
Definition: t0006.c:12
static const struct name_t names[]
#define false
Definition: bool.h:36
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string
Definition: cgiapp.hpp:687
int TEnumValueType
Definition: serialdef.hpp:232
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
constexpr bool empty(list< Ts... >) noexcept
Useful/utility classes and methods.
T max(T x_, T y_)
string Identifier(const string &typeName, bool capitalize)
Definition: srcutil.cpp:40
Modified on Sun May 19 04:46:26 2024 by modify_doxy.py rev. 669887