LuxCore  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
properties.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 1998-2018 by authors (see AUTHORS.txt) *
3  * *
4  * This file is part of LuxCoreRender. *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"); *
7  * you may not use this file except in compliance with the License. *
8  * You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, software *
13  * distributed under the License is distributed on an "AS IS" BASIS, *
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
15  * See the License for the specific language governing permissions and *
16  * limitations under the License. *
17  ***************************************************************************/
18 
19 // NOTE: this file is included in LuxCore so any external dependency must be
20 // avoided here
21 
22 #ifndef _LUXRAYS_PROPERTIES_H
23 #define _LUXRAYS_PROPERTIES_H
24 
25 #include <map>
26 #include <vector>
27 #include <string>
28 #include <istream>
29 #include <cstdarg>
30 #include <stdexcept>
31 
32 #include <luxrays/utils/exportdefs.h>
33 
34 namespace luxrays {
35 
36 //------------------------------------------------------------------------------
37 // Blob class
38 //------------------------------------------------------------------------------
39 
40 CPP_EXPORT class CPP_API Blob {
41 public:
42  Blob(const Blob &blob);
43  Blob(const char *data, const size_t size);
44  Blob(const std::string &base64Data);
45  ~Blob();
46 
47  const char *GetData() const { return data; }
48  size_t GetSize() const { return size; }
49 
50  std::string ToString() const;
51 
52  Blob &operator=(const Blob &blob);
53 
54 private:
55  char *data;
56  size_t size;
57 };
58 
59 CPP_EXPORT CPP_API std::ostream &operator<<(std::ostream &os, const Blob &blob);
60 
61 //------------------------------------------------------------------------------
62 // Property class
63 //------------------------------------------------------------------------------
64 
79 // I was using boost::variant for PropertyValue in the past but I want to avoid
80 // Boost requirement for application using LuxCore API
81 
82 CPP_EXPORT class CPP_API PropertyValue {
83 public:
84  typedef enum {
93  BLOB_VAL
94  } DataType;
95 
96  PropertyValue();
97  PropertyValue(const PropertyValue &propVal);
98  PropertyValue(const bool val);
99  PropertyValue(const int val);
100  PropertyValue(const unsigned int val);
101  PropertyValue(const float val);
102  PropertyValue(const double val);
103  PropertyValue(const unsigned long long val);
104  PropertyValue(const std::string &val);
105  PropertyValue(const Blob &val);
106  ~PropertyValue();
107 
108  template<class T> T Get() const;
109 
110  DataType GetValueType() const;
111 
112  PropertyValue &operator=(const PropertyValue &propVal);
113 
114 private:
115  static void Copy(const PropertyValue &prop0Val, PropertyValue &prop1Val);
116 
118 
119  union {
120  bool boolVal;
121  int intVal;
122  unsigned int uintVal;
123  float floatVal;
124  double doubleVal;
125  unsigned long long ulonglongVal;
126  std::string *stringVal;
128  } data;
129 };
130 
131 // Get<>() basic types specializations
132 template<> CPP_API bool PropertyValue::Get<bool>() const;
133 template<> CPP_API int PropertyValue::Get<int>() const;
134 template<> CPP_API unsigned int PropertyValue::Get<unsigned int>() const;
135 template<> CPP_API float PropertyValue::Get<float>() const;
136 template<> CPP_API double PropertyValue::Get<double>() const;
137 template<> CPP_API unsigned long long PropertyValue::Get<unsigned long long>() const;
138 template<> CPP_API std::string PropertyValue::Get<std::string>() const;
139 template<> CPP_API const Blob &PropertyValue::Get<const Blob &>() const;
140 
144 typedef std::vector<PropertyValue> PropertyValues;
145 
153 CPP_EXPORT class CPP_API Property {
154 public:
161  Property();
170  Property(const std::string &propName);
181  Property(const std::string &propName, const PropertyValue &val);
192  Property(const std::string &propName, const PropertyValues &vals);
193  ~Property();
194 
200  const std::string &GetName() const { return name; }
208  Property AddedNamePrefix(const std::string &prefix) const {
209  Property newProp(prefix + name);
210  newProp.values.insert(newProp.values.begin(), values.begin(), values.end());
211 
212  return newProp;
213  }
221  Property Renamed(const std::string &newName) const {
222  Property newProp(newName);
223  newProp.values.insert(newProp.values.begin(), values.begin(), values.end());
224 
225  return newProp;
226  }
232  unsigned int GetSize() const { return values.size(); }
238  Property &Clear();
249  template<class T> T Get(const unsigned int index) const {
250  if (index >= values.size())
251  throw std::runtime_error("Out of bound error for property: " + name);
252 
253  return values[index].Get<T>();
254  }
264  const PropertyValue::DataType GetValueType(const unsigned int index) const {
265  if (index >= values.size())
266  throw std::runtime_error("Out of bound error for property: " + name);
267 
268  return values[index].GetValueType();
269  }
289  template<class T> T Get() const {
290  throw std::runtime_error("Unsupported data type in property: " + name);
291  }
302  template<class T> Property &Set(const unsigned int index, const T &val) {
303  if (index >= values.size())
304  throw std::runtime_error("Out of bound error for property: " + name);
305 
306  values[index] = val;
307 
308  return *this;
309  }
318  template<class T> Property &Add(const T &val) {
319  values.push_back(val);
320  return *this;
321  }
327  std::string GetValuesString() const;
331  void FromString(std::string &s);
338  std::string ToString() const;
350  template<class T0> Property &operator()(const T0 &val0) {
351  return Add(val0);
352  }
365  template<class T0, class T1> Property &operator()(const T0 &val0, const T1 &val1) {
366  return Add(val0).Add(val1);
367  }
381  template<class T0, class T1, class T2> Property &operator()(const T0 &val0, const T1 &val1, const T2 &val2) {
382  return Add(val0).Add(val1).Add(val2);
383  }
398  template<class T0, class T1, class T2, class T3> Property &operator()(const T0 &val0, const T1 &val1, const T2 &val2, const T3 &val3) {
399  return Add(val0).Add(val1).Add(val2).Add(val3);
400  }
408  template<class T0> Property &operator()(const std::vector<T0> &vals) {
409  for (size_t i = 0; i < vals.size(); ++i)
410  values.push_back(vals[i]);
411 
412  return *this;
413  }
419  template<class T> Property &operator=(const T &val) {
420  values.clear();
421  return Add(val);
422  }
423 
428  Property &Add(const char *val) {
429  values.push_back(std::string(val));
430  return *this;
431  }
436  Property &Add(char *val) {
437  values.push_back(std::string(val));
438  return *this;
439  }
440  Property &operator()(const char *val) {
441  return Add(std::string(val));
442  }
447  Property &operator()(char *val) {
448  return Add(std::string(val));
449  }
454  Property &operator=(const char *val) {
455  values.clear();
456  return Add(std::string(val));
457  }
462  Property &operator=(char *val) {
463  values.clear();
464  return Add(std::string(val));
465  }
466 
467  static unsigned int CountFields(const std::string &name);
468  static std::string ExtractField(const std::string &name, const unsigned int index);
469  static std::string ExtractPrefix(const std::string &name, const unsigned int count);
470 
471 private:
472  std::string name;
474 };
475 
476 // Get<>() basic types specializations
477 template<> CPP_API bool Property::Get<bool>() const;
478 template<> CPP_API int Property::Get<int>() const;
479 template<> CPP_API unsigned int Property::Get<unsigned int>() const;
480 template<> CPP_API float Property::Get<float>() const;
481 template<> CPP_API double Property::Get<double>() const;
482 template<> CPP_API unsigned long long Property::Get<unsigned long long>() const;
483 template<> CPP_API std::string Property::Get<std::string>() const;
484 template<> CPP_API const Blob &Property::Get<const Blob &>() const;
485 
486 inline std::ostream &operator<<(std::ostream &os, const Property &p) {
487  os << p.ToString();
488 
489  return os;
490 }
491 
492 //------------------------------------------------------------------------------
493 // Properties class
494 //------------------------------------------------------------------------------
495 
502 CPP_EXPORT class CPP_API Properties {
503 public:
510  Properties(const std::string &fileName);
512 
518  unsigned int GetSize() const;
519 
520  // The following 2 methods perform the same action
521 
529  Properties &Set(const Property &prop);
537  Properties &operator<<(const Property &prop);
545  Properties &Set(const Properties &prop);
553  Properties &operator<<(const Properties &props);
561  Properties &Set(const Properties &props, const std::string &prefix);
569  Properties &SetFromStream(std::istream &stream);
577  Properties &SetFromFile(const std::string &fileName);
585  Properties &SetFromString(const std::string &propDefinitions);
586 
592  Properties &Clear();
593 
599  const std::vector<std::string> &GetAllNames() const;
609  std::vector<std::string> GetAllNames(const std::string &prefix) const;
618  std::vector<std::string> GetAllNamesRE(const std::string &regularExpression) const;
635  std::vector<std::string> GetAllUniqueSubNames(const std::string &prefix) const;
643  bool HaveNames(const std::string &prefix) const;
651  bool HaveNamesRE(const std::string &regularExpression) const;
660  Properties GetAllProperties(const std::string &prefix) const;
670  const Property &Get(const std::string &propName) const;
680  const Property &Get(const Property &defaultProp) const;
681 
689  bool IsDefined(const std::string &propName) const;
690 
696  void Delete(const std::string &propName);
702  void DeleteAll(const std::vector<std::string> &propNames);
703 
709  std::string ToString() const;
710 
711 private:
712  // This vector is used, among other things, to keep track of the insertion order
713  std::vector<std::string> names;
714  std::map<std::string, Property> props;
715 };
716 
717 CPP_EXPORT CPP_API Properties operator<<(const Property &prop0, const Property &prop1);
718 CPP_EXPORT CPP_API Properties operator<<(const Property &prop0, const Properties &props);
719 
720 inline std::ostream &operator<<(std::ostream &os, const Properties &p) {
721  os << p.ToString();
722 
723  return os;
724 }
725 
726 }
727 
728 #endif /* _LUXRAYS_PROPERTIES_H */
char * data
Definition: properties.h:55
const PropertyValue::DataType GetValueType(const unsigned int index) const
Returns the type of the value at the specified position.
Definition: properties.h:264
const char * GetData() const
Definition: properties.h:47
Property & operator()(const char *val)
Definition: properties.h:440
Property & operator()(char *val)
Required to work around the problem of char* to bool conversion (instead of char* to string)...
Definition: properties.h:447
Property Renamed(const std::string &newName) const
Return a new property with a new name.
Definition: properties.h:221
A generic container for values.
Definition: properties.h:153
Value that can be stored in a Property.
Definition: properties.h:82
unsigned int GetSize() const
Returns the number of values associated to this property.
Definition: properties.h:232
Property & operator=(char *val)
Required to work around the problem of char* to bool conversion (instead of char* to string)...
Definition: properties.h:462
unsigned int uintVal
Definition: properties.h:122
Property & Add(const char *val)
Required to work around the problem of char* to bool conversion (instead of char* to string)...
Definition: properties.h:428
unsigned long long ulonglongVal
Definition: properties.h:125
Property & operator()(const T0 &val0, const T1 &val1, const T2 &val2, const T3 &val3)
Adds a value to a property.
Definition: properties.h:398
Property & Set(const unsigned int index, const T &val)
Sets the value at the specified position.
Definition: properties.h:302
T Get(const unsigned int index) const
Returns the value at the specified position.
Definition: properties.h:249
Property & Add(char *val)
Required to work around the problem of char* to bool conversion (instead of char* to string)...
Definition: properties.h:436
std::string * stringVal
Definition: properties.h:126
Property & Add(const T &val)
Adds an item at the end of the list of values associated with the property.
Definition: properties.h:318
std::string ToString() const
Returns a string with the name of the property followed by " = " and by all values associated to the ...
Property AddedNamePrefix(const std::string &prefix) const
Return a new property with a prefix added to the name.
Definition: properties.h:208
Property & operator()(const T0 &val0)
Adds a value to a property.
Definition: properties.h:350
size_t GetSize() const
Definition: properties.h:48
A container for multiple Property.
Definition: properties.h:502
std::string ToString() const
Converts all Properties in a string.
Property & operator=(const T &val)
Initializes a property with (only) the given value.
Definition: properties.h:419
const std::string & GetName() const
Returns the name of a property.
Definition: properties.h:200
Property & operator=(const char *val)
Required to work around the problem of char* to bool conversion (instead of char* to string)...
Definition: properties.h:454
Property & operator()(const std::vector< T0 > &vals)
Adds a vector of values to a property.
Definition: properties.h:408
T Get() const
Parses all values as a representation of the specified type.
Definition: properties.h:289
Property & operator()(const T0 &val0, const T1 &val1, const T2 &val2)
Adds a value to a property.
Definition: properties.h:381
CPP_EXPORT CPP_API std::ostream & operator<<(std::ostream &os, const Blob &blob)
std::string name
Definition: properties.h:472
size_t size
Definition: properties.h:56
PropertyValues values
Definition: properties.h:473
std::vector< PropertyValue > PropertyValues
A vector of values that can be stored in a Property.
Definition: properties.h:144
Property & operator()(const T0 &val0, const T1 &val1)
Adds a value to a property.
Definition: properties.h:365
std::vector< std::string > names
Definition: properties.h:713
std::map< std::string, Property > props
Definition: properties.h:714