Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Storable / Savable.cpp
blob26b870ba68bbadea6a80030a3428873fd75c4784
1 #include "Savable.h"
2 #include "tao/Storable_Base.h"
3 #include "tao/Storable_Factory.h"
4 #include "tao/Storable_File_Guard.h"
6 #include <memory>
8 const int Savable::bytes_size_max = 128;
10 class Savable_File_Guard: public TAO::Storable_File_Guard
12 public:
13 Savable_File_Guard (Savable & savable, Method_Type method_type);
15 ~Savable_File_Guard ();
17 virtual void set_object_last_changed (const time_t & time);
19 virtual time_t get_object_last_changed ();
21 virtual int load_from_stream ();
23 virtual bool is_loaded_from_stream ();
25 virtual TAO::Storable_Base * create_stream (const char * mode);
27 private:
28 Savable & savable_;
31 Savable_File_Guard::Savable_File_Guard (Savable & savable,
32 Method_Type method_type)
33 : TAO::Storable_File_Guard(false)
34 , savable_(savable)
36 try
38 this->init(method_type);
40 catch (TAO::Storable_Read_Exception &)
42 throw Savable_Exception();
46 Savable_File_Guard::~Savable_File_Guard ()
48 this->release ();
51 void
52 Savable_File_Guard::set_object_last_changed (const time_t & time)
54 savable_.last_changed_ = time;
57 time_t
58 Savable_File_Guard::get_object_last_changed ()
60 return savable_.last_changed_;
63 int
64 Savable_File_Guard::load_from_stream ()
66 savable_.read (this->peer ());
67 savable_.loaded_from_stream_ = true;
68 this->peer ().rewind ();
69 if (this->peer ().good ())
70 return 0;
71 else
72 return -1;
75 bool
76 Savable_File_Guard::is_loaded_from_stream ()
78 return savable_.loaded_from_stream_;
81 TAO::Storable_Base *
82 Savable_File_Guard::create_stream (const char * mode)
84 return savable_.storable_factory_.create_stream ("test.dat", mode);
87 typedef TAO::Storable_File_Guard SFG;
89 Savable::Savable (TAO::Storable_Factory & storable_factory)
90 : storable_factory_(storable_factory)
91 , loaded_from_stream_ (false)
93 for (int index = 0; index < 2; ++index)
95 this->i_[index] = 0;
96 this->ui_[index] = 0;
97 this->bytes_size_[index] = 0;
98 this->bytes_[index] = new char [this->bytes_size_max];
99 for (int i = 0; i < this->bytes_size_max; ++i)
101 this->bytes_[index][i] = ACE_CHAR_MAX;
105 std::unique_ptr<TAO::Storable_Base> stream (storable_factory_.create_stream("test.dat", "r"));
106 if (stream->exists ())
108 Savable_File_Guard fg(*this, SFG::CREATE_WITH_FILE);
110 else
112 Savable_File_Guard fg(*this, SFG::CREATE_WITHOUT_FILE);
113 this->write (fg.peer ());
117 Savable::~Savable ()
119 for (int index = 0; index < 2; ++index)
121 delete []this->bytes_[index];
125 bool
126 Savable::is_loaded_from_stream ()
128 return this->loaded_from_stream_;
131 void
132 Savable::read (TAO::Storable_Base & stream)
134 stream.rewind ();
136 for (int index = 0; index < 2; ++index)
138 stream >> this->string_[index];
139 stream >> this->i_[index];
140 stream >> this->ui_[index];
141 stream >> this->bytes_size_[index];
142 stream.read (this->bytes_size_[index], this->bytes_[index]);
146 void
147 Savable::write (TAO::Storable_Base & stream)
149 stream.rewind ();
151 for (int index = 0; index < 2; ++index)
153 stream << this->string_[index];
154 stream << this->i_[index];
155 stream << this->ui_[index];
156 stream << this->bytes_size_[index];
157 stream.write (this->bytes_size_[index], this->bytes_[index]);
160 stream.flush ();
163 void
164 Savable::string_set (int index, const ACE_CString &s)
166 Savable_File_Guard fg(*this, SFG::MUTATOR);
167 this->string_[index] = s;
168 this->write (fg.peer ());
171 const ACE_CString &
172 Savable::string_get (int index)
174 Savable_File_Guard fg(*this, SFG::ACCESSOR);
175 return this->string_[index];
178 void
179 Savable::int_set (int index, int i)
181 Savable_File_Guard fg(*this, SFG::MUTATOR);
182 this->i_[index] = i;
183 this->write (fg.peer ());
187 Savable::int_get (int index)
189 Savable_File_Guard fg(*this, SFG::ACCESSOR);
190 return this->i_[index];
193 void
194 Savable::unsigned_int_set (int index, unsigned int ui)
196 Savable_File_Guard fg(*this, SFG::MUTATOR);
197 this->ui_[index] = ui;
198 this->write (fg.peer ());
201 unsigned int
202 Savable::unsigned_int_get (int index)
204 Savable_File_Guard fg(*this, SFG::ACCESSOR);
205 return this->ui_[index];
208 void
209 Savable::bytes_set (int index, int size, char * bytes)
211 Savable_File_Guard fg(*this, SFG::MUTATOR);
212 this->bytes_size_[index] = size;
213 for (int i = 0; i < this->bytes_size_[index]; ++i)
215 this->bytes_[index][i] = bytes[i];
217 this->write (fg.peer ());
221 Savable::bytes_get (int index, char * bytes)
223 Savable_File_Guard fg(*this, SFG::ACCESSOR);
224 for (int i = 0; i < this->bytes_size_[index]; ++i)
226 bytes[i] = this->bytes_[index][i];
228 return this->bytes_size_[index];