OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathElementTools.cpp
Go to the documentation of this file.
1 
11 // Copyright 2014 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
28 
29 // Library/third-party includes
30 #include <boost/variant/static_visitor.hpp>
31 #include <boost/variant/apply_visitor.hpp>
32 #include <boost/variant/get.hpp>
33 #include <boost/mpl/for_each.hpp>
34 
35 // Standard includes
36 #include <algorithm>
37 #include <string.h>
38 
39 namespace osvr {
40 namespace common {
41  namespace elements {
42  namespace {
44  template <typename ElementType> struct ElementTypeName {
45  static const char *get();
46  };
47 
50 #define OSVR_ROUTING_TYPENAME_HANDLER(CLASS) \
51  template <> struct ElementTypeName<CLASS> { \
52  static const char *get() { return #CLASS; } \
53  };
54 
57  OSVR_ROUTING_TYPENAME_HANDLER(AliasElement)
58  OSVR_ROUTING_TYPENAME_HANDLER(DeviceElement)
59  OSVR_ROUTING_TYPENAME_HANDLER(InterfaceElement)
61  OSVR_ROUTING_TYPENAME_HANDLER(PluginElement)
62  OSVR_ROUTING_TYPENAME_HANDLER(SensorElement)
63  OSVR_ROUTING_TYPENAME_HANDLER(StringElement)
64 #undef OSVR_ROUTING_TYPENAME_HANDLER
65 
67  class TypeNameVisitor : public boost::static_visitor<const char *> {
68  public:
69  template <typename ElementType>
70  const char *operator()(ElementType const &) const {
71  return ElementTypeName<ElementType>::get();
72  }
73  };
74  } // namespace
75 
76  const char *getTypeName(PathElement const &elt) {
77  return boost::apply_visitor(TypeNameVisitor(), elt);
78  }
79 
80  void ifNullReplaceWith(PathElement &dest, PathElement const &src) {
81  if (boost::get<NullElement>(&dest)) {
82  dest = src;
83  }
84  }
85 
86  bool isNull(PathElement const &elt) {
87  return (nullptr != boost::get<NullElement>(&elt));
88  }
89 
90  namespace {
91  class MaxTypeNameLength {
92  public:
93  MaxTypeNameLength(size_t &maxLen) : m_maxLen(maxLen) {}
95  MaxTypeNameLength &
96  operator=(MaxTypeNameLength const &) = delete;
97  template <typename T> void operator()(T const &) {
98  m_maxLen =
99  (std::max)(m_maxLen, strlen(ElementTypeName<T>::get()));
100  }
101 
102  private:
103  size_t &m_maxLen;
104  };
105  } // namespace
106 
108  size_t maxLen = 0;
109  boost::mpl::for_each<elements::PathElement::types>(
110  MaxTypeNameLength(maxLen));
111  return maxLen;
112  }
113  } // namespace elements
114 } // namespace common
115 } // namespace osvr
size_t getMaxTypeNameLength()
Gets the length of the longest type name.
const char * getTypeName(PathElement const &elt)
Gets a string that indicates the type of path element. Do not use this for conditionals/comparisons u...
bool isNull(PathElement const &elt)
Returns true if the path element provided is a NullElement.
Namespace for the various element types that may constitute a node in the path tree.
void ifNullReplaceWith(PathElement &dest, PathElement const &src)
If dest is a NullElement, replace it with the provided src element.
boost::variant< NullElement, AliasElement, SensorElement, InterfaceElement, DeviceElement, PluginElement, StringElement > PathElement
The variant type containing a particular kind of path element.
#define OSVR_ROUTING_TYPENAME_HANDLER(CLASS)
Macro defining a specialization of ElementTypeName to return the type name as a string literal...