140Fermer142
geogeoLe 07/05/2008 à 17:24
Allez op soyons fou. cheeky

Header en C:
/**********************************************************************************************************
 * MEMBRES PRIVES
 **********************************************************************************************************/
/** @brief Private container */
typedef struct GFAVector {
  void* pAlloc;              // Pointer at the begin of memory allocation and the low level content of another implementation of a container component
  void* pBitmap;             // Pointer at the begin of the the bitmap table
  void* pData;               // Pointer at the begin of the data block
  int nHostBytes;            // Size in bytes of the implementation of the host component
  int nBitmapBytes;          // Size in bytes of the bitmap table
  int nBlockBytes;           // Size in bytes of one data block
  int nMaxNumberOfData;      // Max number of data block in the vector container
  int iFirstBlockFree;       // Index of the first block free in the vector container
  int nCountData;            // Number of data saved in the vector container
} GFAVector;


/**********************************************************************************************************
 * CONSTRUCTEUR(s), DESTRUCTEUR
 **********************************************************************************************************/
/**
 * @brief Constructor
 * Create a vector container
 *
 * @param[in] nBlockBytes : The size in bytes of one data block
 * @param[in] nMaxNumberOfData : The number of data that the container could have
 *
 * @return a vector container created (use GFA_Vector_isValid to determine if the operation was successful)
 *
 * @pre (nBlockBytes > 0) && (nMaxNumberOfData > 0)
 *
 * @note The constructor returns an invalid vector container if there is no enough memory to allocate the component.
 * The vector container organizes the data on even addresses.
 */
GFAVector GFA_Vector_Construct(int nBlockBytes, int nMaxNumberOfData);

/**
 * @brief Destructor
 * Destroy a vector container
 *
 * @param[in] oVector : A vector container
 *
 * @pre (oVector!=NULL) && GFA_Vector_isValid(oVector)
 */
void GFA_Vector_Destruct(GFAVector* oVector);
...


Code en C:
/**********************************************************************************************************
 * METHODES PUBLIQUES (BAS NIVEAU)
 **********************************************************************************************************/
GFAVector GFA_Vector_Construct_(int nHostBytes, int nBlockBytes, int nMaxNumberOfData) {
  Assert((nHostBytes>=0) && (nBlockBytes > 0) && (nMaxNumberOfData > 0));

  GFAVector oVector;
  oVector.nHostBytes = nHostBytes + (nHostBytes & 1);    // Taille paire (alignement des données)
  oVector.nBlockBytes = nBlockBytes + (nBlockBytes & 1); // Taille paire (alignement des données)
  oVector.nMaxNumberOfData = nMaxNumberOfData;
  oVector.iFirstBlockFree = 0;
  oVector.nCountData = 0;
  oVector.nBitmapBytes = (nMaxNumberOfData >> 3) + ((nMaxNumberOfData & 7)!=0);
  oVector.nBitmapBytes += (oVector.nBitmapBytes & 1);    // Taille paire (alignement des données)

  if (!(oVector.pAlloc = malloc(nHostBytes + oVector.nBitmapBytes + (nBlockBytes * nMaxNumberOfData))))
    GFA_Logger_Debug(LOG_ERR, LOG_LEVEL_ERROR, "Couldn't not allocate buffer for the vector container");
  else {
    oVector.pBitmap = oVector.pAlloc + oVector.nHostBytes;    // Pointeur marquant le début de la table bitmap
    oVector.pData = oVector.pBitmap + oVector.nBitmapBytes;   // Pointeur marquant le début de la zone de données

    memset(oVector.pBitmap, 0, oVector.nBitmapBytes);         // Initialise la zone bitmap
    GFA_Logger_Debug(LOG_DEBUG, LOG_LEVEL_NONE, "A new vector container was created (0x%p)", oVector.pAlloc);
  }

  return oVector;
}

inline void* GFA_Vector_GetHostPointer_(const GFAVector* oVector) {Assert(oVector && GFA_Vector_isValid(oVector)); return oVector->pAlloc;}
inline int GFA_Vector_GetHostSize_(const GFAVector* oVector) {Assert(oVector && GFA_Vector_isValid(oVector)); return oVector->nHostBytes;}
inline int GFA_Vector_GetAllocatedSize_(const GFAVector* oVector) {
  Assert(oVector && GFA_Vector_isValid(oVector));

  return (oVector->nHostBytes + oVector->nBitmapBytes + (oVector->nMaxNumberOfData * oVector->nBlockBytes));
}



Header C++:
/**
 * @class Classe de gestion de paramètres de configuration.
 * Elle dérive de QSettings et supporte le format de fichier XML.
 *
 * @note Il est conseillé d'utiliser la méthode statique CSettings::setPath avant l'instanciation de la classe.
 * Cela affectera la classe mère QSettings.
 */
class CSettings : public QSettings
{
  // ---------------------
  // Eumérations publiques
  // ---------------------
  public:
    enum MethodWriteValue {NotEmptyValues, AllValues};


  // ------------------
  // Membres publics
  // ------------------
  public:
    /** @brief Format de stockage d'un fichier XML */
    static const Format XmlFormat;


  // ------------------
  // Méthodes publiques
  // ------------------
  public:
    /**
     * @brief Constructeur
     *
     * @param[in] p_sFileName : Nom du fichier de configuration (pour QSettings correspoond à ApplicationName).
     * @param[in] p_sFolderName : Nom du dossier accueillant le fichier de configuration (pour QSettings correspond à OrganizationName).
     * @param[in] p_enumScope : Définie si le fichier appartient à un seul et unique utilisateur ou est partagé avec tous les utilisateurs du système.
     * @param[in] p_enumMethodeWrite : Définie si les paramètres vident doivent être écrit ou non.
     * @param[in] parent : Objet parent (chargé de la suppression de this).
     */
    CSettings(
      const QString& p_sFileName, const QString& p_sFolderName = QString("\"),
      Scope p_enumScope = CSettings::UserScope,
      QObject* parent = NULL
    );
...
  // ------------------
  // Méthodes privées
  // ------------------
  private:
    /**
     * @brief Méthode d'écriture XML à partir de la lecture de SettingsMap
     *
     * @param[inout] p_oDevice : Périphérique de lecture (fichier XML).
     * @param[in] p_oMap : SettingsMap contenant les paramètres à lire.
     *
     * @return true.
     */
    static bool writeXmlFile_(QIODevice& p_oDevice, const SettingsMap& p_oMap);
...  

  // ------------------
  // Membres privés
  // ------------------
  private:
    static MethodWriteValue m_enumMethodWriteValue;


Code source C++
...
bool CSettings::writeXmlFile_(QIODevice& p_oDevice, const SettingsMap& p_oMap) {
  QXmlStreamWriter xmlWriter(&p_oDevice);
  xmlWriter.setAutoFormatting(true); // Formatte automatiquement la mise en forme du fichier XML

  xmlWriter.writeStartDocument(); // En-tête XML
  xmlWriter.writeStartElement("SettingsMap"); // Section racine

  QStringList treeGroups;

  // Parcours les paramètres
  for (
    SettingsMap::const_iterator mi = p_oMap.begin();
    mi != p_oMap.end();
    ++mi
  )
  {
    QStringList groups(mi.key().split("/")); // Extrait les groupes

    int i;
    int depth = 0;
    for (; (depth < treeGroups.count()) && (depth < (groups.count() - 1)); ++depth) {
      if (treeGroups[depth]!=groups[depth])
        break;
    }

    // Ferme n sections
    for (i = treeGroups.count() - 1; i>=depth; --i) {
      xmlWriter.writeEndElement();
      treeGroups.pop_back(); // Remonte dans la hiérarchie
    }

    // Ouvre les n sections restant de groups
    for (i = depth; i < (groups.count() - 1); ++i) {
      xmlWriter.writeStartElement(groups[i]);
      treeGroups.push_back(groups[i]); // Descent dans la hiérarchie
    }

    // Ecriture de l'élément
    const QString sValue = mi.value().toString();
    if (
      (CSettings::methodWriteValue()==CSettings::AllValues) ||
      ((CSettings::methodWriteValue()==CSettings::NotEmptyValues) && (!sValue.isEmpty()))
    )
    {
      xmlWriter.writeStartElement(groups.last());
      xmlWriter.writeCharacters(sValue);
      xmlWriter.writeEndElement();
    }
  }

  // Ferme la hiérarchie
  for (int i = 0; i < treeGroups.count(); ++i)
    xmlWriter.writeEndElement();

  xmlWriter.writeEndElement(); // Ferme section racine
  xmlWriter.writeEndDocument(); // "Pied de page" du document

  return true;
}
...


On aime ou on aime pas mais perso je fais en sorte que dans mon code je vois tout de suite qui est un membre, qui est un paramètre, quel est son type...