Main MRPT website > C++ reference for MRPT 1.5.6
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(mrpt::utils::CStream &out, int *out_Version) const
28 {
29  if (out_Version)
30  *out_Version = 0;
31  else
32  {
34  uint32_t n = (uint32_t)possibleTypes.size();
35 
36  out << n;
37 
38  for (it=possibleTypes.begin();it<possibleTypes.end();++it)
39  out << *it;
40 
41  // Selection:
42  out << (uint32_t)selection;
43  }
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; selection = i;
71  } break;
72  default:
74 
75  };
76 }
77 
78 
79 /*---------------------------------------------------------------
80  Constructor
81  ---------------------------------------------------------------*/
83  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 }
101 
102 /*---------------------------------------------------------------
103  getType
104  ---------------------------------------------------------------*/
106 {
107  if (selection>=possibleTypes.size())
108  THROW_EXCEPTION("current selection index is out of bounds!");
109 
110  return possibleTypes[selection];
111 }
112 
113 /*---------------------------------------------------------------
114  setType
115  ---------------------------------------------------------------*/
117 {
118  size_t i,n = possibleTypes.size();
119 
120  for (i=0;i<n;i++)
121  {
122  if (!os::_strcmpi(type.c_str(),possibleTypes[i].c_str()))
123  {
124  // Select:
125  selection = (unsigned int)i;
126  return;
127  }
128  }
129 
130  // Not in the list:
131  THROW_EXCEPTION_FMT("Type '%s' is not one of the posibilities", type.c_str());
132 }
133 
134 /*---------------------------------------------------------------
135  getTypePosibilities
136  ---------------------------------------------------------------*/
137 void CTypeSelector::getTypePosibilities( std::vector<std::string> &outPosibilities)const
138 {
139  outPosibilities = possibleTypes;
140 }
141 
142 /*---------------------------------------------------------------
143  isType
144  ---------------------------------------------------------------*/
145 bool CTypeSelector::isType(const char *type) const
146 {
147  return !strcmp( possibleTypes[selection].c_str(),type);
148 }
149 
150 /*---------------------------------------------------------------
151  isType
152  ---------------------------------------------------------------*/
154 {
155  return !strcmp( possibleTypes[selection].c_str(),type.c_str());
156 }
157 
158 /*---------------------------------------------------------------
159  checkTypeIndex
160  ---------------------------------------------------------------*/
162 {
163  size_t n = possibleTypes.size();
164  for (size_t i=0;i<n;i++)
165  if (!os::_strcmpi(type.c_str(),possibleTypes[i].c_str()))
166  return int(i); // Found:
167  return -1; // Not found
168 }
169 
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.
Definition: zip.h:16
virtual ~CTypeSelector()
Destructor.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
#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:4618
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:24
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
int version
Definition: mrpt_jpeglib.h:898
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:3919
std::string getType() const
Gets the currently selected type, from the set of posibilities.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int BASE_IMPEXP _strcmpi(const char *str1, const char *str2) MRPT_NO_THROWS
An OS-independent version of strcmpi.
Definition: os.cpp:320
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:6301
This class represents a std::string derived class which is also CSerializable.
Definition: CTypeSelector.h:25
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
unsigned __int32 uint32_t
Definition: rptypes.h:49
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3512



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019