Panda3D

dcSimpleParameter.h

00001 // Filename: dcSimpleParameter.h
00002 // Created by:  drose (15Jun04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef DCSIMPLEPARAMETER_H
00016 #define DCSIMPLEPARAMETER_H
00017 
00018 #include "dcbase.h"
00019 #include "dcParameter.h"
00020 #include "dcSubatomicType.h"
00021 #include "dcNumericRange.h"
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //       Class : DCSimpleParameter
00025 // Description : This is the most fundamental kind of parameter type:
00026 //               a single number or string, one of the DCSubatomicType
00027 //               elements.  It may also optionally have a divisor,
00028 //               which is meaningful only for the numeric type
00029 //               elements (and represents a fixed-point numeric
00030 //               convention).
00031 ////////////////////////////////////////////////////////////////////
00032 class EXPCL_DIRECT DCSimpleParameter : public DCParameter {
00033 public:
00034   DCSimpleParameter(DCSubatomicType type, unsigned int divisor = 1);
00035   DCSimpleParameter(const DCSimpleParameter &copy);
00036 
00037 PUBLISHED:
00038   virtual DCSimpleParameter *as_simple_parameter();
00039   virtual const DCSimpleParameter *as_simple_parameter() const;
00040   virtual DCParameter *make_copy() const;
00041   virtual bool is_valid() const;
00042 
00043   DCSubatomicType get_type() const;
00044   bool has_modulus() const;
00045   double get_modulus() const;
00046   int get_divisor() const;
00047 
00048 public:
00049   bool is_numeric_type() const;
00050   bool set_modulus(double modulus);
00051   bool set_divisor(unsigned int divisor);
00052   bool set_range(const DCDoubleRange &range);
00053 
00054   virtual int calc_num_nested_fields(size_t length_bytes) const;
00055   virtual DCPackerInterface *get_nested_field(int n) const;
00056 
00057   virtual void pack_double(DCPackData &pack_data, double value,
00058                            bool &pack_error, bool &range_error) const;
00059   virtual void pack_int(DCPackData &pack_data, int value,
00060                         bool &pack_error, bool &range_error) const;
00061   virtual void pack_uint(DCPackData &pack_data, unsigned int value,
00062                          bool &pack_error, bool &range_error) const;
00063   virtual void pack_int64(DCPackData &pack_data, PN_int64 value,
00064                           bool &pack_error, bool &range_error) const;
00065   virtual void pack_uint64(DCPackData &pack_data, PN_uint64 value,
00066                            bool &pack_error, bool &range_error) const;
00067   virtual void pack_string(DCPackData &pack_data, const string &value,
00068                            bool &pack_error, bool &range_error) const;
00069   virtual bool pack_default_value(DCPackData &pack_data, bool &pack_error) const;
00070 
00071   virtual void unpack_double(const char *data, size_t length, size_t &p, 
00072                              double &value, bool &pack_error, bool &range_error) const;
00073   virtual void unpack_int(const char *data, size_t length, size_t &p, 
00074                           int &value, bool &pack_error, bool &range_error) const;
00075   virtual void unpack_uint(const char *data, size_t length, size_t &p, 
00076                            unsigned int &value, bool &pack_error, bool &range_error) const;
00077   virtual void unpack_int64(const char *data, size_t length, size_t &p, 
00078                             PN_int64 &value, bool &pack_error, bool &range_error) const;
00079   virtual void unpack_uint64(const char *data, size_t length, size_t &p, 
00080                              PN_uint64 &value, bool &pack_error, bool &range_error) const;
00081   virtual void unpack_string(const char *data, size_t length, size_t &p, 
00082                              string &value, bool &pack_error, bool &range_error) const;
00083   virtual bool unpack_validate(const char *data, size_t length, size_t &p, 
00084                                bool &pack_error, bool &range_error) const;
00085   virtual bool unpack_skip(const char *data, size_t length, size_t &p,
00086                            bool &pack_error) const;
00087 
00088   virtual void output_instance(ostream &out, bool brief, const string &prename, 
00089                                const string &name, const string &postname) const;
00090   virtual void generate_hash(HashGenerator &hashgen) const;
00091 
00092 protected:
00093   virtual bool do_check_match(const DCPackerInterface *other) const;
00094   virtual bool do_check_match_simple_parameter(const DCSimpleParameter *other) const;
00095   virtual bool do_check_match_array_parameter(const DCArrayParameter *other) const;
00096 
00097 private:
00098   static DCSimpleParameter *create_nested_field(DCSubatomicType type, 
00099                                                 unsigned int divisor);
00100   static DCPackerInterface *create_uint32uint8_type();
00101 
00102 private:
00103   DCSubatomicType _type;
00104   unsigned int _divisor;
00105 
00106   DCSubatomicType _nested_type;
00107   DCPackerInterface *_nested_field;
00108   size_t _bytes_per_element;
00109 
00110   // The rest of this is to maintain the static list of
00111   // DCPackerInterface objects for _nested_field, above.  We allocate
00112   // each possible object once, and don't delete it.
00113   typedef pmap<unsigned int, DCSimpleParameter *> DivisorMap;
00114   typedef pmap<DCSubatomicType, DivisorMap> NestedFieldMap;
00115   static NestedFieldMap _nested_field_map;
00116 
00117   // These are the range and modulus values as specified by the user,
00118   // unscaled by the divisor.
00119   DCDoubleRange _orig_range;
00120   bool _has_modulus;
00121   double _orig_modulus;
00122 
00123   // Only the range appropriate to this type will be filled in.
00124   DCIntRange _int_range;
00125   DCUnsignedIntRange _uint_range;
00126   DCInt64Range _int64_range;
00127   DCUnsignedInt64Range _uint64_range;
00128   DCDoubleRange _double_range;
00129 
00130   // All of these modulus values will be filled in, regardless of the
00131   // type.
00132   unsigned int _uint_modulus;
00133   PN_uint64 _uint64_modulus;
00134   double _double_modulus;
00135 
00136   static DCClassParameter *_uint32uint8_type;
00137 };
00138 
00139 #endif
 All Classes Functions Variables Enumerations