root/trunk/whisperlib/net/rpc/lib/types/rpc_array.h

Revision 7, 4.7 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
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_ARRAY_H__
33 #define __NET_RPC_LIB_TYPES_RPC_ARRAY_H__
34
35 #include <string>
36 #include <vector>
37 #include <whisperlib/common/base/types.h>
38 #include <whisperlib/common/base/log.h>
39 #include <whisperlib/net/rpc/lib/types/rpc_object.h>
40
41 // T must be a superclass of rpc::Object.
42 // T must have an implicit constructor with 0 arguments.
43 // T must have an implicit constructor with 1 arguments: T(const T&).
44 // T must implement static function: static const char* T::Name() {
45 //             .. return type name for T .. }.
46
47 namespace rpc {
48
49 template <typename T>
50 class Array
51     : public Object {
52  public:
53   inline static const char* Name() {
54     return RPC_TYPENAME_ARRAY;
55   }
56
57  public:
58   explicit Array(uint32 size = 0)
59       : Object(), vector_(size) {
60   }
61   Array(const Array<T>& arr)
62       : Object(), vector_(arr.vector_) {
63   }
64   virtual ~Array() {
65   }
66
67   uint32 Size() const {
68     return vector_.size();
69   }
70
71   const vector<T>& Get() const { return vector_; }
72
73   T& Get(uint32 index) {
74     CHECK_LT(index, Size());
75     return vector_[index];
76   }
77   const T& Get(uint32 index) const {
78     CHECK_LT(index, Size());
79     return vector_[index];
80   }
81   void Set(uint32 index, const T& value) {
82     CHECK_LT(index, Size());
83     vector_[index] = value;
84   }
85   void Append(const T& value) {
86     Resize(Size() + 1);
87     Set(Size() - 1, value);
88   }
89   void Append(const Array<T>& v) {
90     const uint32 old_size = Size();
91     const uint32 v_size = v.Size();
92     Resize(old_size + v_size);
93     for ( uint32 i = 0; i < v_size; i++ ) {
94       Set(old_size + i, v.Get(i));
95     }
96   }
97   T& operator[](uint32 index) {
98     return Get(index);
99   }
100   const T& operator[](uint32 index) const {
101     return Get(index);
102   }
103   Array<T>& operator=(const Array<T>& v) {
104     vector_ = v.vector_;
105     return *this;
106   }
107
108   bool operator==(const Array<T>& v) const {
109     return vector_ == v.vector_;
110   }
111   bool operator!=(const Array<T>& v) const {
112     return !operator==(v);
113   }
114
115   void Clear() {
116     vector_.clear();
117   }
118
119   void PushBack(const T& value) {
120     vector_.push_back(value);
121   }
122
123   void Resize(uint32 newsize) {
124     vector_.resize(newsize);
125     LOG_DEBUG << "rpc::Vector resizing to " << newsize << " elements."
126               << " Capacity has become: " << vector_.capacity()
127               << " While size is: " << vector_.size();
128   }
129
130   //////////////////////////////////////////////////////////////////////
131   //
132   //                rpc::Object interface methods
133   //
134   Object* Clone() const {
135     return new Array<T>(*this);
136   }
137   string ToString() const {
138     uint32 count = Size();
139     string text(
140         strutil::StringPrintf(
141             "Array (of \"%s\", #%lu/%lu): {",
142             T::Name(),
143             static_cast<long unsigned int>(count),
144             static_cast<long unsigned int>(vector_.capacity())));
145
146     if ( count == 0 ) {
147       return text + "}";
148     }
149
150     for ( uint32 i = 0; i < count - 1; i++ ) {
151       const T& obj = Get(i);
152       text += obj.ToString() + ", ";
153     }
154     text += Get(count - 1).ToString() + "}";
155
156     return text;
157   }
158
159   virtual void Encode(io::MemoryStream& result, Codec& codec) const;
160  private:
161   vector<T> vector_;
162 };
163 }
164 #endif  //  __NET_RPC_LIB_TYPES_RPC_ARRAY_H__
Note: See TracBrowser for help on using the browser.