Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
Value.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_VALUE_H
4#define ICE_VALUE_H
5
6#include "Config.h"
7#include "SlicedDataF.h"
8#include "ValueF.h"
9
10#include <ostream>
11#include <string>
12#include <type_traits>
13
14namespace Ice
15{
16 class OutputStream;
17 class InputStream;
18
19 /// The base class for instances of Slice-defined classes.
20 /// @headerfile Ice/Ice.h
21 class ICE_API Value
22 {
23 public:
24 /// Default constructor.
25 Value() noexcept = default;
26
27 // There is no copy constructor, move constructor, copy-assignment operator or move-assignment operator to
28 // prevent accidental slicing.
29 Value(Value&&) = delete;
30
31 virtual ~Value();
32
33 Value& operator=(const Value&) = delete;
34 Value& operator=(Value&&) = delete;
35
36 /// Validates or updates the fields of this object before marshaling.
37 /// @remark The Ice runtime calls this function before marshaling a class instance.
38 virtual void ice_preMarshal() {}
39
40 /// Validates or updates the fields of this object after unmarshaling.
41 /// @remark The Ice runtime calls this function after unmarshaling a class instance.
42 virtual void ice_postUnmarshal() {}
43
44 /// Gets the Slice type ID of the most-derived class supported by this object.
45 /// @return The type ID.
46 [[nodiscard]] virtual const char* ice_id() const noexcept;
47
48 /// Gets the Slice type ID of this type.
49 /// @return The return value is always "::Ice::Object".
50 static const char* ice_staticId() noexcept;
51
52 /// Creates a shallow polymorphic copy of this instance.
53 /// @return The cloned value.
54 [[nodiscard]] ValuePtr ice_clone() const { return _iceCloneImpl(); }
55
56 /// Gets the sliced data associated with this instance.
57 /// @return The sliced data if the value has a preserved-slice base class and has been sliced during
58 /// unmarshaling of the value, nil otherwise.
59 [[nodiscard]] SlicedDataPtr ice_getSlicedData() const;
60
61 /// Outputs a description of this instance to the stream. This description includes the type name and the name
62 /// and value of all the fields of this instance.
63 /// Use ["cpp:custom-print"] to tell the Slice compiler to generate an override implemented by the application.
64 /// @param os The output stream.
65 virtual void ice_print(std::ostream& os) const;
66
67 /// Outputs the name and value of each field of this instance, including inherited fields, to the stream.
68 /// Classes generated by the Slice compiler override this function.
69 /// @param os The output stream.
70 virtual void ice_printFields(std::ostream& os) const;
71
72 /// @cond INTERNAL
73
74 virtual void _iceWrite(Ice::OutputStream*) const;
75 virtual void _iceRead(Ice::InputStream*);
76
77 protected:
78 Value(const Value&) = default; // for clone
79
80 // Helper class that allows derived classes to clone "this" even though the copy constructor is protected.
81 template<class T> struct CloneEnabler : public T
82 {
83 CloneEnabler(const T& other) : T(other) {}
84 static std::shared_ptr<T> clone(const T& other) { return std::make_shared<CloneEnabler>(other); }
85 };
86
87 [[nodiscard]] virtual ValuePtr _iceCloneImpl() const;
88
89 virtual void _iceWriteImpl(Ice::OutputStream*) const {}
90 virtual void _iceReadImpl(Ice::InputStream*) {}
91 /// @endcond
92
93 private:
94 SlicedDataPtr _slicedData;
95 };
96
97 /// Outputs the description of a class instance to a stream. This function calls Value::ice_print on @p value.
98 /// @param os The output stream.
99 /// @param value The class instance.
100 /// @return The output stream.
101 inline std::ostream& operator<<(std::ostream& os, const Value& value)
102 {
103 value.ice_print(os);
104 return os;
105 }
106
107 /// Outputs the description of a class instance held in a shared_ptr.
108 /// @tparam T The class type.
109 /// @param os The output stream.
110 /// @param value The class instance held in a shared pointer. May be null.
111 /// @return The output stream.
112 template<class T, std::enable_if_t<std::is_base_of_v<Value, T>, bool> = true>
113 inline std::ostream& operator<<(std::ostream& os, const std::shared_ptr<T>& value)
114 {
115 if (value)
116 {
117 os << *value;
118 }
119 else
120 {
121 os << "nullptr";
122 }
123 return os;
124 }
125}
126
127#endif
Represents a byte buffer used for unmarshaling data encoded using the Slice encoding.
Definition InputStream.h:50
Represents a byte buffer used for marshaling data using the Slice encoding.
virtual void ice_print(std::ostream &os) const
Outputs a description of this instance to the stream.
Value() noexcept=default
Default constructor.
SlicedDataPtr ice_getSlicedData() const
Gets the sliced data associated with this instance.
virtual void ice_printFields(std::ostream &os) const
Outputs the name and value of each field of this instance, including inherited fields,...
ValuePtr ice_clone() const
Creates a shallow polymorphic copy of this instance.
Definition Value.h:54
static const char * ice_staticId() noexcept
Gets the Slice type ID of this type.
virtual void ice_preMarshal()
Validates or updates the fields of this object before marshaling.
Definition Value.h:38
virtual const char * ice_id() const noexcept
Gets the Slice type ID of the most-derived class supported by this object.
virtual void ice_postUnmarshal()
Validates or updates the fields of this object after unmarshaling.
Definition Value.h:42
The base class for instances of Slice-defined classes.
Definition Value.h:22
std::shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition ValueF.h:13
std::ostream & operator<<(std::ostream &os, const Identity &value)
Outputs the description of an Identity to a stream, including all its fields.
std::shared_ptr< SlicedData > SlicedDataPtr
A shared pointer to a SlicedData.
Definition SlicedDataF.h:22
The Ice RPC framework.
Definition SampleEvent.h:59