w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RtclGetList.cpp
Go to the documentation of this file.
1// $Id: RtclGetList.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2013-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-01 1076 1.2 use unique_ptr
8// 2018-11-16 1070 1.1.1 use auto; use emplace,make_pair; use range loop
9// 2015-01-08 631 1.1 add Clear(), add '?' (key list) and '*' (kv list)
10// 2014-08-22 584 1.0.1 use nullptr
11// 2013-02-12 487 1.0 Initial version
12// ---------------------------------------------------------------------------
13
18#include <iostream>
19
21
22#include "RtclGet.hpp"
23#include "RtclGetList.hpp"
24#include "RtclOPtr.hpp"
25
26using namespace std;
27
33// all method definitions in namespace Retro
34namespace Retro {
35
36//------------------------------------------+-----------------------------------
38
40 : fMap()
41{}
42
43//------------------------------------------+-----------------------------------
45
47{}
48
49//------------------------------------------+-----------------------------------
51
52void RtclGetList::Add(const std::string& name, get_uptr_t&& upget)
53{
54 auto ret = fMap.emplace(make_pair(name, move(upget)));
55 if (ret.second == false)
56 throw Rexception("RtclGetList::Add:",
57 string("Bad args: duplicate name: '") + name + "'");
58 return;
59}
60
61//------------------------------------------+-----------------------------------
63
65{
66 fMap.clear();
67 return;
68}
69
70//------------------------------------------+-----------------------------------
72
74{
75 Tcl_Interp* interp = args.Interp();
76 string pname("*");
77 if (!args.GetArg("??pname", pname)) return TCL_ERROR;
78 if (!args.AllDone()) return TCL_ERROR;
79
80 if (pname == "?") {
81 RtclOPtr rlist(Tcl_NewListObj(0,nullptr));
82 for (const auto& kv : fMap) {
83 RtclOPtr pele(Tcl_NewStringObj(kv.first.c_str(), -1));
84 Tcl_ListObjAppendElement(nullptr, rlist, pele);
85 }
86 Tcl_SetObjResult(interp, rlist);
87 return TCL_OK;
88
89 } else if (pname == "*") {
90 RtclOPtr rlist(Tcl_NewListObj(0,nullptr));
91 for (const auto& kv : fMap) {
92 RtclOPtr pele(Tcl_NewStringObj(kv.first.c_str(), -1));
93 Tcl_ListObjAppendElement(nullptr, rlist, pele);
94 Tcl_ListObjAppendElement(nullptr, rlist, kv.second->operator()());
95 }
96 Tcl_SetObjResult(interp, rlist);
97 return TCL_OK;
98 }
99
100 auto it = fMap.lower_bound(pname);
101
102 // complain if not found
103 if (it == fMap.end() || pname != it->first.substr(0,pname.length())) {
104 Tcl_AppendResult(interp, "-E: unknown property '", pname.c_str(),
105 "': must be ", nullptr);
106 const char* delim = "";
107 for (auto& o: fMap) {
108 Tcl_AppendResult(interp, delim, o.first.c_str(), nullptr);
109 delim = ",";
110 }
111 return TCL_ERROR;
112 }
113
114 // check for ambiguous substring match
115 auto it1 = it;
116 it1++;
117 if (it1!=fMap.end() && pname==it1->first.substr(0,pname.length())) {
118 Tcl_AppendResult(interp, "-E: ambiguous property name '", pname.c_str(),
119 "': must be ", nullptr);
120 const char* delim = "";
121 for (it1=it; it1!=fMap.end() &&
122 pname==it1->first.substr(0,pname.length()); it1++) {
123 Tcl_AppendResult(interp, delim, it1->first.c_str(), nullptr);
124 delim = ",";
125 }
126
127 return TCL_ERROR;
128 }
129
130 args.SetResult((it->second)->operator()());
131 return TCL_OK;
132}
133
134} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
FIXME_docs.
Definition: RtclArgs.hpp:41
bool GetArg(const char *name, Tcl_Obj *&pval)
FIXME_docs.
Definition: RtclArgs.cpp:114
void SetResult(const std::string &str)
FIXME_docs.
Definition: RtclArgs.ipp:76
Tcl_Interp * Interp() const
FIXME_docs.
Definition: RtclArgs.ipp:28
bool AllDone()
FIXME_docs.
Definition: RtclArgs.cpp:447
virtual ~RtclGetList()
FIXME_docs.
Definition: RtclGetList.cpp:46
int M_get(RtclArgs &args)
FIXME_docs.
Definition: RtclGetList.cpp:73
std::unique_ptr< RtclGetBase > get_uptr_t
Definition: RtclGetList.hpp:37
RtclGetList()
FIXME_docs.
Definition: RtclGetList.cpp:39
void Add(const std::string &name, get_uptr_t &&upget)
FIXME_docs.
Definition: RtclGetList.cpp:52
void Clear()
FIXME_docs.
Definition: RtclGetList.cpp:64
Implemenation (inline) of RtclOPtr.
Definition: RtclOPtr.hpp:23
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47