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

Go to the SVN repository for this file.

1 /* $Id: Object_id.cpp 82528 2018-06-08 16:41:16Z vasilche $
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: .......
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using specifications from the ASN data definition file
34  * 'general.asn'.
35  */
36 
37 // standard includes
38 
39 // generated includes
40 #include <ncbi_pch.hpp>
42 #include <corelib/ncbistd.hpp>
43 #include <corelib/ncbimtx.hpp>
44 
45 // generated classes
46 
47 #include <serial/objistr.hpp>
48 
50 BEGIN_objects_SCOPE // namespace ncbi::objects::
51 
52 
53 // destructor
55 {
56  return;
57 }
58 
59 
60 // match for identity
61 bool CObject_id::Match(const CObject_id& oid2) const
62 {
63  return Compare(oid2) == 0;
64 }
65 
66 
68 {
69  switch ( oid2.Which() ) {
70  case e_Id:
72  return true;
73  case e_Str:
74  {
75  // try to treat the string as a valid integer the same way as SetStrOrId() does
76  const string& str = oid2.GetStr();
78  // leading zeros and + aren't allowed
79  if ( id > 0 && str[0] != '0' && str[0] != '+' ) {
80  // valid positive integer
81  SetId(id);
82  return true;
83  }
84  // string can not be converted to a positive integer.
85  return false;
86  }
87  default:
88  return false;
89  }
90 }
91 
92 
94 {
95  switch ( Tparent::Which() ) {
96  case e_Id:
98  return e_Id;
99  case e_Str:
100  {
101  // try to treat the string as a valid integer the same way as SetStrOrId() does
102  // but also allowing any Int8 values
103  const string& str = GetStr();
105  if ( !value ) {
106  if ( errno ) {
107  // not convertible to integer
108  return CObject_id::e_Str;
109  }
110  // converted to 0
111  if ( str.size() != 1 ) {
112  // leading zeroes are not allowed
113  return CObject_id::e_Str;
114  }
115  // valid zero as a string
116  return CObject_id::e_Id;
117  }
118  if ( value > 0 ) {
119  // non-zero positive value
120  if ( str[0] == '0' || str[0] == '+' ) {
121  // redundant '+' or leading zeroes are not allowed
122  value = 0;
123  return CObject_id::e_Str;
124  }
125  }
126  else {
127  // non-zero negative value
128  if ( str[1] == '0' ) {
129  // leading zeroes are not allowed
130  value = 0;
131  return CObject_id::e_Str;
132  }
133  }
134  // valid non-zero value as a string
135  return CObject_id::e_Id;
136  }
137  default:
138  value = 0;
139  return CObject_id::e_not_set;
140  }
141 }
142 
143 
144 // match for identity
145 int CObject_id::Compare(const CObject_id& oid2) const
146 {
147  TId8 value, value2;
149  E_Choice type2 = oid2.GetIdType(value2);
150  if ( int diff = type - type2 ) {
151  return diff;
152  }
153  switch ( type ) {
154  case e_Id:
155  return value < value2? -1: (value > value2);
156  case e_Str:
157  return PNocase().Compare(GetStr(), oid2.GetStr());
158  default:
159  return 0;
160  }
161 }
162 
163 
165 {
166  TId8 value;
167  if ( !GetId8(value) ) {
168  NCBI_THROW(CSerialException, eInvalidData,
169  "CObject_id doesn't have Int8 value");
170  }
171  return value;
172 }
173 
174 
176 {
177  if ( TId(value) == value ) {
178  // fits in id
179  SetId(TId(value));
180  }
181  else {
183  }
184 }
185 
186 
188 {
189  if ( !str.empty() && str[0] >= '1' && str[0] <= '9' ) {
191  if ( id > 0 ) {
192  // converted to a positive integer
193  SetId(id);
194  return;
195  }
196  }
197  SetStr(str);
198 }
199 
200 
201 // format contents into a stream
202 ostream& CObject_id::AsString(ostream &s) const
203 {
204  switch ( Tparent::Which() ) {
205  case e_Id:
206  s << Tparent::GetId();
207  break;
208  case e_Str:
209  s << Tparent::GetStr(); // no Upcase() on output as per Ostell 7/2001 - Karl
210  break;
211  default:
212  break;
213  }
214  return s;
215 }
216 
217 
218 CObject_id&
220 {
221  CRef<CObject_id>& shared_id = m_MapByStr[id];
222  if ( !shared_id ) {
223  shared_id = new CObject_id;
224  shared_id->SetStr(id);
225  }
226  return *shared_id;
227 }
228 
229 
230 CObject_id&
232 {
233  CRef<CObject_id>& shared_id = m_MapByInt[id];
234  if ( !shared_id ) {
235  shared_id = new CObject_id;
236  shared_id->SetId(id);
237  }
238  return *shared_id;
239 }
240 
241 
242 DEFINE_STATIC_FAST_MUTEX(s_SharedIdMutex);
243 
244 
245 CObject_id&
247 {
248  CFastMutexGuard guard(s_SharedIdMutex);
249  in.ReadObject(&m_Temp, m_Temp.GetTypeInfo());
250  return GetSharedObject_id(m_Temp);
251 }
252 
253 
254 END_objects_SCOPE // namespace ncbi::objects::
DEFINE_STATIC_FAST_MUTEX(s_SharedIdMutex)
CObjectIStream –.
Definition: objistr.hpp:93
void SetId8(TId8 value)
Definition: Object_id.cpp:175
~CObject_id(void)
Definition: Object_id.cpp:54
E_Choice GetIdType(TId8 &value) const
Definition: Object_id.cpp:93
void SetStrOrId(CTempString str)
Definition: Object_id.cpp:187
bool SetAsMatchingTo(const CObject_id &oid2)
Definition: Object_id.cpp:67
ostream & AsString(ostream &s) const
Definition: Object_id.cpp:202
int Compare(const CObject_id &oid2) const
Definition: Object_id.cpp:145
TId8 GetId8(void) const
Definition: Object_id.cpp:164
bool Match(const CObject_id &oid2) const
Definition: Object_id.cpp:61
CObject_id & ReadSharedObject_id(CObjectIStream &in)
Reads Object-id and returns reference to its shared version.
Definition: Object_id.cpp:246
CObject_id & GetSharedObject_id(const string &id)
Returns shared version of Object-id with specified 'str' field value.
Definition: Object_id.cpp:219
Root class for all serialization exceptions.
Definition: exception.hpp:50
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const char * str(char *buf, int n)
Definition: stats.c:84
#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:704
#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 Compare(const T &s1, const T &s2) const
Return difference between "s1" and "s2".
Definition: ncbistr.hpp:5819
static int StringToNonNegativeInt(const CTempString str, TStringToNumFlags flags=0)
Convert string to non-negative integer value.
Definition: ncbistr.cpp:457
static Int8 StringToInt8(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to Int8.
Definition: ncbistr.cpp:793
PNocase_Generic< string > PNocase
Definition: ncbistr.hpp:4908
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
@ fConvErr_NoErrMessage
Set errno, but do not set CNcbiError message on error.
Definition: ncbistr.hpp:291
@ fConvErr_NoThrow
Do not throw an exception on error.
Definition: ncbistr.hpp:285
E_Choice Which(void) const
Which variant is currently selected.
Definition: Object_id_.hpp:235
const TStr & GetStr(void) const
Get the variant data.
Definition: Object_id_.hpp:297
TStr & SetStr(void)
Select the variant.
Definition: Object_id_.hpp:304
TId & SetId(void)
Select the variant.
Definition: Object_id_.hpp:277
E_Choice
Choice variants.
Definition: Object_id_.hpp:88
TId GetId(void) const
Get the variant data.
Definition: Object_id_.hpp:270
@ e_not_set
No variant selected.
Definition: Object_id_.hpp:89
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Multi-threading – mutexes; rw-locks; semaphore.
std::istream & in(std::istream &in_, double &x_)
Definition: type.c:6
Modified on Sun Apr 21 03:38:28 2024 by modify_doxy.py rev. 669887