pvDatabaseCPP  4.7.0
pvdbcrRemoveRecord.cpp
Go to the documentation of this file.
1 /*
2  * Copyright information and license terms for this software can be
3  * found in the file LICENSE that is included with the distribution
4  */
5 
10 #include <iocsh.h>
11 #include <pv/standardField.h>
12 #include <pv/standardPVField.h>
13 #include <pv/timeStamp.h>
14 #include <pv/pvTimeStamp.h>
15 #include <pv/alarm.h>
16 #include <pv/pvAlarm.h>
17 #include <pv/pvAccess.h>
18 #include <pv/serverContext.h>
19 #include <pv/rpcService.h>
20 
21 #include <epicsExport.h>
22 #define epicsExportSharedSymbols
23 #include "pv/pvDatabase.h"
24 #include "pv/pvdbcrRemoveRecord.h"
25 using namespace epics::pvData;
26 using namespace std;
27 
28 namespace epics { namespace pvDatabase {
29 
30 PvdbcrRemoveRecordPtr PvdbcrRemoveRecord::create(
31  std::string const & recordName,
32  int asLevel,std::string const & asGroup)
33 {
34  FieldCreatePtr fieldCreate = getFieldCreate();
35  PVDataCreatePtr pvDataCreate = getPVDataCreate();
36  StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
37  addNestedStructure("argument")->
38  add("recordName",pvString)->
39  endNested()->
40  addNestedStructure("result") ->
41  add("status",pvString) ->
42  endNested()->
43  createStructure();
44  PVStructurePtr pvStructure = pvDataCreate->createPVStructure(topStructure);
45  PvdbcrRemoveRecordPtr pvRecord(
46  new PvdbcrRemoveRecord(recordName,pvStructure,
47  asLevel,asGroup));
48  if(!pvRecord->init()) pvRecord.reset();
49  return pvRecord;
50 }
51 
52 PvdbcrRemoveRecord::PvdbcrRemoveRecord(
53  std::string const & recordName,
54  epics::pvData::PVStructurePtr const & pvStructure,
55  int asLevel,std::string const & asGroup)
56 : PVRecord(recordName,pvStructure,asLevel,asGroup)
57 {
58 }
59 
60 bool PvdbcrRemoveRecord::init()
61 {
62  initPVRecord();
63  PVStructurePtr pvStructure = getPVStructure();
64  pvRecordName = pvStructure->getSubField<PVString>("argument.recordName");
65  if(!pvRecordName) return false;
66  pvResult = pvStructure->getSubField<PVString>("result.status");
67  if(!pvResult) return false;
68  return true;
69 }
70 
71 void PvdbcrRemoveRecord::process()
72 {
73  string name = pvRecordName->get();
74  PVRecordPtr pvRecord = PVDatabase::getMaster()->findRecord(name);
75  if(!pvRecord) {
76  pvResult->put(name + " not found");
77  return;
78  }
79  pvRecord->remove();
80  pvResult->put("success");
81 }
82 }}
83 
84 static const iocshArg arg0 = { "recordName", iocshArgString };
85 static const iocshArg arg1 = { "asLevel", iocshArgInt };
86 static const iocshArg arg2 = { "asGroup", iocshArgString };
87 static const iocshArg *args[] = {&arg0,&arg1,&arg2};
88 
89 static const iocshFuncDef pvdbcrRemoveRecordFuncDef = {"pvdbcrRemoveRecord", 3,args};
90 
91 static void pvdbcrRemoveRecordCallFunc(const iocshArgBuf *args)
92 {
93  char *sval = args[0].sval;
94  if(!sval) {
95  throw std::runtime_error("pvdbcrRemoveRecord recordName not specified");
96  }
97  string recordName = string(sval);
98  int asLevel = args[1].ival;
99  string asGroup("DEFAULT");
100  sval = args[2].sval;
101  if(sval) {
102  asGroup = string(sval);
103  }
105  record->setAsLevel(asLevel);
106  record->setAsGroup(asGroup);
108  bool result = master->addRecord(record);
109  if(!result) cout << "recordname " << recordName << " not added" << endl;
110 }
111 
112 static void pvdbcrRemoveRecord(void)
113 {
114  static int firstTime = 1;
115  if (firstTime) {
116  firstTime = 0;
117  iocshRegister(&pvdbcrRemoveRecordFuncDef, pvdbcrRemoveRecordCallFunc);
118  }
119 }
120 
121 extern "C" {
122  epicsExportRegistrar(pvdbcrRemoveRecord);
123 }
std::tr1::shared_ptr< PvdbcrRemoveRecord > PvdbcrRemoveRecordPtr
PvdbcrRemoveRecord A record that removes a record from the master database.
epicsExportRegistrar(pvdbcrRemoveRecord)
STL namespace.
Base interface for a PVRecord.
Definition: pvDatabase.h:56
std::tr1::shared_ptr< PVDatabase > PVDatabasePtr
Definition: pvDatabase.h:43
static PvdbcrRemoveRecordPtr create(std::string const &recordName, int asLevel=0, std::string const &asGroup=std::string("DEFAULT"))
Create a record.
static PVDatabasePtr getMaster()
Get the master database.
Definition: pvDatabase.cpp:38
std::tr1::shared_ptr< PVRecord > PVRecordPtr
Definition: pvDatabase.h:21