UniSet  2.0.0
UniSetTypes.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002-2005 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00024 // -------------------------------------------------------------------------- 
00025 #ifndef UniSetTypes_H_
00026 #define UniSetTypes_H_
00027 // -------------------------------------------------------------------------- 
00028 #include <memory>
00029 #include <cstdlib>
00030 #include <cstdio>
00031 #include <string>
00032 #include <list>
00033 #include <limits>
00034 #include <ostream>
00035 #include <unistd.h>
00036 
00037 #include <omniORB4/CORBA.h>
00038 #include "UniSetTypes_i.hh"
00039 #include "IOController_i.hh"
00040 #include "Mutex.h"
00041 #include "UniXML.h"
00042 // -----------------------------------------------------------------------------------------
00044 inline void msleep( unsigned int m ) { usleep(m*1000); }
00045 
00047 namespace UniSetTypes
00048 {
00049     class Configuration;
00050     // ---------------------------------------------------------------
00051     // Вспомогательные типы данных и константы
00052 
00053     const ObjectId DefaultObjectId = -1;    
00054     const ThresholdId DefaultThresholdId = -1;      
00055     const ThresholdId DefaultTimerId = -1;      
00057     typedef unsigned long KeyType;    
00067     inline static KeyType key( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node )
00068     {
00069         return KeyType((id*node)+(id+2*node));
00070     }
00071 
00072     inline static KeyType key( const IOController_i::SensorInfo& si )
00073     {
00074         return key(si.id,si.node);
00075     }
00076 
00077     typedef std::list<std::string> ListObjectName;    
00079     typedef ObjectId SysId;
00080     typedef CORBA::Object_ptr ObjectPtr;    
00081     typedef CORBA::Object_var ObjectVar;    
00083     UniversalIO::IOType getIOType( const std::string& s );
00084     std::ostream& operator<<( std::ostream& os, const UniversalIO::IOType t );
00085 
00087     enum LampCommand
00088     {
00089         lmpOFF      = 0,    
00090         lmpON       = 1,    
00091         lmpBLINK    = 2,    
00092         lmpBLINK2   = 3,    
00093         lmpBLINK3   = 4     
00094     };
00095 
00096     static const long ChannelBreakValue = std::numeric_limits<long>::max();
00097 
00098     class IDList
00099     {
00100         public:
00101             IDList();
00102             ~IDList();
00103 
00104             void add( ObjectId id );
00105             void del( ObjectId id );
00106 
00107             inline int size() const { return lst.size(); }
00108             inline bool empty() const { return lst.empty(); }
00109 
00110             std::list<ObjectId> getList();
00111 
00112             // за освобождение выделеной памяти
00113             // отвечает вызывающий!
00114             IDSeq* getIDSeq() const;
00115 
00116             //
00117             ObjectId getFirst() const;
00118             ObjectId node;    // узел, на котором находятся датчики
00119 
00120         private:
00121             std::list<ObjectId> lst;
00122     };
00123 
00125     struct ObjectInfo
00126     {
00127         ObjectInfo():
00128             id(DefaultObjectId),
00129             repName(0),textName(0),data(0){}
00130 
00131         ObjectId id;        
00132         char* repName;        
00133         char* textName;        
00134         void* data;
00135 
00136         inline bool operator < ( const ObjectInfo& o ) const
00137         {
00138             return (id < o.id);
00139         }
00140     };
00141 
00142     typedef std::list<NodeInfo> ListOfNode;
00143 
00145     const char BadSymbols[]={'.','/'};
00146 
00147     // ---------------------------------------------------------------
00148     // Различные преобразования
00149 
00151     int uni_atoi( const char* str );
00152     inline int uni_atoi( const std::string& str )
00153     {
00154         return uni_atoi(str.c_str());
00155     }
00156 
00157     std::string timeToString(time_t tm=time(0), const std::string& brk=":"); 
00158     std::string dateToString(time_t tm=time(0), const std::string& brk="/"); 
00161     IDList explode( const std::string& str, char sep=',' );
00162     std::list<std::string> explode_str( const std::string& str, char sep=',' );
00163 
00164     struct ParamSInfo
00165     {
00166         IOController_i::SensorInfo si;
00167         long val;
00168         std::string fname; // fullname id@node or id
00169     };
00170 
00174     std::list<ParamSInfo> getSInfoList( const std::string& s, std::shared_ptr<UniSetTypes::Configuration> conf = nullptr );
00175 
00177     bool is_digit( const std::string& s );
00178 
00179     // ---------------------------------------------------------------
00180     // Работа с командной строкой
00181 
00186     inline std::string getArgParam( const std::string& name,
00187                                         int _argc, const char* const* _argv,
00188                                             const std::string& defval="" )
00189     {
00190         for( int i=1; i < (_argc - 1) ; i++ )
00191         {
00192             if( name == _argv[i] )
00193                 return _argv[i+1];
00194         }
00195         return defval;
00196     }
00197 
00198     inline int getArgInt( const std::string& name,
00199                             int _argc, const char* const* _argv,
00200                             const std::string defval="" )
00201     {
00202         return uni_atoi(getArgParam(name, _argc, _argv, defval));
00203     }
00204 
00210     inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
00211     {
00212         for( int i=1; i<_argc; i++ )
00213         {
00214             if( name == _argv[i] )
00215                 return i;
00216         }
00217         return -1;
00218     }
00219 
00220     // ---------------------------------------------------------------
00221     // Калибровка
00222 
00223     std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo c );
00224 
00225     // Функции калибровки значений
00226     // raw         - преобразуемое значение
00227     // rawMin     - минимальная граница исходного диапазона
00228     // rawMax     - максимальная граница исходного диапазона
00229     // calMin     - минимальная граница калиброванного диапазона
00230     // calMin     - минимальная граница калиброванного диапазона
00231     // limit    - обрезать итоговое значение по границам
00232     float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit=true );
00233     long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit=true );
00234 
00235     // установка значения в нужный диапазон
00236     long setinregion(long raw, long rawMin, long rawMax);
00237     // установка значения вне диапазона
00238     long setoutregion(long raw, long rawMin, long rawMax);
00239 
00240     // ---------------------------------------------------------------
00241     // Всякие helper-ы
00242 
00243     bool file_exist( const std::string& filename );
00244 
00245     // Проверка xml-узла на соответствие <...f_prop="f_val">,
00246     // если не задано f_val, то проверяется, что просто f_prop!=""
00247     bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val="" );
00248 
00250     template<typename InputIterator,
00251              typename OutputIterator,
00252              typename Predicate>
00253     OutputIterator copy_if(InputIterator begin,
00254                             InputIterator end,
00255                             OutputIterator destBegin,
00256                             Predicate p)
00257     {
00258         while( begin!=end)
00259         {
00260             if( p(*begin) ) &destBegin++=*begin;
00261             ++begin;
00262         }
00263         return destBegin;
00264     }
00265 // -----------------------------------------------------------------------------
00266 }
00267 
00268 // Варварский запрет на использование atoi вместо uni_atoi..
00269 // #define atoi atoi##_Do_not_use_atoi_function_directly_Use_getIntProp90,_getArgInt_or_uni_atoi
00270 
00271 // -----------------------------------------------------------------------------------------
00272 #endif