Sirikata
libcore/include/sirikata/core/util/SpaceObjectReference.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  SpaceObjectReference.hpp
00003  *
00004  *  Copyright (c) 2009, Stanford University
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 #ifndef _SPACE_OBJECT_REFERENCE_HPP_
00033 #define _SPACE_OBJECT_REFERENCE_HPP_
00034 
00035 #include "SpaceID.hpp"
00036 #include "ObjectReference.hpp"
00037 
00038 namespace Sirikata {
00039 
00040 typedef uint32 MessagePort;
00041 
00048 class SIRIKATA_EXPORT SpaceObjectReference : TotallyOrdered<SpaceObjectReference>{
00049   public:
00050     SpaceObjectReference();
00051     SpaceObjectReference(const SpaceID&sid,
00052         const ObjectReference&oref);
00053     explicit SpaceObjectReference(const String& humanReadable){
00054         String::size_type where=humanReadable.find(":");
00055         if (where==String::npos) {
00056             mObject=ObjectReference::null();
00057             mSpace=SpaceID::null();
00058             throw std::invalid_argument("Unable to find colon separator in SpaceObjectReference");
00059         }else {
00060             mObject=ObjectReference(humanReadable.substr(0,where));
00061             mSpace=SpaceID(humanReadable.substr(where+1));
00062         }
00063     }
00064     explicit SpaceObjectReference(const void * data,
00065                                   unsigned int size)
00066         :mSpace(UUID::Data::construct(reinterpret_cast<const byte*>(data),
00067                                      reinterpret_cast<const byte*>(data)+SpaceID::static_size)),
00068        mObject(UUID::Data::construct(reinterpret_cast<const byte*>(data)+SpaceID::static_size,
00069                                      reinterpret_cast<const byte*>(data)+SpaceID::static_size)){}
00070 
00071     Array<byte,SpaceID::static_size+ObjectReference::static_size> toRawBytes() const {
00072         Array<byte,SpaceID::static_size+ObjectReference::static_size> retval;
00073         std::memcpy(retval.begin(),space().toRawBytes().begin(),SpaceID::static_size);
00074         std::memcpy(retval.begin()+SpaceID::static_size,object().toRawBytes().begin(),ObjectReference::static_size);
00075         return retval;
00076     }
00077 
00078     String toRawHexData() const
00079     { return mSpace.toRawHexData()+mObject.toRawHexData(); }
00080 
00081     static const SpaceObjectReference &null(){
00082         static SpaceObjectReference retval(SpaceID::null(),
00083                                            ObjectReference::null());
00084         return retval;
00085     }
00086 
00087     String toString() const{
00088         return object().toString()+':'+space().toString();
00089     }
00090     unsigned int hash() const{
00091         return object().hash()^space().hash();
00092     }
00093     class Hasher{public:
00094         size_t operator() (const SpaceObjectReference&uuid) const {
00095             return uuid.hash();
00096         }
00097     };
00098     bool isNull() const{
00099         return mObject.isNull()&&mSpace.isNull();
00100     }
00101 
00102     const ObjectReference&object() const {
00103         return mObject;
00104     }
00105     const SpaceID&space() const {
00106         return mSpace;
00107     }
00108 
00109     bool operator==(const SpaceObjectReference& rhs) const{
00110         return space()==rhs.space()&&object()==rhs.object();
00111     }
00112     bool operator!=(const SpaceObjectReference& rhs) const{
00113         return space() != rhs.space() || object() != rhs.object();
00114     }
00115     bool operator<(const SpaceObjectReference& rhs) const{
00116         if (space()==rhs.space()) return object()<rhs.object();
00117         return space()<rhs.space();
00118     }
00119 
00120   private:
00121 
00122     SpaceID mSpace;
00123     ObjectReference mObject;
00124 
00125 };
00126 
00127 inline std::ostream &operator << (std::ostream &os, const SpaceObjectReference&sor) {
00128     os << sor.space() << ":" << sor.object();
00129     return os;
00130 }
00131 
00132 } // namespace Sirikata
00133 
00134 #endif //_OBJECT_REFERENCE_HPP_