UniSet @VERSION@
UniSetTypes.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// --------------------------------------------------------------------------
21// --------------------------------------------------------------------------
22#ifndef UniSetTypes_H_
23#define UniSetTypes_H_
24// --------------------------------------------------------------------------
25#include <memory>
26#include <cstdlib>
27#include <cstdio>
28#include <string>
29#include <list>
30#include <vector>
31#include <limits>
32#include <ostream>
33#include <chrono>
34#include <thread>
35#include <regex>
36
37#include <omniORB4/CORBA.h>
38#include "UniSetTypes_i.hh"
39#include "IOController_i.hh"
40#include "Mutex.h"
41#include "UniXML.h"
42#include "PassiveTimer.h" // for typedef timeout_t
43// -----------------------------------------------------------------------------------------
45inline void msleep( uniset::timeout_t m )
46{
47 std::this_thread::sleep_for(std::chrono::milliseconds(m));
48}
49
51namespace uniset
52{
53 class Configuration;
54 // ---------------------------------------------------------------
55 // Вспомогательные типы данных и константы
56
58 const char BadSymbols[] = {'.', '/'};
59
64 char checkBadSymbols(const std::string& str);
65
67 std::string BadSymbolsToStr();
68
69
70 /* hash32("DefaultObjectId") = 122387491 */
75 const ObjectId AdminID = -2;
77 typedef uint64_t KeyType;
84 KeyType key( const uniset::ObjectId id, const uniset::ObjectId node );
86
87 uint64_t hash64( const std::string& str ) noexcept;
88 uint64_t hash64( const char* buf, size_t sz ) noexcept;
89 uint32_t hash32( const std::string& str ) noexcept;
90 uint32_t hash32( const char* buf, size_t sz ) noexcept;
91
92 typedef std::list<std::string> ListObjectName;
94 typedef CORBA::Object_ptr ObjectPtr;
95 typedef CORBA::Object_var ObjectVar;
97 UniversalIO::IOType getIOType( const std::string& s ) noexcept;
98 std::string iotype2str( const UniversalIO::IOType& t ) noexcept;
99 std::ostream& operator<<( std::ostream& os, const UniversalIO::IOType t );
100 std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdInfo& ti );
101 std::ostream& operator<<( std::ostream& os, const IOController_i::ShortIOInfo& s );
102 std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdState& s);
103
106 {
107 lmpOFF = 0,
108 lmpON = 1,
111 lmpBLINK3 = 4
112 };
113
114 const long ChannelBreakValue = std::numeric_limits<long>::max();
115
116 class IDList
117 {
118 public:
119
120 IDList( const std::vector<std::string>& v );
121#if __cplusplus >= 201703L
122 IDList( const std::vector<std::string_view>& v );
123#endif
124 IDList();
125 ~IDList();
126
127 void add( ObjectId id );
128 void del( ObjectId id );
129
130 inline size_t size() const noexcept
131 {
132 return lst.size();
133 }
134 inline bool empty() const noexcept
135 {
136 return lst.empty();
137 }
138
139 std::list<ObjectId> getList() const noexcept;
140 const std::list<ObjectId>& ref() const noexcept;
141
142 // за освобождение выделенной памяти
143 // отвечает вызывающий!
144 IDSeq* getIDSeq() const;
145
146 //
147 ObjectId getFirst() const noexcept;
148 ObjectId node; // узел, на котором находятся датчики
149
150 private:
151 std::list<ObjectId> lst;
152 };
153
156 {
158 std::string repName = { "" };
159 std::string textName = { "" };
160 std::string name = { "" };
161 xmlNode* xmlnode = { nullptr };
162
163 inline bool operator < ( const ObjectInfo& o ) const
164 {
165 return (id < o.id);
166 }
167 };
168
169 typedef std::list<NodeInfo> ListOfNode;
170
171 // ---------------------------------------------------------------
172 // Различные преобразования
173
175 int uni_atoi( const char* str ) noexcept;
176 int uni_atoi( const std::string& str ) noexcept;
177#if __cplusplus >= 201703L
178 int uni_atoi_sv( std::string_view str ) noexcept;
179#endif
180
181 char* uni_strdup( const std::string& src );
182
183 std::string timeToString(time_t tm = time(0), const std::string& brk = ":") noexcept;
184 std::string dateToString(time_t tm = time(0), const std::string& brk = "/") noexcept;
186 struct timeval to_timeval( const std::chrono::system_clock::duration& d );
187 struct timespec to_timespec( const std::chrono::system_clock::duration& d );
188 struct timespec now_to_timespec();
190 uniset::Timespec_var to_uniset_timespec( const std::chrono::system_clock::duration& d );
191 uniset::Timespec_var now_to_uniset_timespec();
192 int64_t timespec_to_nanosec( const struct timespec& tm );
193
195 IDList explode( const std::string& str, char sep = ',' );
196 std::vector<std::string> explode_str( const std::string& str, char sep = ',' );
197#if __cplusplus >= 201703L
198 IDList split_by_id( std::string_view str, char sep = ',' );
199 std::vector<std::string_view> split_sv( std::string_view str, char sep = ',' );
200#endif
201
203 {
205 long val;
206 std::string fname; // fullname id@node or id
207 };
208
212 std::list<ParamSInfo> getSInfoList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
213
215 uniset::ParamSInfo parseSInfo( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
216#if __cplusplus >= 201703L
217 std::list<ParamSInfo> getSInfoList_sv( std::string_view s, std::shared_ptr<uniset::Configuration> conf = nullptr );
218 uniset::ParamSInfo parseSInfo_sv( std::string_view s, std::shared_ptr<uniset::Configuration> conf = nullptr );
219#endif
222 std::list<uniset::ConsumerInfo> getObjectsList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
223
228 bool is_digit( const std::string& s ) noexcept;
229#if __cplusplus >= 201703L
230 bool is_digit_sv( std::string_view s ) noexcept;
231#endif
232
238 std::string replace_all( const std::string& src, const std::string& from, const std::string& to );
239 // ---------------------------------------------------------------
240 // Работа с командной строкой
241
247 inline std::string getArgParam( const std::string& name,
248 int _argc, const char* const* _argv,
249 const std::string& defval = "" ) noexcept
250 {
251 for( int i = 1; i < (_argc - 1) ; i++ )
252 {
253 if( name == _argv[i] )
254 return _argv[i + 1];
255 }
256
257 return defval;
258 }
259
261 inline std::string getArg2Param(const std::string& name,
262 int _argc, const char* const* _argv,
263 const std::string& defval, const std::string& defval2 = "") noexcept
264 {
265 std::string s(uniset::getArgParam(name, _argc, _argv, ""));
266
267 if( !s.empty() )
268 return s;
269
270 if( !defval.empty() )
271 return defval;
272
273 return defval2;
274 }
275
276 inline int getArgInt( const std::string& name,
277 int _argc, const char* const* _argv,
278 const std::string& defval = "" ) noexcept
279 {
280 return uni_atoi(getArgParam(name, _argc, _argv, defval));
281 }
282
283 inline int getArgPInt( const std::string& name,
284 int _argc, const char* const* _argv,
285 const std::string& strdefval, int defval ) noexcept
286 {
287 std::string param = uniset::getArgParam(name, _argc, _argv, strdefval);
288
289 if( param.empty() && strdefval.empty() )
290 return defval;
291
292 return uniset::uni_atoi(param);
293 }
294
295
305 inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
306 {
307 for( int i = 1; i < _argc; i++ )
308 {
309 if( name == _argv[i] )
310 return i;
311 }
312
313 return -1;
314 }
315
316 // ---------------------------------------------------------------
317 // Калибровка
318
319 std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo& c );
320
321 // Функции калибровки значений
322 // raw - преобразуемое значение
323 // rawMin - минимальная граница исходного диапазона
324 // rawMax - максимальная граница исходного диапазона
325 // calMin - минимальная граница калиброванного диапазона
326 // calMin - минимальная граница калиброванного диапазона
327 // limit - обрезать итоговое значение по границам
328 float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit = true );
329 long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit = true );
330 double dcalibrate(double raw, double rawMin, double rawMax, double calMin, double calMax, bool limit = true );
331
332 // установка значения в нужный диапазон
333 long setinregion(long raw, long rawMin, long rawMax);
334 // установка значения вне диапазона
335 long setoutregion(long raw, long rawMin, long rawMax);
336
337 // ---------------------------------------------------------------
338 // Всякие helper-ы
339
340 bool file_exist( const std::string& filename );
341 bool directory_exist( const std::string& path );
342 bool create_directory( const std::string& path, bool with_parents = true );
343
344 // Проверка xml-узла на соответствие <...f_prop="f_val">,
345 // если не задано f_val, то проверяется, что просто f_prop!=""
346 bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val = "" ) noexcept;
347 bool check_filter_re( UniXML::iterator& it, const std::string& f_prop, const std::regex& re ) noexcept;
348
354 bool containsIgnoreCase( const std::string& text, const std::string& pattern ) noexcept;
355
356 // RAII для флагов форматирования ostream..
358 {
359 public:
360 ios_fmt_restorer( std::ostream& s ):
361 os(s), f(nullptr)
362 {
363 f.copyfmt(s);
364 }
365
367 {
368 os.copyfmt(f);
369 }
370
371 ios_fmt_restorer( const ios_fmt_restorer& ) = delete;
372 ios_fmt_restorer& operator=( const ios_fmt_restorer& ) = delete;
373
374 private:
375 std::ostream& os;
376 std::ios f;
377 };
378
379 // -----------------------------------------------------------------------------------------
380} // end of namespace uniset
381// -----------------------------------------------------------------------------------------
382 inline bool operator==(const struct timespec &r1, const struct timespec &r2)
383 {
384 return (r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec);
385 }
386 inline bool operator!=(const struct timespec &r1, const struct timespec &r2)
387 {
388 return !(operator==(r1, r2));
389 }
390#endif
Definition UniSetTypes.h:117
Definition UniSetTypes.h:358
ThresholdState
Definition IOController_i.idl:209
STL namespace.
Definition AccessConfig.h:30
struct timeval to_timeval(const std::chrono::system_clock::duration &d)
const ObjectId AdminID
Definition UniSetTypes.h:75
std::list< ParamSInfo > getSInfoList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition UniSetTypes.cc:536
std::string dateToString(time_t tm=time(0), const std::string &brk="/") noexcept
Definition UniSetTypes.cc:769
int uni_atoi(const char *str) noexcept
Преобразование строки в число (воспринимает префикс 0, как 8-ное, префикс 0x, как 16-ное,...
Definition UniSetTypes.cc:839
int findArgParam(const std::string &name, int _argc, const char *const *_argv)
Definition UniSetTypes.h:305
std::list< std::string > ListObjectName
Definition UniSetTypes.h:92
bool is_digit(const std::string &s) noexcept
Definition UniSetTypes.cc:409
CORBA::Object_ptr ObjectPtr
Definition UniSetTypes.h:94
std::string getArgParam(const std::string &name, int _argc, const char *const *_argv, const std::string &defval="") noexcept
Definition UniSetTypes.h:247
LampCommand
Definition UniSetTypes.h:106
@ lmpBLINK2
Definition UniSetTypes.h:110
@ lmpBLINK
Definition UniSetTypes.h:109
@ lmpON
Definition UniSetTypes.h:108
@ lmpOFF
Definition UniSetTypes.h:107
@ lmpBLINK3
Definition UniSetTypes.h:111
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition UniSetTypes.cc:1048
CORBA::Object_var ObjectVar
Definition UniSetTypes.h:95
uniset::ParamSInfo parseSInfo(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition UniSetTypes.cc:552
uint64_t KeyType
Definition UniSetTypes.h:77
uniset::Timespec_var now_to_uniset_timespec()
Definition UniSetTypes.cc:981
long ThresholdId
Definition UniSetTypes_i.idl:31
bool containsIgnoreCase(const std::string &text, const std::string &pattern) noexcept
Definition UniSetTypes.cc:744
std::string replace_all(const std::string &src, const std::string &from, const std::string &to)
Definition UniSetTypes.cc:924
const ThresholdId DefaultThresholdId
Definition UniSetTypes.h:72
std::string timeToString(time_t tm=time(0), const std::string &brk=":") noexcept
Definition UniSetTypes.cc:758
struct timespec now_to_timespec()
Definition UniSetTypes.cc:975
sequence< ObjectId > IDSeq
Definition UniSetTypes_i.idl:89
const char BadSymbols[]
Definition UniSetTypes.h:58
struct timespec to_timespec(const std::chrono::system_clock::duration &d)
const ThresholdId DefaultTimerId
Definition UniSetTypes.h:73
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
IDList explode(const std::string &str, char sep=',')
Definition UniSetTypes.cc:359
std::list< uniset::ConsumerInfo > getObjectsList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition UniSetTypes.cc:611
char checkBadSymbols(const std::string &str)
Definition UniSetTypes.cc:1008
std::string BadSymbolsToStr()
Definition UniSetTypes.cc:1023
std::string getArg2Param(const std::string &name, int _argc, const char *const *_argv, const std::string &defval, const std::string &defval2="") noexcept
Definition UniSetTypes.h:261
long ObjectId
Definition UniSetTypes_i.idl:30
Definition IOController_i.idl:89
Definition IOController_i.idl:64
Definition IOController_i.idl:150
Definition IOController_i.idl:216
Definition UniSetTypes.h:156
std::string repName
Definition UniSetTypes.h:158
std::string textName
Definition UniSetTypes.h:159
std::string name
Definition UniSetTypes.h:160
Definition UniSetTypes.h:203