Commits

Tommaso Urli committed e0a9abb

Updated CMakeLists.txt, made cast operators explicit.

  • Participants
  • Parent commits cd4f277

Comments (0)

Files changed (3)

File CMakeLists.txt

 set(CMAKE_CXX_FLAGS "-std=c++11 -O3")
 
 find_package(BISON REQUIRED)
-set(BISON_OUTPUT json.tab.cc)
-set(BISON_DEFINES json.tab.hh)
+set(BISON_OUTPUT ${json_SOURCE_DIR}/json.tab.cc)
+set(BISON_DEFINES ${json_SOURCE_DIR}/json.tab.hh)
+include_directories(${json_SOURCE_DIR})
 if(BISON_FOUND)
 	add_custom_command(
 		OUTPUT ${BISON_OUTPUT}
 		COMMAND ${BISON_EXECUTABLE} 
 			--defines=${BISON_DEFINES}
 			--output=${BISON_OUTPUT}
-			json.y
+			${json_SOURCE_DIR}/json.y
 		COMMENT "Generating json.tab.hh, json.tab.cc (parser)"
 	)
 endif(BISON_FOUND)
 
 find_package(FLEX REQUIRED)
-set(FLEX_OUTPUT lex.yy.cc)
+set(FLEX_OUTPUT ${json_SOURCE_DIR}/lex.yy.cc)
 if(FLEX_FOUND)
 	add_custom_command(
 		OUTPUT ${FLEX_OUTPUT}
 		COMMAND ${FLEX_EXECUTABLE}
 			--outfile=${FLEX_OUTPUT}
-			json.l
+			${json_SOURCE_DIR}/json.l
 		COMMENT "Generating lex.yy.cc (lexer)"
 	)
 endif(FLEX_FOUND)
 
 }
 
+Value& Value::operator[] (const string& key)
+{
+    if (type() != OBJECT)
+        throw std::logic_error("Value not subscriptable");
+    return object_v[key];
+}
+
+const Value& Value::operator[] (const string& key) const
+{
+    if (type() != OBJECT)
+        throw std::logic_error("Value not subscriptable");
+    return object_v[key];
+}
+
+Value& Value::operator[] (size_t i)
+{
+    if (type() != ARRAY)
+        throw std::logic_error("Value not subscriptable");
+    return array_v[i];
+}
+
+const Value& Value::operator[] (size_t i) const
+{
+    if (type() != ARRAY)
+        throw std::logic_error("Value not subscriptable");
+    return array_v[i];
+}
+
+
 Object::Object() { }
 
 Object::~Object() { }
     return _object[key];
 }
 
+const Value& Object::operator[] (const string& key) const
+{
+    return _object.at(key);
+}
+
 pair<map<string, Value>::iterator, bool> Object::insert(const pair<string, Value>& v)
 {
     return _object.insert(v);
 
 Value& Array::operator[] (size_t i)
 {
-    return _array[i];
+    return _array.at(i);
+}
+
+const Value& Array::operator[] (size_t i) const
+{
+    return _array.at(i);
 }
 
 vector<Value>::const_iterator Array::begin() const
         */
         Value& operator[] (const std::string& key);
 
+        /** Subscript operator, access an element by key.
+            @param key key of the object to access
+        */
+        const Value& operator[] (const std::string& key) const;
+
         /** Retrieves the starting iterator (const).
             @remark mainly for printing
         */
             @param i index of the element to access
         */
         Value& operator[] (size_t i);
+        
+        /** Subscript operator, access an element by index. 
+            @param i index of the element to access
+        */
+        const Value& operator[] (size_t i) const;
 
         /** Retrieves the starting iterator (const).
             @remark mainly for printing
             return type_t;
         }
     
+        /** Subscript operator, access an element by key.
+            @param key key of the object to access
+        */
+        Value& operator[] (const std::string& key);
+
+        /** Subscript operator, access an element by key.
+            @param key key of the object to access
+        */
+        const Value& operator[] (const std::string& key) const;
+        
+        /** Subscript operator, access an element by index. 
+            @param i index of the element to access
+        */
+        Value& operator[] (size_t i);
+    
+        /** Subscript operator, access an element by index. 
+            @param i index of the element to access
+        */
+        const Value& operator[] (size_t i) const;
+    
         /** Assignment operator. */
         Value& operator=(const Value& v);
     
         Value& operator=(Value&& v);
     
         /** Cast operator for float */
-        operator long double() const { return float_v; }
+        explicit operator long double() const { return float_v; }
     
         /** Cast operator for float */
-        operator long long int() const { return int_v; }
+        explicit operator long long int() const { return int_v; }
     
         /** Cast operator for float */
-        operator bool() const { return bool_v; }
+        explicit operator bool() const { return bool_v; }
     
         /** Cast operator for float */
-        operator std::string () const { return string_v; }
+        explicit operator std::string () const { return string_v; }
     
         /** Cast operator for Object */
         operator Object () const { return object_v; }
     
         /** Cast operator for Object */
         operator Array () const { return array_v; }
+        
+        /** Cast operator for float */
+        long double to_float() const { return float_v; }
+    
+        /** Cast operator for float */
+        long long int to_int() const { return int_v; }
+    
+        /** Cast operator for float */
+        bool to_bool() const { return bool_v; }
+    
+        /** Cast operator for float */
+        std::string to_string() const { return string_v; }
 
 
     protected: