from_any_value.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Aldebaran
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 #include "from_any_value.hpp"
00019 
00020 namespace naoqi {
00021 
00022 namespace tools {
00023 
00024 NaoqiImage fromAnyValueToNaoqiImage(qi::AnyValue& value){
00025   qi::AnyReferenceVector anyref;
00026   NaoqiImage result;
00027   std::ostringstream ss;
00028   try{
00029     anyref = value.asListValuePtr();
00030   }
00031   catch(std::runtime_error& e)
00032   {
00033     ss << "Could not transform AnyValue into list: " << e.what();
00034     throw std::runtime_error(ss.str());
00035   }
00036   qi::AnyReference ref;
00037 
00039   ref = anyref[0].content();
00040   if(ref.kind() == qi::TypeKind_Int)
00041   {
00042     result.width = ref.asInt32();
00043   }
00044   else
00045   {
00046     ss << "Could not retrieve width";
00047     throw std::runtime_error(ss.str());
00048   }
00049 
00051   ref = anyref[1].content();
00052   if(ref.kind() == qi::TypeKind_Int)
00053   {
00054     result.height = ref.asInt32();
00055   }
00056   else
00057   {
00058     ss << "Could not retrieve height";
00059     throw std::runtime_error(ss.str());
00060   }
00061 
00063   ref = anyref[2].content();
00064   if(ref.kind() == qi::TypeKind_Int)
00065   {
00066     result.number_of_layers = ref.asInt32();
00067   }
00068   else
00069   {
00070     ss << "Could not retrieve number of layers";
00071     throw std::runtime_error(ss.str());
00072   }
00073 
00075   ref = anyref[3].content();
00076   if(ref.kind() == qi::TypeKind_Int)
00077   {
00078     result.colorspace = ref.asInt32();
00079   }
00080   else
00081   {
00082     ss << "Could not retrieve colorspace";
00083     throw std::runtime_error(ss.str());
00084   }
00085 
00087   ref = anyref[4].content();
00088   if(ref.kind() == qi::TypeKind_Int)
00089   {
00090     result.timestamp_s = ref.asInt32();
00091   }
00092   else
00093   {
00094     ss << "Could not retrieve timestamp_s";
00095     throw std::runtime_error(ss.str());
00096   }
00097 
00099   ref = anyref[5].content();
00100   if(ref.kind() == qi::TypeKind_Int)
00101   {
00102     result.timestamp_us = ref.asInt32();
00103   }
00104   else
00105   {
00106     ss << "Could not retrieve timestamp_us";
00107     throw std::runtime_error(ss.str());
00108   }
00109 
00111   ref = anyref[6].content();
00112   if(ref.kind() == qi::TypeKind_Raw)
00113   {
00114     result.buffer = (void*)ref.asRaw().first;
00115   }
00116   else
00117   {
00118     ss << "Could not retrieve buffer";
00119     throw std::runtime_error(ss.str());
00120   }
00121 
00123   ref = anyref[7].content();
00124   if(ref.kind() == qi::TypeKind_Int)
00125   {
00126     result.cam_id = ref.asInt32();
00127   }
00128   else
00129   {
00130     ss << "Could not retrieve cam_id";
00131     throw std::runtime_error(ss.str());
00132   }
00133 
00135   ref = anyref[8].content();
00136   if(ref.kind() == qi::TypeKind_Float)
00137   {
00138     result.fov_left = ref.asFloat();
00139   }
00140   else
00141   {
00142     ss << "Could not retrieve fov_left";
00143     throw std::runtime_error(ss.str());
00144   }
00145 
00147   ref = anyref[9].content();
00148   if(ref.kind() == qi::TypeKind_Float)
00149   {
00150     result.fov_top = ref.asFloat();
00151   }
00152   else
00153   {
00154     ss << "Could not retrieve fov_top";
00155     throw std::runtime_error(ss.str());
00156   }
00157 
00159   ref = anyref[10].content();
00160   if(ref.kind() == qi::TypeKind_Float)
00161   {
00162     result.fov_right = ref.asFloat();
00163   }
00164   else
00165   {
00166     ss << "Could not retrieve fov_right";
00167     throw std::runtime_error(ss.str());
00168   }
00169 
00171   ref = anyref[11].content();
00172   if(ref.kind() == qi::TypeKind_Float)
00173   {
00174     result.fov_bottom = ref.asFloat();
00175   }
00176   else
00177   {
00178     ss << "Could not retrieve fov_bottom";
00179     throw std::runtime_error(ss.str());
00180   }
00181   return result;
00182 }
00183 
00184 
00185 std::vector<float> fromAnyValueToFloatVector(qi::AnyValue& value, std::vector<float>& result){
00186   qi::AnyReferenceVector anyrefs = value.asListValuePtr();
00187 
00188   for(int i=0; i<anyrefs.size();i++)
00189   {
00190     try
00191     {
00192       result.push_back(anyrefs[i].content().toFloat());
00193     }
00194     catch(std::runtime_error& e)
00195     {
00196       result.push_back(-1.0);
00197       std::cout << e.what() << "=> set to -1" << std::endl;
00198     }
00199   }
00200   return result;
00201 }
00202 
00203 std::vector<std::string> fromAnyValueToStringVector(qi::AnyValue& value, std::vector<std::string>& result){
00204   qi::AnyReferenceVector anyrefs = value.asListValuePtr();
00205 
00206   for(int i=0; i<anyrefs.size();i++)
00207   {
00208     try
00209     {
00210       result.push_back(anyrefs[i].content().toString());
00211     }
00212     catch(std::runtime_error& e)
00213     {
00214       result.push_back("Not available");
00215       std::cout << e.what() << " => set to 'Not available'" << std::endl;
00216     }
00217   }
00218   return result;
00219 }
00220 
00221 }
00222 }


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sun Sep 17 2017 02:57:14