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
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
);
32 Savable_File_Guard::Savable_File_Guard (Savable
& savable
,
33 Method_Type method_type
)
34 : TAO::Storable_File_Guard(false)
39 this->init(method_type
);
41 catch (TAO::Storable_Read_Exception
&)
43 throw Savable_Exception();
47 Savable_File_Guard::~Savable_File_Guard ()
53 Savable_File_Guard::set_object_last_changed (const time_t & time
)
55 savable_
.last_changed_
= time
;
59 Savable_File_Guard::get_object_last_changed ()
61 return savable_
.last_changed_
;
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 ())
77 Savable_File_Guard::is_loaded_from_stream ()
79 return savable_
.loaded_from_stream_
;
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
)
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
);
115 Savable_File_Guard
fg(*this, SFG::CREATE_WITHOUT_FILE
);
116 this->write (fg
.peer ());
122 for (int index
= 0; index
< 2; ++index
)
124 delete []this->bytes_
[index
];
129 Savable::is_loaded_from_stream ()
131 return this->loaded_from_stream_
;
135 Savable::read (TAO::Storable_Base
& stream
)
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
]);
150 Savable::write (TAO::Storable_Base
& stream
)
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
]);
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 ());
175 Savable::string_get (int index
)
177 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
178 return this->string_
[index
];
182 Savable::int_set (int index
, int i
)
184 Savable_File_Guard
fg(*this, SFG::MUTATOR
);
186 this->write (fg
.peer ());
190 Savable::int_get (int index
)
192 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
193 return this->i_
[index
];
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 ());
205 Savable::unsigned_int_get (int index
)
207 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
208 return this->ui_
[index
];
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
];