Chic documentation
Houlder Technologies


Hidden data
Home
Features
Tutorial
Examples
FAQ
Reference
Download
Mailing list
Contact

When a file is generated with the hidden data flag set, Chic replaces all the private variables of each class in the file with a single object that takes care of constructing and copying the private variables. The motivation behind this is to enable you to add, modify or remove private variables without changing the memory layout of the class, and thus not have to recompile clients of that class. The technique is sometimes called an implementation bridge or a "pimpl".

A small example

When we generate compile files with hidden data from the hic-file

  class Song
  {
  public:
    explicit
    Song(int in_length)
     :
     length(in_length),
     beat(defaultBeat)
    {
    }

    double
    getBeat() const
    {
      return beat;
    }

  private:
    int length;
    double beat;
    static double defaultBeat = 0.75;
  };
we obtain the header
  #include <hidden.h>

  class Song
  {
    class Song_Data;
    friend class Song_Data;
  public:
    ~Song();

  public:
    explicit
    Song(int in_length);

    double
    getBeat() const;

  private:
    Chic::Hidden<Song_Data> q;
  };
The hidden class is declared as a nested class and made friend since it has to access defaultBeat. Chic also added an out-of-line destructor as there was none in the class. Hidden is a template that comes with Chic that takes care of construction, destruction and copying. Song_Data is a class especially created for Song by Chic. Its definition is put in the cc-file:
  #include "Song.hh"

  struct Song::Song_Data
  {
    Song_Data(Song& __chic_this,
              int in_length);
     :
     length(in_length),
     beat((&__chic_this)->defaultBeat)
    {
    }

    int length;
    double beat;
  };

  Song::Song(int in_length)
   :
   q(*this, in_length)
  {
  }

  double
  Song::getBeat() const
  {
    return q->beat;
  }

  double Song::defaultBeat = 0.75;
Remember that a struct is the same as a class except that the default access is public instead of private.

We use 'q' to denote the name of instance. In case you have to edit the compile files a one letter variable makes the code quite a lot more legible.

Restrictions

There are several restrictions on which classes that can have hidden data:

You may also consult the faq for some common issues related to hidden data.



Home