Skip to content

Commit

Permalink
Preliminary OFConstString implementation and support for @"" literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Midar committed Mar 15, 2009
1 parent da0721d commit 983a6a6
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 39 deletions.
11 changes: 7 additions & 4 deletions configure.ac
Expand Up @@ -2,7 +2,6 @@ AC_INIT(objfw, 0.1, js@webkeks.org)
AC_CONFIG_SRCDIR(src)

AC_CANONICAL_HOST
AC_CANONICAL_TARGET

AC_PROG_CC
AC_PROG_OBJC
Expand All @@ -12,8 +11,15 @@ AC_PROG_EGREP

CFLAGS="$CFLAGS -Wall"
OBJCFLAGS="$OBJCFLAGS -Wall -fobjc-exceptions"
OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstString"
LIBS="$LIBS -lobjc"

AX_CHECK_COMPILER_FLAGS(-pipe, [
CFLAGS="$CFLAGS -pipe"
OBJCFLAGS="$OBJCFLAGS -pipe"])
AX_CHECK_COMPILER_FLAGS(-fno-constant-cfstrings,
[OBJCFLAGS="$OBJCFLAGS -fno-constant-cfstrings"])

AC_DEFINE(OF_CONFIG_H, 1, [Define so that we know we got our config.h])

BUILDSYS_LIB
Expand Down Expand Up @@ -133,9 +139,6 @@ test x"$ac_cv_have_ipv6" = x"yes" && \

AC_CHECK_HEADERS(sys/mman.h)

test x"$GCC" = x"yes" && CFLAGS="$CFLAGS -Werror -pipe -g"
test x"$GCC" = x"yes" && OBJCFLAGS="$OBJCFLAGS -Werror -pipe -g"

if test x"$cross_compiling" = x"yes"; then
case "$target" in
*-*-mingw*)
Expand Down
78 changes: 78 additions & 0 deletions m4/ax_check_compiler_flags.m4
@@ -0,0 +1,78 @@
# ===========================================================================
# http://autoconf-archive.cryp.to/ax_check_compiler_flags.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_COMPILER_FLAGS(FLAGS, [ACTION-SUCCESS], [ACTION-FAILURE])
#
# DESCRIPTION
#
# Check whether the given compiler FLAGS work with the current language's
# compiler, or whether they give an error. (Warnings, however, are
# ignored.)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# LAST MODIFICATION
#
# 2008-04-12
#
# COPYLEFT
#
# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
# Copyright (c) 2008 Matteo Frigo
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# As a special exception, the respective Autoconf Macro's copyright owner
# gives unlimited permission to copy, distribute and modify the configure
# scripts that are the output of Autoconf when processing the Macro. You
# need not follow the terms of the GNU General Public License when using
# or distributing such scripts, even though portions of the text of the
# Macro appear in them. The GNU General Public License (GPL) does govern
# all other use of the material that constitutes the Autoconf Macro.
#
# This special exception to the GPL applies to versions of the Autoconf
# Macro released by the Autoconf Macro Archive. When you make and
# distribute a modified version of the Autoconf Macro, you may extend this
# special exception to the GPL to apply to your modified version as well.

AC_DEFUN([AX_CHECK_COMPILER_FLAGS],
[AC_PREREQ(2.59) dnl for _AC_LANG_PREFIX
AC_MSG_CHECKING([whether _AC_LANG compiler accepts $1])
dnl Some hackery here since AC_CACHE_VAL can't handle a non-literal varname:
AS_LITERAL_IF([$1],
[AC_CACHE_VAL(AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1), [
ax_save_FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$1"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=yes,
AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=no)
_AC_LANG_PREFIX[]FLAGS=$ax_save_FLAGS])],
[ax_save_FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$1"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
eval AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=yes,
eval AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=no)
_AC_LANG_PREFIX[]FLAGS=$ax_save_FLAGS])
eval ax_check_compiler_flags=$AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)
AC_MSG_RESULT($ax_check_compiler_flags)
if test "x$ax_check_compiler_flags" = xyes; then
m4_default([$2], :)
else
m4_default([$3], :)
fi
])dnl AX_CHECK_COMPILER_FLAGS
1 change: 1 addition & 0 deletions src/Makefile
Expand Up @@ -6,6 +6,7 @@ LIB_MINOR = 0

SRCS = OFArray.m \
OFAutoreleasePool.m \
OFConstString.m \
OFDictionary.m \
OFExceptions.m \
OFHashes.m \
Expand Down
49 changes: 49 additions & 0 deletions src/OFConstString.h
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/

#import "OFObject.h"

#if !defined(__objc_INCLUDE_GNU) && !defined(OFCONSTSTRING_M)
/*
* This way, projects using libobjfw don't need -Wno-deprecated-declarations on
* OS X 10.5.
*/
extern void _OFConstStringClassReference;
#endif

/**
* A class for storing static strings using the @"" literal.
*/
@interface OFConstString: Object <OFHashable, OFRetainRelease>
{
char *string;
size_t length;
}

/**
* \return The OFString as a C string
*/
- (const char*)cString;

/**
* \return The length of the OFString
*/
- (size_t)length;

/**
* Compares the OFString to another object.
*
* \param obj An object to compare with
* \return An integer which is the result of the comparison, see for example
* strcmp
*/
- (int)compare: (id)obj;
@end
107 changes: 107 additions & 0 deletions src/OFConstString.m
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/

#import "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OFCONSTSTRING_M
#import "OFConstString.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#ifndef __objc_INCLUDE_GNU
struct objc_class _OFConstStringClassReference;
#endif

@implementation OFConstString
#ifndef __objc_INCLUDE_GNU
+ (void)load
{
Class cls = objc_getClass("OFConstString");
memcpy(&_OFConstStringClassReference, cls,
sizeof(_OFConstStringClassReference));
objc_addClass(&_OFConstStringClassReference);
}
#endif

- (BOOL)isKindOf: (Class)c
{
if (c == [OFConstString class])
return YES;
return NO;
}

- (const char*)cString
{
return string;
}

- (size_t)length
{
return length;
}

- (BOOL)isEqual: (id)obj
{
if (![obj isKindOf: [OFString class]] &&
![obj isKindOf: [OFConstString class]])
return NO;
if (strcmp(string, [obj cString]))
return NO;

return YES;
}

- (int)compare: (id)obj
{
if (![obj isKindOf: [OFString class]] &&
![obj isKindOf: [OFConstString class]])
@throw [OFInvalidArgumentException newWithClass: [self class]];

return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
uint32_t hash;
size_t i;

OF_HASH_INIT(hash);
for (i = 0; i < length; i++)
OF_HASH_ADD(hash, string[i]);
OF_HASH_FINALIZE(hash);

return hash;
}

- retain
{
return self;
}

- (void)release
{
}

- (size_t)retainCount
{
return 1;
}

- autorelease
{
return self;
}
@end
8 changes: 4 additions & 4 deletions src/OFDictionary.h
Expand Up @@ -57,19 +57,19 @@
* \param key The key to set
* \param obj The object to set the key to
*/
- set: (OFObject*)key
to: (OFObject*)obj;
- set: (id <OFHashable, OFRetainRelease>)key
to: (id <OFRetainRelease>)obj;

/*
* \param key The key whose object should be returned
* \return The object for the given key
*/
- get: (OFObject*)key;
- get: (id <OFHashable>)key;

/*
* Remove the object with the given key from the dictionary.
*
* \param key The key whose object should be removed
*/
- remove: (OFObject*)key;
- remove: (id <OFHashable, OFRetainRelease>)key;
@end
8 changes: 4 additions & 4 deletions src/OFDictionary.m
Expand Up @@ -80,8 +80,8 @@ @implementation OFDictionary
return [super free];
}

- set: (OFObject*)key
to: (OFObject*)obj
- set: (id <OFHashable, OFRetainRelease>)key
to: (id <OFRetainRelease>)obj
{
uint32_t hash = [key hash] & (size - 1);
of_list_object_t *iter;
Expand Down Expand Up @@ -109,7 +109,7 @@ @implementation OFDictionary
return self;
}

- get: (OFObject*)key
- get: (id <OFHashable>)key
{
uint32_t hash = [key hash] & (size - 1);
of_list_object_t *iter;
Expand All @@ -124,7 +124,7 @@ @implementation OFDictionary
return nil;
}

- remove: (OFObject*)key
- remove: (id <OFHashable, OFRetainRelease>)key
{
uint32_t hash = [key hash] & (size - 1);
of_list_object_t *iter;
Expand Down

0 comments on commit 983a6a6

Please sign in to comment.