Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
StatusManager.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "StatusManager.h"
14 
15 using namespace kome::objects;
16 
17 
18 #include <crtdbg.h>
19 #ifdef _DEBUG
20  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
21  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
22 #endif // _DEBUG
23 
24 
25 
26 // constructor
28 }
29 
30 // destructor
32 }
33 
34 // set value
35 void StatusManager::setValue( const char* name, const char* value ) {
36  // create string object
37  std::string n = NVL( name, "" );
38  std::string v = NVL( value, "" );
39 
40  // get old value and add name to array
41  std::string oldValue;
42  bool flg = hasStatus( name );
43  if( flg ) {
44  oldValue = m_statusMap[ n ];
45  }
46  else {
47  m_statuses.push_back( n );
48  }
49 
50  // set value to map
51  if( value == NULL ) { // remove
52  m_statusMap.erase( n );
53  int idx = -1;
54  for( int i = 0; i < (int)m_statuses.size() && idx < 0; i++ ) {
55  if( m_statuses[ i ].compare( n ) == 0 ) {
56  idx = i;
57  }
58  }
59  if( idx >= 0 ) {
60  m_statuses.erase( m_statuses.begin() + idx );
61  }
62 
63  if( flg ) {
64  LOG_DEBUG( FMT( "Removing status value [%s=%s]", n.c_str(), oldValue.c_str() ) );
65  }
66  }
67  else { // update
68  m_statusMap[ n ] = v;
69  if( v.compare( oldValue ) != 0 ) {
70  LOG_DEBUG( FMT( "Setting status value [%s=%s]", n.c_str(), v.c_str() ) );
71  }
72  }
73 }
74 
75 // get value
76 const char* StatusManager::getValue( const char* name ) {
77  // check the map
78  if( m_statusMap.find( name ) != m_statusMap.end() ) {
79  return m_statusMap[ NVL( name, "" ) ].c_str();
80  }
81 
82  return NULL;
83 }
84 
85 // there is status specified name or not
86 bool StatusManager::hasStatus( const char* name ) {
87  const char* val = getValue( name );
88 
89  return ( val != NULL );
90 }
91 
92 // get the number of statuses
94  return m_statuses.size();
95 }
96 
97 // get status variable name
98 const char* StatusManager::getStatusName( const unsigned int index ) {
99  // check the parameter
100  if( index >= m_statuses.size() ) {
101  return NULL;
102  }
103 
104  return m_statuses[ index ].c_str();
105 }
106 
107 // set status values to ini file object
108 void StatusManager::setToIniFile( kome::core::IniFile& ini, const char* section ) {
109  // set to ini file
110  for( unsigned int i = 0; i < m_statuses.size(); i++ ) {
111  ini.setString( section, m_statuses[ i ].c_str(), m_statusMap[ m_statuses[ i ] ].c_str() );
112  }
113 }
114 
115 // get status values from ini file object
116 void StatusManager::getFromIniFile( kome::core::IniFile& ini, const char* section ) {
117  // get from ini file
118  for( unsigned int i = 0; i < ini.getNumberOfParameters( section ); i++ ) {
119  setValue( ini.getParameterName( section, i ), ini.getParameterValue( section, i ) );
120  }
121 }
122 
123 // get status value
124 const char* StatusManager::getStatusValue( const unsigned int index ) {
125  // check the parameter
126  if( index >= m_statuses.size() ) {
127  return NULL;
128  }
129 
130  return m_statusMap[ m_statuses[ index ] ].c_str();
131 }
132 
133 // get instance
135  // create object (This is the only object)
136  static StatusManager mgr;
137 
138  return mgr;
139 }
interfaces of StatusManager class
std::vector< std::string > m_statuses
Definition: StatusManager.h:43
static StatusManager & getInstance()
gets status manager object
const char * getParameterName(const char *section, const unsigned int index)
gets parameter name
Definition: IniFile.cpp:360
unsigned int getNumberOfStatuses()
gets the number of status variables
const char * getParameterValue(const char *section, const unsigned int index)
gets parameter value
Definition: IniFile.cpp:372
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
#define NULL
Definition: CoreMacros.h:18
const char * getStatusName(const unsigned int index)
gets status variable name
unsigned int getNumberOfParameters(const char *section)
gets the number of parameters specified section
Definition: IniFile.cpp:348
const char * getValue(const char *name)
gets status value
bool hasStatus(const char *name)
returns true if there is status variable specified name
void setToIniFile(kome::core::IniFile &ini, const char *section)
sets status values to ini file information object
const char * getStatusValue(const unsigned int index)
gets status value
void setValue(const char *name, const char *value)
sets status value
ini file management class
Definition: IniFile.h:30
status variables management class
Definition: StatusManager.h:24
void setString(const char *section, const char *key, const char *value)
sets string value
Definition: IniFile.cpp:64
void getFromIniFile(kome::core::IniFile &ini, const char *section)
gets status values from ini file information object
virtual ~StatusManager()
destructor
std::map< std::string, std::string > m_statusMap
Definition: StatusManager.h:40