ProcedureDeclaration.h
1 /*
2  * Copyright (C) 2020-2026 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA 02110-1301 USA
28  */
29 
30 #ifndef PT_JSONRPC_PROCEDUREDECLARATION_H
31 #define PT_JSONRPC_PROCEDUREDECLARATION_H
32 
33 #include <Pt/JsonRpc/Api.h>
34 #include <string>
35 #include <vector>
36 #include <map>
37 
38 namespace Pt {
39 
40 namespace JsonRpc {
41 
48 class PT_JSONRPC_API ProcedureDeclaration
49 {
50  public:
54  {}
55 
58  void setParamName(std::size_t index, const std::string& name)
59  {
60  if(index >= _names.size())
61  _names.resize(index + 1);
62 
63  _names[index] = name;
64  _indexByName[name] = index;
65  }
66 
71  int getParamIndex(const std::string& name) const
72  {
73  std::map<std::string, std::size_t>::const_iterator it = _indexByName.find(name);
74  if(it == _indexByName.end())
75  return -1;
76 
77  return static_cast<int>(it->second);
78  }
79 
84  const std::string& getParamName(std::size_t index) const
85  {
86  static const std::string empty;
87  if(index >= _names.size())
88  return empty;
89 
90  return _names[index];
91  }
92 
95  std::size_t paramCount() const
96  { return _names.size(); }
97 
100  bool hasNames() const
101  { return ! _names.empty(); }
102 
103  private:
104  std::vector<std::string> _names;
105  std::map<std::string, std::size_t> _indexByName;
106 };
107 
108 } // namespace JsonRpc
109 
110 } // namespace Pt
111 
112 #endif // PT_JSONRPC_PROCEDUREDECLARATION_H
Core module.
Definition: Allocator.h:33
const std::string & getParamName(std::size_t index) const
Lookup parameter name by positional index.
Definition: ProcedureDeclaration.h:84
Maps parameter names to positional indices for named parameters.
Definition: ProcedureDeclaration.h:49
std::size_t paramCount() const
Returns the number of named parameters.
Definition: ProcedureDeclaration.h:95
bool hasNames() const
Returns true if any parameter names are registered.
Definition: ProcedureDeclaration.h:100
int getParamIndex(const std::string &name) const
Lookup positional index by parameter name.
Definition: ProcedureDeclaration.h:71
void setParamName(std::size_t index, const std::string &name)
Register a parameter name at a positional index.
Definition: ProcedureDeclaration.h:58
ProcedureDeclaration()
Default constructor.
Definition: ProcedureDeclaration.h:53