ide-source-range.c 3.25 KB
Newer Older
1 2 3 4
/* ide-source-range.c
 *
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 *
5 6 7 8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
9
 *
10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17 18
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20
#define G_LOG_DOMAIN "ide-source-range"

Christian Hergert's avatar
Christian Hergert committed
21
#include <egg-counter.h>
22

Christian Hergert's avatar
Christian Hergert committed
23 24 25
#include "diagnostics/ide-source-location.h"
#include "diagnostics/ide-source-range.h"
#include "files/ide-file.h"
26

27 28
G_DEFINE_BOXED_TYPE (IdeSourceRange, ide_source_range, ide_source_range_ref, ide_source_range_unref)

29
EGG_DEFINE_COUNTER (instances, "IdeSourceRange", "Instances", "Number of IdeSourceRange instances.")
30 31 32 33 34 35 36 37 38

struct _IdeSourceRange
{
  volatile gint      ref_count;
  IdeSourceLocation *begin;
  IdeSourceLocation *end;
};

IdeSourceRange *
39 40
ide_source_range_new (IdeSourceLocation *begin,
                      IdeSourceLocation *end)
41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
  IdeSourceRange *ret;

  g_return_val_if_fail (begin, NULL);
  g_return_val_if_fail (end, NULL);
  g_return_val_if_fail (ide_file_equal (ide_source_location_get_file (begin),
                                        ide_source_location_get_file (end)),
                        NULL);

  ret = g_slice_new0 (IdeSourceRange);
  ret->ref_count = 1;
  ret->begin = ide_source_location_ref (begin);
  ret->end = ide_source_location_ref (end);

55 56
  EGG_COUNTER_INC (instances);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  return ret;
}

/**
 * ide_source_range_get_begin:
 *
 * Gets the beginning of the source range.
 *
 * Returns: (transfer none): An #IdeSourceLocation.
 */
IdeSourceLocation *
ide_source_range_get_begin (IdeSourceRange *self)
{
  g_return_val_if_fail (self, NULL);

  return self->begin;
}

/**
 * ide_source_range_get_end:
 *
 * Gets the end of the source range.
 *
 * Returns: (transfer none): An #IdeSourceLocation.
 */
IdeSourceLocation *
ide_source_range_get_end (IdeSourceRange *self)
{
  g_return_val_if_fail (self, NULL);

  return self->end;
}

/**
 * ide_source_range_ref:
 * @self: An #IdeSourceRange
 *
 * Increments the reference count of @self by one. When you are done with
 * @self, release it by calling ide_source_range_unref().
 *
 * Returns: (transfer full): @self
 */
IdeSourceRange *
ide_source_range_ref (IdeSourceRange *self)
{
  g_return_val_if_fail (self, NULL);
  g_return_val_if_fail (self->ref_count > 0, NULL);

  g_atomic_int_inc (&self->ref_count);

  return self;
}

/**
 * ide_source_range_unref:
 * @self: (transfer full): An #IdeSourceRange
 *
 * Decrements the reference count of @self by one.
 */
void
ide_source_range_unref (IdeSourceRange *self)
{
  g_return_if_fail (self);
  g_return_if_fail (self->ref_count > 0);

  if (g_atomic_int_dec_and_test (&self->ref_count))
    {
      ide_source_location_unref (self->begin);
      ide_source_location_unref (self->end);
      g_slice_free (IdeSourceRange, self);
127 128

      EGG_COUNTER_DEC (instances);
129 130
    }
}