libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NameIndexInterface.h
Go to the documentation of this file.
1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #ifndef ROCKETCOREPYTHONNAMEINDEXINTERFACE_H
29 #define ROCKETCOREPYTHONNAMEINDEXINTERFACE_H
30 
31 #include "Utilities.h"
32 
33 namespace Rocket {
34 namespace Core {
35 namespace Python {
36 
37 template < typename HostType >
39 {
40 public:
41  python::object operator()(HostType& host, const char* name)
42  {
43  PyErr_SetString(PyExc_KeyError, "Invalid key.");
44  python::throw_error_already_set();
45 
46  return python::object();
47  }
48 };
49 
56 template < typename HostType, typename LengthAccessor, typename IndexAccessor, typename NameAccessor = NameAccessorInvalid< HostType > >
57 class NameIndexInterface : public python::def_visitor< NameIndexInterface< HostType, LengthAccessor, IndexAccessor, NameAccessor > >
58 {
59 public:
61  {
62  }
63 
65  {
66  }
67 
69  template < typename ClassType >
70  void visit(ClassType& _class) const
71  {
74  }
75 
77  static python::object GetItem(HostType& host, python::object key)
78  {
79  static NameAccessor name_accessor;
80  static LengthAccessor length_accessor;
81  static IndexAccessor index_accessor;
82 
83  if (PyString_Check(key.ptr()))
84  {
85  return Rocket::Core::Python::Utilities::MakeObject(name_accessor(host, PyString_AsString(key.ptr())));
86  }
87  else if (PyInt_Check(key.ptr()))
88  {
89  int index = PyInt_AsLong(key.ptr());
90 
91  // Support indexing from both ends.
92  if (index < 0)
93  index = length_accessor(host) + index;
94 
95  // Throw exception if we're out of range, this is required to support python iteration.
96  if (index >= length_accessor(host))
97  {
98  PyErr_SetString(PyExc_IndexError, "Index out of range.");
99  python::throw_error_already_set();
100  }
101 
102  return Rocket::Core::Python::Utilities::MakeObject(index_accessor(host, index));
103  }
104 
105  PyErr_SetString(PyExc_KeyError, "Invalid key.");
106  python::throw_error_already_set();
107 
108  return python::object();
109  }
110 
112  static int Len(HostType& host)
113  {
114  static LengthAccessor length_accessor;
115  return length_accessor(host);
116  }
117 };
118 
119 }
120 }
121 }
122 
123 #endif
~NameIndexInterface()
Definition: NameIndexInterface.h:64
python::object operator()(HostType &host, const char *name)
Definition: NameIndexInterface.h:41
Definition: Clipboard.h:33
NameIndexInterface()
Definition: NameIndexInterface.h:60
Definition: NameIndexInterface.h:57
static python::object GetItem(HostType &host, python::object key)
Python getitem override.
Definition: NameIndexInterface.h:77
static python::object MakeObject(T *object)
Definition: Utilities.h:69
void visit(ClassType &_class) const
Initialise the interface.
Definition: NameIndexInterface.h:70
Definition: NameIndexInterface.h:38
static int Len(HostType &host)
Python len override.
Definition: NameIndexInterface.h:112