OpenImageIO
 All Classes Files Friends Macros Pages
paramlist.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
37 
38 
39 #ifndef OPENIMAGEIO_PARAMLIST_H
40 #define OPENIMAGEIO_PARAMLIST_H
41 
42 #if defined(_MSC_VER)
43 // Ignore warnings about DLL exported classes with member variables that are template classes.
44 // This happens with the Rep m_vals member variable of ParamValueList below, which is a std::vector<T>.
45 # pragma warning (disable : 4251)
46 #endif
47 
48 #include <vector>
49 
50 #include "export.h"
51 #include "typedesc.h"
52 #include "ustring.h"
53 
54 
55 OIIO_NAMESPACE_ENTER
56 {
57 
65 class OIIO_API ParamValue {
66 public:
69  enum Interp {
70  INTERP_CONSTANT = 0,
71  INTERP_PERPIECE = 1,
72  INTERP_LINEAR = 2,
73  INTERP_VERTEX = 3
74  };
75 
76  ParamValue () : m_type(TypeDesc::UNKNOWN), m_nvalues(0),
77  m_interp(INTERP_CONSTANT), m_copy(false), m_nonlocal(false)
78  {
79  m_data.ptr = NULL;
80  }
81  ParamValue (const ustring &_name, TypeDesc _type,
82  int _nvalues, const void *_value, bool _copy=true) {
83  init_noclear (_name, _type, _nvalues, _value, _copy);
84  }
85  ParamValue (const std::string &_name, TypeDesc _type,
86  int _nvalues, const void *_value, bool _copy=true) {
87  init_noclear (ustring(_name.c_str()), _type, _nvalues, _value, _copy);
88  }
89  ParamValue (const char *_name, TypeDesc _type,
90  int _nvalues, const void *_value, bool _copy=true) {
91  init_noclear (ustring(_name), _type, _nvalues, _value, _copy);
92  }
93  ParamValue (const ParamValue &p, bool _copy=true) {
94  init_noclear (p.name(), p.type(), p.nvalues(), p.data(), _copy);
95  }
96  ~ParamValue () { clear_value(); }
97  void init (ustring _name, TypeDesc _type,
98  int _nvalues, const void *_value, bool _copy=true) {
99  clear_value ();
100  init_noclear (_name, _type, _nvalues, _value, _copy);
101  }
102  void init (std::string _name, TypeDesc _type,
103  int _nvalues, const void *_value, bool _copy=true) {
104  init (ustring(_name), _type, _nvalues, _value, _copy);
105  }
106  const ParamValue& operator= (const ParamValue &p) {
107  init (p.name(), p.type(), p.nvalues(), p.data(), p.m_copy);
108  return *this;
109  }
110 
111  const ustring &name () const { return m_name; }
112  TypeDesc type () const { return m_type; }
113  int nvalues () const { return m_nvalues; }
114  const void *data () const { return m_nonlocal ? m_data.ptr : &m_data; }
115  int datasize () const { return m_nvalues * static_cast<int>(m_type.size()); }
116 
117  friend void swap (ParamValue &a, ParamValue &b) {
118  std::swap (a.m_name, b.m_name);
119  std::swap (a.m_type, b.m_type);
120  std::swap (a.m_nvalues, b.m_nvalues);
121  std::swap (a.m_data.ptr, b.m_data.ptr);
122  std::swap (a.m_copy, b.m_copy);
123  std::swap (a.m_nonlocal, b.m_nonlocal);
124  }
125 
126 private:
127  ustring m_name;
128  TypeDesc m_type;
129  int m_nvalues;
130  union {
131  ptrdiff_t localval;
132  const void *ptr;
133  } m_data;
134  unsigned char m_interp;
135  bool m_copy, m_nonlocal;
136 
137  void init_noclear (ustring _name, TypeDesc _type,
138  int _nvalues, const void *_value, bool _copy=true);
139  void clear_value();
140 };
141 
142 
143 
146 class OIIO_API ParamValueList {
147  typedef std::vector<ParamValue> Rep;
148 public:
149  ParamValueList () { }
150 
151  typedef Rep::iterator iterator;
152  typedef Rep::const_iterator const_iterator;
153  typedef ParamValue value_type;
154  typedef value_type & reference;
155  typedef const value_type & const_reference;
156  typedef value_type * pointer;
157  typedef const value_type * const_pointer;
158 
159  iterator begin () { return m_vals.begin(); }
160  iterator end () { return m_vals.end(); }
161  const_iterator begin () const { return m_vals.begin(); }
162  const_iterator end () const { return m_vals.end(); }
163 
164  reference front () { return m_vals.front(); }
165  reference back () { return m_vals.back(); }
166  const_reference front () const { return m_vals.front(); }
167  const_reference back () const { return m_vals.back(); }
168 
169  reference operator[] (int i) { return m_vals[i]; }
170  const_reference operator[] (int i) const { return m_vals[i]; }
171  reference operator[] (size_t i) { return m_vals[i]; }
172  const_reference operator[] (size_t i) const { return m_vals[i]; }
173 
174  void resize (size_t newsize) { m_vals.resize (newsize); }
175  size_t size () const { return m_vals.size(); }
176 
179  reference grow () {
180  resize (size()+1);
181  return back ();
182  }
183 
186  void push_back (ParamValue &p) { m_vals.push_back (p); }
187 
190  iterator erase (iterator position) { return m_vals.erase (position); }
191 
194  iterator erase (iterator first, iterator last) { return m_vals.erase (first, last); }
195 
198  void clear () { m_vals.clear(); }
199 
202  void free () { Rep tmp; std::swap (m_vals, tmp); }
203 
204 private:
205  Rep m_vals;
206 };
207 
208 
209 }
210 OIIO_NAMESPACE_EXIT
211 
212 #endif /* !defined(OPENIMAGEIO_PARAMLIST_H) */