MachineIntelligenceCore:Toolchain
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ApplicationState.cpp
Go to the documentation of this file.
1 
24 
26 
27 #include <logger/Log.hpp>
28 
29 namespace mic {
30 namespace application {
31 
32 // Init application instance - as NULL.
33 boost::atomic<ApplicationState*> ApplicationState::instance_(NULL);
34 
35 // Initilize mutex.
37 
38 // Initilize internal data synchronization mutex.
40 
41 // Initilize external data synchronization mutex.
43 
44 
46  // Try to load the instance - first check.
47  ApplicationState* tmp = instance_.load(boost::memory_order_consume);
48  // If instance does not exist.
49  if (!tmp) {
50  // Enter critical section.
51  boost::mutex::scoped_lock guard(instantiation_mutex);
52  // Try to load the instance - second check.
53  tmp = instance_.load(boost::memory_order_consume);
54  // If still does not exist - create new instance.
55  if (!tmp) {
56  tmp = new ApplicationState;
57  instance_.store(tmp, boost::memory_order_release);
58  }//: if
59  // Exit critical section.
60  }//: if
61  // Return instance.
62  return tmp;
63 }
64 
65 
66 ApplicationState::ApplicationState() : PropertyTree("app_state"),
67  pause_mode("pause_mode", false),
68  single_step_mode("single_step_mode", false),
69  learning_mode("learning_mode", false),
70  application_sleep_interval("application_sleep_interval", 1000),
71  application(NULL)
72 {
73  // Register properties - so their values can be overridden (read from the configuration file).
78 
79  // Set default application state flags.
80  quit_flag = false;
81 
82  // Set using gui/cli/visualization to false.
83  using_opengl = false;
84  using_ncurses = false;
85 }
86 
87 
88 
89 // ---------------------- Quit MANAGEMENT.
90 
92  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
93  return quit_flag;
94 }
95 
97  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
98  quit_flag = true;
99 }
100 
102  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
103  quit_flag = false;
104 }
105 
106 
107 // ---------------------- Pause mode MANAGEMENT.
108 
110  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
111  return pause_mode;
112 }
113 
115  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
117 }
118 
119 
120 // ---------------------- Single step mode MANAGEMENT.
121 
122 
124  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
125  return single_step_mode;
126 }
127 
129  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
131 }
132 
133 
134 // ---------------------- Learning mode MANAGEMENT.
135 
136 
138  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
139  return learning_mode;
140 }
141 
143  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
145 }
146 
148  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
149  learning_mode = true;
150 }
151 
153  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
154  learning_mode = false;
155 }
156 
157 
158 // ---------------------- Using NCURSES mode MANAGEMENT.
159 
160 
162  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
163  return using_ncurses;
164 }
165 
166 
168  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
169  using_ncurses = true;
170 }
171 
173  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
174  using_ncurses = false;
175 }
176 
177 
178 // ---------------------- Using OpenGL mode MANAGEMENT.
179 
180 
182  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
183  return using_opengl;
184 }
185 
187  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
188  using_opengl = true;
189 }
190 
191 
193  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
194  using_opengl = false;
195 }
196 
197 
198 
199 // ---------------------- SLEEP INTERVAL MANAGEMENT.
200 
201 
202 void ApplicationState::setSleepInterval(double sleep_interval_) {
203  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
204  application_sleep_interval = sleep_interval_;
205  // Truncate sleep interval.
208 }
209 
211  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
213 }
214 
215 void ApplicationState::setSleepIntervalS(double sleep_interval_in_seconds) {
216  setSleepInterval(1000000*sleep_interval_in_seconds);
217 }
218 
219 void ApplicationState::setSleepIntervalMS(double sleep_interval_in_miliseconds) {
220  setSleepInterval(1000*sleep_interval_in_miliseconds);
221 }
222 
223 void ApplicationState::setSleepIntervalUS(double sleep_interval_in_microseconds) {
224  setSleepInterval(sleep_interval_in_microseconds);
225 }
226 
228  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
229  // Special case: leave 1, keeping the integer rounding of (>) ? : operator.
232  else
234  // Truncate sleep interval - with rounding to integers.
236  LOG(LSTATUS) << "Setting sleep interval to " << application_sleep_interval << " [us]";
237 }
238 
240  boost::mutex::scoped_lock lock(internal_data_synchronization_mutex);
242  // Truncate sleep interval - with rounding to integers.
244  LOG(LSTATUS) << "Setting sleep interval to " << application_sleep_interval << " [us]";
245 }
246 
247 
249  LOG(LSTATUS) <<"----------------------------------------------------------------";
250  LOG(LSTATUS) <<"Application status:";
251  LOG(LSTATUS) <<"----------------------------------------------------------------";
252  // Quit flag.
253  LOG(LSTATUS) << "QUIT:\t\t\t" << ((quit_flag) ? "YES" : "NO");
254  // Time interval.
255  LOG(LSTATUS) << "SLEEP INTERVAL:\t\t" << application_sleep_interval << " [ms]";
256 
257  // Modes.
258  LOG(LSTATUS) << "PAUSE MODE:\t\t" << ((pause_mode) ? "ON" : "OFF");
259  LOG(LSTATUS) << "SINGLE STEP MODE:\t" << ((single_step_mode) ? "ON" : "OFF");
260  LOG(LSTATUS) << "LEARNING:\t\t" << ((learning_mode) ? "ON" : "OFF");
261 
262  // Using gui/cli/visualization.
263  LOG(LSTATUS) << "USING OPENGL :\t\t" << ((using_opengl) ? "YES" : "NO");
264  LOG(LSTATUS) << "USING NCURSES:\t\t" << ((using_ncurses) ? "YES" : "NO");
265 
266  // Displays application "extended status"
267  if (application)
269  LOG(LSTATUS) <<"----------------------------------------------------------------";
270 }
271 
272 
274  application = application_;
275 }
276 
277 
278 
279 } /* namespace application */
280 } /* namespace mic */
281 
static ApplicationState * getInstance()
Class storing the state of the application (modes, sleep interval etc.). Defined in the form of a sin...
void setSleepIntervalUS(double sleep_interval_in_microseconds)
Base class for all applications.
Definition: Application.hpp:38
mic::configuration::Property< double > application_sleep_interval
void setSleepInterval(double sleep_interval_)
mic::configuration::Property< bool > pause_mode
Property: pause application mode.
Contains declaration of a base class for all applications.
void setApplication(mic::application::Application *application_)
Contains definitions of main logger-related macros.
mic::configuration::Property< bool > learning_mode
Property: learning/testing mode.
void setSleepIntervalS(double sleep_interval_in_seconds)
mic::application::Application * application
bool using_ncurses
Flag denoting whether application is using NCurses-based CLI.
static boost::atomic< ApplicationState * > instance_
ApplicationState declaration.
#define LSTATUS
Definition: LoggerAux.hpp:56
static boost::mutex external_data_synchronization_mutex
#define LOG(level)
Macro for message printing.
Definition: Log.hpp:39
mic::configuration::Property< bool > single_step_mode
Property: single step mode.
bool quit_flag
Quit application flag.
void setSleepIntervalMS(double sleep_interval_in_miliseconds)
void registerProperty(PropertyInterface &prop)
bool using_opengl
Flag denoting whether application is using OpenGL-based visualization.
static boost::mutex internal_data_synchronization_mutex