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

Go to the SVN repository for this file.

1 /* $Id: aliasstr.cpp 73741 2016-07-21 17:25:37Z 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: Aleksey Grichenko
27 *
28 * File Description:
29 * Type info for aliased type generation: includes, used classes, C code etc.
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbiutil.hpp>
34 #include "datatool.hpp"
35 #include "exceptions.hpp"
36 #include "type.hpp"
37 #include "aliasstr.hpp"
38 #include "code.hpp"
39 #include "srcutil.hpp"
40 #include "classstr.hpp"
41 #include "enumstr.hpp"
42 #include <serial/serialdef.hpp>
43 
45 
46 CAliasTypeStrings::CAliasTypeStrings(const string& externalName,
47  const string& className,
48  CTypeStrings& ref_type,
49  const CComments& comments)
50  : TParent(comments),
51  m_ExternalName(externalName),
52  m_ClassName(className),
53  m_RefType(&ref_type),
54  m_FullAlias(false)
55 {
56 }
57 
59 {
60 }
61 
63 {
64  return eKindOther;
65 }
66 
68 {
69  return m_ClassName;
70 }
71 
73 {
74  return m_ExternalName;
75 }
76 
77 string CAliasTypeStrings::GetCType(const CNamespace& /*ns*/) const
78 {
79  return GetClassName();
80 }
81 
83  const string& /*methodPrefix*/) const
84 {
85  return GetCType(ns);
86 }
87 
89 {
90  return m_RefType->HaveSpecialRef();
91 }
92 
93 string CAliasTypeStrings::GetRef(const CNamespace& ns) const
94 {
95  return m_RefType->GetRef(ns);
96 }
97 
99 {
100  return m_RefType->CanBeKey();
101 }
102 
104 {
105  return m_RefType->CanBeCopied();
106 }
107 
109  const string& place) const
110 {
111  return m_RefType->NewInstance(init, place);
112 }
113 
115 {
116  return m_RefType->GetInitializer();
117 }
118 
119 string CAliasTypeStrings::GetDestructionCode(const string& expr) const
120 {
121  return m_RefType->GetDestructionCode(expr);
122 }
123 
124 string CAliasTypeStrings::GetIsSetCode(const string& var) const
125 {
126  return m_RefType->GetIsSetCode(var);
127 }
128 
129 string CAliasTypeStrings::GetResetCode(const string& var) const
130 {
131  return m_RefType->GetResetCode(var);
132 }
133 
134 string CAliasTypeStrings::GetDefaultCode(const string& var) const
135 {
136  // No value - return empty string
137  if ( var.empty() ) {
138  return NcbiEmptyString;
139  }
140  // Return var for classes, else cast var to the aliased type
141  return GetCType(GetNamespace()) + "(" + var + ")";
142 }
143 
145 {
146  const CNamespace& ns = ctx.GetNamespace();
147  string ref_name = m_RefType->GetCType(ns);
148  string className = GetClassName() + "_Base";
149  CClassCode code(ctx, className);
150  string methodPrefix = code.GetMethodPrefix();
151  bool is_class = false;
152  bool is_ref_to_alias = false;
153 
156  switch ( kind ) {
157  case eKindClass:
158  case eKindObject:
159  {
160  string name(ref_name);
161  const CClassRefTypeStrings* cls =
162  dynamic_cast<const CClassRefTypeStrings*>(m_RefType.get());
163  if (cls) {
164  name = cls->GetClassName();
165  }
166  code.SetParentClass(name, m_RefType->GetNamespace());
167  }
168  is_class = true;
169  is_ref_to_alias = (dynamic_cast<const CAliasRefTypeStrings*>(m_RefType.get()) != NULL);
170  break;
171  case eKindStd:
172  case eKindEnum:
173  code.SetParentClass("CStdAliasBase< " + ref_name + " >",
175  break;
176  case eKindString:
177  code.SetParentClass("CStringAliasBase< " + ref_name + " >",
179  break;
180  case eKindOther: // for vector< char >
181  code.SetParentClass("CStringAliasBase< " + ref_name + " >",
183  break;
184  case eKindPointer:
185  case eKindRef:
186  case eKindContainer:
187  NCBI_THROW(CDatatoolException, eNotImplemented,
188  "Invalid aliased type: " + ref_name);
189  }
190 
191  string parentNamespaceRef =
193  // generate type info
194  code.ClassPublic() <<
195  " " << className << "(void);\n" <<
196  "\n";
197  code.MethodStart(true) <<
198  methodPrefix << className << "(void)\n" <<
199  "{\n" <<
200  "}\n" <<
201  "\n";
203  if ( is_class ) {
204  if (is_ref_to_alias) {
205  code.ClassPublic() <<
206  " // type info\n"
207  " DECLARE_STD_ALIAS_TYPE_INFO();\n"
208  "\n";
209  } else {
210  code.ClassPublic() <<
211  " // type info\n"
212  " DECLARE_INTERNAL_TYPE_INFO();\n"
213  "\n";
214  }
215 // m_RefType->GenerateTypeCode(ctx);
216  code.ClassPublic() <<
217  " // parent type getter/setter\n" <<
218  " const " << ref_name << "& Get(void) const;\n" <<
219  " " << ref_name << "& Set(void);\n";
220  code.MethodStart(true) <<
221  "const " << ref_name << "& " << methodPrefix << "Get(void) const\n" <<
222  "{\n" <<
223  " return *this;\n" <<
224  "}\n\n";
225  code.MethodStart(true) <<
226  ref_name << "& " << methodPrefix << "Set(void)\n" <<
227  "{\n" <<
228  " return *this;\n" <<
229  "}\n\n";
230  }
231  else {
232  code.ClassPublic() <<
233  " // type info\n"
234  " DECLARE_STD_ALIAS_TYPE_INFO();\n"
235  "\n";
236  string constr_decl = className + "(const " + ref_name + "& data)";
237  code.ClassPublic() <<
238  " // explicit constructor from the primitive type\n" <<
239  " explicit " << constr_decl << ";\n";
240  code.MethodStart(true) <<
241  methodPrefix << constr_decl << "\n" <<
242  " : " << parentNamespaceRef << code.GetParentClassName() << "(data)\n" <<
243  "{\n" <<
244  "}\n" <<
245  "\n";
246 
247  // I/O operators
248  bool kindIO = kind == eKindStd || kind == eKindEnum ||
249  kind == eKindString || kind == eKindPointer;
250  code.MethodStart(true) <<
251  "NCBI_NS_NCBI::CNcbiOstream& operator<<\n" <<
252  "(NCBI_NS_NCBI::CNcbiOstream& str, const " << className << "& obj)\n" <<
253  "{\n";
254  if (kindIO) {
255  code.Methods(true) <<
256  " if (NCBI_NS_NCBI::MSerial_Flags::HasSerialFormatting(str)) {\n" <<
257  " return WriteObject(str,&obj,obj.GetTypeInfo());\n" <<
258  " }\n" <<
259  " str << obj.Get();\n" <<
260  " return str;\n";
261  } else {
262  code.Methods(true) <<
263  " return WriteObject(str,&obj,obj.GetTypeInfo());\n";
264  }
265  code.Methods(true) << "}\n\n";
266  code.MethodStart(true) <<
267  "NCBI_NS_NCBI::CNcbiIstream& operator>>\n" <<
268  "(NCBI_NS_NCBI::CNcbiIstream& str, " << className << "& obj)\n" <<
269  "{\n";
270  if (kindIO) {
271  code.Methods(true) <<
272  " if (NCBI_NS_NCBI::MSerial_Flags::HasSerialFormatting(str)) {\n" <<
273  " return ReadObject(str,&obj,obj.GetTypeInfo());\n" <<
274  " }\n" <<
275  " str >> obj.Set();\n" <<
276  " return str;\n";
277  } else {
278  code.Methods(true) <<
279  " return ReadObject(str,&obj,obj.GetTypeInfo());\n";
280  }
281  code.Methods(true) << "}\n\n";
282  }
283 
284  // define typeinfo method
285  {
286 // code.CPPIncludes().insert("serial/aliasinfo");
287  CNcbiOstream& methods = code.Methods();
288  methods << "BEGIN";
289  if (dynamic_cast<const CEnumRefTypeStrings*>(m_RefType.get())) {
290  methods << "_ENUM";
291  }
292  methods << "_ALIAS_INFO(\""
293  << GetExternalName() << "\", "
294  << GetClassName() << ", "
295  << m_RefType->GetRef(ns) << ")\n"
296  "{\n";
297  if ( !GetModuleName().empty() ) {
298  methods <<
299  " SET_ALIAS_MODULE(\"" << GetModuleName() << "\");\n";
300  }
301  const CDataType* dataType = DataType();
302  if (dataType) {
303  if (dataType->HasTag()) {
304  methods <<
305  " SET_ASN_TAGGED_TYPE_INFO(" <<"SetTag, (" << dataType->GetTag() <<',' <<
306  dataType->GetTagClassString(dataType->GetTagClass()) << ',' <<
307  dataType->GetTagTypeString(dataType->GetTagType()) <<"));\n";
308  } else if (dataType->GetTagType() != CAsnBinaryDefs::eAutomatic) {
309  methods <<
310  " SET_ASN_TAGGED_TYPE_INFO(" <<"SetTagType, (" <<
311  dataType->GetTagTypeString(dataType->GetTagType()) <<"));\n";
312  }
313  }
314  methods << " SET_";
315  if ( is_class ) {
316  methods << "CLASS";
317  }
318  else {
319  methods << "STD";
320  }
321  methods <<
322  "_ALIAS_DATA_PTR;\n";
323  if (this->IsFullAlias()) {
324  methods <<
325  " SET_FULL_ALIAS;\n"
326  "\n";
327  }
328  methods << " info->CodeVersion(" << DATATOOL_VERSION << ");\n";
329  methods <<
330  "}\n"
331  "END_ALIAS_INFO\n"
332  "\n";
333  }
334 }
335 
337 {
338  // m_RefType->GenerateUserHPPCode(out);
339  const CNamespace& ns = GetNamespace();
340  string ref_name = m_RefType->GetCType(ns);
341  string className = GetClassName();
343  out
344  << "\n"
345  << "/** @addtogroup ";
346  if (!CClassCode::GetDoxygenGroup().empty()) {
348  } else {
349  out << "dataspec_" << GetDoxygenModuleName();
350  }
351  out
352  << "\n *\n"
353  << " * @{\n"
354  << " */\n\n";
355  }
356  out <<
357  "/////////////////////////////////////////////////////////////////////////////\n";
359  out <<
360  "///\n"
361  "/// " << className << " --\n"
362  "///\n\n";
363  }
364  out << "class ";
365  if ( !CClassCode::GetExportSpecifier().empty() )
366  out << CClassCode::GetExportSpecifier() << " ";
367  out << GetClassName()<<" : public "<<GetClassName()<<"_Base\n"
368  "{\n"
369  " typedef "<<GetClassName()<<"_Base Tparent;\n"
370  "public:\n";
371  out <<
372  " " << GetClassName() << "(void) {}\n"
373  "\n";
374 
375  bool is_class = false;
376  switch ( m_RefType->GetKind() ) {
377  case eKindClass:
378  case eKindObject:
379  is_class = true;
380  break;
381  default:
382  is_class = false;
383  break;
384  }
385  if ( !is_class ) {
386  // Generate type convertions
387  out <<
388  " /// Explicit constructor from the primitive type.\n" <<
389  " explicit " << className + "(const " + ref_name + "& data)" << "\n"
390  " : Tparent(data) {}\n\n";
391  }
392  out << "};\n";
394  out << "/* @} */\n";
395  }
396  out << "\n";
397 }
398 
400 {
401  //m_RefType->GenerateUserCPPCode(out);
402 }
403 
405 {
407 }
408 
410 {
412 }
413 
414 
416  const CNamespace& ns,
417  const string& fileName,
418  CTypeStrings& ref_type,
419  const CComments& comments)
420  : CParent(comments),
421  m_ClassName(className),
422  m_Namespace(ns),
423  m_FileName(fileName),
424  m_RefType(&ref_type),
425  m_IsObject(m_RefType->GetKind() == eKindObject)
426 {
427 }
428 
429 
431 {
432  return m_IsObject ? eKindObject : eKindClass;
433 }
434 
436 {
437  return m_Namespace;
438 }
439 
441 {
443 }
444 
446 {
449 }
450 
452 {
454 }
455 
457  const string& /*methodPrefix*/) const
458 {
459  return GetCType(ns);
460 }
461 
463 {
464  return m_RefType->HaveSpecialRef();
465 }
466 
468 {
469  return "CLASS, ("+GetCType(ns)+')';
470 }
471 
473 {
474  return m_RefType->CanBeKey();
475 }
476 
478 {
479  return m_RefType->CanBeCopied();
480 }
481 
483 {
484  return m_IsObject ?
487 }
488 
489 string CAliasRefTypeStrings::GetDestructionCode(const string& expr) const
490 {
491  return m_IsObject ?
494 }
495 
496 string CAliasRefTypeStrings::GetIsSetCode(const string& var) const
497 {
498  return m_RefType->GetIsSetCode(var);
499 }
500 
501 string CAliasRefTypeStrings::GetResetCode(const string& var) const
502 {
503  return m_IsObject ?
504  var + ".Reset();\n" :
505  m_RefType->GetResetCode(var + ".Set()");
506 }
507 
508 string CAliasRefTypeStrings::GetDefaultCode(const string& var) const
509 {
510  // No value - return empty string
511  if ( m_IsObject || var.empty() ) {
512  return NcbiEmptyString;
513  }
514  // Cast var to the aliased type
515  return GetCType(GetNamespace()) + "(" + var + ")";
516 }
517 
519 {
520  m_RefType->GenerateCode(ctx);
521 }
522 
524 {
526 }
527 
529 {
531 }
532 
533 
CAsnBinaryDefs::TLongTag GetTag(void) const
Definition: type.hpp:233
void GenerateCode(CClassContext &ctx) const
Definition: aliasstr.cpp:144
virtual void GenerateTypeCode(CClassContext &ctx) const
Definition: aliasstr.cpp:440
CS_CONTEXT * ctx
Definition: t0006.c:12
Definition: dbpivot.c:60
string GetInitializer(void) const
Definition: aliasstr.cpp:114
string GetIsSetCode(const string &var) const
Definition: aliasstr.cpp:496
virtual void GenerateTypeCode(CClassContext &ctx) const
Definition: typestr.cpp:220
string GetDestructionCode(const string &expr) const
Definition: aliasstr.cpp:119
const string & GetModuleName(void) const
Definition: typestr.hpp:54
string GetDestructionCode(const string &expr) const
Definition: aliasstr.cpp:489
CAsnBinaryDefs::ETagType GetTagType(void) const
Definition: type.hpp:250
virtual string GetInitializer(void) const
Definition: typestr.cpp:109
AutoPtr< CTypeStrings > m_RefType
Definition: aliasstr.hpp:91
std::ofstream out("events_result.xml")
main entry point for tests
void BeginClassDeclaration(CClassContext &ctx) const
Definition: typestr.cpp:237
string GetClassName(void) const
Definition: classstr.cpp:1681
bool CanBeCopied(void) const
Definition: aliasstr.cpp:477
static int init(DBPROCESS *dbproc, const char *name)
Definition: bcp.c:44
string GetExternalName(void) const
Definition: aliasstr.cpp:72
bool CanBeKey(void) const
Definition: aliasstr.cpp:98
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
virtual string GetIsSetCode(const string &var) const
Definition: typestr.cpp:189
CAliasTypeStrings(const string &externalName, const string &className, CTypeStrings &ref_type, const CComments &comments)
Definition: aliasstr.cpp:46
AutoPtr< CTypeStrings > m_RefType
Definition: aliasstr.hpp:137
string m_ClassName
Definition: aliasstr.hpp:90
EKind GetKind(void) const
Definition: aliasstr.cpp:62
const CNamespace & GetNamespace(void) const
Definition: aliasstr.cpp:435
string GetRef(const CNamespace &ns) const
Definition: aliasstr.cpp:467
string GetRef(const CNamespace &ns) const
Definition: aliasstr.cpp:93
static string GetTagTypeString(CAsnBinaryDefs::ETagType ttype)
Definition: type.cpp:156
static bool GetDoxygenComments(void)
Definition: code.cpp:90
virtual void GenerateCode(CClassContext &ctx) const
Definition: typestr.cpp:215
#define NULL
Definition: ncbistd.hpp:225
virtual const CNamespace & GetNamespace(void) const =0
const CDataType * DataType(void) const
Definition: typestr.hpp:134
string m_ExternalName
Definition: aliasstr.hpp:89
virtual void AddForwardDeclaration(const string &className, const CNamespace &ns)=0
bool IsFullAlias(void) const
Definition: aliasstr.hpp:85
virtual void GenerateUserCPPCode(CNcbiOstream &out) const
Definition: typestr.cpp:233
#define NcbiEmptyString
Definition: ncbistr.hpp:119
string GetClassName(void) const
Definition: aliasstr.cpp:67
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
static const CNamespace KNCBINamespace
Definition: namespace.hpp:105
string GetIsSetCode(const string &var) const
Definition: aliasstr.cpp:124
virtual string GetCType(const CNamespace &ns) const =0
virtual void GenerateUserHPPCode(CNcbiOstream &out) const
Definition: aliasstr.cpp:523
string GetCType(const CNamespace &ns) const
Definition: aliasstr.cpp:451
EKind GetKind(void) const
Definition: aliasstr.cpp:430
virtual bool CanBeKey(void) const
Definition: typestr.cpp:139
virtual EKind GetKind(void) const =0
virtual bool CanBeCopied(void) const
Definition: typestr.cpp:151
virtual void GenerateUserHPPCode(CNcbiOstream &out) const
Definition: typestr.cpp:229
string GetNamespaceRef(const CNamespace &ns) const
Definition: namespace.cpp:97
string GetResetCode(const string &var) const
Definition: aliasstr.cpp:501
string NewInstance(const string &init, const string &place) const
Definition: aliasstr.cpp:108
virtual void GeneratePointerTypeCode(CClassContext &ctx) const
Definition: aliasstr.cpp:445
void GeneratePointerTypeCode(CClassContext &ctx) const
Definition: aliasstr.cpp:409
string GetMethodPrefix(void) const
Definition: code.cpp:135
CNcbiOstream & ClassPublic(void)
Definition: code.hpp:92
virtual void GeneratePointerTypeCode(CClassContext &ctx) const
Definition: typestr.cpp:224
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:150
CNamespace m_Namespace
Definition: aliasstr.hpp:135
virtual string NewInstance(const string &init, const string &place=kEmptyStr) const
Definition: typestr.cpp:177
string GetPrefixedCType(const CNamespace &ns, const string &methodPrefix) const
Definition: aliasstr.cpp:82
CNcbiOstream & MethodStart(bool inl=false)
Definition: code.hpp:112
void GenerateTypeCode(CClassContext &ctx) const
Definition: aliasstr.cpp:404
CNcbiOstream & Methods(bool inl=false)
Definition: code.hpp:108
virtual const CNamespace & GetNamespace(void) const
Definition: typestr.cpp:76
Definition: inftrees.h:24
Useful/utility classes and methods.
void GenerateUserCPPCode(CNcbiOstream &out) const
Definition: aliasstr.cpp:399
string GetDoxygenModuleName(void) const
Definition: typestr.hpp:63
bool CanBeCopied(void) const
Definition: aliasstr.cpp:103
static const string & GetExportSpecifier(void)
Definition: code.cpp:81
string GetCType(const CNamespace &ns) const
Definition: aliasstr.cpp:77
string GetDefaultCode(const string &var) const
Definition: aliasstr.cpp:134
virtual string GetDestructionCode(const string &expr) const
Definition: typestr.cpp:114
virtual TIncludes & HPPIncludes(void)=0
bool HasTag(void) const
Definition: type.hpp:237
virtual TIncludes & CPPIncludes(void)=0
const CNamespace & GetNamespace(void) const
Definition: code.cpp:115
void SetParentClass(const string &className, const CNamespace &ns)
Definition: code.cpp:155
~CAliasTypeStrings(void)
Definition: aliasstr.cpp:58
const CNamespace & GetParentClassNamespace(void) const
Definition: code.hpp:63
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:552
bool HaveSpecialRef(void) const
Definition: aliasstr.cpp:88
CAliasRefTypeStrings(const string &className, const CNamespace &ns, const string &fileName, CTypeStrings &ref_type, const CComments &comments)
Definition: aliasstr.cpp:415
virtual void GenerateUserCPPCode(CNcbiOstream &out) const
Definition: aliasstr.cpp:528
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string...
Definition: ncbiexpt.hpp:546
CAsnBinaryDefs::ETagClass GetTagClass(void) const
Definition: type.hpp:244
string GetInitializer(void) const
Definition: aliasstr.cpp:482
static const string & GetDoxygenGroup(void)
Definition: code.cpp:100
string GetPrefixedCType(const CNamespace &ns, const string &methodPrefix) const
Definition: aliasstr.cpp:456
string GetResetCode(const string &var) const
Definition: aliasstr.cpp:129
string GetDefaultCode(const string &var) const
Definition: aliasstr.cpp:508
static string GetTagClassString(CAsnBinaryDefs::ETagClass tclass)
Definition: type.cpp:143
bool HaveSpecialRef(void) const
Definition: aliasstr.cpp:462
const size_t DATATOOL_VERSION
Definition: datatool.hpp:46
bool CanBeKey(void) const
Definition: aliasstr.cpp:472
virtual void GenerateCode(CClassContext &ctx) const
Definition: aliasstr.cpp:518
void GenerateUserHPPCode(CNcbiOstream &out) const
Definition: aliasstr.cpp:336
virtual string GetResetCode(const string &var) const
Definition: typestr.cpp:119
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
virtual bool HaveSpecialRef(void) const
Definition: typestr.cpp:129
const string & GetParentClassName(void) const
Definition: code.hpp:59
virtual string GetRef(const CNamespace &ns) const
Definition: typestr.cpp:134
Modified on Thu Mar 30 17:14:22 2017 by modify_doxy.py rev. 506947