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

Revision 7, 4.1 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_OBJECT_SIMPLE_H__
33 #define __NET_RPC_LIB_TYPES_RPC_OBJECT_SIMPLE_H__
34
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38
39 #include <whisperlib/common/base/types.h>
40 #include <whisperlib/common/base/strutil.h>
41
42 #include <whisperlib/net/rpc/lib/types/rpc_object.h>
43 #include <whisperlib/net/rpc/lib/types/rpc_typenames.h>
44
45 namespace rpc {
46
47 template <typename T, const char* NAME>
48 class ObjectSimple : public Object {
49  public:
50   typedef T value_type;
51
52  public:
53   ObjectSimple()
54       : Object(),
55         value_(0) {
56   }
57   ObjectSimple(T value)
58       : Object(),
59         value_(value) {
60   }
61   ObjectSimple(const ObjectSimple<T, NAME>& value)
62       : Object(),
63         value_(value.Get()) {
64   }
65   virtual ~ObjectSimple() {
66   }
67
68   inline static const char* Name() { return NAME; }
69
70   void Set(T value) {
71     value_ = value;
72   }
73   const T& Get() const {
74     return value_;
75   }
76   void operator=(T value) {
77     Set(value);
78   }
79   operator T() const {
80     return value_;
81   }
82
83   Object* Clone() const {
84     return new ObjectSimple<T, NAME>(*this);
85   }
86
87   // This function is specialized for types: int8, uint8
88   // @see rpc_object_simple.cc
89   string ToString() const {
90     return strutil::StringOf(value_);
91   }
92
93   void Encode(io::MemoryStream& result, Codec& codec) const;
94
95  protected:
96   T value_;
97 };
98 }
99
100 extern const char nameOfRpcInt8[];    // = RPC_TYPENAME_INT8;
101 extern const char nameOfRpcUInt8[];   // = RPC_TYPENAME_UINT8;
102 extern const char nameOfRpcInt16[];   // = RPC_TYPENAME_INT16;
103 extern const char nameOfRpcUInt16[];  // = RPC_TYPENAME_UINT16;
104 extern const char nameOfRpcInt32[];   // = RPC_TYPENAME_INT32;
105 extern const char nameOfRpcUInt32[];  // = RPC_TYPENAME_UINT32;
106 extern const char nameOfRpcInt64[];   // = RPC_TYPENAME_INT64;
107 extern const char nameOfRpcUInt64[];  // = RPC_TYPENAME_UINT64;
108 extern const char nameOfRpcFloat[];   // = RPC_TYPENAME_FLOAT;
109 extern const char nameOfRpcDouble[];  // = RPC_TYPENAME_DOUBLE;
110
111 namespace rpc {
112 typedef ObjectSimple<int8,   nameOfRpcInt8  > Int8;
113 typedef ObjectSimple<uint8,  nameOfRpcUInt8 > UInt8;
114 typedef ObjectSimple<int16,  nameOfRpcInt16 > Int16;
115 typedef ObjectSimple<uint16, nameOfRpcUInt16> UInt16;
116 typedef ObjectSimple<int32,  nameOfRpcInt32 > Int32;
117 typedef ObjectSimple<uint32, nameOfRpcUInt32> UInt32;
118 typedef ObjectSimple<int64,  nameOfRpcInt64 > Int64;
119 typedef ObjectSimple<uint64, nameOfRpcUInt64> UInt64;
120 typedef ObjectSimple<float,  nameOfRpcFloat > Float;
121 typedef ObjectSimple<double, nameOfRpcDouble> Double;
122
123 typedef Int32 Int;
124 typedef Int64 BigInt;
125
126 }
127
128 #endif  // __NET_RPC_LIB_TYPES_RPC_OBJECT_SIMPLE_H__
Note: See TracBrowser for help on using the browser.