EnumHelper.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00021 //----------------------------------------------------------------------
00022 #include <string.h>
00023 
00024 #include "icl_core/EnumHelper.h"
00025 
00026 namespace icl_core {
00027 
00028 bool string2Enum(const String& str, int32_t& value,
00029                  const char * const *descriptions, const char *end_marker)
00030 {
00031   bool result = false;
00032 
00033   for (int32_t index = 0;
00034        ((end_marker == NULL) && (descriptions[index] != NULL))
00035          || ((end_marker != NULL) && (::strcmp(descriptions[index], end_marker) != 0));
00036        ++index)
00037   {
00038     // Return success if a matching description has been found.
00039     if (::strcmp(str.c_str(), descriptions[index]) == 0)
00040     {
00041       value = index;
00042       result = true;
00043     }
00044   }
00045 
00046   return result;
00047 }
00048 
00049 namespace impl {
00050 template<typename T>
00051 bool string2Enum(const String& str, T& value,
00052                  const std::vector<std::string>& descriptions)
00053 {
00054   bool result = false;
00055 
00056   for (T index = 0; index < T(descriptions.size()); ++index)
00057   {
00058     // Return success if a matching description has been found.
00059     if (str == descriptions[std::size_t(index)])
00060     {
00061       value = index;
00062       result = true;
00063     }
00064   }
00065 
00066   return result;
00067 }
00068 }
00069 
00070 bool string2Enum(const String& str, int32_t& value,
00071                  const std::vector<std::string>& descriptions)
00072 {
00073   return impl::string2Enum<int32_t>(str, value, descriptions);
00074 }
00075 
00076 bool string2Enum(const String& str, int64_t& value,
00077                  const std::vector<std::string>& descriptions)
00078 {
00079   return impl::string2Enum<int64_t>(str, value, descriptions);
00080 }
00081 
00083 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00084 
00085 bool String2Enum(const String& str, int32_t& value,
00086                  const char * const *descriptions, const char *end_marker)
00087 {
00088   return string2Enum(str, value, descriptions, end_marker);
00089 }
00090 
00091 #endif
00092 
00093 
00094 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:03