Use C++ std::vector
to set attributes#
This page contains examples on how to use the C++ vector class to set and get attribute values on the server side.
Warning
Tango does not create copies of data for optimization reasons and because of this all of the attribute
set_value()
methods only take pointers as an input. If you are going to
use C++ vectors you should be aware of the fact that you are going to be
copying the data, which may slow down the execution time when working with
large amounts of data.
The std::vector
class takes care of the memory in its entries so it is mandatory to leave the optional
release
parameter of Attribute::set_value
set to the default of false
.
Below are two examples of setting data for a read attribute using a vector of shorts and a vector of strings:
1void MyClass::read_spectrum(Tango::Attribute &attr)
2{
3 DEBUG_STREAM << "MyClass::read_Spectrum(Tango::Attribute &attr) entering... "<< std::endl;
4 /*----- PROTECTED REGION ID(MyClass::read_Spectrum) ENABLED START -----*/
5
6 std::vector<Tango::DevShort> val;
7 vec.emplace_back(1);
8 vec.emplace_back(2);
9 vec.emplace_back(3);
10
11 attr.set_value(val.data(), val.size());
12
13 /*----- PROTECTED REGION END -----*/ // MyClass::read_Spectrum
14}
1void MyClass::read_string_spectrum(Tango::Attribute &attr)
2{
3 DEBUG_STREAM << "MyClass::read_StringSpectrum(Tango::Attribute &attr) entering... "<< std::endl;
4 /*----- PROTECTED REGION ID(MyClass::read_StringSpectrum) ENABLED START -----*/
5
6 std::vector<std::string> vec;
7 vec.emplace_back("Hello");
8 vec.emplace_back("foggy");
9 vec.emplace_back("garden!");
10
11 attr.set_value(vec.data(), vec.size());
12
13 /*----- PROTECTED REGION END -----*/ // MyClass::read_StringSpectrum
14}
Below is an example for a writeable attribute using a vector of doubles:
1void MyClass::write_double_spectrum(Tango::WAttribute &attr)
2{
3 DEBUG_STREAM << "MyClass::write_double_spectrum(Tango::WAttribute &attr) entering... " << std::endl;
4 // Retrieve number of write values
5 int w_length = attr.get_write_value_length();
6
7 // Retrieve pointer on write values (Do not delete !)
8 const Tango::DevDouble *w_val;
9 attr.get_write_value(w_val);
10 /*----- PROTECTED REGION ID(MyClass::write_double_spectrum) ENABLED START -----*/
11
12 // not strictly needed, but makes the code easier to grasp
13 if(w_length == 0)
14 {
15 return;
16 }
17
18 std::vector<double> vec;
19 vec.assign(w_val, w_val + w_length);
20
21 /*----- PROTECTED REGION END -----*/ // MyClass::write_double_spectrum
22}