CMS 3D CMS Logo

value.h
Go to the documentation of this file.
1 #ifndef CPPTL_JSON_H_INCLUDED
2 #define CPPTL_JSON_H_INCLUDED
3 
4 #include "forwards.h"
5 #include <string>
6 #include <vector>
7 
8 #ifndef JSON_USE_CPPTL_SMALLMAP
9 #include <map>
10 #else
11 #include <cpptl/smallmap.h>
12 #endif
13 #ifdef JSON_USE_CPPTL
14 #include <cpptl/forwards.h>
15 #endif
16 
20 namespace jsoncollector {
21  namespace Json {
22 
25  enum ValueType {
26  nullValue = 0,
34  };
35 
41  };
42 
43  //# ifdef JSON_USE_CPPTL
44  // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
45  // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
46  //# endif
47 
63  public:
64  explicit StaticString(const char *czstring) : str_(czstring) {}
65 
66  operator const char *() const { return str_; }
67 
68  const char *c_str() const { return str_; }
69 
70  private:
71  const char *str_;
72  };
73 
101  class JSON_API Value {
102  friend class ValueIteratorBase;
103 #ifdef JSON_VALUE_USE_INTERNAL_MAP
104  friend class ValueInternalLink;
105  friend class ValueInternalMap;
106 #endif
107  public:
108  typedef std::vector<std::string> Members;
111  typedef Json::UInt UInt;
112  typedef Json::Int Int;
113  typedef UInt ArrayIndex;
114 
115  static const Value null;
116  static const Int minInt;
117  static const Int maxInt;
118  static const UInt maxUInt;
119 
120  private:
121 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
122 #ifndef JSON_VALUE_USE_INTERNAL_MAP
123  class CZString {
124  public:
125  enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
126  CZString(int index);
127  CZString(const char *cstr, DuplicationPolicy allocate);
128  CZString(const CZString &other);
129  ~CZString();
130  CZString &operator=(const CZString &other);
131  bool operator<(const CZString &other) const;
132  bool operator==(const CZString &other) const;
133  int index() const;
134  const char *c_str() const;
135  bool isStaticString() const;
136 
137  private:
138  void swap(CZString &other);
139  const char *cstr_;
140  int index_;
141  };
142 
143  public:
144 #ifndef JSON_USE_CPPTL_SMALLMAP
145  typedef std::map<CZString, Value> ObjectValues;
146 #else
147  typedef CppTL::SmallMap<CZString, Value> ObjectValues;
148 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
149 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
150 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
151 
152  public:
169  Value(Int value);
170  Value(UInt value);
171  Value(double value);
172  Value(const char *value);
173  Value(const char *beginValue, const char *endValue);
184  Value(const StaticString &value);
185  Value(const std::string &value);
186 #ifdef JSON_USE_CPPTL
187  Value(const CppTL::ConstString &value);
188 #endif
189  Value(bool value);
190  Value(const Value &other);
191  ~Value();
192 
193  Value &operator=(const Value &other);
197  void swap(Value &other);
198 
199  ValueType type() const;
200 
201  bool operator<(const Value &other) const;
202  bool operator<=(const Value &other) const;
203  bool operator>=(const Value &other) const;
204  bool operator>(const Value &other) const;
205 
206  bool operator==(const Value &other) const;
207  bool operator!=(const Value &other) const;
208 
209  int compare(const Value &other);
210 
211  const char *asCString() const;
212  std::string asString() const;
213 #ifdef JSON_USE_CPPTL
214  CppTL::ConstString asConstString() const;
215 #endif
216  Int asInt() const;
217  UInt asUInt() const;
218  double asDouble() const;
219  bool asBool() const;
220 
221  bool isNull() const;
222  bool isBool() const;
223  bool isInt() const;
224  bool isUInt() const;
225  bool isIntegral() const;
226  bool isDouble() const;
227  bool isNumeric() const;
228  bool isString() const;
229  bool isArray() const;
230  bool isObject() const;
231 
232  bool isConvertibleTo(ValueType other) const;
233 
235  UInt size() const;
236 
239  bool empty() const;
240 
242  bool operator!() const;
243 
247  void clear();
248 
254  void resize(UInt size);
255 
265  const Value &operator[](UInt index) const;
268  Value get(UInt index, const Value &defaultValue) const;
270  bool isValidIndex(UInt index) const;
274  Value &append(const Value &value);
275 
277  Value &operator[](const char *key);
279  const Value &operator[](const char *key) const;
281  Value &operator[](const std::string &key);
283  const Value &operator[](const std::string &key) const;
295  Value &operator[](const StaticString &key);
296 #ifdef JSON_USE_CPPTL
297  Value &operator[](const CppTL::ConstString &key);
300  const Value &operator[](const CppTL::ConstString &key) const;
301 #endif
302  Value get(const char *key, const Value &defaultValue) const;
305  Value get(const std::string &key, const Value &defaultValue) const;
306 #ifdef JSON_USE_CPPTL
307  Value get(const CppTL::ConstString &key, const Value &defaultValue) const;
309 #endif
310  Value removeMember(const char *key);
318  Value removeMember(const std::string &key);
319 
321  bool isMember(const char *key) const;
323  bool isMember(const std::string &key) const;
324 #ifdef JSON_USE_CPPTL
325  bool isMember(const CppTL::ConstString &key) const;
327 #endif
328 
334  Members getMemberNames() const;
335 
336  //# ifdef JSON_USE_CPPTL
337  // EnumMemberNames enumMemberNames() const;
338  // EnumValues enumValues() const;
339  //# endif
340 
342  void setComment(const char *comment, CommentPlacement placement);
344  void setComment(const std::string &comment, CommentPlacement placement);
345  bool hasComment(CommentPlacement placement) const;
347  std::string getComment(CommentPlacement placement) const;
348 
349  std::string toStyledString() const;
350 
351  const_iterator begin() const;
352  const_iterator end() const;
353 
354  iterator begin();
355  iterator end();
356 
357  private:
358  Value &resolveReference(const char *key, bool isStatic);
359 
360 #ifdef JSON_VALUE_USE_INTERNAL_MAP
361  inline bool isItemAvailable() const { return itemIsUsed_ == 0; }
362 
363  inline void setItemUsed(bool isUsed = true) { itemIsUsed_ = isUsed ? 1 : 0; }
364 
365  inline bool isMemberNameStatic() const { return memberNameIsStatic_ == 0; }
366 
367  inline void setMemberNameIsStatic(bool isStatic) { memberNameIsStatic_ = isStatic ? 1 : 0; }
368 #endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
369 
370  private:
371  struct CommentInfo {
372  CommentInfo();
373  ~CommentInfo();
374 
375  void setComment(const char *text);
376 
377  char *comment_;
378  };
379 
380  //struct MemberNamesTransform
381  //{
382  // typedef const char *result_type;
383  // const char *operator()( const CZString &name ) const
384  // {
385  // return name.c_str();
386  // }
387  //};
388 
389  union ValueHolder {
392  double real_;
393  bool bool_;
394  char *string_;
395 #ifdef JSON_VALUE_USE_INTERNAL_MAP
396  ValueInternalArray *array_;
397  ValueInternalMap *map_;
398 #else
400 #endif
401  } value_;
403  int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
404 #ifdef JSON_VALUE_USE_INTERNAL_MAP
405  unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container.
406  int memberNameIsStatic_ : 1; // used by the ValueInternalMap container.
407 #endif
409  };
410 
413  class PathArgument {
414  public:
415  friend class Path;
416 
417  PathArgument();
419  PathArgument(const char *key);
420  PathArgument(const std::string &key);
421 
422  private:
423  enum Kind { kindNone = 0, kindIndex, kindKey };
427  };
428 
440  class Path {
441  public:
442  Path(const std::string &path,
443  const PathArgument &a1 = PathArgument(),
444  const PathArgument &a2 = PathArgument(),
445  const PathArgument &a3 = PathArgument(),
446  const PathArgument &a4 = PathArgument(),
447  const PathArgument &a5 = PathArgument());
448 
449  const Value &resolve(const Value &root) const;
450  Value resolve(const Value &root, const Value &defaultValue) const;
452  Value &make(Value &root) const;
453 
454  private:
455  typedef std::vector<const PathArgument *> InArgs;
456  typedef std::vector<PathArgument> Args;
457 
458  void makePath(const std::string &path, const InArgs &in);
459  void addPathInArg(const std::string &path,
460  const InArgs &in,
461  InArgs::const_iterator &itInArg,
463  void invalidPath(const std::string &path, int location);
464 
466  };
467 
476  public:
477  enum { unknown = (unsigned)-1 };
478 
479  virtual ~ValueAllocator();
480 
481  virtual char *makeMemberName(const char *memberName) const = 0;
482  virtual void releaseMemberName(char *memberName) const = 0;
483  virtual char *duplicateStringValue(const char *value, unsigned int length = unknown) const = 0;
484  virtual void releaseStringValue(char *value) const = 0;
485  };
486 
487 #ifdef JSON_VALUE_USE_INTERNAL_MAP
488 
532  class JSON_API ValueMapAllocator {
533  public:
534  virtual ~ValueMapAllocator();
535  virtual ValueInternalMap *newMap() = 0;
536  virtual ValueInternalMap *newMapCopy(const ValueInternalMap &other) = 0;
537  virtual void destructMap(ValueInternalMap *map) = 0;
538  virtual ValueInternalLink *allocateMapBuckets(unsigned int size) = 0;
539  virtual void releaseMapBuckets(ValueInternalLink *links) = 0;
540  virtual ValueInternalLink *allocateMapLink() = 0;
541  virtual void releaseMapLink(ValueInternalLink *link) = 0;
542  };
543 
547  class JSON_API ValueInternalLink {
548  public:
549  enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture.
550  enum InternalFlags { flagAvailable = 0, flagUsed = 1 };
551 
552  ValueInternalLink();
553 
554  ~ValueInternalLink();
555 
556  Value items_[itemPerLink];
557  char *keys_[itemPerLink];
558  ValueInternalLink *previous_;
559  ValueInternalLink *next_;
560  };
561 
574  class JSON_API ValueInternalMap {
575  friend class ValueIteratorBase;
576  friend class Value;
577 
578  public:
579  typedef unsigned int HashKey;
580  typedef unsigned int BucketIndex;
581 
582 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
583  struct IteratorState {
584  IteratorState() : map_(0), link_(0), itemIndex_(0), bucketIndex_(0) {}
585  ValueInternalMap *map_;
586  ValueInternalLink *link_;
587  BucketIndex itemIndex_;
588  BucketIndex bucketIndex_;
589  };
590 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
591 
592  ValueInternalMap();
593  ValueInternalMap(const ValueInternalMap &other);
594  ValueInternalMap &operator=(const ValueInternalMap &other);
595  ~ValueInternalMap();
596 
597  void swap(ValueInternalMap &other);
598 
599  BucketIndex size() const;
600 
601  void clear();
602 
603  bool reserveDelta(BucketIndex growth);
604 
605  bool reserve(BucketIndex newItemCount);
606 
607  const Value *find(const char *key) const;
608 
609  Value *find(const char *key);
610 
611  Value &resolveReference(const char *key, bool isStatic);
612 
613  void remove(const char *key);
614 
615  void doActualRemove(ValueInternalLink *link, BucketIndex index, BucketIndex bucketIndex);
616 
617  ValueInternalLink *&getLastLinkInBucket(BucketIndex bucketIndex);
618 
619  Value &setNewItem(const char *key, bool isStatic, ValueInternalLink *link, BucketIndex index);
620 
621  Value &unsafeAdd(const char *key, bool isStatic, HashKey hashedKey);
622 
623  HashKey hash(const char *key) const;
624 
625  int compare(const ValueInternalMap &other) const;
626 
627  private:
628  void makeBeginIterator(IteratorState &it) const;
629  void makeEndIterator(IteratorState &it) const;
630  static bool equals(const IteratorState &x, const IteratorState &other);
631  static void increment(IteratorState &iterator);
632  static void incrementBucket(IteratorState &iterator);
633  static void decrement(IteratorState &iterator);
634  static const char *key(const IteratorState &iterator);
635  static const char *key(const IteratorState &iterator, bool &isStatic);
636  static Value &value(const IteratorState &iterator);
637  static int distance(const IteratorState &x, const IteratorState &y);
638 
639  private:
640  ValueInternalLink *buckets_;
641  ValueInternalLink *tailLink_;
642  BucketIndex bucketsSize_;
643  BucketIndex itemCount_;
644  };
645 
657  class JSON_API ValueInternalArray {
658  friend class Value;
659  friend class ValueIteratorBase;
660 
661  public:
662  enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo.
663  typedef Value::ArrayIndex ArrayIndex;
664  typedef unsigned int PageIndex;
665 
666 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
667  struct IteratorState // Must be a POD
668  {
669  IteratorState() : array_(0), currentPageIndex_(0), currentItemIndex_(0) {}
670  ValueInternalArray *array_;
671  Value **currentPageIndex_;
672  unsigned int currentItemIndex_;
673  };
674 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
675 
676  ValueInternalArray();
677  ValueInternalArray(const ValueInternalArray &other);
678  ValueInternalArray &operator=(const ValueInternalArray &other);
679  ~ValueInternalArray();
680  void swap(ValueInternalArray &other);
681 
682  void clear();
683  void resize(ArrayIndex newSize);
684 
685  Value &resolveReference(ArrayIndex index);
686 
687  Value *find(ArrayIndex index) const;
688 
689  ArrayIndex size() const;
690 
691  int compare(const ValueInternalArray &other) const;
692 
693  private:
694  static bool equals(const IteratorState &x, const IteratorState &other);
695  static void increment(IteratorState &iterator);
696  static void decrement(IteratorState &iterator);
697  static Value &dereference(const IteratorState &iterator);
698  static Value &unsafeDereference(const IteratorState &iterator);
699  static int distance(const IteratorState &x, const IteratorState &y);
700  static ArrayIndex indexOf(const IteratorState &iterator);
701  void makeBeginIterator(IteratorState &it) const;
702  void makeEndIterator(IteratorState &it) const;
703  void makeIterator(IteratorState &it, ArrayIndex index) const;
704 
705  void makeIndexValid(ArrayIndex index);
706 
707  Value **pages_;
708  ArrayIndex size_;
709  PageIndex pageCount_;
710  };
711 
771  class JSON_API ValueArrayAllocator {
772  public:
773  virtual ~ValueArrayAllocator();
774  virtual ValueInternalArray *newArray() = 0;
775  virtual ValueInternalArray *newArrayCopy(const ValueInternalArray &other) = 0;
776  virtual void destructArray(ValueInternalArray *array) = 0;
788  virtual void reallocateArrayPageIndex(Value **&indexes,
789  ValueInternalArray::PageIndex &indexCount,
790  ValueInternalArray::PageIndex minNewIndexCount) = 0;
791  virtual void releaseArrayPageIndex(Value **indexes, ValueInternalArray::PageIndex indexCount) = 0;
792  virtual Value *allocateArrayPage() = 0;
793  virtual void releaseArrayPage(Value *value) = 0;
794  };
795 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
796 
801  public:
802  typedef unsigned int size_t;
803  typedef int difference_type;
805 
807 #ifndef JSON_VALUE_USE_INTERNAL_MAP
808  explicit ValueIteratorBase(const Value::ObjectValues::iterator &current);
809 #else
810  ValueIteratorBase(const ValueInternalArray::IteratorState &state);
811  ValueIteratorBase(const ValueInternalMap::IteratorState &state);
812 #endif
813 
814  bool operator==(const SelfType &other) const { return isEqual(other); }
815 
816  bool operator!=(const SelfType &other) const { return !isEqual(other); }
817 
819 
821  Value key() const;
822 
824  UInt index() const;
825 
827  const char *memberName() const;
828 
829  protected:
830  Value &deref() const;
831 
832  void increment();
833 
834  void decrement();
835 
837 
838  bool isEqual(const SelfType &other) const;
839 
840  void copy(const SelfType &other);
841 
842  private:
843 #ifndef JSON_VALUE_USE_INTERNAL_MAP
844  Value::ObjectValues::iterator current_;
845  // Indicates that iterator is for a null value.
846  bool isNull_;
847 #else
848  union {
849  ValueInternalArray::IteratorState array_;
850  ValueInternalMap::IteratorState map_;
851  } iterator_;
852  bool isArray_;
853 #endif
854  };
855 
860  friend class Value;
861 
862  public:
863  typedef unsigned int size_t;
864  typedef int difference_type;
865  typedef const Value &reference;
866  typedef const Value *pointer;
868 
870 
871  private:
874 #ifndef JSON_VALUE_USE_INTERNAL_MAP
875  explicit ValueConstIterator(const Value::ObjectValues::iterator &current);
876 #else
877  ValueConstIterator(const ValueInternalArray::IteratorState &state);
878  ValueConstIterator(const ValueInternalMap::IteratorState &state);
879 #endif
880  public:
882 
884  SelfType temp(*this);
885  ++*this;
886  return temp;
887  }
888 
890  SelfType temp(*this);
891  --*this;
892  return temp;
893  }
894 
896  decrement();
897  return *this;
898  }
899 
901  increment();
902  return *this;
903  }
904 
905  reference operator*() const { return deref(); }
906  };
907 
911  friend class Value;
912 
913  public:
914  typedef unsigned int size_t;
915  typedef int difference_type;
916  typedef Value &reference;
917  typedef Value *pointer;
919 
920  ValueIterator();
923 
924  private:
927 #ifndef JSON_VALUE_USE_INTERNAL_MAP
928  explicit ValueIterator(const Value::ObjectValues::iterator &current);
929 #else
930  ValueIterator(const ValueInternalArray::IteratorState &state);
931  ValueIterator(const ValueInternalMap::IteratorState &state);
932 #endif
933  public:
934  SelfType &operator=(const SelfType &other);
935 
937  SelfType temp(*this);
938  ++*this;
939  return temp;
940  }
941 
943  SelfType temp(*this);
944  --*this;
945  return temp;
946  }
947 
949  decrement();
950  return *this;
951  }
952 
954  increment();
955  return *this;
956  }
957 
958  reference operator*() const { return deref(); }
959  };
960 
961  } // namespace Json
962 } // namespace jsoncollector
963 #endif // CPPTL_JSON_H_INCLUDED
size
Write out results.
static const Int minInt
Definition: value.h:116
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: value.h:475
bool compare(const P &i, const P &j)
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
a comment placed on the line before a value
Definition: value.h:37
Value::ObjectValues::iterator current_
Definition: value.h:844
std::vector< const PathArgument * > InArgs
Definition: value.h:455
std::vector< std::string > Members
Definition: value.h:108
bool operator<(IOVSyncValue const &iLHS, IOVSyncValue const &iRHS)
Definition: IOVSyncValue.h:34
bool operator==(const SelfType &other) const
Definition: value.h:814
virtual char * makeMemberName(const char *memberName) const =0
a comment just after a value on the same line
Definition: value.h:38
ValueIterator iterator
Definition: value.h:109
bool operator!=(const SelfType &other) const
Definition: value.h:816
constexpr bool operator<=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
difference_type computeDistance(const SelfType &other) const
bool operator>(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:74
Iterator for object and array value.
Definition: value.h:910
void copy(const SelfType &other)
&#39;null&#39; value
Definition: value.h:26
location
10.6 INPUT and workflows
Definition: relval_nano.py:81
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
SelfType & operator=(const ValueIteratorBase &other)
std::vector< PathArgument > Args
Definition: value.h:456
void invalidPath(const std::string &path, int location)
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
static const Int maxInt
Definition: value.h:117
difference_type operator-(const SelfType &other) const
Definition: value.h:818
void makePath(const std::string &path, const InArgs &in)
constexpr bool operator>=(ELseverityLevel const &e1, ELseverityLevel const &e2) noexcept
unsigned integer value
Definition: value.h:28
Value key() const
Return either the index or the member name of the referenced value as a Value.
const iterator for object and array value.
Definition: value.h:859
ValueConstIterator const_iterator
Definition: value.h:110
SelfType & operator=(const SelfType &other)
virtual void releaseStringValue(char *value) const =0
static const UInt maxUInt
Definition: value.h:118
#define JSON_API
Definition: config.h:40
bool isEqual(const SelfType &other) const
CommentInfo * comments_
Definition: value.h:408
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
const Value & resolve(const Value &root) const
std::map< CZString, Value > ObjectValues
Definition: value.h:145
signed integer value
Definition: value.h:27
reco::JetExtendedAssociation::JetExtendedData Value
array value (ordered list)
Definition: value.h:32
key
prepare the HTCondor submission files and eventually submit them
Definition: value.py:1
unsigned int UInt
Definition: forwards.h:21
StaticString(const char *czstring)
Definition: value.h:64
bool operator!=(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:81
void addPathInArg(const std::string &path, const InArgs &in, InArgs::const_iterator &itInArg, PathArgument::Kind kind)
bool equals(const edm::RefToBase< Jet > &j1, const edm::RefToBase< Jet > &j2)
Experimental and untested: represents a "path" to access a node.
Definition: value.h:440
Path(const std::string &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
const char * c_str() const
Definition: value.h:68
ValueType
Type of the value held by a Value object.
Definition: value.h:25
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:413
UTF-8 string value.
Definition: value.h:30
static const Value null
Definition: value.h:115
void clear(EGIsoObj &c)
Definition: egamma.h:82
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
T operator[](int i) const
Represents a JSON value.
Definition: value.h:101
virtual char * duplicateStringValue(const char *value, unsigned int length=unknown) const =0
Lightweight wrapper to tag static string.
Definition: value.h:62
JSON (JavaScript Object Notation).
Definition: DataPoint.h:26
virtual void releaseMemberName(char *memberName) const =0
object value (collection of name/value pairs).
Definition: value.h:33
a comment on the line after a value (only make sense for root value)
Definition: value.h:39
reference operator*() const
Definition: value.h:958
const char * memberName() const
Return the member name of the referenced Value. "" if it is not an objectValue.
base class for Value iterators.
Definition: value.h:800