00001 /* 00002 * Copyright (c) 2010, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include <rve_common/registry.h> 00031 #include <rve_render_client/scene_object_collection.h> 00032 #include <rve_render_client/scene.h> 00033 #include <rve_render_client/single_scene_object.h> 00034 00035 namespace rve_render_client 00036 { 00037 00038 SceneObjectCollectionPtr createSceneObjectCollection() 00039 { 00040 SceneObjectCollectionPtr coll(new SceneObjectCollection, destroySceneObject); 00041 return coll; 00042 } 00043 00044 SceneObjectCollectionPtr createSceneObjectCollection(Scene* scene) 00045 { 00046 SceneObjectCollectionPtr coll = createSceneObjectCollection(); 00047 scene->addObject(coll.get()); 00048 return coll; 00049 } 00050 00051 void SceneObjectCollection::doCreate(ContextInfo& context) 00052 { 00053 S_SceneObject::iterator it = objects_.begin(); 00054 S_SceneObject::iterator end = objects_.end(); 00055 for (; it != end; ++it) 00056 { 00057 const SceneObjectPtr& obj = *it; 00058 obj->create(context.scene, context.context); 00059 } 00060 } 00061 00062 void SceneObjectCollection::doDestroy(ContextInfo& context) 00063 { 00064 S_SceneObject::iterator it = objects_.begin(); 00065 S_SceneObject::iterator end = objects_.end(); 00066 for (; it != end; ++it) 00067 { 00068 const SceneObjectPtr& obj = *it; 00069 obj->destroy(context.scene, context.context); 00070 } 00071 } 00072 00073 void SceneObjectCollection::getContextDependencies(V_UUID& deps) 00074 { 00075 S_SceneObject::iterator it = objects_.begin(); 00076 S_SceneObject::iterator end = objects_.end(); 00077 for (; it != end; ++it) 00078 { 00079 const SceneObjectPtr& obj = *it; 00080 obj->getContextDependencies(deps); 00081 } 00082 } 00083 00084 void SceneObjectCollection::addObject(const SceneObjectPtr& obj) 00085 { 00086 if (objects_.insert(obj).second) 00087 { 00088 V_ContextInfo::iterator it = getContexts().begin(); 00089 V_ContextInfo::iterator end = getContexts().end(); 00090 for (; it != end; ++it) 00091 { 00092 ContextInfo& info = *it; 00093 obj->create(info.scene, info.context); 00094 } 00095 } 00096 } 00097 00098 void SceneObjectCollection::removeObject(const SceneObjectPtr& obj) 00099 { 00100 S_SceneObject::iterator it = objects_.find(obj); 00101 if (it != objects_.end()) 00102 { 00103 V_ContextInfo::iterator cit = getContexts().begin(); 00104 V_ContextInfo::iterator cend = getContexts().end(); 00105 for (; cit != cend; ++cit) 00106 { 00107 ContextInfo& info = *cit; 00108 obj->destroy(info.scene, info.context); 00109 } 00110 00111 objects_.erase(it); 00112 } 00113 } 00114 00115 bool SceneObjectCollection::addToRegistry( rve_common::RegistryPtr registry, rve_common::RegistrantWPtr registrant ) 00116 { 00117 // If this object is already registered somewhere, fail. 00118 if( registry_.lock() ) 00119 { 00120 return false; 00121 } 00122 00123 // Register all current sub-objects. 00124 S_SceneObject::iterator it = objects_.begin(); 00125 S_SceneObject::iterator end = objects_.end(); 00126 for (; it != end; ++it) 00127 { 00128 const SceneObjectPtr& obj = *it; 00129 if( !obj->addToRegistry( registry, registrant )) 00130 { 00131 return false; 00132 } 00133 } 00134 00135 // Save the registry information so we can register new sub-objects as they are added. 00136 registry_ = registry; 00137 registrant_ = registrant; 00138 return true; 00139 } 00140 00141 bool SceneObjectCollection::removeFromRegistry( rve_common::RegistryPtr registry, rve_common::RegistrantWPtr registrant ) 00142 { 00143 // If this object is not registered, fail. 00144 rve_common::RegistryPtr reg = registry_.lock(); 00145 if( !reg ) 00146 { 00147 return false; 00148 } 00149 00150 // If previously registered with someone else, fail. 00151 if( reg != registry ) 00152 { 00153 return false; 00154 } 00155 00156 // Register all current sub-objects. 00157 S_SceneObject::iterator it = objects_.begin(); 00158 S_SceneObject::iterator end = objects_.end(); 00159 for (; it != end; ++it) 00160 { 00161 const SceneObjectPtr& obj = *it; 00162 if( !obj->removeFromRegistry( reg, registrant )) 00163 { 00164 return false; 00165 } 00166 } 00167 00168 registry_.reset(); 00169 registrant_.reset(); 00170 return true; 00171 } 00172 00173 } // namespace rve_render_client