oculus1

view libovr/Src/OVR_JSON.h @ 26:75ab0d4ce2bb

backed out the shaderless oculus distortion method, as it's completely pointless
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 04 Oct 2013 14:57:33 +0300
parents
children
line source
1 /************************************************************************************
3 PublicHeader: None
4 Filename : OVR_JSON.h
5 Content : JSON format reader and writer
6 Created : April 9, 2013
7 Author : Brant Lewis
8 Notes :
10 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
12 Use of this software is subject to the terms of the Oculus license
13 agreement provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
16 ************************************************************************************/
18 #ifndef OVR_JSON_H
19 #define OVR_JSON_H
21 #include "Kernel/OVR_RefCount.h"
22 #include "Kernel/OVR_String.h"
23 #include "Kernel/OVR_List.h"
25 namespace OVR {
27 // JSONItemType describes the type of JSON item, specifying the type of
28 // data that can be obtained from it.
29 enum JSONItemType
30 {
31 JSON_None = 0,
32 JSON_Null = 1,
33 JSON_Bool = 2,
34 JSON_Number = 3,
35 JSON_String = 4,
36 JSON_Array = 5,
37 JSON_Object = 6
38 };
41 //-----------------------------------------------------------------------------
42 // ***** JSON
44 // JSON object represents a JSON node that can be either a root of the JSON tree
45 // or a child item. Every node has a type that describes what is is.
46 // New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
48 class JSON : public RefCountBase<JSON>, public ListNode<JSON>
49 {
50 protected:
51 List<JSON> Children;
53 public:
54 JSONItemType Type; // Type of this JSON node.
55 String Name; // Name part of the {Name, Value} pair in a parent object.
56 String Value;
57 double dValue;
59 public:
60 ~JSON();
62 // *** Creation of NEW JSON objects
64 static JSON* CreateObject() { return new JSON(JSON_Object);}
65 static JSON* CreateNull() { return new JSON(JSON_Null); }
66 static JSON* CreateArray() { return new JSON(JSON_Array); }
67 static JSON* CreateBool(bool b) { return createHelper(JSON_Bool, b ? 1.0 : 0.0); }
68 static JSON* CreateNumber(double num) { return createHelper(JSON_Number, num); }
69 static JSON* CreateString(const char *s) { return createHelper(JSON_String, 0.0, s); }
71 // Creates a new JSON object from parsing string.
72 // Returns null pointer and fills in *perror in case of parse error.
73 static JSON* Parse(const char* buff, const char** perror = 0);
75 // Loads and parses a JSON object from a file.
76 // Returns 0 and assigns perror with error message on fail.
77 static JSON* Load(const char* path, const char** perror = 0);
79 // Saves a JSON object to a file.
80 bool Save(const char* path);
83 // *** Object Member Access
85 // These provide access to child items of the list.
86 bool HasItems() const { return Children.IsEmpty(); }
87 // Returns first/last child item, or null if child list is empty
88 JSON* GetFirstItem() { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
89 JSON* GetLastItem() { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
91 // Counts the number of items in the object; these methods are inefficient.
92 unsigned GetItemCount() const;
93 JSON* GetItemByIndex(unsigned i);
94 JSON* GetItemByName(const char* name);
96 // Returns next item in a list of children; 0 if no more items exist.
97 JSON* GetNextItem(JSON* item) { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
98 JSON* GetPrevItem(JSON* item) { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
101 // Child item access functions
102 void AddItem(const char *string, JSON* item);
103 void AddNullItem(const char* name) { AddItem(name, CreateNull()); }
104 void AddBoolItem(const char* name, bool b) { AddItem(name, CreateBool(b)); }
105 void AddNumberItem(const char* name, double n) { AddItem(name, CreateNumber(n)); }
106 void AddStringItem(const char* name, const char* s) { AddItem(name, CreateString(s)); }
107 // void ReplaceItem(unsigned index, JSON* new_item);
108 // void DeleteItem(unsigned index);
110 // *** Array Element Access
112 // Add new elements to the end of array.
113 void AddArrayElement(JSON *item);
114 void AddArrayNumber(double n) { AddArrayElement(CreateNumber(n)); }
115 void AddArrayString(const char* s) { AddArrayElement(CreateString(s)); }
117 // Accessed array elements; currently inefficient.
118 int GetArraySize();
119 double GetArrayNumber(int index);
120 const char* GetArrayString(int index);
123 protected:
124 JSON(JSONItemType itemType = JSON_Object);
126 static JSON* createHelper(JSONItemType itemType, double dval, const char* strVal = 0);
128 // JSON Parsing helper functions.
129 const char* parseValue(const char *buff, const char** perror);
130 const char* parseNumber(const char *num);
131 const char* parseArray(const char* value, const char** perror);
132 const char* parseObject(const char* value, const char** perror);
133 const char* parseString(const char* str, const char** perror);
135 char* PrintValue(int depth, bool fmt);
136 char* PrintObject(int depth, bool fmt);
137 char* PrintArray(int depth, bool fmt);
138 };
141 }
143 #endif