SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtil.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2013 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27  */
28 /// @todo Documentation Core/Utils/StringUtil.h
29 
30 #ifndef CORE_UTILS_STRINGUTIL_H
31 #define CORE_UTILS_STRINGUTIL_H 1
32 
33 #include <sstream>
34 #include <vector>
35 #include <iterator>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/atomic.hpp>
38 #include <Core/Utils/share.h>
39 
40 namespace SCIRun
41 {
42 namespace Core
43 {
44 
45 template <typename T>
46 std::vector<T*> toVectorOfRawPointers(const std::vector<boost::shared_ptr<T>>& vec)
47 {
48  std::vector<T*> raws;
49  raws.reserve(vec.size());
50  std::transform(vec.begin(), vec.end(), std::back_inserter(raws), [](boost::shared_ptr<T> ptr) { return ptr.get(); });
51  return raws;
52 }
53 
54 template <typename T>
55 std::string to_string(const T& t)
56 {
57  std::ostringstream o;
58  o << t;
59  return o.str();
60 }
61 
62 template <typename T>
63 std::vector<T> parseLineOfNumbers(const std::string& line)
64 {
65  std::istringstream stream(line);
66  std::vector<T> numbers((std::istream_iterator<T>(stream)), (std::istream_iterator<T>()));
67 
68  return numbers;
69 }
70 
71 template <class T, class Iter>
72 std::vector<boost::shared_ptr<T>> downcast_range(Iter begin, Iter end)
73 {
74  std::vector<boost::shared_ptr<T>> output;
75  std::transform(begin, end, std::back_inserter(output), [](const typename Iter::value_type& p) { return boost::dynamic_pointer_cast<T>(p); });
76  return std::move(output);
77 }
78 
79 template <class T, class Cont>
80 std::vector<boost::shared_ptr<T>> downcast_range(const Cont& container)
81 {
82  return downcast_range<T>(container.begin(), container.end());
83 }
84 
85 template <class T, class Iter>
86 std::vector<boost::shared_ptr<T>> upcast_range(Iter begin, Iter end)
87 {
88  //BOOST_STATIC_ASSERT(boost::is_base_of<T, typename Iter::value_type>::value);
89  std::vector<boost::shared_ptr<T>> output;
90  std::transform(begin, end, std::back_inserter(output), [](const typename Iter::value_type& p) { return boost::static_pointer_cast<T>(p); });
91  return std::move(output);
92 }
93 
94 template <class T, class Cont>
95 std::vector<boost::shared_ptr<T>> upcast_range(const Cont& container)
96 {
97  return upcast_range<T>(container.begin(), container.end());
98 }
99 
101 {
102  int operator()() const;
103 private:
104  static boost::atomic<int> counter_;
105 };
106 
107 }}
108 
109 namespace std
110 {
111  template <class T>
112  std::ostream& operator<<(std::ostream& o, const std::vector<T>& vec)
113  {
114  o << "[";
115  std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(o, " "));
116  o << "]";
117  return o;
118  }
119 }
120 
121 #endif
std::string to_string(const T &t)
Definition: StringUtil.h:55
std::vector< boost::shared_ptr< T > > upcast_range(Iter begin, Iter end)
Definition: StringUtil.h:86
std::vector< boost::shared_ptr< T > > downcast_range(Iter begin, Iter end)
Definition: StringUtil.h:72
#define SCISHARE
Definition: share.h:39
Definition: StringUtil.h:100
std::vector< T * > toVectorOfRawPointers(const std::vector< boost::shared_ptr< T >> &vec)
Definition: StringUtil.h:46
std::vector< T > parseLineOfNumbers(const std::string &line)
Definition: StringUtil.h:63