void JsonSerializer::recurseRead(Json::Value& jv, any& av) { if(av.type() == typeid(int)) { jv = any_cast<int>(av); } else if(av.type() == typeid(unsigned int)) { jv = any_cast<unsigned int>(av); } else if(av.type() == typeid(long long int)) { jv = any_cast<long long int>(av); } else if(av.type() == typeid(unsigned long long int)) { jv = any_cast<unsigned long long int>(av); } else if(av.type() == typeid(float)) { jv = any_cast<float>(av); } else if(av.type() == typeid(double)) { jv = any_cast<double>(av); } else if(av.type() == typeid(bool)) { jv = any_cast<bool>(av); } else if(av.type() == typeid(string)) { string str = any_cast<string>(av); jv = str; } else if(av.type() == typeid(list<any>)) { auto lst = any_cast<list<any>&>(av); int i = 0; for(auto it:lst) { recurseRead(jv[i], it); ++i; } } else if(av.type() == typeid(list<pair<string, any> >)) { list<pair<string, any> >& lst = any_cast<list<pair<string, any> >&>(av); Json::Value tmp; for(auto it:lst) { recurseRead(tmp[it.first], it.second); } jv.append(tmp); } else { cout<<"error wrong type!"<<endl; } }
void field::set(any& o, const any& value) const { if (this->_setter != nullptr) { any v = this->desc().cast(value); this->_setter (o,v); return; } void* optr = o.ptr(); word8** ptr = memory::ptr(optr,this->_offset); if (this->_descriptor->has(descriptor::Flags::FIELDS)) { if (this->_flags.test(field::FLAGS_IS_POINTER)) { word8** destination = (word8**) ptr; word8* source = (word8*) value.ptr(); *destination = source; } } else { if (value.type() == typeid(std::string)) { std::string svalue = anycast<std::string>(value); this->froms(o,svalue); } else this->_descriptor->set(ptr,value); } }
void test_default_ctor() { const any value; check_true(value.empty(), "empty"); check_null(any_cast<int>(&value), "any_cast<int>"); check_equal(value.type(), typeid(void), "type"); }
void test_null_copying() { const any null; any copied = null, assigned; assigned = null; check_true(null.empty(), "empty on null"); check_true(copied.empty(), "empty on copied"); check_true(assigned.empty(), "empty on copied"); }
//CAST any bitset_descriptor::cast (const any& value) const { if (value.desc() != this) { any destination; std::string svalue; return this->stoa(value.desc()->atos(value), destination); } return any(value); }
//! \brief Return if two any objects are the same or not. bool same( const any &other ) const { if( this->empty() && other.empty() ) return true; else if( this->empty() && !other.empty() ) return false; else if( !this->empty() && other.empty() ) return false; // !this->empty() && !other.empty() return content->same(*other.content); }
// RAW VALUE SERIALIZATION void bitset_descriptor::set(void* ptr, const any& value) const { boost::dynamic_bitset<>* destination = (boost::dynamic_bitset<>*) ptr; if (value.desc() != this) { any a = destination; std::string svalue = std::move(value.desc()->atos(value)); this->stoa(svalue, a); *destination = *anycast<boost::dynamic_bitset<>*>(a); } else { *destination = *anycast<boost::dynamic_bitset<>*>(value); } }
// USeless ? bool serial_manager::remove_object(any object) { const descriptor* d = object.desc(); any serial_value; d->serial().get(object, serial_value); return this->remove(serial_value.tos()); }
/* set a named property */ bool GemState::set(const GemState::key_t key, any value) { if(value.empty()) { data->data.erase(key); return false; } /* wrapper for DEPRECATED access to member variables */ if(1) { try { switch(key) { case(_DIRTY): dirty=gem::any_cast<bool>(value); break; case(_PIX): image=gem::any_cast<pixBlock*>(value); break; case(_GL_TEX_NUMCOORDS): numTexCoords=gem::any_cast<int>(value); break; case(_GL_TEX_COORDS): texCoords=gem::any_cast<TexCoord*>(value); break; case(_GL_LIGHTING): lighting=gem::any_cast<bool>(value); break; case(_GL_SMOOTH): smooth=gem::any_cast<bool>(value); break; case(_GL_TEX_TYPE): texture=gem::any_cast<int>(value); break; case(_GL_TEX_UNITS): multiTexUnits=gem::any_cast<int>(value); break; case(_TIMING_TICK): tickTime=gem::any_cast<float>(value); break; case(_GL_DRAWTYPE): drawType=gem::any_cast<GLenum>(value); break; case(_GL_DISPLAYLIST): inDisplayList=gem::any_cast<bool>(value); break; default: break; } } CATCH_ANY(key); } data->data[key]=value; return true; }
// in_value must either hold an object of value_type or a string that // can be converted to value_type via iostreams. void do_put(const any& in_key, const any& in_value, mpl::bool_<true>) { #if !(defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)) using boost::put; #endif key_type key = any_cast<key_type>(in_key); if (in_value.type() == typeid(value_type)) { #if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95) boost::put(property_map, key, any_cast<value_type>(in_value)); #else put(property_map, key, any_cast<value_type>(in_value)); #endif } else { // if in_value is an empty string, put a default constructed value_type. std::string v = any_cast<std::string>(in_value); if (v.empty()) { #if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95) boost::put(property_map, key, value_type()); #else put(property_map, key, value_type()); #endif } else { #if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95) boost::put(property_map, key, detail::read_value<value_type>(v)); #else put(property_map, key, detail::read_value<value_type>(v)); #endif } } }
any descriptor::cast (const any& value) const { if (value.type() == this->type()) { return any(value); } throw new LAURENA_UNIMPLEMENTED_EXCEPTION("recast not implemented for this class"); }
// TO/FROM STRING SERIALIZATION std::string any_descriptor::atos(const any& value) const { const descriptor* cd = value.desc(); if (!cd) { throw LAURENA_NULL_POINTER_EXCEPTION("unknow class descriptor"); } return cd->atos(value); }
void field::froms(any& object, const std::string& svalue) const { any value; void* ptrObject = object.ptr(); void* ptrAttribute = this->ptr(ptrObject); value = svalue; this->_descriptor->set(ptrAttribute,value); }
static void impl(CONTAINER* c, const descriptor* element_descriptor, any& element) { if ( element.desc()->has(descriptor::Flags::ATOMIC)) { element = element_descriptor->cast(element); c->push_back(anycast<ELEMENT>(element)); } else { ELEMENT* e = anycast<ELEMENT*>(element); c->push_back(*e); } }
// in_value must either hold an object of value_type or a string that // can be converted to value_type via iostreams. void do_put(const any& in_key, const any& in_value, mpl::bool_<true>) { using boost::put; key_type key = any_cast<key_type>(in_key); if (in_value.type() == typeid(value_type)) { put(property_map_, key, any_cast<value_type>(in_value)); } else { // if in_value is an empty string, put a default constructed value_type. std::string v = any_cast<std::string>(in_value); if (v.empty()) { put(property_map_, key, value_type()); } else { put(property_map_, key, detail::read_value<value_type>(v)); } } }
void oarchive_json::serializeObject(const any& value) { const descriptor* cd = value.desc(); if ( !cd) { throw new LAURENA_NULL_POINTER_EXCEPTION("any& value do have nullptr descriptor."); } word32 backup = this->_nb_fields; this->_nb_fields = 0; std::string opening = "{"; std::string ending = "}" ; if (cd->hasFeature(Feature::CONTAINER)) { opening = "[" ; ending = "]" ; } // opening if (this->_compact) this->_data << opening ; else { this->_data << this->_tab << opening << std::endl; this->_tab.increase(); } if (cd->has(descriptor::Flags::FIELDS)) this->serializeFields(*cd,value); if (cd->hasFeature(Feature::CONTAINER)) this->serializeElements(*cd,value); if (this->_compact) this->_data << ending ; else { this->_data << std::endl << this->_tab.decrease() << ending ; } this->_nb_fields = backup; }
bool rpc::manager::validate( const any &message, any &error ) { any err; bool ok = true; if ( !message.is_object() || !message.is_member( "jsonrpc" ) || ( message[ "jsonrpc" ].to_string() != "2.0" ) ) { err[ "code" ] = errc::invalid_request; err[ "message" ] = "Invalid JSON-RPC request."; error[ "id" ] = any::null(); error[ "jsonrpc" ] = "2.0"; error[ "error" ] = err; ok = false; } else if ( !message.is_member( "method" ) && !message.is_member( "id" ) ) { err[ "code" ] = errc::invalid_request; err[ "message" ] = "Invalid JSON-RPC request."; error[ "id" ] = any::null(); error["jsonrpc" ] = "2.0"; error[ "error" ] = err; ok = false; } else if ( message.is_member( "id" ) && !message[ "id" ].is_integer() ) { err[ "code" ] = errc::invalid_request; err[ "message" ] = "Invalid JSON-RPC request."; error[ "id" ] = any::null(); error[ "jsonrpc" ] = "2.0"; error[ "error" ] = err; ok = false; } else if ( message.is_member( "method" ) && !message["method"].is_string() ) { err[ "code" ] = errc::invalid_request; err[ "message" ] = "Invalid JSON-RPC request."; error[ "id" ] = any::null(); error["jsonrpc" ] = "2.0"; error[ "error" ] = err; ok = false; } return ok; }
bool can_cast(any &a) { return typeid(T) == a.type();}
bool is_empty(any<has_empty<bool(), const _self>, const _self&> x) { return x.empty(); }
void append_many(any<has_push_back<void(int)>, _self&> container) { for(int i = 0; i < 10; ++i) container.push_back(i); }
bool is_int(const any & operand) { return operand.type() == typeid(int); }
void print(any val) { val.print(std::cout); std::cout << std::endl; }
bool is_double(const any & operand) { return operand.type() == typeid(double); }
void iarchive_xml::readObject(const std::string& tag, any& object) { token t; std::string keyword; const descriptor* d = object.desc(); // READ ATTRIBUTES this->readAttributes(object); // End of section t = std::move(this->read_token(XML::TOKEN_INFERIOR_SLASH, XML::TOKEN_SUPERIOR)); while(true) { std::string content = this->_tokenizer.readUntil("<",true); if (*this->_tokenizer._ptr == '/') { // Check that incoming keyword is same than the last one this->read_token(XML::TOKEN_SLASH); t = std::move(this->read_token( XML::TOKEN_KEYWORD)); std::string keyword = anycast<std::string>(t); if (keyword == tag) { this->read_token(XML::TOKEN_SUPERIOR); //object.desc()->stoa(content, object); return; } else this->error_tag_not_expected(tag, keyword); } else { // check content is tabs if (!const_charsets<>::TABS.validate(content)) { throw LAURENA_FAILED_PARSING_EXCEPTION("Syntax error",this->_tokenizer._ptr); } // read keyword t=std::move(this->read_token(XML::TOKEN_KEYWORD)); std::string keyword = anycast<std::string>(t); // Identification of keyword if (d->has(descriptor::Flags::FIELDS)) { const field* f = d->findField(keyword); if (f) { this->readField(*f, keyword, object); continue; } } std::ostringstream ss; ss << keyword << " : unknow child for class " << d->name(); throw LAURENA_FAILED_PARSING_EXCEPTION(ss.str().c_str(),this->_tokenizer._ptr); } } }
void oarchive_json::serializeFields(const descriptor& cd, const any& value) { std::string s; const polymorphic_feature* pcf = dynamic_cast<const polymorphic_feature*>(cd.feature(Feature::POLYMORPHIC)); const any_feature* acf = NULL; if (pcf && pcf->has_parent()) this->serializeFields(pcf->parent(),value); if (cd.has(descriptor::Flags::FIELDS) == false) return; void* ptrObject = value.ptr(); any fieldValue; for (const std::unique_ptr<field>& patt : cd.get_fields()) { const field& att = *patt; if (att.need_support()) continue; this->completeLastLine(); att.get(value,fieldValue); const format* fieldFormat = dynamic_cast<const format*>(att.annotations().get(JSON::ANNOTATION_NAME, ANNOTATION_FORMAT_ALL)); if ( fieldFormat ) { this->printFieldName(att); this->_data << "\""; fieldFormat->write(this->_data , fieldValue); this->_data << "\""; continue; } const format* typeFormat = dynamic_cast<const format*>(att.desc().annotations().get(JSON::ANNOTATION_NAME, ANNOTATION_FORMAT_ALL)); if (typeFormat) { this->printFieldName(att); this->_data << "\""; typeFormat->write(this->_data,fieldValue); this->_data << "\""; continue; } const descriptor& acd = att.desc(); void* vptr = att.ptr(ptrObject); if (acd.has(descriptor::Flags::FIELDS)) { this->printFieldName(att); if (!this->_compact) this->_data << std::endl; this->serializeObject(fieldValue); } else if (acd.hasFeature(Feature::CONTAINER)) { this->printFieldName(att); if (!this->_compact) this->_data << std::endl; this->serializeObject(fieldValue); } else { if ( att.ignore_if_default_value()) { if (acd.equals(att.default_value(),fieldValue)) continue; } this->printFieldName(att); bool isString = false; att.tos(value,s); if (acd.type() == typeid(std::string)) isString = true; else { if (acd.has(descriptor::Flags::NUMERIC_VALUE) == false) isString |= !is_integer(s); else isString = true; } if (isString) this->_data << "\""; this->_data << s; if (isString) this->_data << "\""; } } }
void luaPushAny(lua_State* plua_state, const any& a) { if (a.type() == typeid(bool)) { luaPushBoolean(plua_state, any_cast<bool>(a)); } else if (a.type() == typeid(void*)) { luaPushLightUserData(plua_state, any_cast<void*>(a)); } else if (a.type() == typeid(double)) { luaPushDouble(plua_state, any_cast<double>(a)); } else if (a.type() == typeid(std::string)) { luaPushString(plua_state, any_cast<std::string>(a)); } else if (a.type() == typeid(float)) { luaPushDouble(plua_state, (double)any_cast<float>(a)); } else if (a.type() == typeid(int)) { luaPushInteger(plua_state, any_cast<int>(a)); } else if (a.type() == typeid(size_t)) { luaPushInteger(plua_state, (int)any_cast<size_t>(a)); } else if (a.type() == typeid(short)) { luaPushInteger(plua_state, (int)any_cast<short>(a)); } else if (a.type() == typeid(long)) { luaPushInteger(plua_state, (int)any_cast<long>(a)); } else if (a.type() == typeid(uint8_t)) { luaPushInteger(plua_state, (int)any_cast<uint8_t>(a)); } else if (a.type() == typeid(uint16_t)) { luaPushInteger(plua_state, (int)any_cast<uint16_t>(a)); } else if (a.type() == typeid(uint32_t)) { luaPushInteger(plua_state, (int)any_cast<uint32_t>(a)); } else { luaPushNil(plua_state); } }