UniSet  1.4.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 <unistd.h>
00029 #include <cstdlib>
00030 #include <cstdio>
00031 #include <string>
00032 #include <list>
00033 #include <limits>
00034 #include <ostream>
00035 
00036 #include <omniORB4/CORBA.h>
00037 #include "UniSetTypes_i.hh"
00038 #include "IOController_i.hh"
00039 #include "Mutex.h"
00040 #include "UniXML.h"
00041 // -----------------------------------------------------------------------------------------
00043 inline void msleep( unsigned int m ) { usleep(m*1000); }
00044 
00046 namespace UniSetTypes
00047 {
00048     class Configuration;
00049     extern Configuration* conf;
00050 
00051     typedef std::list<std::string> ListObjectName;  
00053     typedef ObjectId SysId;
00054     typedef CORBA::Object_ptr ObjectPtr;    
00055     typedef CORBA::Object_var ObjectVar;    
00058     inline static UniSetTypes::ObjectType getObjectType(const char * name) { const void *t = name;  return (UniSetTypes::ObjectType)t; }
00059 
00060     UniversalIO::IOTypes getIOType( const std::string s );
00061     std::ostream& operator<<( std::ostream& os, const UniversalIO::IOTypes t );
00062 
00063     std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo c );
00064 
00065 
00067     enum LampCommand
00068     {
00069         lmpOFF      = 0,    
00070         lmpON       = 1,    
00071         lmpBLINK    = 2,    
00072         lmpBLINK2   = 3,    
00073         lmpBLINK3   = 4     
00074     };
00075 
00076     static const long ChannelBreakValue = std::numeric_limits<long>::max();
00077 
00078     class IDList
00079     {
00080         public: 
00081             IDList();
00082             ~IDList();
00083 
00084             void add( ObjectId id );
00085             void del( ObjectId id );
00086     
00087             inline int size(){ return lst.size(); }
00088             inline bool empty(){ return lst.empty(); }
00089         
00090             std::list<ObjectId> getList();
00091 
00092             // за освобождение выделеной памяти
00093             // отвечает вызывающий!
00094             IDSeq* getIDSeq();
00095         
00096             // 
00097             ObjectId getFirst();
00098             ObjectId node;  // узел, на котором находятся датчики
00099         
00100         private:
00101             std::list<ObjectId> lst;
00102     };
00103 
00104     const ObjectId DefaultObjectId = -1;    
00106 //  typedef long MessageCode;                   
00107     const MessageCode DefaultMessageCode = 0;   
00109     const ThresholdId DefaultThresholdId = -1;      
00110     const ThresholdId DefaultTimerId = -1;      
00114     struct MessageInfo
00115     {
00116        UniSetTypes::MessageCode code;   
00117        std::string text;                
00118        std::string idname;              
00120         inline bool operator < ( const MessageInfo& m ) const
00121         {
00122             return (code < m.code);
00123         }
00124     };
00125 
00127     struct ObjectInfo
00128     {
00129         ObjectInfo():
00130             id(DefaultObjectId),
00131             repName(0),textName(0),data(0){}
00132 
00133         ObjectId id;        
00134         char* repName;      
00135         char* textName;     
00136         void* data;
00137     
00138         inline bool operator < ( const ObjectInfo& o ) const
00139         {
00140             return (id < o.id);
00141         }
00142     };
00143     
00144     typedef std::list<NodeInfo> ListOfNode;
00145 
00147     const char BadSymbols[]={'.','/'};
00148 
00149     class uniset_mutex;
00150     class uniset_mutex_lock;
00151 
00153     inline int uni_atoi( const char* str )
00154     {
00155         int n = 0; // if str is NULL or sscanf failed, we return 0
00156 
00157         if ( str != NULL )
00158             std::sscanf(str, "%i", &n);
00159         return n;
00160     }
00161     inline int uni_atoi( const std::string str )
00162     {
00163         return uni_atoi(str.c_str());
00164     }
00165 
00166 
00167     typedef long KeyType;   
00175     inline static KeyType key( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node )
00176     {
00177         return KeyType((id*node)+(id+2*node));
00178     }
00179 
00184     inline std::string getArgParam( const std::string name, 
00185                                         int _argc, const char* const* _argv,
00186                                             const std::string defval="" )
00187     {
00188         for( int i=1; i < (_argc - 1) ; i++ )
00189         {
00190             if( name == _argv[i] )
00191                 return _argv[i+1];
00192         }
00193         return defval;
00194     }
00195 
00196     inline int getArgInt( const std::string name, 
00197                             int _argc, const char* const* _argv,
00198                             const std::string defval="" )
00199     {
00200         return uni_atoi(getArgParam(name, _argc, _argv, defval));
00201     }
00202 
00208     inline int findArgParam( const std::string name, int _argc, const char* const* _argv )
00209     {
00210         for( int i=1; i<_argc; i++ )
00211         {
00212             if( name == _argv[i] )
00213                 return i;
00214         }
00215         return -1;
00216     }
00217 
00219     template<typename InputIterator,
00220              typename OutputIterator,
00221              typename Predicate>
00222     OutputIterator copy_if(InputIterator begin,
00223                             InputIterator end,
00224                             OutputIterator destBegin,
00225                             Predicate p)
00226     {
00227         while( begin!=end)
00228         {
00229             if( p(*begin) ) &destBegin++=*begin;
00230             ++begin;
00231         }
00232         return destBegin;
00233     }
00234 
00235     // Функции калибровки значений
00236     // raw      - преобразуемое значение
00237     // rawMin   - минимальная граница исходного диапазона
00238     // rawMax   - максимальная граница исходного диапазона
00239     // calMin   - минимальная граница калиброванного диапазона
00240     // calMin   - минимальная граница калиброванного диапазона
00241     // limit    - обрезать итоговое значение по границам 
00242     float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit=true );
00243     long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit=true );
00244 
00245     // установка значения в нужный диапазон
00246     long setinregion(long raw, long rawMin, long rawMax);
00247     // установка значения вне диапазона
00248     long setoutregion(long raw, long rawMin, long rawMax);
00249 
00250 
00251 
00252     bool file_exist( const std::string filename );
00253     
00254     IDList explode( const std::string str, char sep=',' );
00255     std::list<std::string> explode_str( const std::string str, char sep=',' );
00256     
00257     
00258     struct ParamSInfo
00259     {
00260         IOController_i::SensorInfo si;
00261         long val;
00262         std::string fname; // fullname id@node or id
00263     };
00264     
00265     // Функция разбора строки вида: id1@node1=val1,id2@node2=val2,...
00266     // Если '=' не указано, возвращается val=0
00267     // Если @node не указано, возвращается node=DefaultObjectId
00268     std::list<ParamSInfo> getSInfoList( std::string s, Configuration* conf=UniSetTypes::conf);
00269     bool is_digit( const std::string s );
00270 
00271     // Проверка xml-узла на соответсвие <...f_prop="f_val">,
00272     // если не задано f_val, то проверяется, что просто f_prop!=""
00273     bool check_filter( UniXML_iterator& it, const std::string f_prop, const std::string f_val="" );
00274 // -----------------------------------------------------------------------------
00275 }
00276 
00277 #define atoi atoi##_Do_not_use_atoi_function_directly_Use_getIntProp90,_getArgInt_or_uni_atoi
00278 
00279 // -----------------------------------------------------------------------------------------
00280 #endif