2 #include "tao/Storable_Base.h"
3 #include "tao/Storable_Factory.h"
4 #include "tao/Storable_File_Guard.h"
8 const int Savable::bytes_size_max
= 128;
10 class Savable_File_Guard
: public TAO::Storable_File_Guard
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
);
31 Savable_File_Guard::Savable_File_Guard (Savable
& savable
,
32 Method_Type method_type
)
33 : TAO::Storable_File_Guard(false)
38 this->init(method_type
);
40 catch (TAO::Storable_Read_Exception
&)
42 throw Savable_Exception();
46 Savable_File_Guard::~Savable_File_Guard ()
52 Savable_File_Guard::set_object_last_changed (const time_t & time
)
54 savable_
.last_changed_
= time
;
58 Savable_File_Guard::get_object_last_changed ()
60 return savable_
.last_changed_
;
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 ())
76 Savable_File_Guard::is_loaded_from_stream ()
78 return savable_
.loaded_from_stream_
;
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
)
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
);
112 Savable_File_Guard
fg(*this, SFG::CREATE_WITHOUT_FILE
);
113 this->write (fg
.peer ());
119 for (int index
= 0; index
< 2; ++index
)
121 delete []this->bytes_
[index
];
126 Savable::is_loaded_from_stream ()
128 return this->loaded_from_stream_
;
132 Savable::read (TAO::Storable_Base
& stream
)
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
]);
147 Savable::write (TAO::Storable_Base
& stream
)
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
]);
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 ());
172 Savable::string_get (int index
)
174 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
175 return this->string_
[index
];
179 Savable::int_set (int index
, int i
)
181 Savable_File_Guard
fg(*this, SFG::MUTATOR
);
183 this->write (fg
.peer ());
187 Savable::int_get (int index
)
189 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
190 return this->i_
[index
];
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 ());
202 Savable::unsigned_int_get (int index
)
204 Savable_File_Guard
fg(*this, SFG::ACCESSOR
);
205 return this->ui_
[index
];
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
];