EnumHelper.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 #include <string.h>
23 
24 #include "icl_core/EnumHelper.h"
25 
26 namespace icl_core {
27 
28 bool string2Enum(const String& str, int32_t& value,
29  const char * const *descriptions, const char *end_marker)
30 {
31  bool result = false;
32 
33  for (int32_t index = 0;
34  ((end_marker == NULL) && (descriptions[index] != NULL))
35  || ((end_marker != NULL) && (::strcmp(descriptions[index], end_marker) != 0));
36  ++index)
37  {
38  // Return success if a matching description has been found.
39  if (::strcmp(str.c_str(), descriptions[index]) == 0)
40  {
41  value = index;
42  result = true;
43  }
44  }
45 
46  return result;
47 }
48 
49 namespace impl {
50 template<typename T>
51 bool string2Enum(const String& str, T& value,
52  const std::vector<std::string>& descriptions)
53 {
54  bool result = false;
55 
56  for (T index = 0; index < T(descriptions.size()); ++index)
57  {
58  // Return success if a matching description has been found.
59  if (str == descriptions[std::size_t(index)])
60  {
61  value = index;
62  result = true;
63  }
64  }
65 
66  return result;
67 }
68 }
69 
70 bool string2Enum(const String& str, int32_t& value,
71  const std::vector<std::string>& descriptions)
72 {
73  return impl::string2Enum<int32_t>(str, value, descriptions);
74 }
75 
76 bool string2Enum(const String& str, int64_t& value,
77  const std::vector<std::string>& descriptions)
78 {
79  return impl::string2Enum<int64_t>(str, value, descriptions);
80 }
81 
83 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
84 
85 bool String2Enum(const String& str, int32_t& value,
86  const char * const *descriptions, const char *end_marker)
87 {
88  return string2Enum(str, value, descriptions, end_marker);
89 }
90 
91 #endif
92 
94 }
signed int int32_t
Definition: msvc_stdint.h:90
bool string2Enum(const String &str, int32_t &value, const char *const *descriptions, const char *end_marker)
Definition: EnumHelper.cpp:28
bool string2Enum(const String &str, T &value, const std::vector< std::string > &descriptions)
Definition: EnumHelper.cpp:51
signed __int64 int64_t
Definition: msvc_stdint.h:102
std::string String
Definition: BaseTypes.h:43
Contains helper functions to handle enums with textual descriptions.


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58