From 8abd9e6b3d241279e1fb3d766cc6422bac4521c1 Mon Sep 17 00:00:00 2001
From: gscalamera <graziano.scalamera@elettra.eu>
Date: Mon, 23 Dec 2024 13:40:06 +0100
Subject: [PATCH] Support array slices

---
 src/alarm_table.cpp   | 14 +++++++-------
 src/event_table.cpp   | 34 ++++++++++++----------------------
 src/formula_grammar.h | 17 +++++++++++++++--
 3 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/src/alarm_table.cpp b/src/alarm_table.cpp
index 6c1932c..f1a6d45 100644
--- a/src/alarm_table.cpp
+++ b/src/alarm_table.cpp
@@ -374,14 +374,14 @@ bool alarm_table::update(const string& alm_name, Tango::TimeVal ts, formula_res_
 		found->second.quality = res.quality;
 		bool status_on_delay;
 		if(found->second.on_delay > 0)		//if enabled on delay
-			status_on_delay = ((bool)(res.value[0] != 0)) && (found->second.on_counter >= 1) && ((ts.tv_sec - found->second.on_delay) > found->second.ts_on_delay.tv_sec);	//formula gives true and on delay has passed
+			status_on_delay = ((bool)(!res.value.empty() && res.value[0] != 0)) && (found->second.on_counter >= 1) && ((ts.tv_sec - found->second.on_delay) > found->second.ts_on_delay.tv_sec);	//formula gives true and on delay has passed
 		else
-			status_on_delay = (bool)(res.value[0] != 0);
+			status_on_delay = (bool)(!res.value.empty() && res.value[0] != 0);
 		bool status_off_delay;
 		if(found->second.off_delay > 0)		//if enabled off delay
-			status_off_delay = (!(bool)(res.value[0] != 0)) && (found->second.off_counter >= 1) && ((ts.tv_sec - found->second.off_delay) > found->second.ts_off_delay.tv_sec);	//formula gives false and off delay has passed
+			status_off_delay = (!(bool)(!res.value.empty() && res.value[0] != 0)) && (found->second.off_counter >= 1) && ((ts.tv_sec - found->second.off_delay) > found->second.ts_off_delay.tv_sec);	//formula gives false and off delay has passed
 		else
-			status_off_delay = !(bool)(res.value[0] != 0);
+			status_off_delay = !(bool)(!res.value.empty() && res.value[0] != 0);
 		//if status changed:
 		// - from S_NORMAL to S_ALARM considering also on delay
 		//or
@@ -389,12 +389,12 @@ bool alarm_table::update(const string& alm_name, Tango::TimeVal ts, formula_res_
 		if((status_on_delay && (found->second.stat == S_NORMAL)) || (status_off_delay && (found->second.stat == S_ALARM)))
 		{
 			ret_changed=true;
-			if((bool)(res.value[0] != 0))
+			if((bool)(!res.value.empty() && res.value[0] != 0))
 				found->second.ack = NOT_ACK;	//if changing from NORMAL to ALARM -> NACK
 			//a.grp = found->second.grp2str();
 			//a.msg = (int)(res.value[0]) ? found->second.msg : "";
 			found->second.ts = ts;	/* store event timestamp into alarm timestamp */ //here update ts only if status changed
-			if((bool)(res.value[0] != 0))
+			if((bool)(!res.value.empty() && res.value[0] != 0))
 			{
 				found->second.is_new = 1;		//here set this alarm as new, read attribute set it to 0	//12-06-08: StopNew command set it to 0
 				if(found->second.dp_a && ((ts.tv_sec - startup_complete.tv_sec) > 10))		//action from S_NORMAL to S_ALARM
@@ -525,7 +525,7 @@ bool alarm_table::update(const string& alm_name, Tango::TimeVal ts, formula_res_
 			found->second.error = true;
 		}
 
-		if((bool)(res.value[0] != 0)) {
+		if((bool)(!res.value.empty() && res.value[0] != 0)) {
 			found->second.on_counter++;
 			found->second.off_counter = 0;
 		} else {
diff --git a/src/event_table.cpp b/src/event_table.cpp
index 2f9f977..af8cf4f 100644
--- a/src/event_table.cpp
+++ b/src/event_table.cpp
@@ -201,6 +201,8 @@ event::event(string& s, value_t& v, Tango::TimeVal& t) : \
 	event_id = SUB_ERR;
 	err_counter = 0;
 	valid = false;
+	dim_x=0;
+	dim_y=0;
 }
 
 event::event(string& s) : name(s)
@@ -222,7 +224,9 @@ event::event(string& s) : name(s)
 	type = -1;
 	event_id = SUB_ERR;
 	err_counter = 0;
-	valid = false;	
+	valid = false;
+	dim_x=0;
+	dim_y=0;
 }
 
 bool event::operator==(const event& e)
@@ -289,27 +293,10 @@ void event_table::summary(list<string> &evs)
 
 			ev_summary << KEY(EVENT_TIME_KEY) << time_buf << SEP;
 			ostringstream tmp_val;
-			//if(i->valid)
-			{
-				tmp_val << "[" ;
-				if(i->type != Tango::DEV_STRING)
-				{
-					if(i->read_size > 0 && i->value.size() > 0)
-					{
-						for(size_t k=0; k<i->read_size && k<i->value.size(); k++)
-						{
-							tmp_val << i->value[k];
-							if(k < i->read_size-1 && k<i->value.size()-1)
-								tmp_val << ",";
-						}
-					}
-				}
-				else
-				{
-					tmp_val << "\""<<i->value_string<<"\"";
-				}
-				tmp_val << "]";
-			}
+			if(i->type != Tango::DEV_STRING)
+				tmp_val << print_array(i->value,i->dim_x,i->dim_y);
+			else
+				tmp_val << "[\""<<i->value_string<<"\"]";
 			ev_summary << KEY(ATTR_VALUES_KEY) << tmp_val.str() << SEP;
 			ostringstream tmp_ex;
 			//tmp_ex.str("");
@@ -838,6 +825,9 @@ void event_table::add(string &signame, vector<string> contexts, int to_do, bool
 			signal->running = false;
 			signal->stopped = true;
 			signal->paused = false;
+			signal->valid = false;
+			signal->dim_x=0;
+			signal->dim_y=0;		
 			//DEBUG_STREAM << "event_table::"<<__func__<<": signame="<<signame<<" created signal"<< endl;
 		}
 		else if(found && start)
diff --git a/src/formula_grammar.h b/src/formula_grammar.h
index 8a83118..54aca74 100644
--- a/src/formula_grammar.h
+++ b/src/formula_grammar.h
@@ -125,6 +125,8 @@ struct formula_grammar : public grammar<formula_grammar>
     static const int val_qualityID = 23;
     static const int val_alarm_enum_stID = 24;
     static const int propertyID = 25;
+	static const int index_rangeID = 26; 
+	static const int index_listID = 27; 
 
     
     symbols<unsigned int> tango_states;
@@ -207,9 +209,18 @@ struct formula_grammar : public grammar<formula_grammar>
             			]
             		]
 //            	=	repeat_p(3)[(+symbol) >> ch_p('/')] >> (+symbol) 
-            	; 
+            	;
+            index_range
+            	= 	
+					( uint_p >> !(discard_node_d[ch_p('-')] >> uint_p))	// n or n-m
+            	;
+            index_list
+            	= 	(index_range >> *(discard_node_d[ch_p(',')] >> index_range)) // n-m,k,s-t,..
+            	;
             index
-            	= 	inner_node_d[ch_p('[') >> uint_p >> ch_p(']')]
+				=  discard_node_d[ch_p('[')] >> 
+				(str_p("-1") | index_list) >> 
+				discard_node_d[ch_p(']')]
             	;
             property
             	= 	str_p(".quality")
@@ -399,6 +410,8 @@ struct formula_grammar : public grammar<formula_grammar>
         rule<ScannerT, parser_context<>, parser_tag<expr_atomID> > expr_atom;
         rule<ScannerT, parser_context<>, parser_tag<funcID> > function;
 		rule<ScannerT, parser_context<>, parser_tag<nameID> > name;
+		rule<ScannerT, parser_context<>, parser_tag<index_rangeID> > index_range;
+		rule<ScannerT, parser_context<>, parser_tag<index_listID> > index_list;
 		rule<ScannerT, parser_context<>, parser_tag<indexID> > index;
 		rule<ScannerT, parser_context<>, parser_tag<val_stringID> > val_string;
 		rule<ScannerT, parser_context<>, parser_tag<func_dualID> > function_dual;
-- 
GitLab