libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
VectorInterface.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 ROCKETCOREPYTHONVECTORINTERFACE_H
29 #define ROCKETCOREPYTHONVECTORINTERFACE_H
30 
31 namespace Rocket {
32 namespace Core {
33 namespace Python {
34 
46 template <typename Container>
48 {
49 public:
50  VectorInterface(const char* name)
51  {
52  python::class_< Container >(name)
53  .def("__len__", &VectorInterface< Container >::Size)
54  .def("__setitem__", &VectorInterface< Container >::SetItem)
55  .def("__delitem__", &VectorInterface< Container >::DelItem)
56  .def("__getitem__", &VectorInterface< Container >::GetItem, python::return_value_policy< python::return_by_value >())
57  .def("__contains__", &VectorInterface< Container >::Contains)
58  ;
59  }
60 
61  static size_t Size(const Container& container)
62  {
63  return container.size();
64  }
65 
66  static void SetItem(Container& container, int index, const typename Container::value_type& value)
67  {
68  if (index < 0)
69  index = container.size() + index;
70  if (index >= (int)container.size())
71  container.resize(index + 1);
72 
73  container[index] = value;
74  }
75 
76  static void DelItem(Container& container, int index)
77  {
78  if (index >= (int)container.size())
79  {
80  PyErr_SetString(PyExc_IndexError, String(32, "Invalid index %d", index).CString());
81  python::throw_error_already_set();
82  }
83 
84  container.erase(container.begin() + index);
85  }
86 
87  static typename Container::value_type& GetItem(Container& container, int index)
88  {
89  if (index < 0)
90  index = container.size() + index;
91 
92  if (index >= (int)container.size())
93  {
94  PyErr_SetString(PyExc_IndexError, String(32, "Invalid index %d", index).CString());
95  python::throw_error_already_set();
96  }
97 
98  return container[index];
99  }
100 
101  static bool Contains(Container& container, const typename Container::value_type& value)
102  {
103  for (typename Container::iterator itr = container.begin(); itr != container.end(); ++itr)
104  {
105  if ((*itr) == value)
106  return true;
107  }
108 
109  return false;
110  }
111 };
112 
113 }
114 }
115 }
116 
117 #endif
118 
VectorInterface(const char *name)
Definition: VectorInterface.h:50
static void SetItem(Container &container, int index, const typename Container::value_type &value)
Definition: VectorInterface.h:66
static Container::value_type & GetItem(Container &container, int index)
Definition: VectorInterface.h:87
Definition: Clipboard.h:33
Definition: VectorInterface.h:47
static void DelItem(Container &container, int index)
Definition: VectorInterface.h:76
StringBase< char > String
Definition: String.h:40
static bool Contains(Container &container, const typename Container::value_type &value)
Definition: VectorInterface.h:101
static size_t Size(const Container &container)
Definition: VectorInterface.h:61