| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_LIB_TYPES_RPC_MAP_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_TYPES_RPC_MAP_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <map> |
|---|
| 36 |
#include <string> |
|---|
| 37 |
#include <whisperlib/common/base/types.h> |
|---|
| 38 |
#include <whisperlib/common/base/log.h> |
|---|
| 39 |
#include <whisperlib/common/base/strutil.h> |
|---|
| 40 |
#include <whisperlib/net/rpc/lib/types/rpc_object.h> |
|---|
| 41 |
|
|---|
| 42 |
// Both K and V: |
|---|
| 43 |
// - must be a superclass of RPCObject. |
|---|
| 44 |
// - must have an implicit constructor with 0 arguments. |
|---|
| 45 |
// - must have an implicit constructor with 1 arguments: T(const T&). |
|---|
| 46 |
// - must implement static function: static const char * T::Name() |
|---|
| 47 |
// { .. return type name for T .. }. |
|---|
| 48 |
|
|---|
| 49 |
namespace rpc { |
|---|
| 50 |
|
|---|
| 51 |
template <typename K, typename V> |
|---|
| 52 |
class Map : public Object { |
|---|
| 53 |
public: |
|---|
| 54 |
inline static const char* Name() { return RPC_TYPENAME_MAP; } |
|---|
| 55 |
|
|---|
| 56 |
typedef typename map<K, V>::iterator Iterator; |
|---|
| 57 |
typedef typename map<K, V>::const_iterator ConstIterator; |
|---|
| 58 |
Iterator Begin() { return map_.begin(); } |
|---|
| 59 |
Iterator End() { return map_.end(); } |
|---|
| 60 |
ConstIterator Begin() const { return map_.begin(); } |
|---|
| 61 |
ConstIterator End() const { return map_.end(); } |
|---|
| 62 |
Iterator Find(const K& key) { return map_.find(key); } |
|---|
| 63 |
ConstIterator Find(const K& key) const { return map_.find(key); } |
|---|
| 64 |
|
|---|
| 65 |
public: |
|---|
| 66 |
Map() |
|---|
| 67 |
: Object(), |
|---|
| 68 |
map_() { |
|---|
| 69 |
} |
|---|
| 70 |
Map(const Map<K, V>& m) |
|---|
| 71 |
: Object(), |
|---|
| 72 |
map_(m.map_) { |
|---|
| 73 |
} |
|---|
| 74 |
virtual ~Map() { |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
uint32 Size() const { |
|---|
| 78 |
return map_.size(); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
const V& Get(const K& key) const { |
|---|
| 82 |
return map_[key]; |
|---|
| 83 |
} |
|---|
| 84 |
bool Insert(const K& key, const V& value) { |
|---|
| 85 |
pair<Iterator, bool> result = map_.insert(make_pair(key, value)); |
|---|
| 86 |
return result.second; |
|---|
| 87 |
} |
|---|
| 88 |
bool Contains(const K& key) const { |
|---|
| 89 |
return map_.find(key) != map_.end(); |
|---|
| 90 |
} |
|---|
| 91 |
V& operator[](const K& key) { |
|---|
| 92 |
return map_[key]; |
|---|
| 93 |
} |
|---|
| 94 |
const V& operator[](const K& key) const { |
|---|
| 95 |
return map_[key]; |
|---|
| 96 |
} |
|---|
| 97 |
Map<K, V>& operator=(const Map<K, V>& m) { |
|---|
| 98 |
map_ = m.map_; |
|---|
| 99 |
return *this; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
bool operator==(const Map<K, V>& v) const { |
|---|
| 103 |
return map_ == v.map_; |
|---|
| 104 |
} |
|---|
| 105 |
bool operator!=(const Map<K, V>& v) const { |
|---|
| 106 |
return !operator==(v); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
void Erase(const K& key) { |
|---|
| 110 |
map_.erase(key); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
void Clear() { |
|---|
| 114 |
map_.clear(); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
////////////////////////////////////////////////////////////////////// |
|---|
| 118 |
// |
|---|
| 119 |
// rpc::Object interface methods |
|---|
| 120 |
// |
|---|
| 121 |
Object* Clone() const { |
|---|
| 122 |
return new Map<K, V>(*this); |
|---|
| 123 |
} |
|---|
| 124 |
string ToString() const { |
|---|
| 125 |
string text(strutil::StringPrintf( |
|---|
| 126 |
"Map (of [%s, %s] #%u): {", K::Name(), V::Name(), |
|---|
| 127 |
static_cast<unsigned int>(Size()))); |
|---|
| 128 |
if ( map_.empty() ) { |
|---|
| 129 |
text += "}"; |
|---|
| 130 |
return text; |
|---|
| 131 |
} |
|---|
| 132 |
for ( ConstIterator it = Begin(); ; ) { |
|---|
| 133 |
text += string("[") + it->first.ToString() + |
|---|
| 134 |
", " + it->second.ToString() + "]"; |
|---|
| 135 |
++it; |
|---|
| 136 |
if ( it != End() ) { |
|---|
| 137 |
text += ", "; |
|---|
| 138 |
} else { |
|---|
| 139 |
break; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
text += "}"; |
|---|
| 143 |
return text; |
|---|
| 144 |
} |
|---|
| 145 |
virtual void Encode(io::MemoryStream& result, Codec& codec) const; |
|---|
| 146 |
|
|---|
| 147 |
private: |
|---|
| 148 |
map<K, V> map_; |
|---|
| 149 |
}; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
#endif // __NET_RPC_LIB_TYPES_RPC_MAP_H__ |
|---|