LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
data.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #pragma once
18 #ifndef DATA_H
19 #define DATA_H
20 #include "utils/defs.hpp"
21 #include <string>
22 #include <iterator>
23 #include <sstream>
24 #include <algorithm>
25 #include <memory>
26 #include <functional>
28 class data{
29  //utility typedefs
31  std::stringstream dat;
32 //for some reason icc 14 on nersc won't ;et this be a template parameter, and claims std::function doesn't exist...
33  template<class T>
34  using strfnc = std::function<std::string(const T&)>;//std::string(*strfnc)(const T&);
35  template<class T>
36  inline void addelem(const T& inval){
37  dat << inval << " ";
38  }
39  template<class T>
40  inline void addelem(const T& inval, const strfnc<T>& tostr){
41  dat << tostr(inval) << " ";
42  }
43 
44 
45  friend class writer;
46 
47  //workaround of C++ being stupid with shared pointers and private constructors
48 
49  struct this_is_private{};
50  public:
51  //not a template but might as well eliminate the cpp file
52  virtual std::string to_string() const{
53  return name + ": " + dat.str();
54  }
55  //instead of exposing constructors directly, call through creation functions
56  //to enforce unmodifiable data
57  template<class T>
58  static inline std::shared_ptr<const data> create(const std::string name, const T& inval){
59  return std::make_shared<const data>(this_is_private(), name, inval);
60  }
61  template<class T>
62  static inline std::shared_ptr<const data> create(const std::string _name, T* invals, size_t len){
63  return std::make_shared<const data>(this_is_private(), _name, invals, len);
64  }
65  template<class T>
66  static inline std::shared_ptr<const data> create(const std::string name, T* b,
67  T* e){
68  return std::make_shared<const data>(this_is_private(), name, b, e);
69  }
70  //custom string function creators
71  template<class T>
72  static inline std::shared_ptr<const data> create(const std::string name, const T& inval, const strfnc<T>& tostr){
73  return std::make_shared<const data>(this_is_private(), name, inval, tostr);
74  }
75  template<class T>
76  static inline std::shared_ptr<const data> create(const std::string _name, T* invals, size_t len, const strfnc<T>& tostr){
77  return std::make_shared<const data>(this_is_private(), _name, invals, len, tostr);
78  }
79  //public but not really constructors
80  template<class T>
81  data(const this_is_private& dummy, const std::string _name, T* invals, size_t len):name(_name){
82  std::for_each(invals, invals + len, [&](const T& val){
83  addelem(val);
84  });
85  }
86  template<class T>
87  data(const this_is_private& dummy, const std::string& _name, const T& inval):name(_name){
88  addelem(inval);
89  }
90  //constructors taking a custom string function
91  template<class T>
92  data(const this_is_private& dummy, const std::string _name, T* invals, size_t len, const strfnc<T>& tostr):name(_name){
93  std::for_each(invals, invals+len, [&](const T& val){
94  addelem(val, tostr);
95  });
96  }
97  template<class T>
98  data(const this_is_private& dummy, const std::string& _name, const T& inval, const strfnc<T>& tostr):name(_name){
99  addelem(inval, tostr);
100  }
101 
102 #endif
103  virtual ~data(){}
104 };