namespace the sequence functions
[babeltrace.git] / types / integer.c
index ea292900ce80886141bf21a171ec3b76d2573db8..58b6104f9f08c73e78a9730796afd155962ead7d 100644 (file)
@@ -3,7 +3,9 @@
  *
  * BabelTrace - Integer Type Converter
  *
- * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 #include <babeltrace/format.h>
+#include <babeltrace/types.h>
 #include <stdint.h>
 
 static
-struct type *_integer_type_new(struct type_class *type_class,
-                              struct declaration_scope *parent_scope);
+struct definition *_integer_definition_new(struct declaration *declaration,
+                              struct definition_scope *parent_scope,
+                              GQuark field_name, int index,
+                              const char *root_name);
 static
-void _integer_type_free(struct type *type);
-
-void integer_copy(struct stream_pos *dest, const struct format *fdest, 
-                 struct stream_pos *src, const struct format *fsrc,
-                 struct type *type)
-{
-       struct type_integer *integer = container_of(type, struct type_integer,
-                                                   p);
-       struct type_class_integer *int_class = integer->_class;
-
-       if (!int_class->signedness) {
-               uint64_t v;
-
-               v = fsrc->uint_read(src, int_class);
-               fdest->uint_write(dest, int_class, v);
-       } else {
-               int64_t v;
-
-               v = fsrc->int_read(src, int_class);
-               fdest->int_write(dest, int_class, v);
-       }
-}
+void _integer_definition_free(struct definition *definition);
 
 static
-void _integer_type_class_free(struct type_class *type_class)
+void _integer_declaration_free(struct declaration *declaration)
 {
-       struct type_class_integer *integer_class =
-               container_of(type_class, struct type_class_integer, p);
-       g_free(integer_class);
+       struct declaration_integer *integer_declaration =
+               container_of(declaration, struct declaration_integer, p);
+       g_free(integer_declaration);
 }
 
-struct type_class_integer *
-integer_type_class_new(const char *name, size_t len, int byte_order,
-                      int signedness, size_t alignment)
+struct declaration_integer *
+       integer_declaration_new(size_t len, int byte_order,
+                        int signedness, size_t alignment, int base,
+                        enum ctf_string_encoding encoding,
+                        struct ctf_clock *clock)
 {
-       struct type_class_integer *int_class;
-       int ret;
+       struct declaration_integer *integer_declaration;
 
-       int_class = g_new(struct type_class_integer, 1);
-       int_class->p.name = g_quark_from_string(name);
-       int_class->p.alignment = alignment;
-       int_class->p.copy = integer_copy;
-       int_class->p.class_free = _integer_type_class_free;
-       int_class->p.type_free = _integer_type_free;
-       int_class->p.type_new = _integer_type_new;
-       int_class->p.ref = 1;
-       int_class->len = len;
-       int_class->byte_order = byte_order;
-       int_class->signedness = signedness;
-       if (int_class->p.name) {
-               ret = register_type(&int_class->p);
-               if (ret) {
-                       g_free(int_class);
-                       return NULL;
-               }
-       }
-       return int_class;
+       integer_declaration = g_new(struct declaration_integer, 1);
+       integer_declaration->p.id = CTF_TYPE_INTEGER;
+       integer_declaration->p.alignment = alignment;
+       integer_declaration->p.declaration_free = _integer_declaration_free;
+       integer_declaration->p.definition_free = _integer_definition_free;
+       integer_declaration->p.definition_new = _integer_definition_new;
+       integer_declaration->p.ref = 1;
+       integer_declaration->len = len;
+       integer_declaration->byte_order = byte_order;
+       integer_declaration->signedness = signedness;
+       integer_declaration->base = base;
+       integer_declaration->encoding = encoding;
+       integer_declaration->clock = clock;
+       return integer_declaration;
 }
 
 static
-struct type_integer *_integer_type_new(struct type_class *type_class,
-                                      struct declaration_scope *parent_scope)
+struct definition *
+       _integer_definition_new(struct declaration *declaration,
+                               struct definition_scope *parent_scope,
+                               GQuark field_name, int index,
+                               const char *root_name)
 {
-       struct type_class_integer *integer_class =
-               container_of(type_class, struct type_class_integer, p);
-       struct type_integer *integer;
+       struct declaration_integer *integer_declaration =
+               container_of(declaration, struct declaration_integer, p);
+       struct definition_integer *integer;
+       int ret;
 
-       integer = g_new(struct type_integer, 1);
-       type_class_ref(&integer_class->p);
-       integer->p._class = integer_class;
+       integer = g_new(struct definition_integer, 1);
+       bt_declaration_ref(&integer_declaration->p);
+       integer->p.declaration = declaration;
+       integer->declaration = integer_declaration;
        integer->p.ref = 1;
+       /*
+        * Use INT_MAX order to ensure that all fields of the parent
+        * scope are seen as being prior to this scope.
+        */
+       integer->p.index = root_name ? INT_MAX : index;
+       integer->p.name = field_name;
+       integer->p.path = new_definition_path(parent_scope, field_name,
+                                       root_name);
+       integer->p.scope = NULL;
        integer->value._unsigned = 0;
+       ret = register_field_definition(field_name, &integer->p,
+                                       parent_scope);
+       assert(!ret);
        return &integer->p;
 }
 
 static
-void _integer_type_free(struct type *type)
+void _integer_definition_free(struct definition *definition)
 {
-       struct type_integer *integer =
-               container_of(type, struct type_integer, p);
+       struct definition_integer *integer =
+               container_of(definition, struct definition_integer, p);
 
-       type_class_unref(integer->p._class);
+       bt_declaration_unref(integer->p.declaration);
        g_free(integer);
 }
+
+enum ctf_string_encoding bt_get_int_encoding(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       return integer_declaration->encoding;
+}
+
+int bt_get_int_base(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       return integer_declaration->base;
+}
+
+size_t bt_get_int_len(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       return integer_declaration->len;
+}
+
+int bt_get_int_byte_order(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       return integer_declaration->byte_order;
+}
+
+int bt_get_int_signedness(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       return integer_declaration->signedness;
+}
+
+uint64_t bt_get_unsigned_int(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       if (!integer_declaration->signedness) {
+               return integer_definition->value._unsigned;
+       }
+       fprintf(stderr, "[warning] Extracting unsigned value from a signed int (%s)\n",
+               g_quark_to_string(field->name));
+       return (uint64_t)integer_definition->value._signed;
+}
+
+int64_t bt_get_signed_int(const struct definition *field)
+{
+       struct definition_integer *integer_definition;
+       const struct declaration_integer *integer_declaration;
+
+       integer_definition = container_of(field, struct definition_integer, p);
+       integer_declaration = integer_definition->declaration;
+
+       if (integer_declaration->signedness) {
+               return integer_definition->value._signed;
+       }
+       fprintf(stderr, "[warning] Extracting signed value from an unsigned int (%s)\n", 
+               g_quark_to_string(field->name));
+       return (int64_t)integer_definition->value._unsigned;
+}
This page took 0.025721 seconds and 4 git commands to generate.