LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
LimaCommon.h
Go to the documentation of this file.
1// Copyright 2002-2021 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file LimaCommon.h (from s2Common.h)
9 * @author Gael de Chalendar <Gael.de-Chalendar@cea.fr>
10
11 * Benoit Mathieu <mathieub@zoe.cea.fr>
12
13 * Hervé Le Borgne <herve.le-borgne@cea.fr>
14
15 * @date mar déc 18 2007
16 * copyright Copyright (C) 2003-2021 by CEA LIST
17 * Project mm_common
18 *
19 * @brief (short description)
20 *
21 ***********************************************************************/
22#ifndef LIMA_MMCOMMONS_H
23#define LIMA_MMCOMMONS_H
24
25#include <QtGlobal>
26
27#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
28#include <QtCore/QMutex>
29#define QTENDL endl
30#else
31#include <QtCore/QRecursiveMutex>
32#define QTENDL Qt::endl
33#endif
34
35#include <cstdint>
36
37#ifdef WIN32
38
39#pragma warning( disable : 4512 )
40
41// Avoids compilation errors redefining struc sockaddr in ws2def.h
42#define _WINSOCKAPI_
43
44#undef min
45#undef max
46typedef __int16 int16_t;
47typedef unsigned __int16 uint16_t;
48typedef __int32 int32_t;
49typedef unsigned __int32 uint32_t;
50typedef __int64 int64_t;
51typedef unsigned __int64 uint64_t;
52#endif
53
54
55#include <boost/serialization/strong_typedef.hpp>
56
57#ifdef WIN32
58
59
60#ifdef LIMA_DATA_EXPORTING
61 #define LIMA_DATA_EXPORT __declspec(dllexport)
62#else
63 #define LIMA_DATA_EXPORT __declspec(dllimport)
64#endif
65
66#ifdef LIMA_DATAHANDLER_EXPORTING
67 #define LIMA_DATAHANDLER_EXPORT __declspec(dllexport)
68#else
69 #define LIMA_DATAHANDLER_EXPORT __declspec(dllimport)
70#endif
71
72#ifdef LIMA_FSAACCESS_EXPORTING
73 #define LIMA_FSAACCESS_EXPORT __declspec(dllexport)
74#else
75 #define LIMA_FSAACCESS_EXPORT __declspec(dllimport)
76#endif
77
78#ifdef LIMA_MEDIAPROCESSORS_EXPORTING
79 #define LIMA_MEDIAPROCESSORS_EXPORT __declspec(dllexport)
80#else
81 #define LIMA_MEDIAPROCESSORS_EXPORT __declspec(dllimport)
82#endif
83
84#ifdef LIMA_MEDIATICDATA_EXPORTING
85 #define LIMA_MEDIATICDATA_EXPORT __declspec(dllexport)
86#else
87 #define LIMA_MEDIATICDATA_EXPORT __declspec(dllimport)
88#endif
89
90#ifdef LIMA_COMMONMISC_EXPORTING
91 #define LIMA_COMMONMISC_EXPORT __declspec(dllexport)
92#else
93 #define LIMA_COMMONMISC_EXPORT __declspec(dllimport)
94#endif
95
96#ifdef LIMA_COMMONTOOLS_EXPORTING
97 #define LIMA_COMMONTOOLS_EXPORT __declspec(dllexport)
98#else
99 #define LIMA_COMMONTOOLS_EXPORT __declspec(dllimport)
100#endif
101
102#ifdef LIMA_PROCESSUNITFRAMEWORK_EXPORTING
103 #define LIMA_PROCESSUNITFRAMEWORK_EXPORT __declspec(dllexport)
104#else
105 #define LIMA_PROCESSUNITFRAMEWORK_EXPORT __declspec(dllimport)
106#endif
107
108#ifdef LIMA_TIME_EXPORTING
109 #define LIMA_TIME_EXPORT __declspec(dllexport)
110#else
111 #define LIMA_TIME_EXPORT __declspec(dllimport)
112#endif
113
114#ifdef LIMA_XMLCONFIGURATIONFILES_EXPORTING
115 #define LIMA_XMLCONFIGURATIONFILES_EXPORT __declspec(dllexport)
116#else
117 #define LIMA_XMLCONFIGURATIONFILES_EXPORT __declspec(dllimport)
118#endif
119
120#ifdef LIMA_COMMON_EXPORTING
121 #define LIMA_COMMON_EXPORT __declspec(dllexport)
122#else
123 #define LIMA_COMMON_EXPORT __declspec(dllimport)
124#endif
125
126#else // Not WIN32
127
128#define LIMA_DATA_EXPORT
129#define LIMA_DATAHANDLER_EXPORT
130#define LIMA_FSAACCESS_EXPORT
131#define LIMA_MEDIAPROCESSORS_EXPORT
132#define LIMA_MEDIATICDATA_EXPORT
133#define LIMA_COMMONMISC_EXPORT
134#define LIMA_COMMONTOOLS_EXPORT
135#define LIMA_PROCESSUNITFRAMEWORK_EXPORT
136#define LIMA_TIME_EXPORT
137#define LIMA_XMLCONFIGURATIONFILES_EXPORT
138#define LIMA_COMMON_EXPORT
139
140#endif
141
142#include <string>
143#include <stdexcept>
144
145#ifndef LIMA_DEBUG
146#define LIMA_DEBUG 0
147#endif
148
149// standard include
150#include <sstream>
151
152#include <common/QsLog/QsLog.h>
155
156#define LTRACE QLOG_TRACE()
157#define LDEBUG QLOG_DEBUG()
158#define LINFO QLOG_INFO()
159#define LNOTICE QLOG_INFO()
160#define LWARN QLOG_WARN()
161#define LERROR QLOG_ERROR()
162#define LFATAL QLOG_FATAL()
163
164// #define LOGINIT(X) QsLogging::Logger& logger = QsLogging::Logger::instance(X);
165// logger.setLoggingLevel( QsLogging::Categories::instance().levelFor( X ) );
166
168{
169public:
170 LogInit(char const* x)
171 {
172 // initialisation thread-safe
173#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
174 static QMutex mutex(QMutex::Recursive);
175#else
176 static QRecursiveMutex mutex;
177#endif
178 QMutexLocker locker(&mutex);
179 pLogger = &QsLogging::Logger::instance(QLatin1String(x));
180#ifndef DEBUG_CD
182 pLogger->setLoggingLevel(level);
183#endif
184 }
186};
187#define LOGINIT(X) \
188 static LogInit logInit(X); /*initialisation exécutée une seul fois*/\
189 auto& logger = *(logInit.pLogger); \
190 logger.setLoggingLevel(QsLogging::Categories::instance().levelFor( X ));
191
192//QsLogging::DestinationPtr debugDestination( QsLogging::DestinationFactory::MakeDebugOutputDestination() );
193//logger.addDestination(debugDestination.get());
194
195
196#define LENDL ". Note: LENDL is deprecated. It will be removed from a future release."
197
198#define ABSTRACTFACTORYPATTERNLOGINIT LOGINIT("Common::AbstractFactoryPattern")
199#define ABSTRACTPROCESSINGCLIENTLOGINIT LOGINIT("Common::AbstractProcessingClient")
200#define AGLOGINIT LOGINIT("Common::AnnotationGraph")
201#define BOWLOGINIT LOGINIT("Common::BOW");
202#define CLIENTFACTORYLOGINIT LOGINIT("Common::ClientFactory")
203#define COMPSTRACCESSLOGINIT LOGINIT("Common::CompStrAccess")
204#define FSAAHASHLOGINIT LOGINIT("Common::FsaAccessHash")
205#define FSAAIOLOGINIT LOGINIT("Common::FsaAccessIO")
206#define FSAALOGINIT LOGINIT("Common::FsaAccess")
207#define HANDLERLOGINIT LOGINIT("Common::Handler")
208#define LDATALOGINIT LOGINIT("Common::LanguageData")
209#define MDATALOGINIT LOGINIT("Common::MediaticData")
210#define MISCLOGINIT LOGINIT("Common::Misc")
211#define PROCESSORSLOGINIT LOGINIT("Common::Processors")
212#define PROCESSUNITFRAMEWORKLOGINIT LOGINIT("Common::ProcessUnitFramework")
213#define PROPERTYCODELOGINIT LOGINIT("Common::PropertyCode")
214#define STRINGMAPLOGINIT LOGINIT("Common::StringMap")
215#define STRPOOLLOGINIT LOGINIT("Common::StringPool")
216#define TGVLOGINIT LOGINIT("Common::TGV")
217#define XMLCFGLOGINIT LOGINIT("Common::XMLConfigurationFiles")
218#define DYNAMICLIBMANAGERLOGINIT LOGINIT("Common::DynamicLibrariesManager")
219
220QDebug& operator<< (QDebug& qd, const std::string& str );
221
222
223#ifndef LIMA_UNUSED
224#define LIMA_UNUSED(x) (void)x;
225#endif
226
227#define USE_STD_BITSET_FOR_LINGUISTIC_CODE
228
229#ifdef USE_STD_BITSET_FOR_LINGUISTIC_CODE
231#endif
232
233namespace Lima
234{
235
246
247BOOST_STRONG_TYPEDEF(char, NoParameters);
248
249
250BOOST_STRONG_TYPEDEF(uint8_t, MediaId);
251
252#define UNDEFLANG static_cast<Lima::MediaId>(std::numeric_limits<uint8_t>::max())
261class LimaException : public std::exception
262{
263public:
264 explicit LimaException(const char* message = "") :
265 std::exception(), m_reason(message)
266 {
267 }
268 explicit LimaException(const QString& message) :
269 std::exception(), m_reason(message.toStdString())
270 {
271 }
272 explicit LimaException(const std::string& message) :
273 std::exception(), m_reason(message)
274 {
275 }
276 LimaException(const LimaException&) = default;
278 virtual ~LimaException() throw() = default;
279
280 virtual const char * what () const throw() override
281 {
282 return m_reason.c_str();
283 }
284
285protected:
286 std::string m_reason;
287};
288
293#define LIMA_EXCEPTION(X) { \
294 QString errorString; \
295 QTextStream qts(&errorString); \
296 qts << __FILE__ << ":" << __LINE__ << ": " << X ; \
297 LERROR << errorString; \
298 throw LimaException(errorString); \
299}
300
306#define LIMA_EXCEPTION_SELECT(X,Y) { \
307 QString errorString; \
308 QTextStream qts(&errorString); \
309 qts << __FILE__ << ":" << __LINE__ << ": " << X ; \
310 LERROR << errorString; \
311 throw Y(errorString); \
312}
313
318#define LIMA_EXCEPTION_LOGINIT(X,Y) { \
319 X ; \
320 QString errorString; \
321 QTextStream qts(&errorString); \
322 qts << __FILE__ << ":" << __LINE__ << ": " << Y ; \
323 LERROR << errorString; \
324 throw Lima::LimaException(errorString); \
325}
326
332#define LIMA_EXCEPTION_SELECT_LOGINIT(X,Y,Z) { \
333 X ; \
334 QString errorString; \
335 QTextStream qts(&errorString); \
336 qts << __FILE__ << ":" << __LINE__ << ": " << Y ; \
337 LERROR << errorString; \
338 throw Z(errorString); \
339}
340
345{
346public:
347 explicit InvalidConfiguration(const char* message = "") :
348 LimaException(std::string(message))
349 {
350 }
351 explicit InvalidConfiguration(const std::string& message) :
352 LimaException(message)
353 {
354 }
355 explicit InvalidConfiguration(const QString& message) :
356 LimaException(message)
357 {
358 }
359 virtual ~InvalidConfiguration() throw() = default;
361 InvalidConfiguration& operator=(const InvalidConfiguration& e) = delete;
362};
363
369{
370public :
371 MediaNotInitialized(MediaId medId) :
373 m_medId(medId),
374 m_med(),
375 m_num(true)
376 {
377 if (m_num)
378 {
379 std::ostringstream oo(m_reason);
380 oo << "uninitialized media " << (int)m_medId;
381 }
382 else
383 {
384 m_reason = std::string("uninitialized media ")+m_med;
385 }
386 }
387
388 explicit MediaNotInitialized(const QString& media) :
389 LimaException(media),
390 m_medId(0),
391 m_med(media.toStdString()),
392 m_num(false)
393 {
394 }
395 explicit MediaNotInitialized(const std::string& media) :
396 LimaException(media),
397 m_medId(0),
398 m_med(media),
399 m_num(false)
400 {
401 if (m_num)
402 {
403 std::ostringstream oo(m_reason);
404 oo << "uninitialized media " << (int)m_medId;
405 }
406 else
407 {
408 m_reason = std::string("uninitialized media ")+m_med;
409 }
410
411 }
412
415 virtual ~MediaNotInitialized() throw() = default;
416
417private:
418 MediaId m_medId;
419 std::string m_med;
420 bool m_num;
421};
422
428{
429public :
430 AccessByStringNotInitialized(const QString& reason) :
431 LimaException(reason)
432 {
433 m_reason = std::string("Fsa not initialized because of ") + reason.toStdString();
434 }
435 AccessByStringNotInitialized(const std::string& reason) :
436 LimaException(reason)
437 {
438 m_reason = std::string("Fsa not initialized because of ") + reason;
439 }
442 virtual ~AccessByStringNotInitialized() throw() {}
443
444};
445
450{
451public :
452 AccessByStringOutOfRange(const QString& reason) : LimaException()
453 {
454 m_reason = std::string("parameter out of range ") + reason.toStdString();
455 }
456 AccessByStringOutOfRange(const std::string& reason) : LimaException()
457 {
458 m_reason = std::string("parameter out of range ") + reason;
459 }
462 virtual ~AccessByStringOutOfRange() throw() {}
463
464};
465
470{
471public:
472 explicit XMLException(const char* message="") :
474 {
475 m_reason = std::string("XMLException: ") + message;
476 }
477 explicit XMLException(const QString& message) :
479 {
480 m_reason = std::string("XMLException: ") + message.toStdString();
481 }
482 explicit XMLException(const std::string& message) :
484 {
485 m_reason = std::string("XMLException: ") + message;
486 }
487 XMLException(const XMLException&) = default;
489};
490
491
492} // closing namespace Lima
493
494#endif // LIMA_MMCOMMONS_H
QDebug & operator<<(QDebug &qd, const std::string &str)
Definition QsLog.cpp:39
Use this exception to signal the used of a wrongly initialized LIMA dictionary.
Definition LimaCommon.h:428
AccessByStringNotInitialized & operator=(const AccessByStringNotInitialized &)=delete
AccessByStringNotInitialized(const std::string &reason)
Definition LimaCommon.h:435
AccessByStringNotInitialized(const AccessByStringNotInitialized &)=default
AccessByStringNotInitialized(const QString &reason)
Definition LimaCommon.h:430
Use this exception to signal a wrong access to a LIMA dictionary.
Definition LimaCommon.h:450
AccessByStringOutOfRange & operator=(const AccessByStringOutOfRange &)=delete
AccessByStringOutOfRange(const AccessByStringOutOfRange &)=default
AccessByStringOutOfRange(const QString &reason)
Definition LimaCommon.h:452
AccessByStringOutOfRange(const std::string &reason)
Definition LimaCommon.h:456
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
InvalidConfiguration(const char *message="")
Definition LimaCommon.h:347
InvalidConfiguration(const std::string &message)
Definition LimaCommon.h:351
virtual ~InvalidConfiguration()=default
InvalidConfiguration(const QString &message)
Definition LimaCommon.h:355
The main LIMA exception class.
Definition LimaCommon.h:262
virtual ~LimaException()=default
virtual const char * what() const override
Definition LimaCommon.h:280
LimaException & operator=(const LimaException &e)=delete
LimaException(const char *message="")
Definition LimaCommon.h:264
LimaException(const std::string &message)
Definition LimaCommon.h:272
LimaException(const QString &message)
Definition LimaCommon.h:268
LimaException(const LimaException &)=default
std::string m_reason
Definition LimaCommon.h:286
Use this exception to signal that a media is used which has not been initialized.
Definition LimaCommon.h:369
virtual ~MediaNotInitialized()=default
MediaNotInitialized(const QString &media)
Definition LimaCommon.h:388
MediaNotInitialized(MediaId medId)
Definition LimaCommon.h:371
MediaNotInitialized(const std::string &media)
Definition LimaCommon.h:395
MediaNotInitialized & operator=(const MediaNotInitialized &)=delete
MediaNotInitialized(const MediaNotInitialized &)=default
Throw this exception when encountering problems with XML files opening or parsing.
Definition LimaCommon.h:470
XMLException(const QString &message)
Definition LimaCommon.h:477
XMLException(const std::string &message)
Definition LimaCommon.h:482
XMLException(const char *message="")
Definition LimaCommon.h:472
XMLException & operator=(const XMLException &)=delete
XMLException(const XMLException &)=default
QsLogging::Logger * pLogger
Definition LimaCommon.h:185
LogInit(char const *x)
Definition LimaCommon.h:170
static Categories & instance()
Level levelFor(const QString &category) const
static Logger & instance(const QString &zone=QLatin1String(""))
Definition QsLog.h:63
void setLoggingLevel(Level newLevel)
Logging at a level < 'newLevel' will be ignored.
Definition QsLog.cpp:115
NAUTITIA.
LimaStatusCode
Definition LimaCommon.h:236
@ UNKNOWN_FORMAT
Definition LimaCommon.h:244
@ CANNOT_OPEN_FILE_ERROR
Definition LimaCommon.h:239
@ UNKNOWN_ERROR
Definition LimaCommon.h:238
@ SUCCESS_ID
Definition LimaCommon.h:237
@ INVALID_CONFIGURATION
Definition LimaCommon.h:242
@ MISSING_DATA
Definition LimaCommon.h:243
@ OUT_OF_RANGE_ERROR
Definition LimaCommon.h:240
@ UNSUPPORTED_LANGUAGE
Definition LimaCommon.h:241
BOOST_STRONG_TYPEDEF(char, NoParameters)
STL namespace.