NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgCast.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
19 #ifndef TG_CAST_H
20 #define TG_CAST_H
21 
29 // This application
30 #include "tgTagSearch.h"
31 #include "tgTaggable.h"
32 // The C++ Standard Library
33 #include <vector>
34 
38 class tgCast
39 {
40 public:
41 
49  template <typename T_FROM, typename T_TO>
50  static std::vector<T_TO*> filter(const std::vector<T_FROM*>& v)
51  {
52  std::vector<T_TO*> result;
53  for(int i = 0; i < v.size(); i++) {
54  T_TO* t = cast<T_FROM, T_TO>(v[i]);
55  if(t != 0) {
56  result.push_back(t);
57  }
58  }
59  return result;
60  }
61 
70  template <typename T_FROM, typename T_TO>
71  static std::vector<const T_TO*> constFilter(const std::vector<T_FROM*>& v)
72  {
73  std::vector<const T_TO*> result;
74  for(int i = 0; i < v.size(); i++) {
75  const T_TO* t = cast<T_FROM, T_TO>(v[i]);
76  if(t != 0) {
77  result.push_back(t);
78  }
79  }
80  return result;
81  }
82 
86  template <typename T_FROM, typename T_TO>
87  static T_TO* cast(T_FROM* obj)
88  {
89  // @todo: dynamic_cast may just return 0 on fail...
90  try {
91  return dynamic_cast<T_TO*>(obj);
92  } catch (std::exception& e) {
93  return 0;
94  }
95  }
96 
101  template <typename T_FROM, typename T_TO>
102  static const T_TO* cast(const T_FROM* obj)
103  {
104  // @todo: dynamic_cast may just return 0 on fail...
105  try {
106  return dynamic_cast<const T_TO*>(obj);
107  } catch (std::exception& e) {
108  return 0;
109  }
110  }
111 
115  template <typename T_FROM, typename T_TO>
116  static T_TO* cast(T_FROM& obj)
117  {
118  return cast<T_FROM, T_TO>(&obj);
119  }
120 
121  template <typename T_FROM, typename T_TO>
122  static std::vector<T_TO*> find(const tgTagSearch& tagSearch, const std::vector<T_FROM*> haystack)
123  {
124  // Filter to the correct type
125  std::vector<T_TO*> filtered = filter<T_FROM, T_TO>(haystack);
126  std::vector<T_TO*> result;
127 
128  // Check each element in filtered to see if it is castable to tgTaggable and matches the search
129  for(int i = 0; i < filtered.size(); i++) {
130  tgTaggable* t = cast<T_TO, tgTaggable>(filtered[i]);
131  if(t != 0 && tagSearch.matches(*t)) {
132  // Note: add filtered[i] instead of t to maintain correct type
133  result.push_back(filtered[i]);
134  }
135  }
136  return result;
137  }
138 
139 };
140 
141 
142 #endif
static T_TO * cast(T_FROM &obj)
Definition: tgCast.h:116
static T_TO * cast(T_FROM *obj)
Definition: tgCast.h:87
Contains the definition of class tgTagSearch.
Definition: tgCast.h:38
static std::vector< const T_TO * > constFilter(const std::vector< T_FROM * > &v)
Definition: tgCast.h:71
const bool matches(const tgTags &tags) const
Definition: tgTagSearch.h:62
Contains the definition of class tgTaggable.
static std::vector< T_TO * > filter(const std::vector< T_FROM * > &v)
Definition: tgCast.h:50
static const T_TO * cast(const T_FROM *obj)
Definition: tgCast.h:102