UniSet 2.32.1
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#if __cplusplus >= 201703L
214 std::list<ParamSInfo> getSInfoList_sv( std::string_view s, std::shared_ptr<uniset::Configuration> conf = nullptr );
215#endif
218 std::list<uniset::ConsumerInfo> getObjectsList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
219
224 bool is_digit( const std::string& s ) noexcept;
225#if __cplusplus >= 201703L
226 bool is_digit_sv( std::string_view s ) noexcept;
227#endif
228
234 std::string replace_all( const std::string& src, const std::string& from, const std::string& to );
235 // ---------------------------------------------------------------
236 // Работа с командной строкой
237
243 inline std::string getArgParam( const std::string& name,
244 int _argc, const char* const* _argv,
245 const std::string& defval = "" ) noexcept
246 {
247 for( int i = 1; i < (_argc - 1) ; i++ )
248 {
249 if( name == _argv[i] )
250 return _argv[i + 1];
251 }
252
253 return defval;
254 }
255
257 inline std::string getArg2Param(const std::string& name,
258 int _argc, const char* const* _argv,
259 const std::string& defval, const std::string& defval2 = "") noexcept
260 {
261 std::string s(uniset::getArgParam(name, _argc, _argv, ""));
262
263 if( !s.empty() )
264 return s;
265
266 if( !defval.empty() )
267 return defval;
268
269 return defval2;
270 }
271
272 inline int getArgInt( const std::string& name,
273 int _argc, const char* const* _argv,
274 const std::string& defval = "" ) noexcept
275 {
276 return uni_atoi(getArgParam(name, _argc, _argv, defval));
277 }
278
279 inline int getArgPInt( const std::string& name,
280 int _argc, const char* const* _argv,
281 const std::string& strdefval, int defval ) noexcept
282 {
283 std::string param = uniset::getArgParam(name, _argc, _argv, strdefval);
284
285 if( param.empty() && strdefval.empty() )
286 return defval;
287
288 return uniset::uni_atoi(param);
289 }
290
291
301 inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
302 {
303 for( int i = 1; i < _argc; i++ )
304 {
305 if( name == _argv[i] )
306 return i;
307 }
308
309 return -1;
310 }
311
312 // ---------------------------------------------------------------
313 // Калибровка
314
315 std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo& c );
316
317 // Функции калибровки значений
318 // raw - преобразуемое значение
319 // rawMin - минимальная граница исходного диапазона
320 // rawMax - максимальная граница исходного диапазона
321 // calMin - минимальная граница калиброванного диапазона
322 // calMin - минимальная граница калиброванного диапазона
323 // limit - обрезать итоговое значение по границам
324 float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit = true );
325 long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit = true );
326 double dcalibrate(double raw, double rawMin, double rawMax, double calMin, double calMax, bool limit = true );
327
328 // установка значения в нужный диапазон
329 long setinregion(long raw, long rawMin, long rawMax);
330 // установка значения вне диапазона
331 long setoutregion(long raw, long rawMin, long rawMax);
332
333 // ---------------------------------------------------------------
334 // Всякие helper-ы
335
336 bool file_exist( const std::string& filename );
337 bool directory_exist( const std::string& path );
338
339 // Проверка xml-узла на соответствие <...f_prop="f_val">,
340 // если не задано f_val, то проверяется, что просто f_prop!=""
341 bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val = "" ) noexcept;
342 bool check_filter_re( UniXML::iterator& it, const std::string& f_prop, const std::regex& re ) noexcept;
343
344 // RAII для флагов форматирования ostream..
346 {
347 public:
348 ios_fmt_restorer( std::ostream& s ):
349 os(s), f(nullptr)
350 {
351 f.copyfmt(s);
352 }
353
355 {
356 os.copyfmt(f);
357 }
358
359 ios_fmt_restorer( const ios_fmt_restorer& ) = delete;
360 ios_fmt_restorer& operator=( const ios_fmt_restorer& ) = delete;
361
362 private:
363 std::ostream& os;
364 std::ios f;
365 };
366
367 // -----------------------------------------------------------------------------------------
368} // end of namespace uniset
369// -----------------------------------------------------------------------------------------
370 inline bool operator==(const struct timespec &r1, const struct timespec &r2)
371 {
372 return (r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec);
373 }
374 inline bool operator!=(const struct timespec &r1, const struct timespec &r2)
375 {
376 return !(operator==(r1, r2));
377 }
378#endif
Definition UniSetTypes.h:117
Definition UniSetTypes.h:346
ThresholdState
Definition IOController_i.idl:203
STL namespace.
Definition Calibration.h:27
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:487
std::string dateToString(time_t tm=time(0), const std::string &brk="/") noexcept
Definition UniSetTypes.cc:701
int uni_atoi(const char *str) noexcept
Преобразование строки в число (воспринимает префикс 0, как 8-ное, префикс 0x, как 16-ное,...
Definition UniSetTypes.cc:771
int findArgParam(const std::string &name, int _argc, const char *const *_argv)
Definition UniSetTypes.h:301
std::list< std::string > ListObjectName
Definition UniSetTypes.h:92
bool is_digit(const std::string &s) noexcept
Definition UniSetTypes.cc:386
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:243
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:980
CORBA::Object_var ObjectVar
Definition UniSetTypes.h:95
uint64_t KeyType
Definition UniSetTypes.h:77
uniset::Timespec_var now_to_uniset_timespec()
Definition UniSetTypes.cc:913
long ThresholdId
Definition UniSetTypes_i.idl:31
std::string replace_all(const std::string &src, const std::string &from, const std::string &to)
Definition UniSetTypes.cc:856
const ThresholdId DefaultThresholdId
Definition UniSetTypes.h:72
std::string timeToString(time_t tm=time(0), const std::string &brk=":") noexcept
Definition UniSetTypes.cc:690
struct timespec now_to_timespec()
Definition UniSetTypes.cc:907
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:336
std::list< uniset::ConsumerInfo > getObjectsList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition UniSetTypes.cc:557
char checkBadSymbols(const std::string &str)
Definition UniSetTypes.cc:940
std::string BadSymbolsToStr()
Definition UniSetTypes.cc:955
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:257
long ObjectId
Definition UniSetTypes_i.idl:30
Definition IOController_i.idl:83
Definition IOController_i.idl:58
Definition IOController_i.idl:144
Definition IOController_i.idl:210
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