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

Go to the SVN repository for this file.

1 /* $Id: static_set.cpp 53965 2012-04-30 14:12:22Z 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: Eugene Vasilchenko
27  *
28  * File Description: Implementation of CStaticSet/CStaticMap
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <util/static_set.hpp>
34 #include <corelib/ncbi_param.hpp>
35 #include <corelib/ncbi_stack.hpp>
36 
38 
39 
41  eParam_NoThread, NCBI_STATIC_ARRAY_COPY_WARNING);
42 
43 
45  eParam_NoThread, NCBI_STATIC_ARRAY_UNSAFE_TYPE_WARNING);
46 
47 
48 BEGIN_NAMESPACE(NStaticArray);
49 
50 
51 DEFINE_CLASS_STATIC_FAST_MUTEX(IObjectConverter::sx_InitMutex);
52 
53 
55 {
56 }
57 
58 
60  : m_Converter(converter),
61  m_ArrayPtr(0),
62  m_ElementCount(0)
63 {
64 }
65 
66 
68 {
69  if ( m_ArrayPtr ) {
70  size_t dst_size = m_Converter->GetDstTypeSize();
71  for ( size_t i = GetElementCount(); i--; ) { // delete in reverse order
72  m_Converter->Destroy(static_cast<char*>(GetArrayPtr())+i*dst_size);
73  }
74  free(GetArrayPtr());
75  }
76 }
77 
78 
79 void CArrayHolder::Convert(const void* src_array,
80  size_t size,
81  const char* file,
82  int line,
83  ECopyWarn warn)
84 {
85  if ( warn == eCopyWarn_show ||
86  (warn == eCopyWarn_default &&
87  TParamStaticArrayCopyWarning::GetDefault()) ) {
88  // report incorrect usage
89  CDiagCompileInfo diag_compile_info
90  (file? file: __FILE__,
91  file? line: __LINE__,
94  CNcbiDiag diag(diag_compile_info, eDiag_Warning,
96  diag.GetRef()
97  << ErrCode(NCBI_ERRCODE_X_NAME(Util_StaticArray), 3)
98  << ": converting static array from "
99  << m_Converter->GetSrcTypeInfo().name() << "[] to "
100  << m_Converter->GetDstTypeInfo().name() << "[]";
101  if ( !file ) {
102  diag.GetRef() << CStackTrace();
103  }
104  diag.GetRef() << Endm;
105  }
106 
107  size_t src_size = m_Converter->GetSrcTypeSize();
108  size_t dst_size = m_Converter->GetDstTypeSize();
109  m_ArrayPtr = malloc(size*dst_size);
110  for ( size_t i = 0; i < size; ++i ) {
111  m_Converter->Convert(static_cast<char*>(GetArrayPtr())+i*dst_size,
112  static_cast<const char*>(src_array)+i*src_size);
113  m_ElementCount = i+1;
114  }
115 }
116 
117 
119  const char* file,
120  int line)
121 {
122  if ( TParamStaticArrayUnsafeTypeWarning::GetDefault() ) {
123  // report incorrect usage
124  CDiagCompileInfo diag_compile_info
125  (file? file: __FILE__,
126  file? line: __LINE__,
129  CNcbiDiag diag(diag_compile_info, eDiag_Warning,
131  diag.GetRef()
132  << ErrCode(NCBI_ERRCODE_X_NAME(Util_StaticArray), 2)
133  << ": static array type is not MT-safe: " << type_name << "[]";
134  if ( !file ) {
135  diag.GetRef() << CStackTrace();
136  }
137  diag.GetRef() << Endm;
138  }
139 }
140 
141 
142 void ReportIncorrectOrder(size_t curr_index,
143  const char* file,
144  int line)
145 {
146  { // report incorrect usage
147  CDiagCompileInfo diag_compile_info
148  (file? file: __FILE__,
149  file? line: __LINE__,
152  CNcbiDiag diag(diag_compile_info, eDiag_Fatal,
154  diag.GetRef()
155  << ErrCode(NCBI_ERRCODE_X_NAME(Util_StaticArray), 1)
156  << "keys are out of order: "
157  << "key["<<curr_index<<"] < key["<<(curr_index-1)<<"]";
158  if ( !file ) {
159  diag.GetRef() << CStackTrace();
160  }
161  diag.GetRef() << Endm;
162  }
163 }
164 
165 
166 END_NAMESPACE(NStaticArray);
167 
void Convert(const void *src_array, size_t size, const char *file, int line, ECopyWarn warn)
Convert data from static array of different type using the holder's converter.
Definition: static_set.cpp:79
void * m_ArrayPtr
Definition: static_set.hpp:230
CArrayHolder(IObjectConverter *converter) THROWS_NONE
Definition: static_set.cpp:59
size_t m_ElementCount
Definition: static_set.hpp:231
void * GetArrayPtr(void) const
Definition: static_set.hpp:196
~CArrayHolder(void) THROWS_NONE
Definition: static_set.cpp:67
unique_ptr< IObjectConverter > m_Converter
Definition: static_set.hpp:229
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
CNcbiDiag –.
Definition: ncbidiag.hpp:924
Helper class for single object conversion from static type to work type.
Definition: static_set.hpp:168
virtual ~IObjectConverter(void) THROWS_NONE
Definition: static_set.cpp:54
#define NCBI_MODULE
#define NCBI_MAKE_MODULE(module)
Set default module name based on NCBI_MODULE macro.
Definition: ncbidiag.hpp:149
#define NCBI_CURRENT_FUNCTION
Get current function name.
Definition: ncbidiag.hpp:142
#define NCBI_ERRCODE_X_NAME(name)
Returns value of error code by its name defined by NCBI_DEFINE_ERRCODE_X.
Definition: ncbidiag.hpp:369
const CNcbiDiag & GetRef(void) const
Some compilers (e.g.
Definition: ncbidiag.hpp:946
@ eDPF_Default
Use global default flags (merge with).
Definition: ncbidiag.hpp:771
@ eDPF_Line
Source line.
Definition: ncbidiag.hpp:695
@ eDPF_File
File name (not full path)
Definition: ncbidiag.hpp:693
@ eDPF_LongFilename
Full file path.
Definition: ncbidiag.hpp:694
@ eDiag_Warning
Warning message.
Definition: ncbidiag.hpp:652
@ eDiag_Fatal
Fatal error – guarantees exit(or abort)
Definition: ncbidiag.hpp:655
#define THROWS_NONE
Do not use 'throw' dynamic exception specification for C++11 compilers.
Definition: ncbiexpt.hpp:74
@ eParam_NoThread
Do not use per-thread values.
Definition: ncbi_param.hpp:418
#define ErrCode()
Get the error code for the last failed system function.
Definition: mdb.c:377
FILE * file
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
NCBI_PARAM_DEF_EX(bool, NCBI, STATIC_ARRAY_COPY_WARNING, false, eParam_NoThread, NCBI_STATIC_ARRAY_COPY_WARNING)
void ReportUnsafeStaticType(const char *type_name, const char *file, int line)
Log error message about non-MT-safe static type (string, pair<>) if it's configured by TParamStaticAr...
Definition: static_set.cpp:118
DEFINE_CLASS_STATIC_FAST_MUTEX(IObjectConverter::sx_InitMutex)
END_NAMESPACE(NStaticArray)
END_NCBI_NAMESPACE
Definition: static_set.cpp:168
BEGIN_NCBI_NAMESPACE
Definition: static_set.cpp:37
void ReportIncorrectOrder(size_t curr_index, const char *file, int line)
Log error message about wrong order of elements in array and abort.
Definition: static_set.cpp:142
BEGIN_NAMESPACE(NStaticArray)
ECopyWarn
Definition: static_set.hpp:182
@ eCopyWarn_default
Definition: static_set.hpp:183
@ eCopyWarn_show
Definition: static_set.hpp:184
STATIC_ARRAY_COPY_WARNING
Definition: static_set.hpp:72
STATIC_ARRAY_UNSAFE_TYPE_WARNING
Definition: static_set.hpp:78
NCBI
Definition: static_set.hpp:72
static const char * type_name(CS_INT value)
Definition: will_convert.c:122
void free(voidpf ptr)
voidp malloc(uInt size)
Modified on Thu Jun 13 17:31:34 2024 by modify_doxy.py rev. 669887