5 #ifndef CNOID_BASE_ITEM_MANAGER_H
6 #define CNOID_BASE_ITEM_MANAGER_H
15 #include "exportdecl.h"
29 virtual bool initializeCreation(
Item* protoItem,
Item* parentItem) = 0;
30 virtual bool updateItem(
Item* protoItem,
Item* parentItem) = 0;
33 template<
class ItemType>
39 virtual bool updateItem(ItemType* protoItem,
Item* parentItem) = 0;
45 virtual bool updateItem(Item* protoItem, Item* parentItem)
override final {
46 return updateItem(
dynamic_cast<ItemType*
>(protoItem), parentItem);
55 virtual bool initializeCreation(
Item* protoItem,
Item* parentItem)
override;
56 virtual bool updateItem(
Item* protoItem,
Item* parentItem)
override;
65 void detachAllManagedTypeItemsFromRoot();
68 template <
class ItemType>
class Factory {
70 virtual Item* operator()() {
return new ItemType(); }
73 class OverwritingCheckFunctionBase
76 ~OverwritingCheckFunctionBase();
77 virtual bool operator()(
Item* item) = 0;
85 virtual bool operator()(
Item* item,
const std::string& filename, std::ostream& os,
Item* parentItem) = 0;
88 void bindTextDomain(
const std::string& domain);
96 PRIORITY_DEFAULT = Standard,
97 PRIORITY_COMPATIBILITY = Internal,
98 PRIORITY_OPTIONAL = Internal,
99 PRIORITY_CONVERSION = Conversion
105 typedef std::function<bool(ItemType* item,
const std::string& filename, std::ostream& os,
Item* parentItem)>
Function;
107 virtual bool operator()(
Item* item,
const std::string& filename, std::ostream& os,
Item* parentItem){
108 return function(
static_cast<ItemType*
>(item), filename, os, parentItem);
114 template <
class ItemType,
class SuperItemType = Item>
116 registerClass_(className,
typeid(ItemType),
typeid(SuperItemType), Factory<ItemType>(),
nullptr);
121 template <
class ItemType,
class SuperItemType = Item>
123 registerClass_(className,
typeid(ItemType),
typeid(SuperItemType),
nullptr, singletonInstance);
127 template <
class ItemType,
class SuperItemType = Item>
129 registerClass_(
"",
typeid(ItemType),
typeid(SuperItemType),
nullptr,
nullptr);
133 template <
class ItemType>
135 addAlias_(
typeid(ItemType), className, moduleName);
139 static bool getClassIdentifier(
Item* item, std::string& out_moduleName, std::string& out_className);
142 return static_cast<ItemType*
>(getSingletonInstance(
typeid(ItemType)));
146 addCreationPanel_(
typeid(ItemType), panel);
150 template <
class ItemType>
152 registerFileIO_(
typeid(ItemType), fileIO);
157 template <
class ItemType>
158 [[deprecated(
"Use addFileIO")]]
160 return addFileIO<ItemType>(fileIO);
163 template <
class ItemType>
165 return getFileIOs(
typeid(ItemType));
168 static std::vector<ItemFileIO*> getFileIOs(
const std::type_info& type);
170 static ItemFileIO* findFileIO(
const std::type_info& type,
const std::string& format);
172 template <
class ItemType>
174 const std::string& caption,
const std::string& format,
const std::string& extensions,
177 addLoader_(
typeid(ItemType), caption, format, [extensions](){
return extensions; },
178 std::make_shared<FileFunction<ItemType>>(
function), usage);
182 template <
class ItemType>
184 const std::string& caption,
const std::string& format, std::function<std::string()> getExtensions,
187 addLoader_(
typeid(ItemType), caption, format, getExtensions,
192 template<
class ItemType>
194 const std::string& caption,
const std::string& format,
const std::string& extensions,
197 addSaver_(
typeid(ItemType), caption, format, [extensions](){
return extensions; },
198 std::make_shared<FileFunction<ItemType>>(
function), usage);
202 template<
class ItemType>
204 const std::string& caption,
const std::string& format, std::function<std::string()> getExtensions,
207 addSaver_(
typeid(ItemType), caption, format, getExtensions,
212 template<
class ItemType>
214 const std::string& caption,
const std::string& format,
const std::string& extensions,
217 int usage = Standard)
219 addLoader<ItemType>(caption, format, extensions, loaderFunction, usage);
220 addSaver<ItemType>(caption, format, extensions, saverFunction, usage);
224 template<
class ItemType>
226 const std::string& caption,
const std::string& format, std::function<std::string()> getExtensions,
229 int usage = Standard)
231 addLoader<ItemType>(caption, format, getExtensions, loaderFunction, usage);
232 addSaver<ItemType>(caption, format, getExtensions, saverFunction, usage);
236 void addMenuItemToImport(
const std::string& caption, std::function<
void()> slot);
238 static Item* createItem(
const std::string& moduleName,
const std::string& itemClassName);
239 static Item* createItem(
int itemClassId);
250 template <
class ItemType>
252 Item* parentItem,
bool doAddition =
true,
Item* nextItem =
nullptr,
253 Item* protoItem =
nullptr,
const std::string& title = std::string())
255 return static_cast<ItemType*
>(
256 createItemWithDialog_(
257 typeid(ItemType), parentItem, doAddition, nextItem, protoItem, title));
260 template <
class ItemType>
262 Item* parentItem,
bool doAddtion =
true,
Item* nextItem =
nullptr)
264 return loadItemsWithDialog_(
typeid(ItemType), parentItem, doAddtion, nextItem);
267 template <
class ItemType>
269 return saveItemWithDialog_(
typeid(ItemType), item);
272 [[deprecated(
"Use Item::reload().")]]
273 static void reloadItems(
const ItemList<>& items);
275 [[deprecated(
"Use Item::findOriginalItem().")]]
276 static Item* findOriginalItemForReloadedItem(
Item* item);
278 template<
class AddonType>
280 registerAddon_(
typeid(AddonType), name, factory ? factory : [](){
return new AddonType; });
282 static ItemAddon* createAddon(
const std::type_info& type);
283 static ItemAddon* createAddon(
const std::string& moduleName,
const std::string& addonName);
284 static bool getAddonIdentifier(
ItemAddon* addon, std::string& out_moduleName, std::string& out_addonName);
290 const std::string& className,
const std::type_info& type,
const std::type_info& superType,
291 std::function<
Item*()> factory,
Item* singletonInstance);
292 void addAlias_(
const std::type_info& type,
const std::string& className,
const std::string& moduleName);
294 void registerFileIO_(
const std::type_info& type,
ItemFileIO* fileIO);
296 const std::type_info& type,
const std::string& caption,
const std::string& format,
297 std::function<std::string()> getExtensions, std::shared_ptr<FileFunctionBase>
function,
int usage);
299 const std::type_info& type,
const std::string& caption,
const std::string& format,
300 std::function<std::string()> getExtensions, std::shared_ptr<FileFunctionBase>
function,
int usage);
302 static Item* getSingletonInstance(
const std::type_info& type);
304 static Item* createItemWithDialog_(
305 const std::type_info& type,
Item* parentItem,
bool doAddition,
Item* nextItem,
306 Item* protoItem,
const std::string& title);
308 const std::type_info& type,
Item* parentItem,
bool doAddtion,
Item* nextItem);
309 static bool saveItemWithDialog_(
const std::type_info& type,
Item* item);
313 Item* item,
const std::string& filename,
Item* parentItem,
const std::string& format,
314 const Mapping* options =
nullptr);
316 Item* item,
const std::string& filename,
const std::string& format,
const Mapping* options =
nullptr);
317 static bool overwrite(
Item* item,
bool forceOverwrite,
const std::string& format);
320 const std::type_info& type,
const std::string& name,
const std::function<
ItemAddon*(
void)>& factory);
327 CNOID_EXPORT std::string
getOpenFileName(
const std::string& caption,
const std::string& extensions);
328 CNOID_EXPORT std::vector<std::string>
getOpenFileNames(
const std::string& caption,
const std::string& extensions);