home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / array.h < prev    next >
C/C++ Source or Header  |  2008-11-30  |  4KB  |  110 lines

  1. #ifndef K3DSDK_ARRAY_H
  2. #define K3DSDK_ARRAY_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. #include "almost_equal.h"
  24. #include "pipeline_data.h"
  25. #include <map>
  26.  
  27. namespace k3d
  28. {
  29.  
  30. /// Abstract interface that can be used to store heterogeneous collections of arrays.
  31. /// Methods are provided for cloning arrays (virtual ctor pattern), plus type-agnostic
  32. /// methods similar to std::vector, and storage for arbitrary metadata (name-value pairs).
  33. class array
  34. {
  35. public:
  36.     /// Storage for array metadata
  37.     typedef std::map<string_t, string_t> metadata_t;
  38.  
  39.     array();
  40.     array(const metadata_t& Metadata);
  41.     virtual ~array();
  42.  
  43.     /// Returns an empty array with the same concrete type as this array (a variation on virtual ctor)
  44.     virtual array* clone_type() const = 0;
  45.     /// Returns a copy of this array (virtual ctor)
  46.     virtual array* clone() const = 0;
  47.     /// Returns a copy of a half-open range of this array (a variation on virtual ctor)
  48.     virtual array* clone(const uint_t Begin, const uint_t End) const = 0;
  49.  
  50.     /// Sets the size of this array.
  51.     virtual void resize(const uint_t NewSize) = 0;
  52.     /// Returns the size of this array
  53.     virtual const uint_t size() const = 0;
  54.     /// Returns true iff this array is empty
  55.     virtual const bool_t empty() const = 0;
  56.     /// Returns true iff this array is equivalent to another, using the imprecise semantics of almost_equal to compare values.
  57.     /// \note: Returns false if given an array with a different concrete type.
  58.     virtual const bool_t almost_equal(const array& Other, const uint64_t Threshold) const = 0;
  59.  
  60.     /// Sets a new name-value pair, overwriting the value if the name already exists
  61.     void set_metadata_value(const string_t& Name, const string_t& Value);
  62.     /// Sets a collection of name-value pairs, overwriting any existing values
  63.     void set_metadata(const metadata_t& Values);
  64.     /// Returns the set of all name-value pairs
  65.     metadata_t get_metadata() const;
  66.     /// Returns a value by name, or empty-string if the name doesn't exist
  67.     const string_t get_metadata_value(const string_t& Name) const;
  68.     /// Erases an existing name-value pair
  69.     void erase_metadata_value(const string_t& Name);
  70.  
  71. protected:
  72.     /// Storage for array metadata
  73.     metadata_t metadata;
  74. };
  75.  
  76. /// Specialization of almost_equal that tests k3d::array for equality
  77. template<>
  78. class almost_equal<array>
  79. {
  80.     typedef array T;
  81. public:
  82.     almost_equal(const uint64_t Threshold) :
  83.         threshold(Threshold)
  84.     {
  85.     }
  86.  
  87.     inline const bool operator()(const T& A, const T& B) const
  88.     {
  89.         return A.almost_equal(B, threshold);
  90.     }
  91.  
  92.     const uint64_t threshold;
  93. };
  94.  
  95. /// Specialization of pipeline_data_traits for use with k3d::array
  96. template<>
  97. class pipeline_data_traits<array>
  98. {
  99. public:
  100.     static array* clone(const array& Other)
  101.     {
  102.         return Other.clone();
  103.     }
  104. };
  105.  
  106. } // namespace k3d
  107.  
  108. #endif // !K3DSDK_ARRAY_H
  109.  
  110.