Storable* Storable::createByCode(std::int32_t id) { Storable* storable = nullptr; std::int32_t subCode = 0; switch (id) { case StoreInt8::code: storable = new StoreInt8(); break; case StoreInt16::code: storable = new StoreInt16(); break; case StoreInt32::code: storable = new StoreInt32(); break; case StoreInt64::code: storable = new StoreInt64(); break; case StoreVocab::code: storable = new StoreVocab(); break; case StoreFloat32::code: storable = new StoreFloat32(); break; case StoreFloat64::code: storable = new StoreFloat64(); break; case StoreString::code: storable = new StoreString(); break; case StoreBlob::code: storable = new StoreBlob(); break; case StoreList::code: storable = new StoreList(); yAssert(storable != nullptr); storable->asList()->implementation->setNested(true); break; default: if ((id & GROUP_MASK) != 0) { // typed list subCode = (id & UNIT_MASK); if (id & BOTTLE_TAG_DICT) { storable = new StoreDict(); yAssert(storable != nullptr); } else { storable = new StoreList(); yAssert(storable != nullptr); storable->asList()->implementation->specialize(subCode); storable->asList()->implementation->setNested(true); } } break; } return storable; }
void BottleImpl::synch() { if (dirty) { if (!nested) { subCode(); YMSG(("bottle code %d\n",StoreList::code + subCode())); } data.clear(); BufferedConnectionWriter writer; if (!nested) { writer.appendInt(StoreList::code + speciality); YMSG(("wrote bottle code %d\n",StoreList::code + speciality)); } YMSG(("bottle length %d\n",size())); writer.appendInt((int)size()); for (unsigned int i=0; i<content.size(); i++) { Storable *s = content[i]; if (speciality==0) { YMSG(("subcode %d\n",s->getCode())); writer.appendInt(s->getCode()); } else { YMSG(("skipped subcode %d\n",s->getCode())); YARP_ASSERT(speciality==s->getCode()); } if (s->isList()) { s->asList()->setNested(true); } s->writeRaw(writer); } data.resize(writer.dataSize(),' '); MemoryOutputStream m(&data[0]); writer.write(m); dirty = false; } }
bool BottleImpl::fromBytes(ConnectionReader& reader) { if (reader.isError()) return false; int id = speciality; YMSG(("READING, nest flag is %d\n", nested)); if (id==0) { id = reader.expectInt(); YMSG(("READ subcode %d\n", id)); } else { YMSG(("READ skipped subcode %d\n", speciality)); } Storable *storable = Storable::createByCode(id); if (storable==NULL) { YARP_SPRINTF1(Logger::get(),error,"BottleImpl reader failed, unrecognized object code %d",id); return false; } storable->readRaw(reader); add(storable); return true; }
Storable *Storable::createByCode(int id) { Storable *storable = NULL; int subCode = 0; switch (id) { case StoreInt::code: storable = new StoreInt(); break; case StoreVocab::code: storable = new StoreVocab(); break; case StoreDouble::code: storable = new StoreDouble(); break; case StoreString::code: storable = new StoreString(); break; case StoreBlob::code: storable = new StoreBlob(); break; case StoreList::code: storable = new StoreList(); YARP_ASSERT(storable!=NULL); storable->asList()->setNested(true); break; default: if ((id&GROUP_MASK)!=0) { // typed list subCode = (id&UNIT_MASK); if (id&BOTTLE_TAG_DICT) { storable = new StoreDict(); YARP_ASSERT(storable!=NULL); } else { storable = new StoreList(); YARP_ASSERT(storable!=NULL); storable->asList()->specialize(subCode); storable->asList()->setNested(true); } } break; } return storable; }
bool Value::read(ConnectionReader& connection) { if (proxy) { delete proxy; proxy = 0; } int x = connection.expectInt(); if ((x&0xffff) != x) return false; if (!(x&BOTTLE_TAG_LIST)) return false; int len = connection.expectInt(); if (len==0) return true; if (len!=1) return false; if (x==BOTTLE_TAG_LIST) { x = connection.expectInt(); } else { x &= ~BOTTLE_TAG_LIST; } if (connection.isError()) return false; Storable *s = Storable::createByCode(x); setProxy(s); if (!proxy) return false; return s->readRaw(connection); }
void BottleImpl::smartAdd(const String& str) { if (str.length()>0) { char ch = str[0]; Storable *s = NULL; StoreString *ss = NULL; bool numberLike = true; bool preamble = true; bool hexActive = false; int hexStart = 0; int periodCount = 0; int signCount = 0; bool hasPeriodOrE = false; for (int i=0; i<(int)str.length(); i++) { char ch2 = str[i]; if (ch2=='.') { hasPeriodOrE = true; periodCount++; if (periodCount>1) { numberLike = false; } } if (!hexActive && (ch2=='e'||ch2=='E')) { hasPeriodOrE = true; } if (preamble) { if (ch2=='x'||ch2=='X') { hexActive = true; hexStart = i; continue; } } if (preamble) { if (ch2=='0'||ch2=='+'||ch2=='-') { if (ch2=='+'||ch2=='-') { signCount++; if (signCount>1) { numberLike = false; } } continue; } } preamble = false; if (!((ch2>='0'&&ch2<='9')||ch2=='.'||ch2=='e'||ch2=='E'|| ch2=='+'||ch2=='-'|| (hexActive&&((ch2>='a'&&ch2<='f')|| (ch2>='A'&&ch2<='F'))))) { numberLike = false; break; } } if (hexActive) { if ((int)str.length()-(hexStart+1)>8) { // we can only deal with 32bit hexadecimal numberLike = false; } } if (numberLike && ((ch>='0'&&ch<='9')||ch=='+'||ch=='-'||ch=='.') && (ch!='.'||str.length()>1)) { if (!hasPeriodOrE) { s = new StoreInt(0); } else { s = new StoreDouble(0); } } else if (ch=='(') { s = new StoreList(); } else if (ch=='[') { s = new StoreVocab(); } else if (ch=='{') { s = new StoreBlob(); } else { s = ss = new StoreString(""); } if (s!=NULL) { s->fromStringNested(str); if (ss!=NULL) { if (str.length() == 0 || str[0]!='\"') { String val = ss->asStringFlex(); if (val=="true") { delete s; s = new StoreVocab((int)'1'); } else if (val=="false") { delete s; s = new StoreVocab(0); } } } add(s); } ss = NULL; } }