void ValueBuilder::_add(const std::string& name, ScalarType stype, const void *V) { const children_t::iterator it(children.find(name)); if(it!=children.end()) { if(it->second->type!=scalar && it->second->type!=scalarArray) THROW_EXCEPTION2(std::logic_error, "Not allowed to replace field. wrong type"); } epics::auto_ptr<child> store; switch(stype) { #define STYPE(stype) case stype: store.reset(new child_scalar<ScalarTypeTraits<stype>::type>(V)); break STYPE(pvBoolean); STYPE(pvByte); STYPE(pvShort); STYPE(pvInt); STYPE(pvLong); STYPE(pvUByte); STYPE(pvUShort); STYPE(pvUInt); STYPE(pvULong); STYPE(pvFloat); STYPE(pvDouble); STYPE(pvString); #undef STYPE } if(!store.get()) THROW_EXCEPTION2(std::logic_error, "Unhandled ScalarType"); if(it!=children.end()) { delete it->second; children.erase(it); } children[name] = store.get(); store.release(); }
ValueBuilder& ValueBuilder::endNested() { if(!parent) { THROW_EXCEPTION2(std::logic_error, "Can't end top of structure"); } return *parent; }
void PVUnionArray::swap(const_svector &other) { if(this->isImmutable()) THROW_EXCEPTION2(std::logic_error,"Immutable"); // no checkLength call here value.swap(other); }
void DefaultPVArray<T>::swap(const_svector &other) { if (this->isImmutable()) THROW_EXCEPTION2(std::logic_error, "immutable"); // no checkLength call here value.swap(other); }
void DefaultPVArray<T>::setCapacity(size_t capacity) { if(this->isCapacityMutable()) { this->checkLength(capacity); value.reserve(capacity); } else THROW_EXCEPTION2(std::logic_error, "capacity immutable"); }
ValueBuilder& ValueBuilder::addNested(const std::string& name, Type type, const std::string &id) { if(type!=structure) THROW_EXCEPTION2(std::invalid_argument, "addNested() only supports structure"); child_struct *sub; children_t::const_iterator it(children.find(name)); if(it==children.end()) { epics::auto_ptr<child_struct> store(new child_struct(this, id)); sub = store.get(); children[name] = store.get(); store.release(); } else if(it->second->type==structure) { sub = static_cast<child_struct*>(it->second); } else { std::ostringstream msg; msg<<"Can't replace non-struct field '"<<name<<"' with struct"; THROW_EXCEPTION2(std::invalid_argument, msg.str()); } sub->builder.id = id; return sub->builder; }
void ValueBuilder::_add(const std::string& name, const shared_vector<const void>& V) { const children_t::iterator it(children.find(name)); if(it!=children.end()) { if(it->second->type!=scalar && it->second->type!=scalarArray) THROW_EXCEPTION2(std::logic_error, "Not allowed to replace field. wrong type"); } epics::auto_ptr<child> store(new child_scalar_array(V)); children[name] = store.get(); store.release(); }
void DefaultPVArray<T>::setLength(size_t length) { if(this->isImmutable()) THROW_EXCEPTION2(std::logic_error, "immutable"); if (length == value.size()) return; this->checkLength(length); if (length < value.size()) value.slice(0, length); else value.resize(length); }
void PVUnionArray::setCapacity(size_t capacity) { if(this->isCapacityMutable()) { checkLength(capacity); const_svector value; swap(value); if(value.capacity()<capacity) { svector mvalue(thaw(value)); mvalue.reserve(capacity); value = freeze(mvalue); } swap(value); } else THROW_EXCEPTION2(std::logic_error, "capacity immutable"); }
void PVUnionArray::setLength(size_t length) { if(this->isImmutable()) THROW_EXCEPTION2(std::logic_error, "immutable"); const_svector value; swap(value); if (length == value.size()) return; checkLength(length); if (length < value.size()) { value.slice(0, length); } else { svector mvalue(thaw(value)); mvalue.resize(length); value = freeze(mvalue); } swap(value); }
PVStructure::shared_pointer ValueBuilder::buildPVStructure() const { if(parent) THROW_EXCEPTION2(std::logic_error, "Only top level structure may be built. Missing endNested() ?"); StructureConstPtr type; { FieldBuilderPtr tbuild(getFieldCreate()->createFieldBuilder()); child_struct::buildStruct(*this, tbuild); type = tbuild->createStructure(); } PVStructure::shared_pointer root(type->build()); child_struct::storeStruct(*this, root); return root; }
size_t elementSize(ScalarType id) { switch(id) { #define OP(ENUM, TYPE) case ENUM: return sizeof(TYPE) OP(pvBoolean, boolean); OP(pvUByte, uint8); OP(pvByte, int8); OP(pvUShort, uint16); OP(pvShort, int16); OP(pvUInt, uint32); OP(pvInt, int32); OP(pvULong, uint64); OP(pvLong, int64); OP(pvFloat, float); OP(pvDouble, double); OP(pvString, string); #undef OP default: THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType"); } }
SOCKET BlockingTCPConnector::tryConnect(osiSockAddr& address, int tries) { char strBuffer[64]; ipAddrToDottedIP(&address.ia, strBuffer, sizeof(strBuffer)); for(int tryCount = 0; tryCount<tries; tryCount++) { LOG(logLevelDebug, "Opening socket to PVA server %s, attempt %d.", strBuffer, tryCount+1); SOCKET socket = epicsSocketCreate(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (socket == INVALID_SOCKET) { epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); std::ostringstream temp; temp<<"Socket create error: "<<strBuffer; THROW_EXCEPTION2(std::runtime_error, temp.str()); } else { // TODO: use non-blocking connect() to have controllable timeout if(::connect(socket, &address.sa, sizeof(sockaddr))==0) { return socket; } else { epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); char saddr[32]; sockAddrToDottedIP(&address.sa, saddr, sizeof(saddr)); epicsSocketDestroy (socket); std::ostringstream temp; temp<<"error connecting to "<<saddr<<" : "<<strBuffer; throw std::runtime_error(temp.str()); } } } return INVALID_SOCKET; }
const char* name(ScalarType t) { if (t<pvBoolean || t>pvString) THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType"); return names[t]; }
ScalarType getScalarType(const string& pvalue) { for(size_t i=0; i<NELEMENTS(names); i++) if(pvalue==names[i]) return ScalarType(i); THROW_EXCEPTION2(std::invalid_argument, "error unknown ScalarType"); }
const char* name(Type t) { if (t<int(scalar) || t>int(unionArray)) THROW_EXCEPTION2(std::invalid_argument, "logic error unknown Type"); return names[t]; }