Main MRPT website > C++ reference for MRPT 1.9.9
CTypeSelector.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/utils/CStream.h>
15 #include <mrpt/system/os.h>
16 
17 using namespace mrpt::utils;
18 using namespace mrpt::system;
19 using namespace std;
20 
21 // This must be added to any CSerializable class implementation file.
23 
24 /*---------------------------------------------------------------
25  writeToStream
26  ---------------------------------------------------------------*/
27 void CTypeSelector::writeToStream(
28  mrpt::utils::CStream& out, int* out_Version) const
29 {
30  if (out_Version)
31  *out_Version = 0;
32  else
33  {
35  uint32_t n = (uint32_t)possibleTypes.size();
36 
37  out << n;
38 
39  for (it = possibleTypes.begin(); it < possibleTypes.end(); ++it)
40  out << *it;
41 
42  // Selection:
43  out << (uint32_t)selection;
44  }
45 }
46 
47 /*---------------------------------------------------------------
48  readFromStream
49  ---------------------------------------------------------------*/
51 {
52  switch (version)
53  {
54  case 0:
55  {
56  uint32_t i, n;
57 
58  in >> n;
59 
60  possibleTypes.clear();
61 
62  for (i = 0; i < n; i++)
63  {
64  std::string aux;
65  in >> aux;
66  possibleTypes.push_back(aux);
67  }
68 
69  // Selection:
70  in >> i;
71  selection = i;
72  }
73  break;
74  default:
76  };
77 }
78 
79 /*---------------------------------------------------------------
80  Constructor
81  ---------------------------------------------------------------*/
83  std::string posibilitiesList, std::string defaultType)
84 {
85  // Init:
86  possibleTypes.clear();
87 
88  // Extract tokens from the string:
89  mrpt::system::tokenize(posibilitiesList, ",", possibleTypes);
90 
91  // Select default type:
92  setType(defaultType);
93 }
94 
95 /*---------------------------------------------------------------
96  Destructor
97  ---------------------------------------------------------------*/
99 /*---------------------------------------------------------------
100  getType
101  ---------------------------------------------------------------*/
103 {
104  if (selection >= possibleTypes.size())
105  THROW_EXCEPTION("current selection index is out of bounds!");
106 
107  return possibleTypes[selection];
108 }
109 
110 /*---------------------------------------------------------------
111  setType
112  ---------------------------------------------------------------*/
114 {
115  size_t i, n = possibleTypes.size();
116 
117  for (i = 0; i < n; i++)
118  {
119  if (!os::_strcmpi(type.c_str(), possibleTypes[i].c_str()))
120  {
121  // Select:
122  selection = (unsigned int)i;
123  return;
124  }
125  }
126 
127  // Not in the list:
129  "Type '%s' is not one of the posibilities", type.c_str());
130 }
131 
132 /*---------------------------------------------------------------
133  getTypePosibilities
134  ---------------------------------------------------------------*/
136  std::vector<std::string>& outPosibilities) const
137 {
138  outPosibilities = possibleTypes;
139 }
140 
141 /*---------------------------------------------------------------
142  isType
143  ---------------------------------------------------------------*/
144 bool CTypeSelector::isType(const char* type) const
145 {
146  return !strcmp(possibleTypes[selection].c_str(), type);
147 }
148 
149 /*---------------------------------------------------------------
150  isType
151  ---------------------------------------------------------------*/
153 {
154  return !strcmp(possibleTypes[selection].c_str(), type.c_str());
155 }
156 
157 /*---------------------------------------------------------------
158  checkTypeIndex
159  ---------------------------------------------------------------*/
161 {
162  size_t n = possibleTypes.size();
163  for (size_t i = 0; i < n; i++)
164  if (!os::_strcmpi(type.c_str(), possibleTypes[i].c_str()))
165  return int(i); // Found:
166  return -1; // Not found
167 }
void setType(const std::string &type)
Sets the currently selected type.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
virtual ~CTypeSelector()
Destructor.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLsizei n
Definition: glext.h:5074
int checkTypeIndex(const std::string &type) const
Returns the index of a given type within the list of all possible types, or -1 if the given string is...
CTypeSelector(std::string posibilitiesList="", std::string defaultType="")
Default constructor.
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
bool isType(const char *type) const
Fast check for a given type, returns true if the selection is exactly the specified type name...
GLsizei const GLchar ** string
Definition: glext.h:4101
std::string getType() const
Gets the currently selected type, from the set of posibilities.
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void getTypePosibilities(std::vector< std::string > &outPosibilities) const
Returns the set of posibilities in the "type" represented by this class.
GLuint in
Definition: glext.h:7274
This class represents a std::string derived class which is also CSerializable.
Definition: CTypeSelector.h:23
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:319



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019