config.cc
Go to the documentation of this file.
00001 /*
00002  * This file is part of the rc_genicam_api package.
00003  *
00004  * Copyright (c) 2017 Roboception GmbH
00005  * All rights reserved
00006  *
00007  * Author: Heiko Hirschmueller
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright notice,
00013  * this list of conditions and the following disclaimer.
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  * this list of conditions and the following disclaimer in the documentation
00017  * and/or other materials provided with the distribution.
00018  *
00019  * 3. Neither the name of the copyright holder nor the names of its contributors
00020  * may be used to endorse or promote products derived from this software without
00021  * specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  * POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 #include "config.h"
00037 
00038 #include <stdexcept>
00039 #include <iomanip>
00040 
00041 #include "Base/GCException.h"
00042 
00043 namespace rcg
00044 {
00045 
00046 bool setBoolean(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00047                 bool value, bool exception)
00048 {
00049   bool ret=false;
00050 
00051   try
00052   {
00053     GenApi::INode *node=nodemap->_GetNode(name);
00054 
00055     if (node != 0)
00056     {
00057       if (GenApi::IsWritable(node))
00058       {
00059         GenApi::IBoolean *val=dynamic_cast<GenApi::IBoolean *>(node);
00060 
00061         if (val != 0)
00062         {
00063           val->SetValue(value);
00064           ret=true;
00065         }
00066         else if (exception)
00067         {
00068           throw std::invalid_argument(std::string("Feature not boolean: ")+name);
00069         }
00070       }
00071       else if (exception)
00072       {
00073         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00074       }
00075     }
00076     else if (exception)
00077     {
00078       throw std::invalid_argument(std::string("Feature not found: ")+name);
00079     }
00080   }
00081   catch (const GENICAM_NAMESPACE::GenericException &ex)
00082   {
00083     if (exception)
00084     {
00085       throw std::invalid_argument(ex.what());
00086     }
00087   }
00088 
00089   return ret;
00090 }
00091 
00092 bool setInteger(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00093                 int64_t value, bool exception)
00094 {
00095   bool ret=false;
00096 
00097   try
00098   {
00099     GenApi::INode *node=nodemap->_GetNode(name);
00100 
00101     if (node != 0)
00102     {
00103       if (GenApi::IsWritable(node))
00104       {
00105         GenApi::IInteger *val=dynamic_cast<GenApi::IInteger *>(node);
00106 
00107         if (val != 0)
00108         {
00109           val->SetValue(value);
00110           ret=true;
00111         }
00112         else if (exception)
00113         {
00114           throw std::invalid_argument(std::string("Feature not integer: ")+name);
00115         }
00116       }
00117       else if (exception)
00118       {
00119         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00120       }
00121     }
00122     else if (exception)
00123     {
00124       throw std::invalid_argument(std::string("Feature not found: ")+name);
00125     }
00126   }
00127   catch (const GENICAM_NAMESPACE::GenericException &ex)
00128   {
00129     if (exception)
00130     {
00131       throw std::invalid_argument(ex.what());
00132     }
00133   }
00134 
00135   return ret;
00136 }
00137 
00138 bool setIPV4Address(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00139                     const char *value, bool exception)
00140 {
00141   bool ret=false;
00142 
00143   try
00144   {
00145     GenApi::INode *node=nodemap->_GetNode(name);
00146 
00147     if (node != 0)
00148     {
00149       if (GenApi::IsWritable(node))
00150       {
00151         GenApi::IInteger *val=dynamic_cast<GenApi::IInteger *>(node);
00152 
00153         if (val != 0)
00154         {
00155           int64_t ip=0;
00156 
00157           std::stringstream in(value);
00158           std::string elem;
00159 
00160           for (int i=0; i<4; i++)
00161           {
00162             getline(in, elem, '.');
00163             ip=(ip<<8)|(stoi(elem)&0xff);
00164           }
00165 
00166           val->SetValue(ip);
00167           ret=true;
00168         }
00169         else if (exception)
00170         {
00171           throw std::invalid_argument(std::string("Feature not integer: ")+name);
00172         }
00173       }
00174       else if (exception)
00175       {
00176         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00177       }
00178     }
00179     else if (exception)
00180     {
00181       throw std::invalid_argument(std::string("Feature not found: ")+name);
00182     }
00183   }
00184   catch (const GENICAM_NAMESPACE::GenericException &ex)
00185   {
00186     if (exception)
00187     {
00188       throw std::invalid_argument(ex.what());
00189     }
00190   }
00191 
00192   return ret;
00193 }
00194 
00195 bool setFloat(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00196               double value, bool exception)
00197 {
00198   bool ret=false;
00199 
00200   try
00201   {
00202     GenApi::INode *node=nodemap->_GetNode(name);
00203 
00204     if (node != 0)
00205     {
00206       if (GenApi::IsWritable(node))
00207       {
00208         GenApi::IFloat *val=dynamic_cast<GenApi::IFloat *>(node);
00209 
00210         if (val != 0)
00211         {
00212           val->SetValue(value);
00213           ret=true;
00214         }
00215         else if (exception)
00216         {
00217           throw std::invalid_argument(std::string("Feature not float: ")+name);
00218         }
00219       }
00220       else if (exception)
00221       {
00222         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00223       }
00224     }
00225     else if (exception)
00226     {
00227       throw std::invalid_argument(std::string("Feature not found: ")+name);
00228     }
00229   }
00230   catch (const GENICAM_NAMESPACE::GenericException &ex)
00231   {
00232     if (exception)
00233     {
00234       throw std::invalid_argument(ex.what());
00235     }
00236   }
00237 
00238   return ret;
00239 }
00240 
00241 bool setEnum(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00242              const char *value, bool exception)
00243 {
00244   bool ret=false;
00245 
00246   try
00247   {
00248     GenApi::INode *node=nodemap->_GetNode(name);
00249 
00250     if (node != 0)
00251     {
00252       if (GenApi::IsWritable(node))
00253       {
00254         GenApi::IEnumeration *val=dynamic_cast<GenApi::IEnumeration *>(node);
00255 
00256         if (val != 0)
00257         {
00258           GenApi::IEnumEntry *entry=val->GetEntryByName(value);
00259 
00260           if (entry != 0)
00261           {
00262             val->SetIntValue(entry->GetValue());
00263 
00264             return true;
00265           }
00266           else if (exception)
00267           {
00268             throw std::invalid_argument(std::string("Enumeration '")+name+
00269                                         "' does not contain: "+value);
00270           }
00271         }
00272         else if (exception)
00273         {
00274           throw std::invalid_argument(std::string("Feature not enumeration: ")+name);
00275         }
00276       }
00277       else if (exception)
00278       {
00279         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00280       }
00281     }
00282     else if (exception)
00283     {
00284       throw std::invalid_argument(std::string("Feature not found: ")+name);
00285     }
00286   }
00287   catch (const GENICAM_NAMESPACE::GenericException &ex)
00288   {
00289     if (exception)
00290     {
00291       throw std::invalid_argument(ex.what());
00292     }
00293   }
00294 
00295   return ret;
00296 }
00297 
00298 bool setString(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00299                const char *value, bool exception)
00300 {
00301   bool ret=false;
00302 
00303   try
00304   {
00305     GenApi::INode *node=nodemap->_GetNode(name);
00306 
00307     if (node != 0)
00308     {
00309       if (GenApi::IsWritable(node))
00310       {
00311         switch (node->GetPrincipalInterfaceType())
00312         {
00313           case GenApi::intfIBoolean:
00314             {
00315               GenApi::IBoolean *p=dynamic_cast<GenApi::IBoolean *>(node);
00316               p->SetValue(std::stoi(std::string(value)));
00317             }
00318             break;
00319 
00320           case GenApi::intfIInteger:
00321             {
00322               GenApi::IInteger *p=dynamic_cast<GenApi::IInteger *>(node);
00323 
00324               switch (p->GetRepresentation())
00325               {
00326                 case GenApi::HexNumber:
00327                   p->SetValue(std::stoll(std::string(value), 0, 16));
00328                   break;
00329 
00330                 case GenApi::IPV4Address:
00331                   {
00332                     int64_t ip=0;
00333 
00334                     std::stringstream in(value);
00335                     std::string elem;
00336 
00337                     for (int i=0; i<4; i++)
00338                     {
00339                       getline(in, elem, '.');
00340                       ip=(ip<<8)|(stoi(elem)&0xff);
00341                     }
00342 
00343                     p->SetValue(ip);
00344                   }
00345                   break;
00346 
00347                 case GenApi::MACAddress:
00348                   {
00349                     int64_t mac=0;
00350 
00351                     std::stringstream in(value);
00352                     std::string elem;
00353 
00354                     for (int i=0; i<4; i++)
00355                     {
00356                       getline(in, elem, ':');
00357                       mac=(mac<<8)|(stoi(elem, 0, 16)&0xff);
00358                     }
00359 
00360                     p->SetValue(mac);
00361                   }
00362                   break;
00363 
00364                 default:
00365                   p->SetValue(std::stoll(std::string(value)));
00366                   break;
00367               }
00368             }
00369             break;
00370 
00371           case GenApi::intfIFloat:
00372             {
00373               GenApi::IFloat *p=dynamic_cast<GenApi::IFloat *>(node);
00374               p->SetValue(std::stof(std::string(value)));
00375             }
00376             break;
00377 
00378           case GenApi::intfIEnumeration:
00379             {
00380               GenApi::IEnumeration *p=dynamic_cast<GenApi::IEnumeration *>(node);
00381               GenApi::IEnumEntry *entry=p->GetEntryByName(value);
00382 
00383               if (entry != 0)
00384               {
00385                 p->SetIntValue(entry->GetValue());
00386               }
00387               else if (exception)
00388               {
00389                 throw std::invalid_argument(std::string("Enumeration '")+name+
00390                                             "' does not contain: "+value);
00391               }
00392             }
00393             break;
00394 
00395           case GenApi::intfIString:
00396             {
00397               GenApi::IString *p=dynamic_cast<GenApi::IString *>(node);
00398               p->SetValue(value);
00399             }
00400             break;
00401 
00402           default:
00403             if (exception)
00404             {
00405               throw std::invalid_argument(std::string("Feature of unknown datatype: ")+name);
00406             }
00407             break;
00408         }
00409       }
00410       else if (exception)
00411       {
00412         throw std::invalid_argument(std::string("Feature not writable: ")+name);
00413       }
00414     }
00415     else if (exception)
00416     {
00417       throw std::invalid_argument(std::string("Feature not found: ")+name);
00418     }
00419   }
00420   catch (const GENICAM_NAMESPACE::GenericException &ex)
00421   {
00422     if (exception)
00423     {
00424       throw std::invalid_argument(ex.what());
00425     }
00426   }
00427 
00428   return ret;
00429 }
00430 
00431 bool getBoolean(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00432                 bool exception)
00433 {
00434   bool ret=false;
00435 
00436   try
00437   {
00438     GenApi::INode *node=nodemap->_GetNode(name);
00439 
00440     if (node != 0)
00441     {
00442       if (GenApi::IsReadable(node))
00443       {
00444         GenApi::IBoolean *val=dynamic_cast<GenApi::IBoolean *>(node);
00445 
00446         if (val != 0)
00447         {
00448           ret=val->GetValue();
00449         }
00450         else if (exception)
00451         {
00452           throw std::invalid_argument(std::string("Feature not boolean: ")+name);
00453         }
00454       }
00455       else if (exception)
00456       {
00457         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00458       }
00459     }
00460     else if (exception)
00461     {
00462       throw std::invalid_argument(std::string("Feature not found: ")+name);
00463     }
00464   }
00465   catch (const GENICAM_NAMESPACE::GenericException &ex)
00466   {
00467     if (exception)
00468     {
00469       throw std::invalid_argument(ex.what());
00470     }
00471   }
00472 
00473   return ret;
00474 }
00475 
00476 int64_t getInteger(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00477                    int64_t *vmin, int64_t *vmax, bool exception)
00478 {
00479   int64_t ret=0;
00480 
00481   if (vmin != 0) *vmin=0;
00482   if (vmax != 0) *vmax=0;
00483 
00484   try
00485   {
00486     GenApi::INode *node=nodemap->_GetNode(name);
00487 
00488     if (node != 0)
00489     {
00490       if (GenApi::IsReadable(node))
00491       {
00492         GenApi::IInteger *val=dynamic_cast<GenApi::IInteger *>(node);
00493 
00494         if (val != 0)
00495         {
00496           ret=val->GetValue();
00497 
00498           if (vmin != 0) *vmin=val->GetMin();
00499           if (vmax != 0) *vmax=val->GetMax();
00500         }
00501         else if (exception)
00502         {
00503           throw std::invalid_argument(std::string("Feature not integer: ")+name);
00504         }
00505       }
00506       else if (exception)
00507       {
00508         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00509       }
00510     }
00511     else if (exception)
00512     {
00513       throw std::invalid_argument(std::string("Feature not found: ")+name);
00514     }
00515   }
00516   catch (const GENICAM_NAMESPACE::GenericException &ex)
00517   {
00518     if (exception)
00519     {
00520       throw std::invalid_argument(ex.what());
00521     }
00522   }
00523 
00524   return ret;
00525 }
00526 
00527 double getFloat(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00528                 double *vmin, double *vmax, bool exception)
00529 {
00530   double ret=0;
00531 
00532   if (vmin != 0) *vmin=0;
00533   if (vmax != 0) *vmax=0;
00534 
00535   try
00536   {
00537     GenApi::INode *node=nodemap->_GetNode(name);
00538 
00539     if (node != 0)
00540     {
00541       if (GenApi::IsReadable(node))
00542       {
00543         GenApi::IFloat *val=dynamic_cast<GenApi::IFloat *>(node);
00544 
00545         if (val != 0)
00546         {
00547           ret=val->GetValue();
00548 
00549           if (vmin != 0) *vmin=val->GetMin();
00550           if (vmax != 0) *vmax=val->GetMax();
00551         }
00552         else if (exception)
00553         {
00554           throw std::invalid_argument(std::string("Feature not float: ")+name);
00555         }
00556       }
00557       else if (exception)
00558       {
00559         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00560       }
00561     }
00562     else if (exception)
00563     {
00564       throw std::invalid_argument(std::string("Feature not found: ")+name);
00565     }
00566   }
00567   catch (const GENICAM_NAMESPACE::GenericException &ex)
00568   {
00569     if (exception)
00570     {
00571       throw std::invalid_argument(ex.what());
00572     }
00573   }
00574 
00575   return ret;
00576 }
00577 
00578 std::string getEnum(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00579                     bool exception)
00580 {
00581   std::string ret;
00582 
00583   try
00584   {
00585     GenApi::INode *node=nodemap->_GetNode(name);
00586 
00587     if (node != 0)
00588     {
00589       if (GenApi::IsReadable(node))
00590       {
00591         GenApi::IEnumeration *val=dynamic_cast<GenApi::IEnumeration *>(node);
00592 
00593         if (val != 0)
00594         {
00595           ret=val->GetCurrentEntry()->GetSymbolic();
00596         }
00597         else if (exception)
00598         {
00599           throw std::invalid_argument(std::string("Feature not enumeration: ")+name);
00600         }
00601       }
00602       else if (exception)
00603       {
00604         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00605       }
00606     }
00607     else if (exception)
00608     {
00609       throw std::invalid_argument(std::string("Feature not found: ")+name);
00610     }
00611   }
00612   catch (const GENICAM_NAMESPACE::GenericException &ex)
00613   {
00614     if (exception)
00615     {
00616       throw std::invalid_argument(ex.what());
00617     }
00618   }
00619 
00620   return ret;
00621 }
00622 
00623 std::string getEnum(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00624                     std::vector<std::string> &list, bool exception)
00625 {
00626   std::string ret;
00627 
00628   list.clear();
00629 
00630   try
00631   {
00632     GenApi::INode *node=nodemap->_GetNode(name);
00633 
00634     if (node != 0)
00635     {
00636       if (GenApi::IsReadable(node))
00637       {
00638         GenApi::IEnumeration *val=dynamic_cast<GenApi::IEnumeration *>(node);
00639 
00640         if (val != 0)
00641         {
00642           ret=val->GetCurrentEntry()->GetSymbolic();
00643 
00644           GenApi::StringList_t entry;
00645           val->GetSymbolics(entry);
00646 
00647           for (size_t i=0; i<entry.size(); i++)
00648           {
00649             list.push_back(std::string(entry[i]));
00650           }
00651         }
00652         else if (exception)
00653         {
00654           throw std::invalid_argument(std::string("Feature not enumeration: ")+name);
00655         }
00656       }
00657       else if (exception)
00658       {
00659         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00660       }
00661     }
00662     else if (exception)
00663     {
00664       throw std::invalid_argument(std::string("Feature not found: ")+name);
00665     }
00666   }
00667   catch (const GENICAM_NAMESPACE::GenericException &ex)
00668   {
00669     if (exception)
00670     {
00671       throw std::invalid_argument(ex.what());
00672     }
00673   }
00674 
00675   return ret;
00676 }
00677 
00678 std::string getString(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00679                       bool exception)
00680 {
00681   std::ostringstream out;
00682 
00683   try
00684   {
00685     GenApi::INode *node=nodemap->_GetNode(name);
00686 
00687     if (node != 0)
00688     {
00689       if (GenApi::IsReadable(node))
00690       {
00691         switch (node->GetPrincipalInterfaceType())
00692         {
00693           case GenApi::intfIBoolean:
00694             {
00695               GenApi::IBoolean *p=dynamic_cast<GenApi::IBoolean *>(node);
00696               out << p->GetValue();
00697             }
00698             break;
00699 
00700           case GenApi::intfIInteger:
00701             {
00702               GenApi::IInteger *p=dynamic_cast<GenApi::IInteger *>(node);
00703               int64_t value=p->GetValue();
00704 
00705               switch (p->GetRepresentation())
00706               {
00707                 case GenApi::HexNumber:
00708                   out << std::hex << value;
00709                   break;
00710 
00711                 case GenApi::IPV4Address:
00712                    out << ((value>>24)&0xff) << '.' << ((value>>16)&0xff) << '.'
00713                        << ((value>>8)&0xff) << '.' << (value&0xff);
00714                    break;
00715 
00716                 case GenApi::MACAddress:
00717                    out << std::hex << std::setfill('0');
00718                    out << std::setw(2) << ((value>>40)&0xff) << ':'
00719                        << std::setw(2) << ((value>>32)&0xff) << ':'
00720                        << std::setw(2) << ((value>>24)&0xff) << ':'
00721                        << std::setw(2) << ((value>>16)&0xff) << ':'
00722                        << std::setw(2) << ((value>>8)&0xff) << ':'
00723                        << std::setw(2) << (value&0xff);
00724                    break;
00725 
00726                 default:
00727                   out << value;
00728                   break;
00729               }
00730             }
00731             break;
00732 
00733           case GenApi::intfIFloat:
00734             {
00735               GenApi::IFloat *p=dynamic_cast<GenApi::IFloat *>(node);
00736               out << p->GetValue();
00737             }
00738             break;
00739 
00740           case GenApi::intfIEnumeration:
00741             {
00742               GenApi::IEnumeration *p=dynamic_cast<GenApi::IEnumeration *>(node);
00743               out << p->GetCurrentEntry()->GetSymbolic();
00744             }
00745             break;
00746 
00747           case GenApi::intfIString:
00748             {
00749               GenApi::IString *p=dynamic_cast<GenApi::IString *>(node);
00750               out << p->GetValue();
00751             }
00752             break;
00753 
00754           default:
00755             if (exception)
00756             {
00757               throw std::invalid_argument(std::string("Feature of unknown datatype: ")+name);
00758             }
00759             break;
00760         }
00761       }
00762       else if (exception)
00763       {
00764         throw std::invalid_argument(std::string("Feature not readable: ")+name);
00765       }
00766     }
00767     else if (exception)
00768     {
00769       throw std::invalid_argument(std::string("Feature not found: ")+name);
00770     }
00771   }
00772   catch (const GENICAM_NAMESPACE::GenericException &ex)
00773   {
00774     if (exception)
00775     {
00776       throw std::invalid_argument(ex.what());
00777     }
00778   }
00779 
00780   return out.str();
00781 }
00782 
00783 void checkFeature(const std::shared_ptr<GenApi::CNodeMapRef> &nodemap, const char *name,
00784                   const char *value)
00785 {
00786   std::string cvalue=rcg::getString(nodemap, name, true);
00787 
00788   if (cvalue != "" && cvalue != value)
00789   {
00790     std::ostringstream out;
00791     out << name << " == " << value << " expected: " << cvalue;
00792     throw std::invalid_argument(out.str());
00793   }
00794 }
00795 
00796 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02