8 #define epicsExportSharedSymbols 10 #include <pv/valueBuilder.h> 12 namespace epics{
namespace pvData{
14 struct ValueBuilder::child {
17 child(Type t) : type(t) {}
18 virtual void build(
const std::string& name, FieldBuilderPtr& builder)=0;
88 struct ValueBuilder::child_scalar :
public ValueBuilder::child_scalar_base
90 virtual ~child_scalar() {}
92 child_scalar(
const void* v) : child_scalar_base(
static_cast<ScalarType>(ScalarTypeID<T>::value)), value(*
static_cast<
const T*>(v)) {}
96 if(val->getField()->getType()!=scalar)
99 PVScalarPtr scalar(std::tr1::static_pointer_cast<PVScalar>(val));
100 scalar->putFrom(value);
104 ValueBuilder::ValueBuilder(
const std::string &id) :parent(0),id(id) {}
121 #define STYPE(stype) case pv##stype: { const PV ##stype* ptr(static_cast<const PV##stype*>(subs)); PV##stype::value_type temp(ptr->get()); self._add(*it, pv##stype, &temp); } break 147 ValueBuilder::ValueBuilder(
const PVStructure& clone) :parent(0)
151 child_struct::fillStruct(*
this, clone);
155 ValueBuilder::ValueBuilder(ValueBuilder* par,
const std::string &id)
160 ValueBuilder::~ValueBuilder()
162 for(children_t::const_iterator it=children.begin(), end=children.end(); it!=end; ++it)
167 void ValueBuilder::_add(
const std::string& name,
const PVStructure& V)
170 ValueBuilder& self = addNested(name, structure, T->getID());
171 child_struct::fillStruct(self, V);
175 void ValueBuilder::_add(
const std::string& name,
ScalarType stype,
const void *V)
177 const children_t::iterator it(children.find(name));
178 if(it!=children.end()) {
179 if(it->second->type!=scalar && it->second->type!=scalarArray)
180 THROW_EXCEPTION2(std::logic_error,
"Not allowed to replace field. wrong type");
183 epics::auto_ptr<child> store;
185 #define STYPE(stype) case stype: store.reset(new child_scalar<ScalarTypeTraits<stype>::type>(V)); break 203 if(it!=children.end()) {
207 children[name] = store.get();
211 void ValueBuilder::_add(
const std::string& name,
const shared_vector<
const void>& V)
213 const children_t::iterator it(children.find(name));
214 if(it!=children.end()) {
215 if(it->second->type!=scalar && it->second->type!=scalarArray)
216 THROW_EXCEPTION2(std::logic_error,
"Not allowed to replace field. wrong type");
219 epics::auto_ptr<child> store(
new child_scalar_array(V));
221 children[name] = store.get();
225 ValueBuilder& ValueBuilder::addNested(
const std::string& name,
Type type,
const std::string &id)
228 THROW_EXCEPTION2(std::invalid_argument,
"addNested() only supports structure");
230 children_t::const_iterator it(children.find(name));
231 if(it==children.end()) {
232 epics::auto_ptr<child_struct> store(
new child_struct(
this, id));
234 children[name] = store.get();
236 }
else if(it->second->type==structure) {
237 sub =
static_cast<child_struct*>(it->second);
239 std::ostringstream msg;
240 msg<<
"Can't replace non-struct field '"<<name<<
"' with struct";
243 sub->builder.id = id;
247 ValueBuilder& ValueBuilder::endNested()
274 PVStructure::shared_pointer ValueBuilder::buildPVStructure()
const 277 THROW_EXCEPTION2(std::logic_error,
"Only top level structure may be built. Missing endNested() ?");
281 FieldBuilderPtr tbuild(getFieldCreate()->createFieldBuilder());
283 child_struct::buildStruct(*
this, tbuild);
285 type = tbuild->createStructure();
288 PVStructure::shared_pointer root(type->build());
290 child_struct::storeStruct(*
this, root);
std::tr1::shared_ptr< PVScalar > PVScalarPtr
std::tr1::shared_ptr< const Structure > StructureConstPtr
std::tr1::shared_ptr< PVField > PVFieldPtr
#define THROW_EXCEPTION2(TYPE, MSG)
epicsShareFunc bool yajl_parse_helper(std::istream &src, yajl_handle handle)