View Single Post
Old 13th November 2011, 22:10   #3  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Page 3, Class Member Functions Part_1:-

Code:

/***************************
 * The following are the implementation / definition
 * of the declared class member functions.
 ***************************/


// This is the destructor definition. This is called when the filter is destroyed.
// Where declared or defined within the class, it uses the name eg '~INTERNALNAME', the class name preceded by tilde (~). 
// The destructor in this case, uses this form 'INTERNALNAME::~INTERNALNAME', where 'INTERNALNAME::' gives the name of
// the class it belongs to using the scope operator '::'.
// As with all class member functions, when defined outside of the class, the scope operator (::)  is used and specifies
// the class to which the member being defined belongs. Thus granting exactly the same scope properties as if this
// function definition was directly included within the class definition. This allows you to use a name of eg a class 
// member function or variable within the body of a function without any additional specifiers.

INTERNALNAME::~INTERNALNAME()
{
  // Clean up and Kill 'lifelong' arrays in here
  if(LifeLong != NULL) {
    OutputDebugString(AVISYNTHNAME ": Destructor delete[] LifeLong\n");
    delete [] LifeLong;                         // free(LifeLong)
    LifeLong=NULL;  // Not necessary, personal preference. On exit entire class instance including LifeLong gone.
  }
  OutputDebugString(AVISYNTHNAME ": Destructor now complete & exiting.\n");
  // The 'delete []' above will first call the destructor on each element of LifeLong before freeing the array.
  // As it happens, the built-in types (int, short, char etc) dont have destructors, they dont need any
  // special handling for destruction, and so there are no destructor calls in this case for LifeLong.
  //
  // The destructor does NOT return any type, not even void.
}




// This is the implementation of the constructor.
// As with all class members defined outside of the class, it is preceded by the class name and scope operator. 

INTERNALNAME::INTERNALNAME(
    PClip _child,int _intarg,const bool _version, bool _show, int _bounce, IScriptEnvironment* env) \
    :  \
    GenericVideoFilter(_child),version(_version),show(_show), LifeLong(NULL)     // initialization list
{
  //  Note above, the 1st line (after the name) contains the function arguments and closing
  //  parenthesis matching the prototype in the class structure.
  //  The following  ':' introduces an initialization list which initializes 'const' members.
  //  It will be necessary for the initialization of 'GenericVideoFilter' in this case and also 'version' & 'show'.
  //  In the above format, 'GenericVideoFilter(_child)' means initialize 'GenericVideoFilter' with the
  //  variable held in the _child fn argument. Same type of thing for 'version' & 'show' class variables. 
  //  Note that we did not initialize the intarg class variable with the _intarg fn argument, we will do
  //  that later. However, as the type of the 'version' class variable is 'const bool version' we have to 
  //  initialize it here. C++ differentiates between assignment and initialization, you can only initialize
  //  a const, you cannot assign to one, and that is what all that above weird stuff is.
  //  Quite often, coders just use the initializer versions if it results in an empty constructor.
  //  We also initialized 'LifeLong' above, even though it was not a function argument, nor a const (for
  //. no particular reason, just to show that you can).
  //
    // The child clip (source clip) is inherited by the GenericVideoFilter,
    //  where the following variables get defined:
    //   PClip child;   // Contains the source clip.
    //   VideoInfo vi;  // Contains video info on the source clip.
  //
  //
  // What the above is saying is that GenericVideoFilter has a child variable which is intialized
  // by the above GenericVideoFilter(_child). As the INTERNALNAME class inherits from GenericVideoFilter
  // ("class INTERNALNAME : public GenericVideoFilter {" at the very start of the class definition)
  // so we can in class member functions refer to child and actually access the 'child' PClip in the
  // GenericVideoFilter. The _child constructor argument is replicated to the GenericVideoFilter 'child',
  // and that is inherited by the INTERNALNAME class instance.
  //   Dont worry too much about that, you can access the child source clip and that is all
  // you really need to know. Also VideoInfo vi for the source clip, is likewise available without extra
  // specifiers

  intarg = _intarg; // We did not initialize intarg in the initialization list above so we assign to it here.

//  version = true;   // This would produce a compile time error as it is 'const', we used the initialization list instead.

//  show = true;      // This would produce a compile time error as it is 'const', we used the initialization list instead.


  bounce = (_bounce <= 64) ? _bounce : 64; // assign limited bounce with maximum of 64 (allow -ve bounce though)

//  LifeLong=NULL;    // Set to invalid - Commented Out, we used the above constructor initializer list instead.


  num_frames_hi = vi.num_frames;
  // We get the number of frames in clip, used by GETFRAME() macro (NOTE direct use of vi in GenericVideoFilter).

  errbf[0]=0;       // Clear error message

  int err = 0;      // So far, so good.

  OutputDebugString(AVISYNTHNAME ": Constructor now testing ColorSpace\n");
  // Above strings are concatenated (AVISYNTHNAME is a string)

  if (!vi.IsPlanar() && !vi.IsYUY2() && !vi.IsRGB32() && !vi.IsRGB24()) {
    strcpy(errbf,AVISYNTHNAME ": Input must be Planar, YUY2, RGB32 or RGB24\n");
    err=1;
  } else {  
    #ifdef AVISYNTH_PLUGIN_25
    // Plugin v2.5 and Avisynth v2.6
    AVSValue args[1];         // 1 undefined arg
    AVSValue res=env->Invoke("VersionNumber", AVSValue(args, 0)); 
    double ver=res.AsFloat();     // v2.6 comes out as 2.599999
    if(ver > 2.58 && vi.IsPlanar()) {
      if( vi.pixel_type!=0xA0000008 &&  // YV12
        vi.pixel_type!=0xE0000000   // Y8
      ) {
        strcpy(errbf,AVISYNTHNAME " ColorSpace unsupported in v2.5 plugin\n");
        err=1;
      }
    }
    # endif
  }
  if(err==0) {
    OutputDebugString(AVISYNTHNAME ": Constructor now trying 'LifeLong = new int[256]'\n");
    LifeLong = new int[256];                // (int *) malloc(256 * sizeof(LifeLong[0]));
  // Above new would call constructor on each element of LifeLong if it pointed to user defined type array,
  // but as basic types eg int do not have constructors, there are no constructor calls in this instance.
    if (LifeLong == NULL) {
      strcpy(errbf,AVISYNTHNAME ": Cannot allocate LifeLong\n");
      err=1;
    } else {
      OutputDebugString(AVISYNTHNAME ": Setting LifeLong dummy data\n");
      for(int i=0;i<256;++i)
        LifeLong[i]=i;            // Give it some dummy data
    }
  }
  
  if(err) {                       // Goodness me!
    OutputDebugString(AVISYNTHNAME ": Error Aborting\n");
    // Constructor not completing, destructor will NOT be called. (must free any allocated resources before exit)
    if(LifeLong!=NULL) {
      OutputDebugString(AVISYNTHNAME ": delete[] LifeLong\n");
      delete [] LifeLong;                       // equiv 'free(LifeLong);'
      LifeLong=NULL;              
    }

    if(errbf[0] == '\0') {
      char *p=errbf,*s=AVISYNTHNAME ": ERROR, *UKNOWN* Error, Dial 911 and ask for the Coastguard.\n";
      while(*p++=*s++);                 // strcpy()
    }

    OutputDebugString(errbf);           // Output the actual error message to DebugView Window
    env->ThrowError(errbf);             // Kiss me Hardy aaarh... (Last words of Stan Laurel).
    // Exit reporting avisynth error (will present message in Avisynth little red error box).
  }
  OutputDebugString(AVISYNTHNAME ": Constructor now complete & exiting OK.\n");
  // Constructors DONT return anything
}
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 31st March 2015 at 19:25. Reason: Update
StainlessS is offline   Reply With Quote