| 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_CUSTOM_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_TYPES_RPC_CUSTOM_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <whisperlib/net/rpc/lib/types/rpc_object.h> |
|---|
| 37 |
#include <whisperlib/net/rpc/lib/codec/rpc_decode_result.h> |
|---|
| 38 |
|
|---|
| 39 |
namespace rpc { |
|---|
| 40 |
|
|---|
| 41 |
class Encoder; |
|---|
| 42 |
class Decoder; |
|---|
| 43 |
|
|---|
| 44 |
class Custom : public Object { |
|---|
| 45 |
public: |
|---|
| 46 |
Custom(); |
|---|
| 47 |
virtual ~Custom(); |
|---|
| 48 |
|
|---|
| 49 |
protected: |
|---|
| 50 |
friend class Encoder; |
|---|
| 51 |
friend class Decoder; |
|---|
| 52 |
|
|---|
| 53 |
// Write object data using the given encoder. |
|---|
| 54 |
// There's no reason for this serialization to fail. |
|---|
| 55 |
virtual void SerializeSave(Encoder& output) const = 0; |
|---|
| 56 |
|
|---|
| 57 |
// Read object data from the given decoder. |
|---|
| 58 |
virtual DECODE_RESULT SerializeLoad(Decoder& input) = 0; |
|---|
| 59 |
|
|---|
| 60 |
public: |
|---|
| 61 |
////////////////////////////////////////////////////////////////////// |
|---|
| 62 |
// |
|---|
| 63 |
// rpc::Custom interface methods |
|---|
| 64 |
// |
|---|
| 65 |
virtual const char* GetName() const = 0; |
|---|
| 66 |
|
|---|
| 67 |
////////////////////////////////////////////////////////////////////// |
|---|
| 68 |
// |
|---|
| 69 |
// rpc::Object interface methods |
|---|
| 70 |
// |
|---|
| 71 |
virtual Object* Clone() const = 0; |
|---|
| 72 |
virtual void Encode(io::MemoryStream& result, Codec& codec) const; |
|---|
| 73 |
|
|---|
| 74 |
////////////////////////////////////////////////////////////////////// |
|---|
| 75 |
// |
|---|
| 76 |
// rpc::Loggable interface methods |
|---|
| 77 |
// |
|---|
| 78 |
virtual string ToString() const = 0; |
|---|
| 79 |
|
|---|
| 80 |
protected: |
|---|
| 81 |
// Attribute type for Custom derived classes. |
|---|
| 82 |
// |
|---|
| 83 |
// Usage example: |
|---|
| 84 |
// class Person : public Custom { |
|---|
| 85 |
// ... |
|---|
| 86 |
// public: |
|---|
| 87 |
// Attribute<rpc::String> name_; // |
|---|
| 88 |
// Attribute<rpc::Date> birthday_; |
|---|
| 89 |
// ... |
|---|
| 90 |
// }; |
|---|
| 91 |
// Person p; |
|---|
| 92 |
// p.name_ = rpc::String("Gigel"); |
|---|
| 93 |
// p.birthday_ = rpc::Date(1990, 2, 28, 12, 0, 0, 0, true); |
|---|
| 94 |
template <typename T> |
|---|
| 95 |
class Attribute : public Loggable { |
|---|
| 96 |
public: |
|---|
| 97 |
Attribute() |
|---|
| 98 |
: value_(), |
|---|
| 99 |
is_set_(false) { |
|---|
| 100 |
} |
|---|
| 101 |
Attribute(T& value) |
|---|
| 102 |
: value_(value), |
|---|
| 103 |
is_set_(true) { |
|---|
| 104 |
} |
|---|
| 105 |
virtual ~Attribute() { |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
void Set(const T& value) { |
|---|
| 109 |
value_ = value; |
|---|
| 110 |
is_set_ = true; |
|---|
| 111 |
} |
|---|
| 112 |
void Set(const Attribute<T>& attr) { |
|---|
| 113 |
if ( attr.IsSet() ) { |
|---|
| 114 |
Set(attr.Get()); |
|---|
| 115 |
} else { |
|---|
| 116 |
Reset(); |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
bool IsSet() const { |
|---|
| 120 |
return is_set_; |
|---|
| 121 |
} |
|---|
| 122 |
void Reset() { |
|---|
| 123 |
is_set_ = false; |
|---|
| 124 |
} |
|---|
| 125 |
Attribute<T>& operator=(const T& value) { |
|---|
| 126 |
Set(value); |
|---|
| 127 |
return *this; |
|---|
| 128 |
} |
|---|
| 129 |
Attribute<T>& operator=(const Attribute<T>& attr) { |
|---|
| 130 |
Set(attr); |
|---|
| 131 |
return *this; |
|---|
| 132 |
} |
|---|
| 133 |
const T& Get() const { |
|---|
| 134 |
return value_; |
|---|
| 135 |
} |
|---|
| 136 |
operator const T& () const { |
|---|
| 137 |
return value_; |
|---|
| 138 |
} |
|---|
| 139 |
T& Ref() { |
|---|
| 140 |
is_set_ = true; |
|---|
| 141 |
return value_; |
|---|
| 142 |
} |
|---|
| 143 |
bool operator==(const T& value) const { |
|---|
| 144 |
return IsSet() && Get() == value; |
|---|
| 145 |
} |
|---|
| 146 |
bool operator==(const Attribute<T>& attr) const { |
|---|
| 147 |
return ((!IsSet() && !attr.IsSet()) || |
|---|
| 148 |
(IsSet() && attr.IsSet() && Get() == attr.Get())); |
|---|
| 149 |
} |
|---|
| 150 |
string ToString() const { |
|---|
| 151 |
if ( IsSet() ) { |
|---|
| 152 |
return value_.ToString(); |
|---|
| 153 |
} else { |
|---|
| 154 |
return string("[missing]"); |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
private: |
|---|
| 158 |
T value_; |
|---|
| 159 |
bool is_set_; |
|---|
| 160 |
}; |
|---|
| 161 |
}; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
#endif // __NET_RPC_LIB_TYPES_RPC_CUSTOM_H__ |
|---|