Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Storable / Savable.cpp
blob11aad54585a08b1cd275c1f24c67f593293daa17
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 "ace/Auto_Ptr.h"
8 const int Savable::bytes_size_max = 128;
10 class Savable_File_Guard: public TAO::Storable_File_Guard
12 public:
14 Savable_File_Guard (Savable & savable, Method_Type method_type);
16 ~Savable_File_Guard ();
18 virtual void set_object_last_changed (const time_t & time);
20 virtual time_t get_object_last_changed ();
22 virtual int load_from_stream ();
24 virtual bool is_loaded_from_stream ();
26 virtual TAO::Storable_Base * create_stream (const char * mode);
28 private:
29 Savable & savable_;
32 Savable_File_Guard::Savable_File_Guard (Savable & savable,
33 Method_Type method_type)
34 : TAO::Storable_File_Guard(false)
35 , savable_(savable)
37 try
39 this->init(method_type);
41 catch (TAO::Storable_Read_Exception &)
43 throw Savable_Exception();
47 Savable_File_Guard::~Savable_File_Guard ()
49 this->release ();
52 void
53 Savable_File_Guard::set_object_last_changed (const time_t & time)
55 savable_.last_changed_ = time;
58 time_t
59 Savable_File_Guard::get_object_last_changed ()
61 return savable_.last_changed_;
64 int
65 Savable_File_Guard::load_from_stream ()
67 savable_.read (this->peer ());
68 savable_.loaded_from_stream_ = true;
69 this->peer ().rewind ();
70 if (this->peer ().good ())
71 return 0;
72 else
73 return -1;
76 bool
77 Savable_File_Guard::is_loaded_from_stream ()
79 return savable_.loaded_from_stream_;
82 TAO::Storable_Base *
83 Savable_File_Guard::create_stream (const char * mode)
85 return savable_.storable_factory_.create_stream ("test.dat", mode);
88 typedef TAO::Storable_File_Guard SFG;
90 Savable::Savable (TAO::Storable_Factory & storable_factory)
91 : storable_factory_(storable_factory)
92 , loaded_from_stream_ (false)
95 for (int index = 0; index < 2; ++index)
97 this->i_[index] = 0;
98 this->ui_[index] = 0;
99 this->bytes_size_[index] = 0;
100 this->bytes_[index] = new char [this->bytes_size_max];
101 for (int i = 0; i < this->bytes_size_max; ++i)
103 this->bytes_[index][i] = ACE_CHAR_MAX;
107 ACE_Auto_Ptr<TAO::Storable_Base>
108 stream (storable_factory_.create_stream("test.dat", "r"));
109 if (stream->exists ())
111 Savable_File_Guard fg(*this, SFG::CREATE_WITH_FILE);
113 else
115 Savable_File_Guard fg(*this, SFG::CREATE_WITHOUT_FILE);
116 this->write (fg.peer ());
120 Savable::~Savable ()
122 for (int index = 0; index < 2; ++index)
124 delete []this->bytes_[index];
128 bool
129 Savable::is_loaded_from_stream ()
131 return this->loaded_from_stream_;
134 void
135 Savable::read (TAO::Storable_Base & stream)
137 stream.rewind ();
139 for (int index = 0; index < 2; ++index)
141 stream >> this->string_[index];
142 stream >> this->i_[index];
143 stream >> this->ui_[index];
144 stream >> this->bytes_size_[index];
145 stream.read (this->bytes_size_[index], this->bytes_[index]);
149 void
150 Savable::write (TAO::Storable_Base & stream)
152 stream.rewind ();
154 for (int index = 0; index < 2; ++index)
156 stream << this->string_[index];
157 stream << this->i_[index];
158 stream << this->ui_[index];
159 stream << this->bytes_size_[index];
160 stream.write (this->bytes_size_[index], this->bytes_[index]);
163 stream.flush ();
166 void
167 Savable::string_set (int index, const ACE_CString &s)
169 Savable_File_Guard fg(*this, SFG::MUTATOR);
170 this->string_[index] = s;
171 this->write (fg.peer ());
174 const ACE_CString &
175 Savable::string_get (int index)
177 Savable_File_Guard fg(*this, SFG::ACCESSOR);
178 return this->string_[index];
181 void
182 Savable::int_set (int index, int i)
184 Savable_File_Guard fg(*this, SFG::MUTATOR);
185 this->i_[index] = i;
186 this->write (fg.peer ());
190 Savable::int_get (int index)
192 Savable_File_Guard fg(*this, SFG::ACCESSOR);
193 return this->i_[index];
196 void
197 Savable::unsigned_int_set (int index, unsigned int ui)
199 Savable_File_Guard fg(*this, SFG::MUTATOR);
200 this->ui_[index] = ui;
201 this->write (fg.peer ());
204 unsigned int
205 Savable::unsigned_int_get (int index)
207 Savable_File_Guard fg(*this, SFG::ACCESSOR);
208 return this->ui_[index];
211 void
212 Savable::bytes_set (int index, int size, char * bytes)
214 Savable_File_Guard fg(*this, SFG::MUTATOR);
215 this->bytes_size_[index] = size;
216 for (int i = 0; i < this->bytes_size_[index]; ++i)
218 this->bytes_[index][i] = bytes[i];
220 this->write (fg.peer ());
224 Savable::bytes_get (int index, char * bytes)
226 Savable_File_Guard fg(*this, SFG::ACCESSOR);
227 for (int i = 0; i < this->bytes_size_[index]; ++i)
229 bytes[i] = this->bytes_[index][i];
231 return this->bytes_size_[index];