00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #ifndef OBJECT_BASE_H 00021 #define OBJECT_BASE_H 00022 00023 #include "type-id.h" 00024 #include "callback.h" 00025 #include <string> 00026 00027 /** 00028 * This macro should be invoked once for every class which 00029 * defines a new GetTypeId method. 00030 */ 00031 #define NS_OBJECT_ENSURE_REGISTERED(type) \ 00032 static struct X##type##RegistrationClass \ 00033 { \ 00034 X##type##RegistrationClass () { \ 00035 ns3::TypeId tid = type::GetTypeId (); \ 00036 tid.GetParent (); \ 00037 } \ 00038 } x_##type##RegistrationVariable 00039 00040 namespace ns3 { 00041 00042 class AttributeList; 00043 00044 /** 00045 * \ingroup object 00046 * 00047 * \brief implement the ns-3 type and attribute system 00048 * 00049 * Every class which wants to integrate in the ns-3 type and attribute 00050 * system should derive from this base class. This base class provides: 00051 * - a way to associate an ns3::TypeId to each object instance 00052 * - a way to set and get the attributes registered in the ns3::TypeId. 00053 */ 00054 class ObjectBase 00055 { 00056 public: 00057 static TypeId GetTypeId (void); 00058 00059 virtual ~ObjectBase (); 00060 00061 /** 00062 * \returns the TypeId associated to the most-derived type 00063 * of this instance. 00064 * 00065 * This method is typically implemented by ns3::Object::GetInstanceTypeId 00066 * but some classes which derive from ns3::ObjectBase directly 00067 * have to implement it themselves. 00068 */ 00069 virtual TypeId GetInstanceTypeId (void) const = 0; 00070 00071 /** 00072 * \param name the name of the attribute to set 00073 * \param value the name of the attribute to set 00074 * 00075 * Set a single attribute. This cannot fail: if the input is invalid, 00076 * it will crash immediately. 00077 */ 00078 void SetAttribute (std::string name, const AttributeValue &value); 00079 /** 00080 * \param name the name of the attribute to set 00081 * \param value the name of the attribute to set 00082 * \returns true if the requested attribute exists and could be set, 00083 * false otherwise. 00084 */ 00085 bool SetAttributeFailSafe (std::string name, const AttributeValue &value); 00086 /** 00087 * \param name the name of the attribute to read 00088 * \param value a reference to the value where the result should be stored. 00089 * \returns the attribute read. 00090 * 00091 * If the input attribute name does not exist, this method crashes. 00092 */ 00093 void GetAttribute (std::string name, AttributeValue &value) const; 00094 /** 00095 * \param name the name of the attribute to read 00096 * \param attribute the attribute where the result value should be stored 00097 * \returns true if the requested attribute was found, false otherwise. 00098 * 00099 * If the input attribute name does not exist, this method crashes. 00100 */ 00101 bool GetAttributeFailSafe (std::string name, AttributeValue &attribute) const; 00102 00103 /** 00104 * \param name the name of the targetted trace source 00105 * \param context the trace context associated to the callback 00106 * \param cb the callback to connect to the trace source. 00107 * 00108 * The targetted trace source should be registered with TypeId::AddTraceSource. 00109 */ 00110 bool TraceConnect (std::string name, std::string context, const CallbackBase &cb); 00111 /** 00112 * \param name the name of the targetted trace source 00113 * \param cb the callback to connect to the trace source. 00114 * 00115 * The targetted trace source should be registered with TypeId::AddTraceSource. 00116 */ 00117 bool TraceConnectWithoutContext (std::string name, const CallbackBase &cb); 00118 /** 00119 * \param name the name of the targetted trace source 00120 * \param context the trace context associated to the callback 00121 * \param cb the callback to disconnect from the trace source. 00122 * 00123 * The targetted trace source should be registered with TypeId::AddTraceSource. 00124 */ 00125 bool TraceDisconnect (std::string name, std::string context, const CallbackBase &cb); 00126 /** 00127 * \param name the name of the targetted trace source 00128 * \param cb the callback to disconnect from the trace source. 00129 * 00130 * The targetted trace source should be registered with TypeId::AddTraceSource. 00131 */ 00132 bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb); 00133 00134 protected: 00135 /** 00136 * This method is invoked once all member attributes have been 00137 * initialized. Subclasses can override this method to be notified 00138 * of this event but if they do this, they must chain up to their 00139 * parent's NotifyConstructionCompleted method. 00140 */ 00141 virtual void NotifyConstructionCompleted (void); 00142 /** 00143 * \param attributes the attribute values used to initialize 00144 * the member variables of this object's instance. 00145 * 00146 * Invoked from subclasses to initialize all of their 00147 * attribute members. This method will typically be invoked 00148 * automatically from ns3::CreateObject if your class derives 00149 * from ns3::Object. If you derive from ns3::ObjectBase directly, 00150 * you should make sure that you invoke this method from 00151 * your most-derived constructor. 00152 */ 00153 void ConstructSelf (const AttributeList &attributes); 00154 00155 private: 00156 bool DoSet (Ptr<const AttributeAccessor> spec, 00157 Ptr<const AttributeChecker> checker, 00158 const AttributeValue &value); 00159 00160 }; 00161 00162 } // namespace ns3 00163 00164 #endif /* OBJECT_BASE_H */