diff src/utils.h @ 132:537cf7deadc9

Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
author Atul Varma <varmaa@toolness.com>
date Sun, 23 Aug 2009 18:43:46 -0700
parents 00e874d9a6a7
children dd32a92f6b4f
line wrap: on
line diff
--- a/src/utils.h	Sun Aug 23 18:04:21 2009 -0700
+++ b/src/utils.h	Sun Aug 23 18:43:46 2009 -0700
@@ -43,6 +43,27 @@
 #include <jsdhash.h>
 #include <Python.h>
 
+// Simple class that holds on to a UTF-16 string created by
+// PyArg_ParseTuple() for as long as it's in scope.  It also
+// provides easy BOM-stripping accessors for JS-land.
+class PYM_UTF16String {
+public:
+  PYM_UTF16String(char *buffer, int size) : jsbuffer((jschar *) (buffer + 2)),
+    jslen(size / 2 - 1), pybuffer(buffer), pysize(size) {
+  }
+
+  ~PYM_UTF16String() {
+    PyMem_Free(pybuffer);
+  }
+
+  jschar *jsbuffer;
+  size_t jslen;
+
+protected:
+  char *pybuffer;
+  int pysize;
+};
+
 // Simple class that holds the Python global interpreter lock (GIL)
 // for as long as it's in scope.
 class PYM_PyAutoEnsureGIL {