8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-24 00:03:03 +01:00

Implement Claudio suggestion regarding clean generic map class

This commit is contained in:
skidder 2004-11-24 20:38:31 +00:00
parent c8c1c988a5
commit 016fcdcce5
4 changed files with 141 additions and 139 deletions

View File

@ -0,0 +1,125 @@
/*
* PROGRAM: Client/Server Common Code
* MODULE: StringMap.h
* DESCRIPTION: Secure handling of clumplet buffers
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
* $Id: GenericMap.h,v 1.1 2004-11-24 20:38:31 skidder Exp $
*
*/
#ifndef STRINGMAP_H
#define STRINGMAP_H
#include "fb_string.h"
#include "../common/classes/fb_pair.h"
#include "../common/classes/tree.h"
namespace Firebird {
//
// Generic map which allows to have POD and non-POD keys and values.
// The class is memory pools friendly.
//
// Usage
//
// POD key (integer), non-POD value (string):
// GenericMap<Pair<Right<int, string> > >
//
// non-POD key (string), POD value (integer):
// GenericMap<Pair<Left<string, int> > >
//
// non-POD key (string), non-POD value (string):
// GenericMap<Pair<Full<string, string> > >
//
template <typename KeyValuePair, typename KeyComparator = DefaultComparator<KeyValuePair::first_type> >
class GenericMap : public AutoStorage {
public:
typedef typename KeyValuePair::first_type KeyType;
typedef typename KeyValuePair::second_type ValueType;
GenericMap() : tree(&getPool()), mCount(0) { }
GenericMap(MemoryPool& pool) : AutoStorage(pool), tree(&getPool()), mCount(0) { }
~GenericMap() {
if (tree.getFirst()) {
while (true) {
KeyValuePair* temp = tree.current();
bool haveMore = tree.fastRemove();
delete temp;
if (!haveMore) break;
}
}
}
// Returns true if value existed
bool remove(const KeyType& key) {
if (tree.locate(key)) {
KeyValuePair* var = tree.current();
tree.fastRemove();
delete var;
mCount--;
return true;
}
return false;
}
// Returns true if value existed previously
bool put(const KeyType& key, const ValueType& value) {
if (tree.locate(key)) {
tree.current()->second = value;
return true;
}
KeyValuePair *var = FB_NEW(getPool()) KeyValuePair(getPool(), key, value);
tree.add(var);
mCount++;
return false;
}
// Returns true if value is found
bool get(const KeyType& key, ValueType& value) {
if (tree.locate(key)) {
value = tree.current()->second;
return true;
}
return false;
}
size_t count() const { return mCount; }
typedef BePlusTree<KeyValuePair*, KeyType, MemoryPool, FirstObjectKey<KeyValuePair>, KeyComparator> ValuesTree;
private:
ValuesTree tree;
size_t mCount;
};
typedef GenericMap<Pair<Full<string, string> > > StringMap;
}
#endif

View File

@ -1,60 +0,0 @@
#include "firebird.h"
#include "../common/classes/StringMap.h"
namespace Firebird {
StringMap::StringMap() : tree(&getPool()), mCount(0) {
}
StringMap::StringMap(MemoryPool& pool) : AutoStorage(pool), tree(&getPool()), mCount(0) {
}
StringMap::~StringMap() {
if (tree.getFirst()) {
while (true) {
StringPair temp = tree.current();
bool haveMore = tree.fastRemove();
delete temp.cvar_name;
delete temp.cvar_value;
if (!haveMore) break;
}
}
}
bool StringMap::remove(const string& name) {
if (tree.locate(&name)) {
StringPair var = tree.current();
tree.fastRemove();
delete var.cvar_name;
delete var.cvar_value;
mCount--;
return true;
}
return false;
}
bool StringMap::put(const string& name, const string& value) {
if (tree.locate(&name)) {
*tree.current().cvar_value = value;
return true;
}
StringPair var;
var.cvar_name = FB_NEW(getPool()) string(getPool(), name);
var.cvar_value = FB_NEW(getPool()) string(getPool(), value);
tree.add(var);
mCount++;
return false;
}
bool StringMap::get(const string& name, string& value) {
if (tree.locate(&name)) {
value = *tree.current().cvar_value;
return true;
}
return false;
}
} // namespace Firebird

View File

@ -1,79 +0,0 @@
/*
* PROGRAM: Client/Server Common Code
* MODULE: StringMap.h
* DESCRIPTION: Secure handling of clumplet buffers
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
* $Id: StringMap.h,v 1.3 2004-11-23 04:05:49 robocop Exp $
*
*/
#ifndef STRINGMAP_H
#define STRINGMAP_H
#include "fb_string.h"
#include "../common/classes/tree.h"
namespace Firebird {
class StringMap : public AutoStorage {
public:
StringMap();
StringMap(MemoryPool& pool);
~StringMap();
// Returns true if value existed previously
bool put(const string& name, const string& value);
// Returns true if value existed
bool remove(const string& name);
// Returns true if value is found
bool get(const string& name, string& value);
size_t count() const { return mCount; }
struct StringPair {
// We use string class here because we want to handle possible embedded
// zeros properly
const string* cvar_name;
string* cvar_value;
static const string* const & generate(const void* sender, const StringPair& item) {
return item.cvar_name;
}
static bool greaterThan(const string* const &i1, const string* const &i2) {
return *i1 > *i2;
}
};
typedef BePlusTree<StringPair, const string*, MemoryPool, StringPair, StringPair> ValuesTree;
private:
ValuesTree tree;
size_t mCount;
};
}
#endif

View File

@ -34,6 +34,22 @@
namespace Firebird
{
// Right pair - right object in such pair has MemoryPool'ed constructor,
// left one - doesn't (typically POD or builtin type)
template<typename parLeft, typename parRight>
struct Right {
typedef parLeft first_type;
typedef parRight second_type;
explicit Right(MemoryPool& p) : first(), second(p) { }
explicit Right(MemoryPool& p, const parLeft& v1, const parRight& v2)
: first(v1), second(p, v2) { }
explicit Right(MemoryPool& p, const Right& lp)
: first(lp.first), second(p, lp.second) { }
parLeft first;
parRight second;
};
// Left pair - left object in such pair has MemoryPool'ed constructor,
// right one - doesn't (typically POD or builtin type)