Re: [Fresco-devel] A Graphic Dictionary

Tobias Hunger <[email protected]>
Newsgroups gmane.comp.video.fresco.devel
Message-ID <[email protected]>
I appended the header to this file:

-- 
Gruss,
Tobias

------------------------------------------------------------
Tobias Hunger           The box said: 'Windows 95 or better'
[email protected]                      So I installed Linux.
------------------------------------------------------------
Dictionary.hh (text/x-c++hdr, 4.3 KB)
/*$Id: Dictionary.hh,v 1.5 2002/06/02 23:42:15 tobias Exp $
 *
 * This source file is a part of the Berlin Project.
 * Copyright (C) 2003 Tobias Hunger <[email protected]> 
 * http://www.fresco.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 * MA 02139, USA.
 */

#ifndef _Berlin_Dictionary_hh
#define _Berlin_Dictionary_hh

#include <Prague/Sys/Thread.hh>

#include <Fresco/Graphic.hh>

#include <string>
#include <vector>
#include <map>

namespace Berlin
{

  class Dictionary_internal
  {
  public:
      typedef std::vector<std::string> creator_list;

      Dictionary_internal();
      ~Dictionary_internal();

      //. Finds the current dictionary.
      //. If no dictionary exists it will create one.
      static Dictionary_internal * instance();

      //. Push a new creator to the end of of the creator list.
      //.
      //. This list gets added to all Graphics new to the Dictionary.
      //. Its purpose is to track which Kits were creating which
      //. Graphics. This should come in useful once we want to
      //. switch WidgetKits at runtime etc.
      void push_creator(const std::string &);
      //. Shorten the creator list by removing the last creator.
      void pop_creator();

      //. Register a new Graphic with the given name. This function returns
      //. the total number of times this Graphic was registered (including
      //. this try).
      unsigned int add(const Fresco::Graphic_ptr, const std::string &);
      //. Remove a Graphic from the Dictionary.
      //.
      //. Returns the number of times this Graphic used to be registered
      //. BEFORE this one is removed.
      unsigned int remove(const Fresco::Graphic_ptr);

      //. Query the Dictionary 
      std::string query(const Fresco::Graphic_ptr);
      //. Return the list of creators associated with a given Graphic.
      std::vector<std::string> creator(const Fresco::Graphic_ptr);
      //. Returns the number of tries to store the given Graphic in
      //. the dictionary.
      unsigned int count(const Fresco::Graphic_ptr);
  private:
      //. A dictionary contains words...
      struct word {
	  std::vector<std::string>    name;
	  Fresco::Graphic_ptr         graphic;
	  std::vector<unsigned short> creator;
	  unsigned int                count;

	  word();
	  word(const Fresco::Graphic_ptr, const std::string &,
	       const std::vector<unsigned short> &);
      };

      typedef std::multimap<unsigned int, word> dictionary_map;
      dictionary_map my_dictionary;

      typedef std::map<std::string, unsigned short> creator_map;
      creator_map my_creator_map;

      std::map<unsigned long, std::vector<unsigned short> > my_creators;

      dictionary_map::iterator search(const Fresco::Graphic_ptr);
      std::vector<std::string>
      decode_creator(const std::vector<unsigned short> &) const;

      unsigned short my_cur_id;

      Prague::Mutex my_mutex;

      static Dictionary_internal * my_self;
      static Prague::Mutex my_singleton_mutex;
  };


  
  class Dictionary
  {
  public:
      Dictionary(std::string c) :
	  my_dict(Berlin::Dictionary_internal::instance())
	  { my_dict->push_creator(c); }
      ~Dictionary()
	  { my_dict->pop_creator(); }

      unsigned int add(const Fresco::Graphic_ptr g, const std::string & s)
	  { return my_dict->add(g, s); }
      unsigned int remove(const Fresco::Graphic_ptr g)
	  { return my_dict->remove(g); }

      std::string query(const Fresco::Graphic_ptr g) const
	  { return my_dict->query(g); }
      std::vector<std::string> creator(const Fresco::Graphic_ptr g) const
	  { return my_dict->creator(g); }
      unsigned int count(const Fresco::Graphic_ptr g) const
	  { return my_dict->count(g); }
  private:
      Dictionary_internal * my_dict;
  };

} // namespace

#endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.