Savarese Software Research Corporation
GroupList.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright 2006 Savarese Software Research Corporation
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.savarese.com/software/ApacheLicense-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 #ifndef __SSRC_SPREAD_GROUP_LIST_H
00024 #define __SSRC_SPREAD_GROUP_LIST_H
00025 
00026 #include <string>
00027 #include <ostream>
00028 
00029 #include <ssrc/spread/detail/Buffer.h>
00030 
00031 __BEGIN_NS_SPREAD_INCLUDE
00032 # include <sp.h>
00034   typedef char group_type[MAX_GROUP_NAME];
00035 __END_NS_SPREAD_INCLUDE
00036 
00037 __BEGIN_NS_SSRC_SPREAD
00038 
00039 using std::string;
00040 
00041 enum {
00049   MaxSizePrivateName = MAX_PRIVATE_NAME,
00051   MaxSizeProcessName = MAX_PROC_NAME,
00053   MaxSizeGroupName = MAX_GROUP_NAME,
00055   MinValidGroupNameChar = 36,
00057   MaxValidGroupNameChar = 126,
00059   GroupNameSeparatorChar = 35
00060 };
00061 
00062 
00073 inline
00074 std::pair<string,string> split_private_group(const string & private_group) {
00075   string::size_type length = private_group.find(GroupNameSeparatorChar, 1) - 1;
00076   return std::pair<string,string>(string(private_group, 1, length),
00077                                   string(private_group, length + 2,
00078                                          private_group.size() - length - 2));
00079 }
00080 
00088 class GroupList {
00089 private:
00090   struct group_type {
00091     Spread::group_type group;
00092   };
00093 
00094   typedef detail::Buffer<group_type> group_vector;
00095 
00096   group_vector _groups;
00097 
00098   const Spread::group_type *groups() const {
00099     return reinterpret_cast<const Spread::group_type *>(&_groups[0]);
00100   }
00101 
00102   Spread::group_type *groups() {
00103     return
00104       const_cast<Spread::group_type *>(reinterpret_cast<const Spread::group_type *>(&_groups[0]));
00105   }
00106 
00107   unsigned int capacity() const {
00108     return _groups.capacity();
00109   }
00110 
00111   void resize(const unsigned int size) {
00112     _groups.resize(size);
00113   }
00114 
00115 public:
00116 
00120   explicit GroupList(const unsigned int capacity = 10) : _groups(capacity) { }
00121 
00127   void add(const string & group) {
00128     int size;
00129     group_type gname;
00130     size = group.copy(gname.group, sizeof(Spread::group_type) - 1);
00131     gname.group[size] = 0;
00132     _groups.add(gname);
00133   }
00134 
00140   void add(const GroupList & groups) {
00141     if(&groups != this) {
00142       unsigned int offset = size();
00143       resize(size() + groups.size());
00144       std::memcpy(GroupList::groups() + offset, groups.groups(),
00145                   groups.size()*sizeof(group_type));
00146     }
00147   }
00148 
00157   string group(const unsigned int index) const {
00158     return _groups[index].group;
00159   }
00160 
00162   string operator[](const unsigned int index) const {
00163     return group(index);
00164   }
00165 
00172   GroupList & operator=(const GroupList & groups) {
00173     if(&groups != this) {
00174       resize(groups.size());
00175       std::memcpy(GroupList::groups(), groups.groups(),
00176                   size()*sizeof(group_type));
00177     }
00178     return *this;
00179   }
00180 
00182   void clear() {
00183     _groups.clear();
00184   }
00185 
00190   unsigned int size() const {
00191     return _groups.size();
00192   }
00193 
00194 private:
00195   friend class Mailbox;
00196   friend class BaseMessage;
00197 
00208   friend bool operator==(const GroupList & groups1,
00209                          const GroupList & groups2)
00210   {
00211     unsigned int size = groups1.size();
00212 
00213     if(size != groups2.size())
00214       return false;
00215     else
00216       while(size-- > 0) {
00217         if(std::strncmp(groups1.groups()[size], groups2.groups()[size],
00218                         MaxSizeGroupName))
00219           return false;
00220       }
00221 
00222     return true;
00223   }
00224 
00232   friend std::ostream & operator<<(std::ostream & output,
00233                                    const GroupList & groups)
00234   {
00235     for(unsigned int i = 0; i < groups.size(); ++i)
00236       output << groups._groups[i].group << std::endl;
00237     return output;
00238   }
00239 };
00240 
00241 __END_NS_SSRC_SPREAD
00242 
00243 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.