changed Awk to use HashTable instead of htb.

added Cstr, Mcstr, Wcstr.
changed Awk not to use in-class placement new in allocating a value
This commit is contained in:
2015-03-11 13:33:23 +00:00
parent cb62c4ecd1
commit a212a8ebf2
14 changed files with 339 additions and 84 deletions

156
qse/include/qse/Cstr.hpp Normal file
View File

@ -0,0 +1,156 @@
/*
* $Id$
*
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _QSE_CSTR_HPP_
#define _QSE_CSTR_HPP_
#include <qse/Types.hpp>
#include <qse/Hashable.hpp>
#include <qse/cmn/str.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
/// The Mcstr class encapsulates a multibyte string pointer and length.
class Mcstr: public Types::mcstr_t, public Hashable
{
public:
Mcstr (qse_mchar_t* ptr, qse_size_t len)
{
this->ptr = ptr;
this->len = len;
}
Mcstr (const qse_mchar_t* ptr, qse_size_t len)
{
this->ptr = (qse_mchar_t*)ptr;
this->len = len;
}
Mcstr (Types::mcstr_t& str)
{
this->ptr = (qse_mchar_t*)str.ptr;
this->len = str.len;
}
qse_size_t getHashCode () const
{
return Hashable::getHashCode (this->ptr, this->len * QSE_SIZEOF(*this->ptr));
}
bool operator== (const Mcstr& str) const
{
return qse_mbsxncmp (this->ptr, this->len, str.ptr, str.len) == 0;
}
bool operator!= (const Mcstr& str) const
{
return qse_mbsxncmp (this->ptr, this->len, str.ptr, str.len) != 0;
}
};
/// The Mcstr class encapsulates a wide-character string pointer and length.
class Wcstr: public Types::wcstr_t, public Hashable
{
public:
Wcstr (qse_wchar_t* ptr, qse_size_t len)
{
this->ptr = ptr;
this->len = len;
}
Wcstr (const qse_wchar_t* ptr, qse_size_t len)
{
this->ptr = (qse_wchar_t*)ptr;
this->len = len;
}
Wcstr (Types::wcstr_t& str)
{
this->ptr = (qse_wchar_t*)str.ptr;
this->len = str.len;
}
qse_size_t getHashCode () const
{
return Hashable::getHashCode (this->ptr, this->len * QSE_SIZEOF(*this->ptr));
}
bool operator== (const Wcstr& str) const
{
return qse_wcsxncmp (this->ptr, this->len, str.ptr, str.len) == 0;
}
bool operator!= (const Wcstr& str) const
{
return qse_wcsxncmp (this->ptr, this->len, str.ptr, str.len) != 0;
}
};
/// The Mcstr class encapsulates a character string pointer and length.
class Cstr: public Types::cstr_t, public Hashable
{
public:
Cstr (qse_char_t* ptr, qse_size_t len)
{
this->ptr = ptr;
this->len = len;
}
Cstr (const qse_char_t* ptr, qse_size_t len)
{
this->ptr = (qse_char_t*)ptr;
this->len = len;
}
Cstr (Types::cstr_t& str)
{
this->ptr = (qse_char_t*)str.ptr;
this->len = str.len;
}
qse_size_t getHashCode () const
{
return Hashable::getHashCode (this->ptr, this->len * QSE_SIZEOF(*this->ptr));
}
bool operator== (const Cstr& str) const
{
return qse_strxncmp (this->ptr, this->len, str.ptr, str.len) == 0;
}
bool operator!= (const Cstr& str) const
{
return qse_strxncmp (this->ptr, this->len, str.ptr, str.len) != 0;
}
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -27,7 +27,8 @@
#ifndef _QSE_HASHABLE_HPP_
#define _QSE_HASHABLE_HPP_
#include <qse/Types.hpp>
#include <qse/types.h>
#include <qse/macros.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)

View File

@ -9,7 +9,7 @@ pkginclude_HEADERS = \
if ENABLE_CXX
pkginclude_HEADERS += \
Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
Exception.hpp
Exception.hpp Cstr.hpp
endif
install-data-hook:

View File

@ -52,7 +52,7 @@ build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = \
@ENABLE_CXX_TRUE@ Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
@ENABLE_CXX_TRUE@ Exception.hpp
@ENABLE_CXX_TRUE@ Exception.hpp Cstr.hpp
subdir = include/qse
DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \
@ -95,7 +95,7 @@ am__can_run_installinfo = \
am__pkginclude_HEADERS_DIST = conf-msw.h conf-os2.h conf-dos.h \
conf-vms.h conf-mac.h conf-inf.h types.h macros.h pack1.h \
unpack.h Types.hpp Hashable.hpp Uncopyable.hpp RefCounted.hpp \
Exception.hpp
Exception.hpp Cstr.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \

View File

@ -80,7 +80,13 @@ public:
typedef qse_flt_t flt_t;
/** redefines a structure of a character pointer and length */
typedef qse_cstr_t cstr_t;
typedef qse_cstr_t cstr_t;
/** redefines a structure of a character pointer and length */
typedef qse_wcstr_t wcstr_t;
/** redefines a structure of a character pointer and length */
typedef qse_mcstr_t mcstr_t;
};
/////////////////////////////////

View File

@ -28,13 +28,23 @@
#define _QSE_AWK_AWK_HPP_
#include <qse/awk/awk.h>
#include <qse/cmn/htb.h>
#include <qse/cmn/chr.h>
#include <qse/Uncopyable.hpp>
#include <qse/Types.hpp>
#include <qse/cmn/Mmged.hpp>
#include <stdarg.h>
//#define QSE_AWK_USE_HTB_FOR_FUNCTION_MAP 1
//#define QSE_AWK_VALUE_USE_IN_CLASS_PLACEMENT_NEW 1
#if defined(QSE_AWK_USE_HTB_FOR_FUNCTION_MAP)
# include <qse/cmn/htb.h>
#else
# include <qse/cmn/HashTable.hpp>
# include <qse/Cstr.hpp>
#endif
/// \file
/// AWK Interpreter
@ -49,8 +59,6 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT Awk: public Uncopyable, public Types, public Mmged
{
public:
typedef qse_htb_t htb_t;
typedef qse_htb_pair_t pair_t;
// define a primitive handle
typedef qse_awk_t awk_t;
@ -522,6 +530,7 @@ public:
public:
friend class Awk;
#if defined(QSE_AWK_VALUE_USE_IN_CLASS_PLACEMENT_NEW)
// initialization
void* operator new (size_t n, Run* run) throw ();
void* operator new[] (size_t n, Run* run) throw ();
@ -535,6 +544,7 @@ public:
// normal deletion
void operator delete (void* p);
void operator delete[] (void* p);
#endif
///
/// The Index class encapsulates an index of an arrayed value.
@ -634,7 +644,7 @@ public:
}
protected:
IndexIterator (pair_t* pair, size_t buckno)
IndexIterator (qse_htb_pair_t* pair, size_t buckno)
{
this->pair = pair;
this->buckno = buckno;
@ -939,7 +949,7 @@ public:
/// The Awk() function creates an interpreter without fully
/// initializing it. You must call open() for full initialization
/// before calling other functions.
Awk (Mmgr* mmgr);
Awk (Mmgr* mmgr = QSE_NULL);
/// The ~Awk() function destroys an interpreter. Make sure to have
/// called close() for finalization before the destructor is executed.
@ -1333,7 +1343,21 @@ protected:
errstr_t dflerrstr;
errinf_t errinf;
htb_t* functionMap;
#if defined(QSE_AWK_USE_HTB_FOR_FUNCTION_MAP)
qse_htb_t* functionMap;
#else
class FunctionMap: public HashTable<Cstr,FunctionHandler>
{
public:
FunctionMap (Awk* awk): awk(awk) {}
protected:
Awk* awk;
};
FunctionMap functionMap;
#endif
Source* source_reader;
Source* source_writer;

View File

@ -87,9 +87,8 @@ public:
const char_t* str;
const char_t* ptr;
};
StdAwk (Mmgr* mmgr = StdMmgr::getDFL()):
Awk (mmgr), console_cmgr (QSE_NULL)
StdAwk (Mmgr* mmgr = QSE_NULL): Awk(mmgr), console_cmgr(QSE_NULL)
{
}

View File

@ -39,7 +39,7 @@ struct HashListHasher
{
qse_size_t operator() (const T& v) const
{
return v.hashCode();
return v.getHashCode();
}
};

View File

@ -39,7 +39,7 @@ struct HashTableHasher
{
qse_size_t operator() (const T& v) const
{
return v.hashCode();
return v.getHashCode();
}
};

View File

@ -49,6 +49,8 @@ QSE_BEGIN_NAMESPACE(QSE)
class QSE_EXPORT HeapMmgr: public Mmgr, public Mmged
{
public:
HeapMmgr (qse_size_t heap_size);
/// The constructor function accepts an memory manager \a mmgr that
/// is used to create a heap of the size \a heap_size.
HeapMmgr (Mmgr* mmgr, qse_size_t heap_size);